如何将浮点值发送到片段着色器?

How to send float value to fragment shader?

我不知道如何将浮点数传递给我的片段着色器。我要改变

float ambientStrength;

作为取自主循环的值。例如,应该有一个案例(“O”键)将此 ambientStrength 更改为 1.0。

case sf::Event::KeyPressed:
                switch (windowEvent.key.code) {
                case sf::Keyboard::Escape:
                    running = false;
                    break;
                case sf::Keyboard::O:
                    
                    break;
                }

片段着色器看起来像这样:

const GLchar* fragmentSource = R"glsl(
#version 150 core
in vec3 Color;
in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;
in vec3 lightPos;
in int lightOn
out vec4 outColor;
uniform sampler2D texture1;

void main()
{
float ambientStrength = 0.1; //I WANT CHANGE THIS FROM THE PRESSED KEY
vec3 ambientlightColor = vec3(1.0,1.0,1.0);
vec4 ambient = ambientStrength * vec4(ambientlightColor,1.0);
vec3 difflightColor = vec3(1.0,1.0,1.0);
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * difflightColor;
outColor = vec4(Color, 1.0);
outColor = (ambient+vec4(diffuse,1.0)) * texture(texture1, TexCoord);

//outColor=texture(texture1, TexCoord);
}
)glsl";

在片段着色器中包含“uniform float ambientStrength”并在您的 C++ 代码击键中使用它:

    GLint loc = glGetUniformLocation(ProgramObject, "ambientStrength");
    if (loc != -1)
    {
      const float value = 0.5;
      glUniform1f(loc, value);
    }