我正在做一个游戏,玩家需要飞过戒指才能赢得积分。我想为船舶使用不同的灯光,为场景使用圆环和灯光。我在点亮这三样东西时遇到问题。如果我在main.cpp中使用三个功能调用来运行代码,则仅第一个会正确点亮。如果我对第一个进行注释,则第二个被执行,依此类推,但是三个都不会。
World.cpp
glm::vec4 lightPositionWorld = Model * glm::vec4(.0f, 20.0f, -30.0f, 1.0f);
glUniform3f(glGetUniformLocation(programID, "LightPositionWorld"), lightPositionWorld.x, lightPositionWorld.y, lightPositionWorld.z);
Ship.cpp
glm::vec3 lightPositionSpaceShip = glm::vec3(.0, 7.0, .0);
glUniform3f(glGetUniformLocation(programID, "LightPositionSpaceShip"), lightPositionSpaceShip.x, lightPositionSpaceShip.y, lightPositionSpaceShip.z);
Gate.cpp
glm::vec3 lightPositionGate = glm::vec3(5.0, 3.0, .0);
glUniform3f(glGetUniformLocation(programID, "LightPositionGate"), lightPositionGate.x, lightPositionGate.y, lightPositionGate.z);
SimpleFragmentShader.fragmentshader
#version 330 core
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirectionCameraWorld;
in vec3 LightDirectionCameraSpaceShip;
in vec3 LightDirectionCameraGate;
out vec3 colorWorld;
out vec3 colorSpaceShip;
out vec3 colorGate;
uniform sampler2D objectTexture;
uniform vec3 LightPositionWorld;
uniform vec3 LightPositionSpaceShip;
uniform vec3 LightPositionGate;
vec3 lighting(float red, float green, float blue, float power, vec2 UV, vec3 Position_worldspace, vec3 Normal_cameraspace, vec3 EyeDirection_cameraspace, vec3 LightDirectionCamera, vec3 LightPosition)
{
vec3 LightColor = vec3(red, green, blue);
float LightPower = power;
vec3 MaterialDiffuseColor = texture(objectTexture, UV ).rgb;
vec3 MaterialAmbientColor = vec3(0.3, 0.3, 0.3) * MaterialDiffuseColor;
vec3 MaterialSpecularColor = vec3(1.0, 1.0, 1.0);
float distance = length( LightPosition - Position_worldspace );
vec3 normal = normalize(Normal_cameraspace);
vec3 light = normalize(LightDirectionCamera);
float cosTheta = clamp(dot(normal, light), .0, 1.0);
vec3 eye = normalize(EyeDirection_cameraspace);
vec3 reflect = reflect(-light, normal);
float cosAlpha = clamp( dot(eye, reflect), .0, 1.0);
vec3 color = MaterialAmbientColor +
MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distance * distance) +
MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha, 5) / (distance * distance);
return color;
}
void main()
{
colorWorld = lighting(0.81f, 0.71f, 0.23f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraWorld, LightPositionWorld);
colorSpaceShip = lighting(0.329412f, 0.329412f, 0.329412f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraSpaceShip, LightPositionSpaceShip);
colorGate = lighting(0.81f, 0.71f, 0.23f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraGate, LightPositionGate);
}
我希望场景和两个物体以不同的颜色和强度点亮。
答案 0 :(得分:2)
要获得期望的行为,不要输出多种颜色,应该为每个片段输出一种颜色:
out vec4 fragColor;
然后,更正代码的最简单方法是添加3种不同的照明结果:
fragColor = vec4(lighting(0.81f, 0.71f, 0.23f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraWorld, LightPositionWorld) +
lighting(0.329412f, 0.329412f, 0.329412f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraSpaceShip, LightPositionSpaceShip) +
lighting(0.81f, 0.71f, 0.23f, 90.0f, UV, Position_worldspace, Normal_cameraspace, EyeDirection_cameraspace, LightDirectionCameraGate, LightPositionGate), 1.0);
在您的照明方法中,MaterialAmbientColor
也应随距离而衰减,否则在计算最终颜色时将环境光添加3次。