在车里旋转轮胎(打开gl)

时间:2010-10-19 03:35:44

标签: python opengl

根据我之前得到的相同问题的答案,我改变了我的代码,按照我必须使用glmultmatrix的作业。但这不起作用。这是代码,我正在做的是我将轮胎的中心转换到汽车的中心,旋转轮胎,然后转回。但它并没有将轮胎放回原位:

if (name == 'Front Driver tire' ) & (self.fFlag == "true"):

            self.getCenterTireRim()

            bodyFace = self.mini.group(name)

            glPushMatrix()

            x = self.carCenterX - self.xtFront
            y = self.carCenterY - self.ytFront
            z = self.carCenterZ - self.ztFront

            A = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1)
            glMultMatrixd(cast(A, POINTER(c_double)))

            #print self.carCenterX, self.carCenterY, self.carCenterZ
            #print self.xtFront, self.ytFront, self.ztFront

            B = self.matrix(1,0,0,0,0, math.cos(math.radians(self.angle1 + 45)), math.sin(math.radians(self.angle1 + 45)), 0, 0, -math.sin(math.radians(self.angle1 + 45)), math.cos(math.radians(self.angle1 + 45)), 0, 0,0,0, 1)
            glMultMatrixd(cast(B, POINTER(c_double)))


            for face in bodyFace:
                if len(face) == 3:
                    glBegin(GL_TRIANGLES) 
                elif len(face) == 4:
                    glBegin(GL_QUADS) 
                else: 
                    glBegin(GL_POLYGON)
                for i in face:
                    glNormal3f(*self.mini.normal(i))
                    glVertex3f(*self.mini.vertex(i))
                glEnd()

            C = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x - self.carCenterX   , y - self.carCenterY,   z - self.carCenterZ, 1)
            glMultMatrixd(cast(C, POINTER(c_double)))


            glPopMatrix()







        elif (name == 'Front Driver tire rim') & (self.fFlag == "true"):
            bodyFace = self.mini.group(name)

            glPushMatrix()

            self.getCenterTireRim()

            bodyFace = self.mini.group(name)


            A1 = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, self.carCenterX - self.xrFront, self.carCenterY - self.yrFront, self.carCenterZ - self.zrFront, 1)
            glMultMatrixd(cast(A1, POINTER(c_double)))

            #print self.carCenterX, self.carCenterY, self.carCenterZ
            #print self.xrFront, self.yrFront, self.zrFront

            B1 = self.matrix(1,0,0,0,0, math.cos(math.radians(self.angle1 + 45)), math.sin(math.radians(self.angle1 + 45)), 0, 0, -math.sin(math.radians(self.angle1 + 45)), math.cos(math.radians(self.angle1 + 45)), 0, 0, 0, 0, 1)
            glMultMatrixd(cast(B1, POINTER(c_double)))


            for face in bodyFace:
                if len(face) == 3:
                    glBegin(GL_TRIANGLES) 
                elif len(face) == 4:
                    glBegin(GL_QUADS) 
                else: 
                    glBegin(GL_POLYGON)
                for i in face:
                    glNormal3f(*self.mini.normal(i))
                    glVertex3f(*self.mini.vertex(i))
                glEnd()

            C1 = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, self.xrFront - self.carCenterX   , self.yrFront - self.carCenterY,   self.zrFront - self.carCenterZ, 1)
            glMultMatrixd(cast(C1, POINTER(c_double)))

            glPopMatrix() 

1 个答案:

答案 0 :(得分:1)

我很确定这是因为这一行

C = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x - self.carCenterX   , y - self.carCenterY,   z - self.carCenterZ, 1)

应该是

C = self.matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -x   , -y,   -z, 1)

我不确定你为什么使用x - self.carCenterX等等。你翻译了一种方式(x,y,z),所以只需返回相反的方向,-(x,y,z) = (-x,-y,-z)

我希望有所帮助。