opengl 如何为每对三角形添加颜色?

dffbzjpn  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(156)
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()

输出量:

所需输出:

zbq4xfa0

zbq4xfa01#

定义一个颜色列表,其中一种颜色用于立方体的侧面:

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和OpenGL即时模式
注意,你的脸索引也有问题。最小正确示例:

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()
u4vypkhs

u4vypkhs2#

你好,请检查此链接Python Color Constants Module,然后创建一个变量

pinkcolor = (255,0,255)

或将其插入到构建三角形的任何位置
然后让我们说它是一个平台:

pinkPlatform = pygame.draw.rect(screen,pinkcolor,
    pygame.Rect(pinkPlatformX, pinkPlatformY, pinkPlatformWidth, pinkPlatformHeight))

我想应该可以

相关问题