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

时间:2019-06-25 00:39:40

标签: python opengl lighting pyopengl opengl-compat

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

我尝试了Tweak the brightness/gamma of the whole scene in OpenGLhttps://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()

我希望场景是黑暗的,但是它返回有关装饰器的回溯。即使删除这些内容,场景也不会改变。

1 个答案:

答案 0 :(得分:1)

您必须减小环境散射光的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;