简体   繁体   中英

OpenGL Uniform Across Multiple Shaders

I have created an application in OpenGL that uses a Vertex Shader, Geometry Shader, and Fragment Shader.

I have a uniform variable, eyePositionWorld that I would like to use both in the Geometry Shader and the Fragment Shader.

(I am rendering the position of the verticies compared to the eyePositionWorld as the color)

Vertex Shader

#version 430

in vec4 vertexPositionModel;
in vec3 vertexColor;
in vec3 vertexNormalModel;

in mat4 modelMatrix;

uniform mat4 viewMatrix;//World To View
uniform mat4 projectionMatrix;//View to Projection

struct fData
{
    vec3 fragColor;
    vec3 fragPositionWorld;
    vec3 fragNormalWorld;
};

out fData geomData;

void main()
{
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vertexPositionModel;
    geomData.fragColor = vertexColor;
    geomData.fragPositionWorld = (modelMatrix * vertexPositionModel).xyz;
    geomData.fragNormalWorld = (modelMatrix * vec4(vertexNormalModel, 0.0)).xyz;
}

Geometry Shader

#version 430

layout(triangles_adjacency) in;
layout(triangle_strip, max_vertices=3) out;

struct fData
{
    vec3 fragColor;
    vec3 fragPositionWorld;
    vec3 fragNormalWorld;
};

uniform vec3 eyePositionWorldGeomShader;

in fData geomData[];
out fData fragData;

void main() {
    gl_Position = gl_in[0].gl_Position;
    fragData = geomData[0];
    fragData.fragColor = gl_in[0].gl_Position.xyz - eyePositionWorldGeomShader;
    EmitVertex();

    gl_Position = gl_in[2].gl_Position;
    fragData = geomData[2];
    fragData.fragColor = gl_in[2].gl_Position.xyz - eyePositionWorldGeomShader;
    EmitVertex();

    gl_Position = gl_in[4].gl_Position;
    fragData = geomData[4];
    fragData.fragColor = gl_in[4].gl_Position.xyz - eyePositionWorldGeomShader;
    EmitVertex();

    EndPrimitive();
}

Fragment Shader

#version 430

struct fData
{
    vec3 fragColor;
    vec3 fragPositionWorld;
    vec3 fragNormalWorld;
};

in fData fragData;

uniform vec4 ambientLight;
uniform vec3 lightPositionWorld;
uniform vec3 eyePositionWorld;
uniform bool isLighted;

out vec4 color;

void main()
{
    if (!isLighted)
    {
        color = vec4(fragData.fragColor, 1.0);
    }
    else
    {
        vec3 lightVectorWorld = normalize(lightPositionWorld - fragData.fragPositionWorld);

        float brightness = clamp(dot(lightVectorWorld, normalize(fragData.fragNormalWorld)), 0.0, 1.0);
        vec4 diffuseLight = vec4(brightness, brightness, brightness, 1.0);

        vec3 reflectedLightVectorWorld = reflect(-lightVectorWorld, fragData.fragNormalWorld);
        vec3 eyeVectorWorld = normalize(eyePositionWorld - fragData.fragPositionWorld);

        float specularity = pow(clamp(dot(reflectedLightVectorWorld, eyeVectorWorld), 0.0, 1.0), 40) * 0.5;
        vec4 specularLight = vec4(specularity, specularity, specularity, 1.0);

        //Maximum Distance of All Lights
        float maxDist = 55.0;

        float attenuation = clamp((maxDist - length(lightPositionWorld - fragData.fragPositionWorld)) / maxDist, 0.0, 1.0);

        color = (ambientLight + (diffuseLight + specularLight) * attenuation) * vec4(fragData.fragColor, 1.0);
    }
}

C++ Code (the m_eyePositionUL and m_eyePositionGeomShaderUL are both just loaded with glGetUniformLocation )

glUniform3fv(m_eyePositionUL, 1, &m_camera.getPosition()[0]);
glUniform3fv(m_eyePositionGeomShaderUL, 1, &m_camera.getPosition()[0]);

How can I only upload one uniform to OpenGL and use it in both the Geometry Shader and Vertex Shader?

It's a bit surprising but OpenGL makes it easy. All that you have to do is use the same uniform name in both Shaders!

Then just upload it once under that uniform location.

Replace uniform vec3 eyePositionWorldGeomShader; with uniform vec3 eyePositionWorld; in your Geometry Shader and keep the uniform name the same in the Fragment Shader.

Then just don't upload the other Uniform so your C++ code will simply be

glUniform3fv(m_eyePositionUL, 1, &m_camera.getPosition()[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM