【问题标题】:Python OpenGL not working properly when trying to color 3D objects尝试为 3D 对象着色时 Python OpenGL 无法正常工作
【发布时间】:2020-09-26 19:19:28
【问题描述】:

我开始使用 PyOpenGL,当我尝试着色时,它给了我一些奇特的结果

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = (
    (-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,1),
    (0,5),
    (1,2),
    (1,6),
    (2,3),
    (2,7),
    (0,3),
    (3,4),
    (4,7),
    (6,7),
    (5,6),
    (4,5)
)

surfaces = (
    (0,1,2,3),
    (0,1,6,5),
    (0,3,4,5),
    (3,2,7,4),
    (1,2,7,6),
    (4,5,6,7),
)

colors= (




)
def color_in_face(color, face_index):
    for vertex in surfaces[face_index]:
        glColor3fv(color)
        glVertex3fv(verticies[vertex])

def Cube():
    glBegin(GL_QUADS)
    color_in_face((1,0,0), 0)
    color_in_face((0,1,0), 1)
    color_in_face((0,0,1), 2)
    color_in_face((1,1,0), 3)
    color_in_face((0,1,1), 4)
    color_in_face((1,0,1), 5)
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

def main():
    to_rotate = False
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(45,  (display[0]/display[1]), 0.1, 70.0)
    glTranslatef(0.0,0.0, -5)

    glRotatef(0,0,0,0)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    to_rotate = "left"
                elif event.key == pygame.K_RIGHT:
                    to_rotate = "right"
                elif event.key == pygame.K_UP:
                    to_rotate = "up"
                elif event.key == pygame.K_DOWN:
                    to_rotate = "down"
                elif event.key == pygame.K_a:
                    to_rotate = "t-l"
                elif event.key == pygame.K_s:
                    to_rotate = "t-r"
                elif event.key == pygame.K_z:
                    to_rotate = "b-l"
                elif event.key == pygame.K_x:
                    to_rotate = "b-r"
                elif event.key == pygame.K_q:
                    to_rotate = "stop"
                elif event.key == pygame.K_w:
                    to_rotate = "reload"

        if to_rotate!=None:
            if to_rotate==False:
                glRotatef(0, 0, 0, 0)
            elif to_rotate=="left":
                glRotatef(0.5, 0, 1, 0)
            elif to_rotate=="right":
                glRotatef(0.5, 0, -1, 0)
            elif to_rotate=="up":
                glRotatef(0.5, 1, 0, 0)
            elif to_rotate=="down":
                glRotatef(0.5, -1, 0, 0)
            elif to_rotate=="t-l":
                glRotatef(0.5, 1, 1, 0)
            elif to_rotate=="t-r":
                glRotatef(0.5, 1, -1, 0)
            elif to_rotate=="b-l":
                glRotatef(0.5, -1, 1, 0)
            elif to_rotate=="b-r":
                glRotatef(0.5, -1, -1, 0)
            elif to_rotate=="stop":
                glRotatef(0, 0, 0, 0)
            elif to_rotate=="reload":
                pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
                gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
                glTranslatef(0.0, 0.0, -5)
                glRotatef(0, 0, 0, 0)

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

main()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    glRotatef(1, 1, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    Cube()
    pygame.display.flip()
    pygame.time.wait(10)

我正在使用 python 3.7,
我试过使用 python 3.5 但结果是一样的 pygame 版本 1.9.6,
PyOpenGL 版本 3.1.5

我猜这个问题可能是由于 Python 版本,但我不确定


编辑

添加答案中的建议后的新代码。

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (-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, 1),
    (0, 5),
    (1, 2),
    (1, 6),
    (2, 3),
    (2, 7),
    (0, 3),
    (3, 4),
    (4, 7),
    (6, 7),
    (5, 6),
    (4, 5)
)

surfaces = (
    (0, 1, 2, 3),
    (0, 1, 6, 5),
    (0, 3, 4, 5),
    (3, 2, 7, 4),
    (1, 2, 7, 6),
    (4, 5, 6, 7),
)

colors = (
    (1, 0, 0),
    (0, 1, 0),
    (0, 0, 1),
    (0, 0, 0),
    (1, 1, 1),
    (0, 1, 1),
    (1, 0, 0),
    (0, 1, 0),
    (0, 0, 1),
    (0, 0, 0),
    (1, 1, 1),
    (0, 1, 1)
)


def Cube():
    glBegin(GL_QUADS)
    for surface in surfaces:
        x = 0

        for vertex in surface:
            x += 1
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])

    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    to_rotate = False
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    glEnable(GL_DEPTH_TEST)
    pygame.display.gl_set_attribute(GL_DEPTH_SIZE, 24)

    gluPerspective(45, (display[0] / display[1]), 0.1, 70.0)
    glTranslatef(0.0, 0.0, -5)

    glRotatef(0, 0, 0, 0)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    to_rotate = "left"
                elif event.key == pygame.K_RIGHT:
                    to_rotate = "right"
                elif event.key == pygame.K_UP:
                    to_rotate = "up"
                elif event.key == pygame.K_DOWN:
                    to_rotate = "down"
                elif event.key == pygame.K_a:
                    to_rotate = "t-l"
                elif event.key == pygame.K_s:
                    to_rotate = "t-r"
                elif event.key == pygame.K_z:
                    to_rotate = "b-l"
                elif event.key == pygame.K_x:
                    to_rotate = "b-r"
                elif event.key == pygame.K_q:
                    to_rotate = "stop"
                elif event.key == pygame.K_w:
                    to_rotate = "reload"
                elif event.key == pygame.K_f:
                    to_rotate = "f"
                elif event.key == pygame.K_v:
                    to_rotate = "v"

        if to_rotate != None:
            if to_rotate == False:
                glRotatef(0, 0, 0, 0)
            elif to_rotate == "left":
                glRotatef(0.5, 0, 1, 0)
            elif to_rotate == "right":
                glRotatef(0.5, 0, -1, 0)
            elif to_rotate == "up":
                glRotatef(0.5, 1, 0, 0)
            elif to_rotate == "down":
                glRotatef(0.5, -1, 0, 0)
            elif to_rotate == "t-l":
                glRotatef(0.5, 1, 1, 0)
            elif to_rotate == "t-r":
                glRotatef(0.5, 1, -1, 0)
            elif to_rotate == "b-l":
                glRotatef(0.5, -1, 1, 0)
            elif to_rotate == "b-r":
                glRotatef(0.5, -1, -1, 0)
            elif to_rotate == "stop":
                glRotatef(0, 0, 0, 0)

            elif to_rotate == "f":
                glRotatef(0.5, 0, 0, 1)
            elif to_rotate == "v":
                glRotatef(0.5, 0, 0, -1)

            elif to_rotate == "reload":
                pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
                gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
                glTranslatef(0.0, 0.0, -5)
                glRotatef(0, 0, 0, 0)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)


