📄 gles_utils.py
字号:
class T3DModel:
# Abstraction of a 3D model, represented by a position vector and single-precision floating point Yaw, Pitch, Roll orientation
def __init__(self, aPosition, aYaw, aPitch, aRoll):
self.position = aPosition
self.yaw = aYaw
self.pitch = aPitch
self.roll = aRoll
def MakeWorldViewMatrix(aCamera, aPosition, aYaw=0, aPitch=0, aRoll=0):
# Sets up a world + a view matrix.
glMultMatrixf(aCamera.iViewMatrix)
glTranslatef(aPosition.iX-aCamera.iPosition.iX, aPosition.iY-aCamera.iPosition.iY, aPosition.iZ-aCamera.iPosition.iZ)
if aRoll:
glRotatef( aRoll , 0, 0, 1)
if aYaw:
glRotatef( aYaw , 0, 1, 0)
if aPitch:
glRotatef( aPitch, 1, 0, 0)
MakeWorldViewMatrix = staticmethod(MakeWorldViewMatrix)
def MakeBillboardWorldViewMatrix(aCamera, aPosition):
# Sets up a billboard matrix, which is a matrix that rotates objects in such a
# way that they always face the camera.
# Refer to the billboard example to see how this method is used.
# Set up a rotation matrix to orient the billboard towards the camera.
Dir = aCamera.iLookAt - aCamera.iPosition;
#TReal Angle, SrcT, SrcB;
SrcT = Dir.iZ;
SrcB = Dir.iX;
Angle = atan2( SrcT, SrcB)
# The Yaw angle is computed in such a way that the object always faces the
# camera.
Angle = -(degrees( Angle ) + 90)
T3DModel.MakeWorldViewMatrix(aCamera, aPosition, Angle)
MakeBillboardWorldViewMatrix = staticmethod(MakeBillboardWorldViewMatrix)
class T3DModelx:
# Abstraction of a 3D model, represented by a position vector and fixed-point Yaw, Pitch, Roll orientation
def __init__(self, aPosition=TVectorx(int2fixed(0),int2fixed(0),int2fixed(0)), aYaw=int2fixed(0), aPitch=int2fixed(0), aRoll=int2fixed(0)):
# Constructs and initializes a T3DModelx to position aPosition, with
# orientation [aYaw, aPitch, aRoll].
self.position = aPosition
self.yaw = aYaw
self.pitch = aPitch
self.roll = aRoll
def MakeWorldViewMatrix(aCamera, aPosition, aYaw=0, aPitch=0, aRoll=0):
# Sets up a world + a view matrix.
glMultMatrixx(aCamera.iViewMatrix)
glTranslatex(aPosition.iX-aCamera.iPosition.iX, aPosition.iY-aCamera.iPosition.iY, aPosition.iZ-aCamera.iPosition.iZ)
if aRoll != int2fixed(0):
glRotatex( aRoll , int2fixed(0), int2fixed(0), int2fixed(1))
if aYaw != int2fixed(0):
glRotatex( aYaw , int2fixed(0), int2fixed(1), int2fixed(0))
if aPitch != int2fixed(0):
glRotatex( aPitch, int2fixed(1), int2fixed(0), int2fixed(0))
MakeWorldViewMatrix = staticmethod(MakeWorldViewMatrix)
def MakeBillboardWorldViewMatrix(aCamera, aPosition):
# Sets up a billboard matrix, which is a matrix that rotates objects in such a
# way that they always face the camera.
# Refer to the billboard example to see how this method is used.
#if not aPosition:
# aPosition = self.position
# Set up a rotation matrix to orient the billboard towards the camera.
Dir = aCamera.iLookAt - aCamera.iPosition
#TReal Angle, SrcT, SrcB;
#return
SrcT = fixed2float(Dir.iZ)
SrcB = fixed2float(Dir.iX)
print "SrcT: %x" % (SrcT)
print "SrcB: %x" % (SrcB)
angle = atan2( SrcT, SrcB)
print "Angle = %f" % (angle)
# The Yaw angle is computed in such a way that the object always faces the camera.
angle = -(degrees( angle ) + 90)
print "Angle = %f" % (angle)
T3DModelx.MakeWorldViewMatrix(aCamera, aPosition, float2fixed(angle))
MakeBillboardWorldViewMatrix = staticmethod(MakeBillboardWorldViewMatrix)
class TCamera:
# Abstraction of a Camera in 3D space.
#
# The camera is represented by the eye point, the reference point, and the up vector.
# This class is very useful since it provides an implementation of the gluLookAt method
# which is not part of the OpenGL ES specification.
def __init__(self, aPosition=TVector(0, 0, 0), aLookAt=TVector(0, 0, -1), aUp=TVector(0, 1, 0)):
self.iViewMatrix = []
self.LookAt(aPosition, aLookAt, aUp)
def LookAt(self, aPosition, aLookAt, aUp):
#Initializes a TCamera to aPosition, aLookAt, aUp.
#TVector XAxis, YAxis, ZAxis;
self.iPosition = aPosition
self.iLookAt = aLookAt
self.iUp = aUp
# Get the z basis vector, which points straight ahead; the
# difference from the position (eye point) to the look-at point.
# This is the direction of the gaze (+z).
ZAxis = (self.iLookAt - self.iPosition)
# Normalize the z basis vector.
ZAxis.Normalize()
# Compute the orthogonal axes from the cross product of the gaze
# and the Up vector.
#print ZAxis
#print self.iUp
if isinstance(ZAxis, TVectorx):
XAxis = TVectorx.CrossProduct(ZAxis, self.iUp)
elif isinstance(ZAxis, TVector):
XAxis = TVector.CrossProduct(ZAxis, self.iUp)
XAxis.Normalize()
if isinstance(ZAxis, TVectorx):
YAxis = TVectorx.CrossProduct(XAxis, ZAxis)
if isinstance(ZAxis, TVector):
YAxis = TVector.CrossProduct(XAxis, ZAxis)
# Start building the matrix. The first three rows contain the
# basis vectors used to rotate the view to point at the look-at point.
self.MakeIdentity()
self.iViewMatrix[0][0] = XAxis.iX;
self.iViewMatrix[1][0] = XAxis.iY;
self.iViewMatrix[2][0] = XAxis.iZ;
self.iViewMatrix[0][1] = YAxis.iX;
self.iViewMatrix[1][1] = YAxis.iY;
self.iViewMatrix[2][1] = YAxis.iZ;
self.iViewMatrix[0][2] = -ZAxis.iX;
self.iViewMatrix[1][2] = -ZAxis.iY;
self.iViewMatrix[2][2] = -ZAxis.iZ;
def MakeIdentity(self):
self.iViewMatrix = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
]
class TCameraX:
# Abstraction of a Camera in 3D space using fixed-point arithmetic.
#
# The camera is represented by the eye point, the reference point, and the up vector.
#
# This class is very useful since it provides an implementation of the gluLookAt method
# which is not part of the OpenGL ES specification.
def __init__(self, aPosition=TVectorx(0, 0, 0), aLookAt=TVectorx(0, 0, -1), aUp=TVectorx(0, 1, 0)):
self.LookAt(aPosition, aLookAt, aUp)
self.iViewMatrix = []
def LookAt(self, aPosition, aLookAt, aUp):
# Initializes a TCamera to aPosition, aLookAt, aUp.
#TVectorx XAxis, YAxis, ZAxis;
self.iPosition = aPosition
self.iLookAt = aLookAt
self.iUp = aUp
# Get the z basis vector, which points straight ahead; the
# difference from the position (eye point) to the look-at point.
# This is the direction of the gaze (+z).
ZAxis = (iLookAt - iPosition)
# Normalize the z basis vector.
ZAxis.Normalize();
# Compute the orthogonal axes from the cross product of the gaze
# and the Up vector.
XAxis = TVectorx.CrossProduct(ZAxis, iUp)
XAxis.Normalize()
YAxis = TVectorx.CrossProduct(XAxis, ZAxis)
# Start building the matrix. The first three rows contain the
# basis vectors used to rotate the view to point at the look-at point.
self.iViewMatrix = self.MakeIdentity(self.iViewMatrix)
iViewMatrix[0][0] = XAxis.iX
iViewMatrix[1][0] = XAxis.iY
iViewMatrix[2][0] = XAxis.iZ
iViewMatrix[0][1] = YAxis.iX
iViewMatrix[1][1] = YAxis.iY
iViewMatrix[2][1] = YAxis.iZ
iViewMatrix[0][2] = -ZAxis.iX
iViewMatrix[1][2] = -ZAxis.iY
iViewMatrix[2][2] = -ZAxis.iZ
def MakeIdentity(self, aMatrix):
self.iViewMatrix = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
]
aMatrix[0 + 4 * 0] = int2fixed(1); aMatrix[0 + 4 * 1] = int2fixed(0)
aMatrix[0 + 4 * 2] = int2fixed(0); aMatrix[0 + 4 * 3] = int2fixed(0)
aMatrix[1 + 4 * 0] = int2fixed(0); aMatrix[1 + 4 * 1] = int2fixed(1)
aMatrix[1 + 4 * 2] = int2fixed(0); aMatrix[1 + 4 * 3] = int2fixed(0)
aMatrix[2 + 4 * 0] = int2fixed(0); aMatrix[2 + 4 * 1] = int2fixed(0)
aMatrix[2 + 4 * 2] = int2fixed(1); aMatrix[2 + 4 * 3] = int2fixed(0)
aMatrix[3 + 4 * 0] = int2fixed(0); aMatrix[3 + 4 * 1] = int2fixed(0)
aMatrix[3 + 4 * 2] = int2fixed(0); aMatrix[3 + 4 * 3] = int2fixed(1)
return aMatrix
class TParticle:
# This structure is used by the class CParticleEngine.
# It is an abstraction of a particle.
# Position
#TVector iPosition
# Velocity
#TVector iVelocity
# Acceleration
#TVector iAcceleration
# Empty implementation
pass
class CParticleEngine:
# Abstraction of a particle engine.
# Particles engines are used to create special effects like Rain, Smoke, Snow, Sparks, etc...
def __init__(self, aParticlesCount, aPosition):
# Constructs a CParticleEngine object with aParticlesCount particles at
# position aPosition.
self.iParticlesCount = aParticlesCount
self.iParticles = [TParticle() for x in xrange(self.iParticlesCount)]
self.position = aPosition
def ResetEngine(self):
# Resets the particle engine
for p in self.iParticles:
self.ResetParticle(p)
def ResetParticle(self, aIndex):
# Resets the particle at index aIndex
pass
def UpdateEngine(self, aElapsedTime):
# Updates the engine.
pass
def RenderEngine(self, aCamera):
# Renders the system.
pass
class Utils:
# A set of useful functions.
pass
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -