我尝试制作一个立方体,但我只能制作两个。如您所见,第三个立方体已损坏。我无法再制作三个立方体,因为我很难理解顶点和边。
我试着从顶点添加和删除数字,虽然面有时是正确的,但边是坏的。
有人能帮帮我吗?
下面是代码:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies0 = (
(0, 0.5, -0.5),
(0.5, 0.5, -0.5),
(0.5, 0, -0.5),
(0, 0, -0.5),
(0, 0.5, 0),
(0.5, 0.5, 0),
(0.5, 0, 0),
(0, 0, 0)
)
edges0 = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
verticies1 = (
(0.5, 0.5, -0.5),
(1, 0.5, -0.5),
(1, 0, -0.5),
(0.5, 0, -0.5),
(0.5, 0.5, 0),
(1, 0.5, 0),
(1, 0, 0),
(0.5, 0, 0)
)
edges1 = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
verticies2 = (
(-0.5, 0.5, 0),
(0, 0.5, 0),
(-0.5, 0, 0.5),
(-0.5, 0, 0),
(0, 0.5, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0)
)
edges2 = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Cube():
glBegin(GL_LINES)
for edge in edges0:
for vertex in edge:
glVertex3fv(verticies0[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges1:
for vertex in edge:
glVertex3fv(verticies1[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges2:
for vertex in edge:
glVertex3fv(verticies2[vertex])
glEnd()
def main():
pygame.init()
display = (1200,800)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 500)
button_down = False
glMatrixMode(GL_MODELVIEW)
modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glTranslatef(0.0,0.0, -5)
while True:
glPushMatrix()
glLoadIdentity()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-1, 0, 0)
if event.key == pygame.K_RIGHT:
glTranslatef(1, 0, 0)
if event.key == pygame.K_UP:
glTranslatef(0, 1, 0)
if event.key == pygame.K_DOWN:
glTranslatef(0, -1, 0)
if event.type == pygame.MOUSEMOTION:
if button_down == True:
glRotatef(event.rel[1], 1, 0, 0)
glRotatef(event.rel[0], 0, 1, 0)
print(event.rel)
for event in pygame.mouse.get_pressed():
print(pygame.mouse.get_pressed())
if pygame.mouse.get_pressed()[0] == 1:
button_down = True
elif pygame.mouse.get_pressed()[0] == 0:
button_down = False
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMultMatrixf(modelMatrix)
modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
glTranslatef(0, 0, -5)
glMultMatrixf(modelMatrix)
Cube()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
main()
1条答案
按热度按时间ql3eal8s1#
一个立方体网格就足够了。绘制这个立方体五次,使用
glTranslate
将立方体移动到世界上的不同位置。当前的变换矩阵可以使用glPushMatrix
和glPopMatrix
保存和恢复: