python - 如何在 PyOpenGL 中旋转二维线?
问题描述
我写了一个代码来画一条线。这是功能:
def drawLines():
r,g,b = 255,30,20
#drawing visible axis
glClear(GL_COLOR_BUFFER_BIT)
glColor3ub(r,g,b)
glBegin(GL_LINES)
#glRotate(10,500,-500,0)
glVertex2f(0,500)
glVertex2f(0,-500)
glEnd()
glFlush()
现在我正在尝试旋转这条线。我正在尝试遵循此文档,但无法理解。根据文档,旋转功能定义如下:
def glRotate( angle , x , y , z ):
我没有z轴。所以我保持z=0
。我在这里想念什么?
解决方案
请注意,由glBegin
/glEnd
序列和固定函数管道矩阵堆栈绘制,几十年来已被弃用。阅读Fixed Function Pipeline并查看Vertex Specification and Shader了解最先进的渲染方式:
传递给的 和参数x
是旋转轴。由于几何图形是在 xy 平面中绘制的,因此旋转轴必须是 z 轴 (0,0,1):y
z
glRotate
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)
#drawing visible axis
glBegin(GL_LINES)
glVertex2f(0,500)
glVertex2f(0,-500)
glEnd()
glFlush()
如果您只想旋转线条而不影响其他对象,那么您可以通过glPushMatrix
/glPopMatrix
保存和恢复 matirx 堆栈:
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)
#drawing visible axis
glBegin(GL_LINES)
glVertex2f(0,500)
glVertex2f(0,-500)
glEnd()
glPopMatrix()
glFlush()
推荐阅读
- flutter - 修剪和删除飞镖中字符串开头和结尾的特定字符
- javascript - 在 Google Apps 脚本中执行节点 JS 代码
- javascript - 代码仅在编辑模式下工作,而不是在实时视图中
- reactjs - 使用 spfx 的 Sharepoint 列表中的项目计数
- nuget - 是否有用于 UNO 的 UWP nuget 的 Telerik UI?
- c - 数组中的额外大小(尤其是所需大小的 +1)以及我们为什么需要它?
- c# - 在数据表上使用从设计器添加的 datagridview 列
- html - 在背景矩形的中心创建一列框
- python - 如何切换布尔字段
- python - 如何将数据框行合并为单行,每列的所有行值都集中在一起?