使用pyopengl,我正在尝试创建一个圆柱体。圆柱体的顶部不是圆形的,而底部是如下图所示的图像所示。我想知道如何解决这个问题,如果它是我编码它的方式,或者只是我做的方式不适用于pyopengl。我使用的是Pygame 1.9.2,Python 3.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()
答案 0 :(得分:0)
圆柱体可分为两个圆形和一个连接两个圆形的矩形管/片。为避免重复某些顶点,我使用GL_TRIANGLE_FAN
(对于圆圈)和GL_TRIANGLE_STRIP
(对于管道)而不是GL_TRIANGLE
和GL_QUAD
。如果您想了解更多相关信息,建议您查看this回答。
下面是一个演示,显示一个旋转圆柱,中间有一个红色端,一个蓝色端和一个绿色管(重要代码在draw_cylinder
函数中):
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import sys
def draw_cylinder(radius, height, num_slices):
r = radius
h = height
n = float(num_slices)
circle_pts = []
for i in range(int(n) + 1):
angle = 2 * math.pi * (i/n)
x = r * math.cos(angle)
y = r * math.sin(angle)
pt = (x, y)
circle_pts.append(pt)
glBegin(GL_TRIANGLE_FAN)#drawing the back circle
glColor(1, 0, 0)
glVertex(0, 0, h/2.0)
for (x, y) in circle_pts:
z = h/2.0
glVertex(x, y, z)
glEnd()
glBegin(GL_TRIANGLE_FAN)#drawing the front circle
glColor(0, 0, 1)
glVertex(0, 0, h/2.0)
for (x, y) in circle_pts:
z = -h/2.0
glVertex(x, y, z)
glEnd()
glBegin(GL_TRIANGLE_STRIP)#draw the tube
glColor(0, 1, 0)
for (x, y) in circle_pts:
z = h/2.0
glVertex(x, y, z)
glVertex(x, y, -z)
glEnd()
pygame.init()
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height), OPENGL | DOUBLEBUF)
clock = pygame.time.Clock()
rotation = 0.0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
rotation += 1.0
glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(30, float(width)/height, 1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(0, 0, -50)#move back far enough to see this object
glRotate(rotation, 0, 1, 0)#NOTE: this is applied BEFORE the translation due to OpenGL multiply order
draw_cylinder(5, 10, 20)
pygame.display.flip()
clock.tick(60)
顺便说一句,你可能已经意识到像glBegin
和glVertex
这样的固定功能方法真的很古老而且已经过时了,但我认为提及它并不会有什么坏处。使用这种代码,您基本上是一次向GPU发送一个顶点,这不是很有效。如果您在未来遇到性能问题,可能需要查看VBO。希望这有帮助!