main()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    glRotatef(1, 1, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    Cube()
    pygame.display.flip()
    pygame.time.wait(8)

【问题讨论】:

    标签: python opengl pygame pyopengl opengl-compat


    【解决方案1】:

    我遇到了类似的问题。根本原因是图形硬件相关。我的 pygame 1.9.4+pyOpenGL 3.1.2 用于正确渲染。最近我的 Intel 和 Nvidia 驱动程序+控制应用程序更新了,pygame+pyOpenGL 渲染不正确。

    Bad render

    我升级到pygame 2.0.1+pyOpenGL 3.1.5,问题依旧。

    我的解决方案是在 Nvidia 控制面板的全局设置中更改选项

    • 首选图形处理器:“自动选择” 到
    • 首选图形处理器:“高性能 NVIDIA 处理器”

    如下英伟达控制面板截图所示:

    Nvidia Control Panel with Preferred graphics processor set to "Auto-select"

    Nvidia Control Panel with Preferred graphics processor set to "High performance NVIDIA processor"

    随着图形硬件设置的这一变化,pygame+pyOpenGL 再次被正确渲染,产生了正确的 3D 表面阴影,如下图所示。

    Good render

    就我而言,我的笔记本电脑 (Dell Precision 7530) 具有集成显卡 (Intel UHD 630) 和高性能显卡 (Nvidia Quadro P2000)。使用工厂的原始设置从未遇到过这个问题(我的笔记本电脑已经使用了两年)。显然,在某些驱动程序/控制面板更新后,配置发生了变化。

    渲染问题还有另一个“肮脏”的修复,我评论一下,以防在其他情况下可能有用。此修复是关闭双缓冲并手动执行,即:

    初始化时发出命令:

    pygame.display.set_mode(display, OPENGL)   # <--- Note "DOUBLEBUF|" was removed
    
    # [... GL drawing commands ...]
    

    在所有 GL 绘图命令强制执行之后:

    glFlush()               # <--- Force the execution of GL commands
    pygame.display.flip()   #      before pygame.display.flip()
    
    # [...  rest of the code   ...]
    

    当可用的图形硬件无法进行双缓冲时,此解决方案可能很有用。

    【讨论】:

      【解决方案2】:

      您错过了启用Depth Test,如果您想使用Depth Testt,您需要确保默认帧缓冲区具有深度缓冲区。使用pygame.display.gl_set_attribute 设置深度缓冲区大小属性(GL_DEPTH_SIZE)(尝试大小为 24,如果不起作用则切换到 16):

      def main():
          to_rotate = False
          pygame.init()
          display = (800,600)
      
          pygame.display.gl_set_attribute(GL_DEPTH_SIZE, 24) # <--- set depth buffer size
      
          pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
          
          glEnable(GL_DEPTH_TEST) # <--- enable depth test
          
          gluPerspective(45,  (display[0]/display[1]), 0.1, 70.0)
          glTranslatef(0.0,0.0, -5)
      
          glRotatef(0,0,0,0)
          while True:
              # [...]
      

      默认深度测试函数为GL_LESS。如果深度测试失败,则丢弃一个片段。因此,如果一个片段被绘制在另一个片段之前绘制的更靠近相机的位置,则新片段将被丢弃。


      glRotateglTranslategluPerspective 等所有矩阵运算都指定一个新矩阵并将当前矩阵乘以新矩阵。

      如果要重置视图,则必须在glLoadIdentity之前加载Identity Matrix

      def main():
          # [...]
      
          while True:
              # [...]
      
                  
              elif to_rotate=="reload":
      
                  glLoadIdentity()  # <--- load identity matrix
      
                  gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
                  glTranslatef(0.0, 0.0, -5)
      

      旧版 OpenGL 提供了不同的当前矩阵(请参阅glMatrixMode)。投影矩阵应设置为当前投影矩阵(GL_PROJECTION),模型视图矩阵应设置为当前模型视图矩阵(GL_MODELVIEW):

      def main():
          # [...]
          
          glMatrixMode(GL_PROJECTION)
          glLoadIdentity()
          gluPerspective(45,  (display[0]/display[1]), 0.1, 70.0)
          
          glMatrixMode(GL_MODELVIEW)
          glLoadIdentity()
          glTranslatef(0.0,0.0, -5)
      

      完整示例:

      import pygame
      from pygame.locals import *
      from OpenGL.GL import *
      from OpenGL.GLU import *
      verticies = (
          (-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,1),
          (0,5),
          (1,2),
          (1,6),
          (2,3),
          (2,7),
          (0,3),
          (3,4),
          (4,7),
          (6,7),
          (5,6),
          (4,5)
      )
      
      surfaces = (
          (0,1,2,3),
          (0,1,6,5),
          (0,3,4,5),
          (3,2,7,4),
          (1,2,7,6),
          (4,5,6,7),
      )
      
      colors= ()
      def color_in_face(color, face_index):
          for vertex in surfaces[face_index]:
              glColor3fv(color)
              glVertex3fv(verticies[vertex])
      
      def Cube():
          glBegin(GL_QUADS)
          color_in_face((1,0,0), 0)
          color_in_face((0,1,0), 1)
          color_in_face((0,0,1), 2)
          color_in_face((1,1,0), 3)
          color_in_face((0,1,1), 4)
          color_in_face((1,0,1), 5)
          glEnd()
      
          glBegin(GL_LINES)
          for edge in edges:
              for vertex in edge:
                  glVertex3fv(verticies[vertex])
          glEnd()
      
      def main():
          to_rotate = False
          pygame.init()
          display = (800,600)
          pygame.display.gl_set_attribute(GL_DEPTH_SIZE, 24)
          pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
          
          glEnable(GL_DEPTH_TEST)
          
          glMatrixMode(GL_PROJECTION)
          glLoadIdentity()
          gluPerspective(45,  (display[0]/display[1]), 0.1, 70.0)
          
          glMatrixMode(GL_MODELVIEW)
          glLoadIdentity()
          glTranslatef(0.0,0.0, -5)
          
          while True:
              for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                      pygame.quit()
                      quit()
                  elif event.type == pygame.KEYDOWN:
                      if event.key == pygame.K_LEFT:
                          to_rotate = "left"
                      elif event.key == pygame.K_RIGHT:
                          to_rotate = "right"
                      elif event.key == pygame.K_UP:
                          to_rotate = "up"
                      elif event.key == pygame.K_DOWN:
                          to_rotate = "down"
                      elif event.key == pygame.K_a:
                          to_rotate = "t-l"
                      elif event.key == pygame.K_s:
                          to_rotate = "t-r"
                      elif event.key == pygame.K_z:
                          to_rotate = "b-l"
                      elif event.key == pygame.K_x:
                          to_rotate = "b-r"
                      elif event.key == pygame.K_q:
                          to_rotate = "stop"
                      elif event.key == pygame.K_w:
                          to_rotate = "reload"
      
              if to_rotate=="left":
                  glRotatef(0.5, 0, 1, 0)
              elif to_rotate=="right":
                  glRotatef(0.5, 0, -1, 0)
              elif to_rotate=="up":
                  glRotatef(0.5, 1, 0, 0)
              elif to_rotate=="down":
                  glRotatef(0.5, -1, 0, 0)
              elif to_rotate=="t-l":
                  glRotatef(0.5, 1, 1, 0)
              elif to_rotate=="t-r":
                  glRotatef(0.5, 1, -1, 0)
              elif to_rotate=="b-l":
                  glRotatef(0.5, -1, 1, 0)
              elif to_rotate=="b-r":
                  glRotatef(0.5, -1, -1, 0)
              elif to_rotate=="reload":
                  glLoadIdentity()
                  glTranslatef(0.0, 0.0, -5)
      
              glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
              Cube()
              pygame.display.flip()
              pygame.time.wait(10)
      
      main()
      

      【讨论】:

      • 我试过了,但没有任何改进我添加了glEnable(GL_EQUAL),但它保持不变我添加了glLoadIdentity(),但结果仍然相同我什至复制了完整的代码,但我仍然没有得到不同的结果还有我的 OpenGL 旧版还是新版?我不太了解它
      • @Lakshya 您的窗口(默认帧缓冲区)很可能没​​有深度缓冲区。设置深度缓冲区的大小:pygame.display.gl_set_attribute(GL_DEPTH_SIZE, 24)pygame.display.gl_set_attribute(GL_DEPTH_SIZE, 16)。我已经扩展了答案。
      • 那没有帮助我现在该怎么办?顺便说一句,我已经在问题中编辑了代码
      • 我添加了,但仍然没有结果。您可以复制我的代码并进行一些调整并将代码发送给我,然后我会完全理解我的错误是什么
      • 是 @Rabbid76 我的显卡不工作 我在其他电脑上运行了代码,它工作得非常好
      猜你喜欢
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多