请注意,自几十年前以来,使用
glBegin
/
glEnd
序列和固定功能管线矩阵堆栈进行绘图的方法已经被弃用。请了解
固定功能管线,并查看
顶点规范和
着色器,以获取一种最先进的渲染方式:
传递给
glRotate
的
x
、
y
和
z
参数是旋转轴。由于几何图形是在xy平面上绘制的,因此旋转轴必须是z轴(0,0,1):
glRotatef(10, 0, 0, 1)
要围绕一个旋转点旋转,您需要定义一个模型矩阵,该矩阵通过倒置的旋转点进行位移,然后旋转并最终通过旋转点再次变换(使用
glTranslate
)。
glTranslatef(pivot_x, pivot_y, 0)
glRotatef(10, 0, 0, 1)
glTranslatef(-pivot_x, -pivot_y, 0)
请注意,像
glRotate
这样的操作不允许在
glBegin
/glEnd
序列中使用。在
glBegin
/glEnd
序列中,只允许设置顶点属性,如
glVertex
或
glColor
。您必须在
glBegin
之前设置矩阵:
例如:
def drawLines():
pivot_x, pivot_y = 0, 250
r,g,b = 255,30,20
glTranslatef(pivot_x, pivot_y, 0)
glRotatef(2, 0, 0, 1)
glTranslatef(-pivot_x, -pivot_y, 0)
glClear(GL_COLOR_BUFFER_BIT)
glColor3ub(r,g,b)
glBegin(GL_LINES)
glVertex2f(0,500)
glVertex2f(0,-500)
glEnd()
glFlush()
如果您只想旋转线条,而不影响其他对象,则必须通过使用glPushMatrix
/glPopMatrix
来保存和恢复矩阵堆栈:
angle = 0
def drawLines():
global angle
pivot_x, pivot_y = 0, 250
r,g,b = 255,30,20
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glTranslatef(pivot_x, pivot_y, 0)
glRotatef(angle, 0, 0, 1)
angle += 2
glTranslatef(-pivot_x, -pivot_y, 0)
glColor3ub(r,g,b)
glBegin(GL_LINES)
glVertex2f(0,500)
glVertex2f(0,-500)
glEnd()
glPopMatrix()
glFlush()
glRotatef
用于单精度浮点数 (float
),例如glVertex2f
。网络上有很多教程。谷歌找到了一个不错的 Python & OpenGL for Scientific Visualization。 - Rabbid76