简体   繁体   English

如何在PyOpenGL中旋转object3D?

[英]How to Rotate a object3D in PyOpenGL?

I am trying to rotate one object in the X-axis, but I don't get. 我试图在X轴上旋转一个对象,但没有。

I have a class Object 3D it's: 我有一个3D类的对象:

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. 当Vector4,Vector3,四元数,Matrix4是我自己的类时。

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: 我不知道这是否有帮助,但是移动或旋转对象的一般工作流程如下:

  1. draw static stuff 画静态的东西
  2. push actual matrix on stack ( glPushMatrix ) 将实际矩阵推入堆栈( glPushMatrix
  3. load identity matrix (initial matrix) ( glLoadIdentity ) 负载标识矩阵(初始矩阵)( glLoadIdentity
  4. use your own matrix and load and set it as the actual matrix 使用您自己的矩阵并加载并将其设置为实际矩阵
  5. transform the actual matrix via glRotate / gl.... 通过glRotate / gl转换实际矩阵
  6. save the actual matrix as your own matrix 将实际矩阵另存为自己的矩阵
  7. draw your object 画你的对象
  8. pop matrix from stack ( glPopMatrix ) 堆栈中的弹出矩阵( glPopMatrix
  9. draw rest of the static stuff 提取其余的静态内容

In step 5/6 you have updated your transformation matrix. 在步骤5/6中,您更新了转换矩阵。 This is necessary, because glRotate is like a makro for multiply an rotation matrix onto the actual transformation matrix. 这是必需的,因为glRotate就像一个将旋转矩阵乘到实际变换矩阵上的makro。 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... 如果您总是加载单位矩阵然后执行glRotate则它仅按给定的度数转换单位矩阵->您的对象将被旋转此度数绘制,然后再不执行其他操作-我想这是您的错。 ..

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. 如果使用上面的9个步骤,则对象矩阵将与旋转矩阵相乘,结果将用于下一个迭代步骤中的下一个乘法。

I hope, this will help in general understanding =) 我希望这将有助于大致了解=)

In your rotate func, it should be degrees not angle: 在旋转函数中,它应该是度而不是角度:

glRotatef(degrees, 0, 0, -1) glRotatef(度,0,0,-1)

Does that work? 那样有用吗?

what this my mistake? 这是我的错吗?

You mistake OpenGL for a math library, which it is not. 您将OpenGL误认为数学库,事实并非如此。 Do not use OpenGL for doing math on matrices you keep around in some variable. 请勿使用OpenGL对您在某个变量中保留的矩阵进行数学运算。 Actually, don't use OpenGL matrix functions at all. 实际上,根本不使用OpenGL矩阵函数。 They're part of the fixed function pipeline and have been removed from later versions of OpenGL. 它们是固定功能管道的一部分,已从更高版本的OpenGL中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM