PyOpenGL 光照颜色格式问题

PyOpenGL Lighting Color Format Issue

我正在尝试制作带照明的 3D 旋转环面。旋转圆环工作正常。照明是问题所在;如果我将 GL_SPECULAR 保留为默认值,则灯会正常工作。当我尝试将其设置为 RGBA 浮点四元组(它应该是什么)时,它说这是不正确的格式。我尝试使用 print(str(int(GL_SPECULAR))) 打印 GL_SPECULAR 的实际默认值 returns 浮点数 4611.0,但我找不到有关此类颜色格式的任何信息。这是我的代码:

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

glutInit()

GL_SPECULAR=(0.0,0.0,1.0,1.0)

def display():
    glClearColor(1,1,1,1)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT1)
    glEnable(GL_DEPTH_TEST)
    glLight(GL_LIGHT1,GL_SPECULAR,GL_POSITION)
    glutSolidTorus(0.3,0.5,10,10)
    glRotatef(1,1,1,0)
    glutSwapBuffers()
    glutPostRedisplay()

def main():
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA)
    glutCreateWindow('window')
    glutDisplayFunc(display)
    glutMainLoop()

if __name__=='__main__':
    main()

错误:

Traceback (most recent call last):
  File "C:\Users\trian\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\OpenGL\GLUT\special.py", line 130, in safeCall
    return function( *args, **named )
  File "c:\Users\trian\python\glut.py", line 15, in display
    glLight(GL_LIGHT1,GL_SPECULAR,GL_POSITION)
  File "src\latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
  File "src\wrapper.pyx", line 314, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "src\wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "C:\Users\trian\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__
    return self( *args, **named )
ctypes.ArgumentError: ("argument 2: <class 'TypeError'>: wrong type", (GL_LIGHT1, (0.0, 0.0, 1.0, 1.0), c_float(4611.0)))
GLUT Display callback <function display at 0x000001EBCB08E820> with (),{} failed: returning None ("argument 2: <class 'TypeError'>: wrong type", (GL_LIGHT1, (0.0, 0.0, 1.0, 1.0), c_float(4611.0)))
PS C:\Users\trian\python> 

点亮时(GL_LIGHTING) is enabled, then the color which is associated, is taken from the material parameters (glMaterial)。

如果您仍想使用当前颜色属性(由 glColor), then you have to enable GL_COLOR_MATERIAL 设置 并设置颜色 material 参数 (glColorMaterial):

glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

另见 Basic OpenGL Lighting

指令glLight(GL_LIGHT1,GL_SPECULAR,GL_POSITION)根本没有任何意义。阅读 glLight。例如:

glLightfv(GL_LIGHT1, GL_POSITION, [0, 100, 0, 0])
glLightfv(GL_LIGHT1, GL_DIFFUSE, [1, 0, 0, 1]) # red