对齐 pyOpenGL 中的单个对象以创建更大的聚合对象
Align individual objects in pyOpenGL to create a bigger aggregated object
我正在尝试使用环面、圆柱体和长方体的组合在 PyOpenGL 中创建一个关键对象。我能够在 window 的中心分别获得这三个单独的对象,但是当我尝试将所有这些对象合并为一个 window 时,我无法做到这一点。
这是代码。
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
vertices = (
(0.75, -0.25, -0.5),
(0.75, 0.25, -0.5),
(-0.75, 0.25, -0.5),
(-0.75, -0.25, -0.5),
(0.75, -0.25, 0.5),
(0.75, 0.25, 0.5),
(-0.75, -0.25, 0.5),
(-0.75, 0.25, 0.5),
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0),
(1,1,1),
(0,1,1),
)
def draw_cuboid():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for egde in edges:
for vertex in egde:
glVertex3fv(vertices[vertex])
glEnd()
def draw_cylinder():
glRotatef(1, 1, 1.25, 12.5)
glColor3f(1.0, 1.0, 0.0)
quadratic = gluNewQuadric()
gluQuadricNormals(quadratic, GLU_SMOOTH)
gluQuadricTexture(quadratic, GL_TRUE)
gluCylinder(quadratic, 0.15, 0.15, 2.5, 32, 32)
def draw_torus():
glColor3f(0.0, 1.0, 0.0)
glutWireTorus(0.2, 0.8, 50, 50)
def main():
pygame.init()
glutInit()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-0.5, 0, 0)
if event.key == pygame.K_RIGHT:
glTranslatef(0.5, 0, 0)
if event.key == pygame.K_UP:
glTranslatef(0, 0.5, 0)
if event.key == pygame.K_DOWN:
glTranslatef(0, -0.5, 0)
if event.key == pygame.K_SPACE:
glRotatef(5, 0, 1, 0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0, 0, 1.0)
if event.button == 5:
glTranslatef(0, 0, -1.0)
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
draw_cuboid()
draw_torus()
draw_cylinder()
pygame.display.flip()
pygame.time.wait(10)
main()
我为此得到的输出如下。
output
如您所见,所有三个对象都是从 window 的中心生成的。
- 如何更改它们的位置以获得类似对象的键,即中心的圆柱体(与 y 轴对齐),圆环体在其顶部,长方体在另一端的尖端?
- 如何让这个键与 y 轴对齐(默认情况下保持不变,不旋转),然后按下 space 键,我可以 start/stop 整个对象沿 y 轴旋转?
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glTranslate(0.9, 0, 2)
draw_cuboid()
glTranslate(-0.9, 0, -2)
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glTranslate(0, 1, 0);
glRotate(-90, 1, 0, 0)
draw_cylinder()
pygame.display.flip()
pygame.time.wait(10)
像 glRotatef
, glScale
and glTranslate
这样的矩阵运算定义一个矩阵并将当前矩阵乘以新矩阵。
如果必须对对象应用变换,则可以将模型矩阵乘以当前矩阵。如果此转换仅应用于单个对象,则在绘制对象后,可以将逆模型矩阵乘以当前矩阵。这会撤消转换。
但这不是执行此操作的预期方式。
通常的做法是在模型变换前先把当前矩阵存起来,画好物体后再恢复。因此,Legacy OpenGL 个矩阵组织在堆栈上。存储当前矩阵,可以将其压入堆栈。要恢复它,可以从堆栈中弹出它。
使用glPushMatrix
/glPopMatrix
存储和恢复当前矩阵。例如:
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslate(0.9, 0, 2)
draw_cuboid()
glPopMatrix()
glPushMatrix()
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glPopMatrix()
glPushMatrix()
draw_cylinder()
glPopMatrix()
pygame.display.flip()
请注意,不需要以相反的顺序执行逆矩阵运算,因为 glPopMatrix
始终与 glPushMatrix
相对。
进一步注意,存在多个不同的电流矩阵(参见 glMatrixMode
)。 Ecah顶点坐标首先由模型视图矩阵(GL_MODELVIEW
)转换。之后它被(GL_PROJECTION
)投影矩阵变换。
建议将透视投影矩阵(gluPerspective
)设置为GL_PROJECTION
矩阵,视图和模型矩阵设置为GL_MODELVIEW
矩阵:
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
最小示例:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
vertices = ((0.75, -0.25, -0.5), (0.75, 0.25, -0.5), (-0.75, 0.25, -0.5), (-0.75, -0.25, -0.5),
(0.75, -0.25, 0.5), (0.75, 0.25, 0.5), (-0.75, -0.25, 0.5), (-0.75, 0.25, 0.5))
edges = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7), (6,3), (6,4), (6,7), (5,1), (5,4), (5,7))
surfaces = ((0,1,2,3), (3,2,7,6), (6,7,5,4), (4,5,1,0), (1,5,7,2), (4,0,3,6))
colors = ((1,0,0), (0,1,0), (0,0,1), (0,0,0), (1,1,1), (0,1,1))
def draw_cuboid():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for egde in edges:
for vertex in egde:
glVertex3fv(vertices[vertex])
glEnd()
def draw_cylinder():
glRotatef(1, 1, 1.25, 12.5)
glColor3f(1.0, 1.0, 0.0)
quadratic = gluNewQuadric()
gluQuadricNormals(quadratic, GLU_SMOOTH)
gluQuadricTexture(quadratic, GL_TRUE)
gluCylinder(quadratic, 0.15, 0.15, 2.5, 32, 32)
def draw_torus():
glColor3f(0.0, 1.0, 0.0)
glutWireTorus(0.2, 0.8, 50, 50)
def main():
glutInit()
pygame.init()
glutCreateWindow(b"OpenGL Window") # prevent glutWireTorus OSError: exception: access violation reading 0x0000000000000034
display = (400, 300)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-0.5, 0, 0)
if event.key == pygame.K_RIGHT:
glTranslatef(0.5, 0, 0)
if event.key == pygame.K_UP:
glTranslatef(0, 0.5, 0)
if event.key == pygame.K_DOWN:
glTranslatef(0, -0.5, 0)
if event.key == pygame.K_SPACE:
glRotatef(5, 0, 1, 0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0, 0, 1.0)
if event.button == 5:
glTranslatef(0, 0, -1.0)
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslate(0.9, 0, 2)
draw_cuboid()
glPopMatrix()
glPushMatrix()
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glPopMatrix()
glPushMatrix()
draw_cylinder()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
main()
我正在尝试使用环面、圆柱体和长方体的组合在 PyOpenGL 中创建一个关键对象。我能够在 window 的中心分别获得这三个单独的对象,但是当我尝试将所有这些对象合并为一个 window 时,我无法做到这一点。
这是代码。
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
vertices = (
(0.75, -0.25, -0.5),
(0.75, 0.25, -0.5),
(-0.75, 0.25, -0.5),
(-0.75, -0.25, -0.5),
(0.75, -0.25, 0.5),
(0.75, 0.25, 0.5),
(-0.75, -0.25, 0.5),
(-0.75, 0.25, 0.5),
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0),
(1,1,1),
(0,1,1),
)
def draw_cuboid():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for egde in edges:
for vertex in egde:
glVertex3fv(vertices[vertex])
glEnd()
def draw_cylinder():
glRotatef(1, 1, 1.25, 12.5)
glColor3f(1.0, 1.0, 0.0)
quadratic = gluNewQuadric()
gluQuadricNormals(quadratic, GLU_SMOOTH)
gluQuadricTexture(quadratic, GL_TRUE)
gluCylinder(quadratic, 0.15, 0.15, 2.5, 32, 32)
def draw_torus():
glColor3f(0.0, 1.0, 0.0)
glutWireTorus(0.2, 0.8, 50, 50)
def main():
pygame.init()
glutInit()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-0.5, 0, 0)
if event.key == pygame.K_RIGHT:
glTranslatef(0.5, 0, 0)
if event.key == pygame.K_UP:
glTranslatef(0, 0.5, 0)
if event.key == pygame.K_DOWN:
glTranslatef(0, -0.5, 0)
if event.key == pygame.K_SPACE:
glRotatef(5, 0, 1, 0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0, 0, 1.0)
if event.button == 5:
glTranslatef(0, 0, -1.0)
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
draw_cuboid()
draw_torus()
draw_cylinder()
pygame.display.flip()
pygame.time.wait(10)
main()
我为此得到的输出如下。 output
如您所见,所有三个对象都是从 window 的中心生成的。
- 如何更改它们的位置以获得类似对象的键,即中心的圆柱体(与 y 轴对齐),圆环体在其顶部,长方体在另一端的尖端?
- 如何让这个键与 y 轴对齐(默认情况下保持不变,不旋转),然后按下 space 键,我可以 start/stop 整个对象沿 y 轴旋转?
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glTranslate(0.9, 0, 2)
draw_cuboid()
glTranslate(-0.9, 0, -2)
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glTranslate(0, 1, 0);
glRotate(-90, 1, 0, 0)
draw_cylinder()
pygame.display.flip()
pygame.time.wait(10)
像 glRotatef
, glScale
and glTranslate
这样的矩阵运算定义一个矩阵并将当前矩阵乘以新矩阵。
如果必须对对象应用变换,则可以将模型矩阵乘以当前矩阵。如果此转换仅应用于单个对象,则在绘制对象后,可以将逆模型矩阵乘以当前矩阵。这会撤消转换。
但这不是执行此操作的预期方式。
通常的做法是在模型变换前先把当前矩阵存起来,画好物体后再恢复。因此,Legacy OpenGL 个矩阵组织在堆栈上。存储当前矩阵,可以将其压入堆栈。要恢复它,可以从堆栈中弹出它。
使用glPushMatrix
/glPopMatrix
存储和恢复当前矩阵。例如:
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslate(0.9, 0, 2)
draw_cuboid()
glPopMatrix()
glPushMatrix()
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glPopMatrix()
glPushMatrix()
draw_cylinder()
glPopMatrix()
pygame.display.flip()
请注意,不需要以相反的顺序执行逆矩阵运算,因为 glPopMatrix
始终与 glPushMatrix
相对。
进一步注意,存在多个不同的电流矩阵(参见 glMatrixMode
)。 Ecah顶点坐标首先由模型视图矩阵(GL_MODELVIEW
)转换。之后它被(GL_PROJECTION
)投影矩阵变换。
建议将透视投影矩阵(gluPerspective
)设置为GL_PROJECTION
矩阵,视图和模型矩阵设置为GL_MODELVIEW
矩阵:
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
最小示例:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
vertices = ((0.75, -0.25, -0.5), (0.75, 0.25, -0.5), (-0.75, 0.25, -0.5), (-0.75, -0.25, -0.5),
(0.75, -0.25, 0.5), (0.75, 0.25, 0.5), (-0.75, -0.25, 0.5), (-0.75, 0.25, 0.5))
edges = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7), (6,3), (6,4), (6,7), (5,1), (5,4), (5,7))
surfaces = ((0,1,2,3), (3,2,7,6), (6,7,5,4), (4,5,1,0), (1,5,7,2), (4,0,3,6))
colors = ((1,0,0), (0,1,0), (0,0,1), (0,0,0), (1,1,1), (0,1,1))
def draw_cuboid():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for egde in edges:
for vertex in egde:
glVertex3fv(vertices[vertex])
glEnd()
def draw_cylinder():
glRotatef(1, 1, 1.25, 12.5)
glColor3f(1.0, 1.0, 0.0)
quadratic = gluNewQuadric()
gluQuadricNormals(quadratic, GLU_SMOOTH)
gluQuadricTexture(quadratic, GL_TRUE)
gluCylinder(quadratic, 0.15, 0.15, 2.5, 32, 32)
def draw_torus():
glColor3f(0.0, 1.0, 0.0)
glutWireTorus(0.2, 0.8, 50, 50)
def main():
glutInit()
pygame.init()
glutCreateWindow(b"OpenGL Window") # prevent glutWireTorus OSError: exception: access violation reading 0x0000000000000034
display = (400, 300)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-0.5, 0, 0)
if event.key == pygame.K_RIGHT:
glTranslatef(0.5, 0, 0)
if event.key == pygame.K_UP:
glTranslatef(0, 0.5, 0)
if event.key == pygame.K_DOWN:
glTranslatef(0, -0.5, 0)
if event.key == pygame.K_SPACE:
glRotatef(5, 0, 1, 0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0, 0, 1.0)
if event.button == 5:
glTranslatef(0, 0, -1.0)
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslate(0.9, 0, 2)
draw_cuboid()
glPopMatrix()
glPushMatrix()
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glPopMatrix()
glPushMatrix()
draw_cylinder()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
main()