Sending data to the shader(GLSL) is failing when I change a line of code in the shader that has nothing to do with those uniforms

StackOverflow https://stackoverflow.com/questions/22154128

  •  19-10-2022
  •  | 
  •  

Question

working on a shader manager system using Jon Teapots tutorials. Currently Trying to integrate it with my code and find out why nothing is drawing.

When Uniforms cannot be found I print an error message to the console, using this code

GLuint ShaderProgram::getUniformLocation(std::string _name) const
{
    GLint loc = glGetUniformLocation( m_ProgramID, _name.c_str() );
    if( loc == -1 )
    {
        std::cerr << "Uniform \"" << _name.c_str() << "\" not found in program \"" <<m_ProgramName.c_str() << "\"\n";
    }
   return loc;
}

So, when I send the uniforms normally it doesn't show any errors, although nothing draws. In an attempt to see if vertices or anything were being recieved correctly by the shaders I tried avoiding using lighting calculations or anything and tried to output just a colour with full alpha. So changed my fragment output from:

out_Color = vec4(finalColour, 1.0) * texture(textureUnit0, ex_TexCoord);

Which takes into account the texture and lighting calculations(thats what finalColour is).I changed this to:

out_Color = vec4(1.0f, 1.0f, 1.0f, 1.0f);

My console now states that my light and material uniforms cannot be found? When this doesnt deal with the finding of these uniforms in the shader? so why would that affect it?

I define a material and light struct in my shader and use that to receive the uniforms to the gpu.

like:

struct lightStruct
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
};

struct materialStruct
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};

uniform lightStruct light;
uniform materialStruct material;

I Send the data like so(after I declare material and light structs globally atm):

shaderManager.setLight( "Phong-Tex", light0 );
shaderManager.setMaterial( "Phong-Tex", material0 );

Here is the set light and set material functions, I know the setUniform functions work inside here as I use them for other variables.

void ShaderManager::setLight(std::string _program, const lightStruct light)
{
    auto program = m_ShaderPrograms.find(_program);

//check the program exists
if( program != m_ShaderPrograms.end( ) )
{
    program->second->setUniform4fv("light.ambient", 1, light.ambient);
    program->second->setUniform4fv("light.diffuse", 1, light.diffuse);
    program->second->setUniform4fv("light.specular", 1, light.specular);
    program->second->setUniform4fv("lightPosition", 1, light.position);
}
else{std::cerr << "Program " <<_program <<" could not be found in shader manager getProgramID" << " /n " ;}

}

void ShaderManager::setMaterial(std::string _program, const materialStruct material)
{
        auto program = m_ShaderPrograms.find(_program);

//check the program exists
if( program != m_ShaderPrograms.end( ) )
{
    program->second->setUniform4fv("material.ambient", 1, material.ambient);
    program->second->setUniform4fv("material.diffuse", 1, material.diffuse);
    program->second->setUniform4fv("material.specular", 1, material.specular);
    program->second->setUniform1f("material.shininess", material.shininess);
}
else{std::cerr << "Program " <<_program <<" could not be found in shader manager getProgramID" << " /n " ;}
}

As far as I am aware it should be working. If you guys need more code/info let me know. Tried to give all that I thought was relevant.

No correct solution

OTHER TIPS

This is the expected behaviour. Uniforms which are not used will be optimized out of the compiled shader and invisible to glGetUniformLocation. This still happens if you use the variables in an intermediate calculation if the results of that calculation are not used in calculating any variables declared as out (or varying or gl_FragColor in compatibility mode shaders).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top