【问题标题】:How do I add color for every pair of triangle?如何为每对三角形添加颜色?
【发布时间】:2022-06-16 01:48:51
【问题描述】:
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()

输出:

期望的输出:

【问题讨论】:

    标签: python opengl pygame pyopengl


    【解决方案1】:

    为立方体的一侧定义一种颜色的颜色列表:

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

    【讨论】:

      【解决方案2】:

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

      pinkcolor = (255,0,255)
      

      或将其插入到您构建三角形的任何位置

      然后让我们说它是一个平台:

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

      我觉得应该可以

      【讨论】:

      • 不,它不起作用!您不能将 pygame.draw.rect 与 PyOpenGL 一起使用。
      猜你喜欢
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多