gpt4 book ai didi

c++ - 用不同的颜色和强度照亮物体

转载 作者:行者123 更新时间:2023-11-28 01:25:29 26 4
gpt4 key购买 nike

我正在制作一款游戏,玩家需要飞过圆环才能获得积分。我想为我的船、戒指和场景使用不同的灯光。我在点亮这三样东西时遇到了问题。如果我在 main.cpp 中运行带有三个函数调用的代码,只有第一个被正确点亮。如果我评论第一个,第二个就会被执行,依此类推,但不会同时执行三个。

世界.cpp

glm::vec4 lightPositionWorld = Model * glm::vec4(.0f, 20.0f, -30.0f, 1.0f);  
glUniform3f(glGetUniformLocation(programID, "LightPositionWorld"), lightPositionWorld.x, lightPositionWorld.y, lightPositionWorld.z);

发货.cpp

glm::vec3 lightPositionSpaceShip = glm::vec3(.0, 7.0, .0);
glUniform3f(glGetUniformLocation(programID, "LightPositionSpaceShip"), lightPositionSpaceShip.x, lightPositionSpaceShip.y, lightPositionSpaceShip.z);

门.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);
}

我希望场景和两个物体以不同的颜色和强度发光。

最佳答案

要获得您期望的行为,您不应输出多种颜色,您应该为每个片段输出一种颜色:
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 倍。

关于c++ - 用不同的颜色和强度照亮物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com