How to Rotate a object3D in PyOpenGL?
I am trying to rotate one object in the X-axis, but I don't get.
I have a class Object 3D it's:
class Object3D():
implements( IRenderizable )
def __init__(self, parameters={} ):
self.parameters = parameters
self.ID= Engine().addObject3D()
self.parent = None
self.childrens =[]
self.position = (parameters.get('POSITION') is None ) is True and Vector4() or parameters.get('POSITION')
self.rotation = (parameters.get('ROTATION') is None ) is True and Quaternion() or parameters.get('ROTATION')
self.direction = Vector4()
self.scale = Vector3(1,1,1)
self.matrix = Matrix4()
self.matrixLocal = Matrix4()
self.matrixWorld = Matrix4()
self.matrixRotationWorld = Matrix4()
self.up = Vector3(0,1,0 )
self.parameters =parameters
self.rotationAngle= 10.
self.currentMatrix = None
self.initCurrentMatrix()
def initCurrentMatrix(self):
glPushMatrix()
glLoadIdentity()
self.currentMatrix = glGetDoublev(GL_MODELVIEW_MATRIX)
glPopMatrix()
return
def setID(self, Id ):
self.ID = Id
def moveTo( self, x,y,z ):
v=Vector4(x,y,z)
#self.position.addSelf( self.rotation.rotateVector(v) )
self.position.addSelf( v )
glPushMatrix()
glLoadIdentity()
glTranslatef( float(self.position.x),float(self.position.y),float(self.position.z) )
self.currentMatrix = glGetDoublev(GL_MODELVIEW_MATRIX)
glPopMatrix()
return self
def render(self):
pass
In this chunk of code you see how to implements the rotation:
def rotateX(self, angle=2 ):
glPushMatrix()
glRotated( angle, 0,1,0)
glPopMatrix()
return self
when Vector4, Vector3 , Quaternion, Matrix4 are my own classes.
what this my mistake? and How to make a Rotation?
I don't know, whether it helps, but the general workflow for moving or rotating an object is like the following:
glPushMatrix
) glLoadIdentity
) glPopMatrix
) In step 5/6 you have updated your transformation matrix. This is necessary, because glRotate
is like a makro for multiply an rotation matrix onto the actual transformation matrix. If you always load the identity matrix and then do an glRotate
then it transforms the identity matrix by only the given degree --> your object will be drawn rotated by this degree and then never do something else - i guess, this is your fault...
if you use the 9 steps above, the matrix for your object will be multiplied by the rotation matrix and the result is used for the next multiply in the next iteration step.
I hope, this will help in general understanding =)
In your rotate func, it should be degrees not angle:
glRotatef(degrees, 0, 0, -1)
Does that work?
what this my mistake?
You mistake OpenGL for a math library, which it is not. Do not use OpenGL for doing math on matrices you keep around in some variable. Actually, don't use OpenGL matrix functions at all. They're part of the fixed function pipeline and have been removed from later versions of OpenGL.
链接地址: http://www.djcxy.com/p/81854.html上一篇: 四元数不能运行的OpenGL旋转