python - 无法理解 pyOpenGl 顶点和边
问题描述
因此,我正在尝试学习 Python 中非常基本的 3D 建模,但是我很难理解顶点和边的位置以及我传递的数字的作用。这是一个例子:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
"""
- A Cube has 8 Nodes/Verticies
- 12 Lines/connections
- 6 Sides
"""
vertices = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = ( #Contains vertexes/nodes
(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 edges:
for vertex in edge:
glVertex3fv(vertices[vertex]) #Draws vertex's in position given according to vertices array
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(35, (display[0]/display[1]), 0.1, 50.0) #FOV, aspect ratio. clipping plane
glTranslatef(0.0, 0.0, -5) #X,Y,Z -5 to zoom out on z axis
glRotatef(20, 0, 0, 0) #Degrees, x,y,z
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clears the screen
Cube()
pygame.display.flip() #Cant use update
pygame.time.wait(10)
main()
pygame.quit()
quit()
我是根据 sentdex Open GL 和 Python的精彩教程制作的。但是,我很难理解他为什么要输入他为顶点所做的数字。如果有人能解释编号系统,将不胜感激!谢谢!
解决方案
vertices
是一个由 8 个不同的 3 维笛卡尔坐标组成的数组,索引从 0 到 7:
vertices = (
( 1, -1, -1), # 0
( 1, 1, -1), # 1
(-1, 1, -1), # 2
(-1, -1, -1), # 3
( 1, -1, 1), # 4
( 1, 1, 1), # 5
(-1, -1, 1), # 6
(-1, 1, 1) # 7
)
坐标定义立方体的角点。
edges
是一个定义立方体边缘的数组。数组中的每一对索引定义了从一个角点到另一个角点的线。
例如 (0, 1) 定义从 (1, -1, -1) 到 (1, 1, -1) 的边。
以下函数获取数组的每对索引,读取属于索引的 2 个坐标,并从第一个坐标到第二个坐标画一条线。为此使用 OpenGL Primitive类型GL_LINE
,它在 2 个连续点之间绘制一堆线段。
def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
推荐阅读
- html - 如何将 CSS 文件连接到 HTML 文件?
- python - 似乎无法从该网站上抓取 tbody
- python - 如何根据每个单元格的值并使用数字索引一次更改多列中的值?
- python - 如何从嵌套的 div html 中抓取内容
- java - 尝试连接到 MYSQL 数据库时出现 EJBTransactionRolledbackException
- mysql - 如何获取 MySql 数据库查询日志
- javascript - 为不同语言编辑字体的方法——HTML、CSS、JQUERY
- angular - 为什么“setAttribute”函数不适用于变量值?
- python - 这个熊猫代码是否等效?很快
- php - 属性或方法“订阅”未在实例上定义,但在 render.in Vue,js 期间引用