如何在 PyOpenGL 中使整个场景的光线变暗?

How to darken the light for the whole scene in PyOpenGL?

我正在用 PyOpenGL 制作游戏,我想让灯光在玩家死亡时变暗。我该怎么做?

我尝试了多种资源,例如 Tweak the brightness/gamma of the whole scene in OpenGL and https://www.gamedev.net/forums/topic/535812-disable-opengl-lighting-completely-to-dark-scene/,但没有成功。

#!/usr/local/bin/python3
import pygame
from pygame.locals import *

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

#[...]

glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glShadeModel(GL_SMOOTH)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_BLEND)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1])

#[...]

while True:

    #[...]

            glClearColor(0.53, 0.63, 0.98, 1) #Change background colour (sky blue)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    #[...]

    if health <= 0:
        pass #this is where the darkening should go
        #---Tried---
        glDisable(GL_LIGHT1)
        glDisable(GL_LIGHT2)
        glDisable(GL_LIGHT3)
        glDisable(GL_LIGHT4)
        glDisable(GL_LIGHT5)
        glDisable(GL_LIGHT6)
        glDisable(GL_LIGHT7)
        glMaterialfv(GL_FRONT, GL_SPECULAR, @mat_specular)
        glMaterialfv(GL_FRONT, GL_SHININESS, @mat_shininess)
        glLightModelfv( GL_LIGHT_MODEL_AMBIENT, @globala )

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glDepthFunc(GL_LEQUAL)
        glEnable(GL_DEPTH_TEST)
        #---Tried---
    pygame.display.flip()

我原以为场景会很暗,但 returns 却是关于装饰器的回溯。即使我删除了这些,我的场景也没有任何变化。

您必须降低环境漫射光的 RGB 值(参见 glLight)。例如:

glLightfv(GL_LIGHT0, GL_AMBIENT, [0.2, 0.2, 0.2, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [0.8, 0.8, 0.8, 1])

环境光应用于所有表面,与光的位置或方向无关。漫射光取决于光的位置和方向以及表面的法向量(glNormal)。

灯光颜色乘以当前颜色(因为glEnable(GL_COLOR_MATERIAL)),然后将环境光、漫反射光和镜面光相加。

标准 OpenGL Blinn-Phong 光照模型是这样计算的:

Ka ... ambient material
Kd ... difusse material
Ks ... specular material

La ... ambient light
Ld ... diffuse light
Ls ... specular light
sh ... shininess

N  ... norlmal vector 
L  ... light vector (from the vertex position to the light) 
V  ... view vector (from the vertex position to the eye)

Id    = max(dot(N, L), 0.0);

H     = normalize(V + L);
NdotH = max(dot(N, H), 0.0);
Is    = pow(NdotH, sh);

fs    = Ka*La + Id*Kd*Ld + Is*Ks*Ls;