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

发布于 2025-01-23 13:23:58 字数 1473 浏览 0 评论 0 原文

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

输出:

“

所需的输出:

“”

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

Output:

Desired Output:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长梦不多时 2025-01-30 13:23:58

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

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

启用:

glEnable(GL_DEPTH_TEST)

See also

注意,您的面部索引也存在问题。最小正确的示例:

“”

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

Define an list of colors with one color for side of the cube:

colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]

Use glColor3fv to set the color:

def draw_cube(): 
    glBegin(GL_TRIANGLES)
    for faceIndex, face in enumerate(faces):
        glColor3fv(colors[faceIndex // 2])
        for index in face:
            glVertex3fv(vertices[index])
    glEnd()

Enable the Depth Test:

glEnable(GL_DEPTH_TEST)

See also PyGame and OpenGL immediate mode

Note, there is also a problem with your face indices. Minimal correct example:

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()
悟红尘 2025-01-30 13:23:58

嗨,请查看此链接 python color Stonstants模块
然后创建一个变量

pinkcolor = (255,0,255)

或在构建三角形的任何地方插入它

,然后说一个平台:

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

我认为它应该起作用

Hi so check out this link Python Color Constants Module
then create a variable

pinkcolor = (255,0,255)

or insert it wherever u build the triangle

and then lets say its a platform:

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

it should work i think

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文