添加三个立方体遇到困难
我正在尝试制作一个立方体,但我只能管理两个。如您所见,第三个立方体被打破了。我很难再制作三个立方体,因为我很难理解顶点和边缘。
我尝试从顶点添加和删除数字,而虽然面部有时则正确并且边缘损坏。
有人可以帮我吗?
这是代码:
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()
I'm attempting to make a cube, but I can only manage two. As you can see, the third cube is broken. I am having trouble making three more cubes because I am having a hard time understanding vertices and edges.
I tried adding and removing numbers from vertices, and while the faces are sometimes correct and the edges are broken.
Can somebody help me?
Here's the code:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个立方体网完全足够。绘制此一个立方体fife时间,然后使用
gltranslate
将立方体移至世界上不同位置。当前的转换矩阵可以保存并使用glpushmatrix
和glpopmatrix
:One cube mesh is completely sufficient. Draw this one cube fife times and use
glTranslate
to move the cubes to different positions in the world. The current transformation matrix can be saved and restored withglPushMatrix
andglPopMatrix
: