如何为 PyOpengl 中的每对三角形添加颜色?
How do I add color for every pair of triangle in PyOpengl?
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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 = ((0, 4, 3), (6, 4, 3), (1, 3, 2), (5, 7, 2), (0, 4, 1), (7, 1, 4),
(3,6, 2), (5, 1, 6), (0, 3, 1), (2, 3, 5), (6, 5, 4), (7, 2, 4))
def draw_cube():
glBegin(GL_TRIANGLES)
for edge in edges:
for index in edge:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
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)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()
输出:
期望的输出:
你好,看看这个 link https://www.webucator.com/article/python-color-constants-module/
然后创建一个变量
pinkcolor = (255,0,255)
或者把它插入你构建三角形的任何地方
然后假设它是一个平台:
pinkPlatform = pygame.draw.rect(screen,pinkcolor,pygame.Rect(pinkPlatformX, pinkPlatformY, pinkPlatformWidth, pinkPlatformHeight))
我觉得应该可以
定义一个颜色列表,其中一种颜色用于立方体的一侧:
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
使用glColor3fv
设置颜色:
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
启用 Depth Test:
glEnable(GL_DEPTH_TEST)
另见 PyGame and OpenGL immediate mode
请注意,您的面部指数也有问题。最小正确示例:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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)]
faces = [(0,1,2), (0,2,3), (5,4,7), (5,7,6), (4,0,3), (4,3,7),
(1,5,6), (1,6,2), (4,5,1), (4,1,0), (3,2,6), (3,6,7)]
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (300, 200)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
glEnable(GL_DEPTH_TEST)
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)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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 = ((0, 4, 3), (6, 4, 3), (1, 3, 2), (5, 7, 2), (0, 4, 1), (7, 1, 4),
(3,6, 2), (5, 1, 6), (0, 3, 1), (2, 3, 5), (6, 5, 4), (7, 2, 4))
def draw_cube():
glBegin(GL_TRIANGLES)
for edge in edges:
for index in edge:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
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)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()
输出:
期望的输出:
你好,看看这个 link https://www.webucator.com/article/python-color-constants-module/ 然后创建一个变量
pinkcolor = (255,0,255)
或者把它插入你构建三角形的任何地方
然后假设它是一个平台:
pinkPlatform = pygame.draw.rect(screen,pinkcolor,pygame.Rect(pinkPlatformX, pinkPlatformY, pinkPlatformWidth, pinkPlatformHeight))
我觉得应该可以
定义一个颜色列表,其中一种颜色用于立方体的一侧:
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
使用glColor3fv
设置颜色:
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
启用 Depth Test:
glEnable(GL_DEPTH_TEST)
另见 PyGame and OpenGL immediate mode
请注意,您的面部指数也有问题。最小正确示例:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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)]
faces = [(0,1,2), (0,2,3), (5,4,7), (5,7,6), (4,0,3), (4,3,7),
(1,5,6), (1,6,2), (4,5,1), (4,1,0), (3,2,6), (3,6,7)]
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (300, 200)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
glEnable(GL_DEPTH_TEST)
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)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()