汽车轮胎轮圈的OpenGL变换旋转
这里是一个绘制汽车各个部分的函数。在这个函数里,会检查汽车的轮圈和一个标志。我的目标是,当我移动汽车时,轮胎的轮圈也要转动。但是现在出现了问题:当我按上箭头键时,轮圈虽然转动了,但却从汽车模型中被移除了,而汽车本身是可以移动的。
我在初始化函数里也设置了 self.fFlag = "false":
def on_draw(self):
# Clears the screen and draws the car
# If needed, extra transformations may be set-up here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
for name in self.parts:
colors = self.colors
color = colors.get(name, colors["default"])
glColor3f(*color)
if (name == 'Front Driver tire rim') & (self.fFlag == "true"):
bodyFace = self.mini.group(name)
glPushMatrix()
glRotatef(45,1,0,0)
# Drawing the rim
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()
glPopMatrix()
self.fFlag == "false"
else:
bodyFace = self.mini.group(name)
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()
def on_key_release(self, symbol, modifiers):
"""Process a key pressed event.
"""
if symbol == key.UP:
# Move car forward
# TODO
glTranslatef(0,-1,0)
self.fFlag = "true"
self.on_draw()
pass
补充说明:我想让汽车的轮圈在我按上箭头键时转动,这样汽车就会向前移动。
3 个回答
1
你很可能是把旋转和变换的顺序搞错了,这样轮胎的边缘就不是围绕轮胎中心旋转了。
你可以试着在模型视图矩阵中进行旋转,而在投影矩阵中进行平移。
2
我建议你把这个发到班级论坛上。我觉得TJ看到这个可能不太高兴,而且很容易被找到。
1
为了让一个部分围绕它自己的中心旋转,你需要先把它移动到坐标原点,然后进行旋转,最后再把它移回原来的位置。
所以你的
glRotatef(45,1,0,0) # rotate 45 deg about x axis (thru the world origin)
需要在前面和后面加上移动的操作。
可以参考这个问题的被接受的答案。