📄 gles_utils.py
字号:
#
# ====================================================================
# gles_utils.py - Utility functions for OpenGL ES
#
# Ported from the C++ utils
#
# Copyright (c) 2006 Nokia Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from gles import *
from math import *
import types
try:
radians
except NameError:
def radians(degrees):
"""Convert degrees to radians"""
return degrees * (pi/180)
try:
degrees
except NameError:
def degrees(radians):
"""Convert radians to degrees"""
return radians * (180/pi)
def int2fixed(i):
"""Convert an integer to fixed point"""
return i << 16
def float2fixed(v):
"""Convert a float to fixed point"""
print "float2fixed"
print type(v)
print "v = %x" % (v)
ret = v*pow(2,16)
print "ret = %x" % (ret)
return int(ret)
def fixed2float(v):
"""Convert fixed to float"""
return v * (1/65536.0)
def floats2fixed(values):
"""Convert a sequence of floats to fixed point"""
return [float2fixed(v) for v in values]
def fixed_mul(a, b):
"""Multiply fixed values"""
return (a >> 8) * (b >> 8)
def fixed_div(a,b):
return ((a * (1/b)) * 65536.0)
class TVector:
"""A 3D vector that is represented by single-precision floating point x,y,z coordinates."""
def __init__(self, aX=0.0, aY=0.0, aZ=0.0):
self.iX = aX
self.iY = aY
self.iZ = aZ
def __add__(self, other):
# + operator
return TVector(self.iX + other.iX, self.iY + other.iY, self.iZ + other.iZ)
def __iadd__(self, other):
# += operator
ret = self + other
self.iX = ret.aX
self.iY = ret.aY
self.iZ = ret.aZ
return ret
def __sub__(self, other):
# - operator
return (self + (other * -1))
def __neg__(self):
# -self operator
return (self * -1)
def __mul__(self, other):
# * operator
if isinstance(other, TVector):
return (self.iX * other.iX + self.iY * other.iY + self.iZ * other.iZ)
if type(other) in (types.FloatType,types.IntType):
return TVector(self.iX * other, self.iY * other, self.iZ * other)
def Magnitude(self):
# Calculate the magnitude of this vector. Standard trigonometric calculation:
# sqrt(x**2 + y**2 + z**2)
return sqrt(self * self)
def Normalize(self):
# Normalizes this vector, Panics if this vector = (0, 0, 0)
magnitude = self.Magnitude()
if magnitude == 0:
return
self = self * (1 / magnitude)
def CrossProduct(aVector1, aVector2):
# Computes the crossproduct of vector aVector1 and vector aVector2.
iX = aVector1.iY * aVector2.iZ - aVector1.iZ * aVector2.iY
iY = aVector1.iZ * aVector2.iX - aVector1.iX * aVector2.iZ
iZ = aVector1.iX * aVector2.iY - aVector1.iY * aVector2.iX
return TVector(iX, iY, iZ)
class TVectorx:
"""A 3D vector that is represented by fixed-point x,y,z coordinates."""
def __init__(self, aX=0, aY=0, aZ=0):
self.iX = int2fixed(int(aX))
self.iY = int2fixed(int(aY))
self.iZ = int2fixed(int(aZ))
def __add__(self, other):
# + operator
return TVectorx(self.iX + other.iX, self.iY + other.iY, self.iZ + other.iZ)
def __iadd__(self, other):
# += operator
ret = self + other
self.iX = ret.aX
self.iY = ret.aY
self.iZ = ret.aZ
return ret
def __sub__(self, other):
# - operator
return (self + (other * int2fixed(-1)))
def __neg__(self):
# -self operator
return (self * int2fixed(-1))
def __mul__(self, other):
# * operator
if isinstance(other, TVectorx):
return (fixed_mul(self.iX, other.iX) + fixed_mul(self.iY, other.iY) + fixed_mul(self.iZ, other.iZ))
if type(other) in (types.FloatType, types.IntType):
return TVectorx(fixed_mul(self.iX, other), fixed_mul(self.iY, other), fixed_mul(self.iZ, other))
else:
raise TypeError("Unsupported type")
def Magnitude(self):
# Calculate the magnitude of this vector. Standard trigonometric calculation:
# sqrt(x**2 + y**2 + z**2)
src = fixed2float(self * self)
#print src
return float2fixed(sqrt(src))
def Normalize(self):
# Normalizes the vector by dividing each component with the length of the vector.
magnitude = self.Magnitude()
#print magnitude
if magnitude == 0:
return
ret = self * float2fixed(1 / fixed2float(magnitude))
self.iX = ret.aX
self.iY = ret.aY
self.iZ = ret.aZ
def CrossProduct(aVector1, aVector2):
# Computes the crossproduct of vector aVector1 and vector aVector2.
iX = fixed_mul(aVector1.iY, aVector2.iZ) - fixed_mul(aVector1.iZ, aVector2.iY)
iY = fixed_mul(aVector1.iZ, aVector2.iX) - fixed_mul(aVector1.iX, aVector2.iZ)
iZ = fixed_mul(aVector1.iX, aVector2.iY) - fixed_mul(aVector1.iY, aVector2.iX)
return TVectorx(iX, iY, iZ)
class FiniteStateMachine:
# An abstraction of a finite state machine
def __init__(self):
self.iState = None
self.iPrevState = None
def SetState(self, aNewState ):
# Set the current state and trigger OnEnterState.
if aNewState != -1:
if aNewState != self.iState:
if self.iPrevState != -1:
self.OnLeaveState( self.iState )
self.iPrevState = self.iState
self.iState = aNewState
self.OnEnterState( self.iState )
def OnLeaveState( self, aState ):
# Empty implementation
pass
def OnEnterState( self, iState ):
# Empty implementation
pass
class TFlareConfig:
# Index of the texture used by this element.
#iIndex
# Length scaling.
#iLengthScale
# Texture scaling.
#iImageScale;
pass
class CLensFlareEffect:
# An abstraction of a lens flare effect.
def __init__(self, aTextureNames, aFlareConfigs, aTextureManager, aScreenWidth, aScreenHeight):
self.iTextures = TTexture(len(aTextureNames))
self.iFlareConfigs = aFlareConfigs
#self.iFlareConfigCount = aFlareConfigCount
self.iTextureManager = aTextureManager
self.iCenterX = aScreenWidth>>1
self.iCenterY = aScreenHeight>>1
def DrawAt(self, aLightX, aLightY):
# Renders the lens flare effect at a given screen coordinates.
# Uses the CTextureManager::Blit, which in turn draws two triangles (forming
# a single quad)
# Computing the lens flare vector.
DirX = aLightX - iCenterX
DirY = aLightY - iCenterY
#TReal Scale;
#TReal BlitCenterX, BlitCenterY;
#TReal BlitWidth_div_2, BlitHeight_div_2;
glEnable( GL_BLEND )
glBlendFunc(GL_ONE, GL_ONE)
glEnable( GL_TEXTURE_2D )
for i in range(len(self.iFlareConfigs)):
TextureIndex = self.iFlareConfigs[i].iIndex
Scale = self.iFlareConfigs[i].iLengthScale
BlitCenterX = DirX*Scale+self.iCenterX
BlitCenterY = DirY*Scale+self.iCenterY
BlitWidth_div_2 = (self.iTextures[TextureIndex].iTextureWidth * self.iFlareConfigs[i].iImageScale) / 4
BlitHeight_div_2 = (self.iTextures[TextureIndex].iTextureHeight * self.iFlareConfigs[i].iImageScale) / 4
iTextureManager.Blit(self.iTextures[TextureIndex],
(BlitCenterX - BlitWidth_div_2),
(BlitCenterY - BlitHeight_div_2),
(BlitCenterX + BlitWidth_div_2),
(BlitCenterY + BlitHeight_div_2))
glDisable( GL_TEXTURE_2D )
glDisable( GL_BLEND )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -