根据我之前针对同一问题的答案,根据必须使用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()
最佳答案
我很确定这是因为
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)
希望对您有所帮助。
关于python - 旋转汽车轮胎(open gl),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3965166/