Search code examples
openglglslshaderglut

OpenGL, fragment shader : No such uniform named


I have an issue, I want my light to come from my object, I managed to pass its position as uniform but I can't pass its rotation matrix. My fragment shader:

#version 120

varying vec3 coordonnee_3d;
varying vec3 coordonnee_3d_locale;
varying vec3 normale;
varying vec4 color;

uniform sampler2D texture;

// vec3 light=vec3(0.5,0.5,5.0);
uniform vec4 pos_lamp;
uniform mat4 rot_lamp;
vec3 light = vec3(pos_lamp.x, pos_lamp.y, pos_lamp.z);


void main (void)
{
    vec3 n = normalize(normale);
    vec3 d = normalize(light-coordonnee_3d_locale);
    mat4 test = rot_lamp;
    vec3 r = reflect(d,n);
    vec3 o = normalize(-coordonnee_3d_locale);

    float diffuse  = 0.7*clamp(dot(n,d),0.0,1.0);
    float specular = -0.2*pow(clamp(dot(r,o),0.0,1.0),128.0);
    float ambiant  = 0.2;

    vec4 white = vec4(1.0,1.0,1.0,0.0);

    vec2 tex_coord     = gl_TexCoord[0].xy;
    vec4 color_texture = texture2D(texture,tex_coord);
    vec4 color_final   = color*color_texture;

    gl_FragColor = (ambiant+diffuse)*color_final+specular*white;

}

And in my function display_callback in my main.cpp

static void display_callback(){
    glUniformMatrix4fv(get_uni_loc(shader_program_id, "rotation_view"), 1, false, pointeur(transformation_view.rotation));
    PRINT_OPENGL_ERROR();

    vec3 cv = transformation_view.rotation_center;
    glUniform4f(get_uni_loc(shader_program_id, "rotation_center_view"), cv.x, cv.y, cv.z, 0.0f);
    PRINT_OPENGL_ERROR();

    vec3 tv = transformation_view.translation;
    glUniform4f(get_uni_loc(shader_program_id, "translation_view"), tv.x, tv.y, tv.z, 0.0f);
    PRINT_OPENGL_ERROR();

    glUniform4f(get_uni_loc(shader_program_id, "pos_lamp"), transformation_model_1.translation.x, transformation_model_1.translation.y, transformation_model_1.translation.z, 0.0f);
    PRINT_OPENGL_ERROR();

    glUniformMatrix4fv(get_uni_loc(shader_program_id, "rot_lamp"), 1, false, pointeur(transformation_model_1.rotation));
    PRINT_OPENGL_ERROR();
}

But I have this issue: No such uniform named "rot_lamp"


Solution

  • Following on from the comment, you have...

    uniform sampler2D texture;
    
    // ...
    
    mat4 test = rot_lamp;
    

    So rot_lamp is only ever use in the assignment to test and test is never used after this statement. Since setting rot_lamp cannot, therefore, affect the output of the fragment shader it will be removed from the shader program.