我正在使用opengl创建一个圆柱体。圆柱体的顶部不是圆形,而底部如下图所示。我想知道如何解决这个问题,如果这是我编写它的方式,或者只是我做它的方式不适用于pyopengl。我使用的是pygame1.9.2、python3.5和PyOpenGL-3.1.0。在
https://i.stack.imgur.com/KYPLY.jpg
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def securityCamera(radius,halflength,slices):
glBegin(GL_TRIANGLES)
for i in range(1,slices+1):
angleSize=(2*math.pi)/slices
theta=i*angleSize
nextTheta=(i+1)*angleSize
glColor3fv((0/256,0/256,0/256))
glVertex3f(radius*math.cos(nextTheta), halflength, radius*math.cos(nextTheta))
glVertex3f(radius*math.cos(theta), halflength, radius*math.sin(theta))
glVertex3f(0.0, halflength, 0.0)
glVertex3f(radius*math.cos(nextTheta), -halflength, radius*math.sin(nextTheta))
glVertex3f(radius*math.cos(theta), -halflength, radius*math.sin(theta))
glVertex3f(0.0, -halflength, 0.0)
glEnd()
glBegin(GL_QUADS)
for i in range(1,slices+1):
angleSize=(2*math.pi)/slices
theta=i*angleSize
nextTheta=(i+1)*angleSize
glColor3fv((256/256,256/256,256/256))
glVertex3f(radius*math.cos(theta), halflength, radius*math.sin(theta))
glVertex3f(radius*math.cos(nextTheta), halflength, radius*math.cos(nextTheta))
glVertex3f(radius*math.cos(theta), -halflength, radius*math.sin(theta))
glVertex3f(radius*math.cos(nextTheta), -halflength, radius*math.sin(nextTheta))
glEnd()
一个圆柱体可以分成两个圆和一个连接这两个圆的矩形管/薄板。为了避免重复某些顶点,我使用了}和{}。如果你想了解更多,我建议你看看this答案。在
GL_TRIANGLE_FAN
(对于圆)和GL_TRIANGLE_STRIP
(对于管状体),而不是{下面是一个演示,它显示了一个旋转的圆柱体,一端是红色,一端是蓝色,中间是一个绿色的管子(重要的代码在
draw_cylinder
函数中):顺便说一句,您可能知道固定函数方法,如
glBegin
和glVertex
确实很古老和过时,但我认为不必赘述。使用这种代码,基本上每次只向GPU发送一个顶点,这不是很有效。如果你想以后再去看看VBOs的性能,你可能会发现问题。希望这有帮助!在相关问题 更多 >
编程相关推荐