1

I am using OpenGL with Python, and have the following code:

glTranslatef(0, 0, -3.0)
glRotatef(rotation * 100.0, 0.0, 1.0, 0.0)
square.render()

- where rotation is a integer which increases by frame and square.render() simply renders a quad to the screen.

Most sources will advise translating the matrix and then rotating it, as I have done here. However, when the quad renders to the screen, it rotates around a circle rather than a fixed point. My issue is that I want the quad to rotate around a fixed point, and I have been unsuccessful thus far in doing so. What am I doing wrong?

4

1 回答 1

2

想象一下glRotatef(),好像您实际上“绘制”了一个轴,该轴从 (0, 0, 0) 开始并指向您给它的任何坐标。您的示例使用 (0, 1, 0) 这是世界 Y 轴。

但是假设您想P围绕轴 [(3, 3, 3), (3, 4, 3)] 旋转您的点 (10, 10, 0) A。这个轴 - 如您所见 - 是平行的与 Y。

首先你必须平移你的整个世界,所以现在你的A轴将位于世界 Y 轴上。

glClear( GL_COLOR_BUFFER_BIT )
glTranslatef( 3, 3, 3 )

现在你必须围绕世界 Y 轴旋转一切:

glRotatef( rotation*100.0, 0, 1, 0 )

然后你必须把它翻译回来:

glTranslatef( -3, -3, -3 )

现在你可以得出你的P观点了:

glBegin( GL_POINTS )
glVertex3f( 10, 10, 0 )
glEnd()

注意:忘记我愚蠢的 glBegin/End 示例,它很古老,使用 VAO 和 VBO!


我不知道您使用的是什么框架,但这是 pyglet 中的一个非常基本的示例:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window( 256, 256 )

x = 0
def update(dt):
    global x
    x += 1


@window.event
def on_draw():
    global x

    glClear( GL_COLOR_BUFFER_BIT )
    glLoadIdentity()
    glTranslatef( 128, 128, 0 )
    glRotatef( x, 0, 0, 1 )
    glTranslatef( -128, -128, 0 )

    glBegin( GL_TRIANGLES )
    glColor3f( 1, 0, 0 )
    glVertex3f( 128, 128, 0 )
    glColor3f( 0, 1, 0 )
    glVertex3f( 200, 128, 0 )
    glColor3f( 0, 0, 1 )
    glVertex3f( 200, 200, 0 )
    glEnd()

pyglet.clock.schedule_interval(update, 1/60)
pyglet.app.run()

结果就在这里!

于 2013-11-13T08:46:34.513 回答