1
votes

Somehow, modifying gl_FragColor does not change the final color of my 3D objects. How can this be possible? This has only happened suddenly ... as the EXACT SAME CODE was working before ...

Fragment Shader:

#version 120

uniform vec4 ambient;
uniform vec4 diffuse;
uniform vec4 specular;
uniform vec4 objColor;
uniform float shininess;
uniform bool shade;
uniform sampler2D texMap;

varying vec2 st;
varying vec3 N;
varying vec3 L;
varying vec3 E;

void main(void) {

    vec3 NN = normalize(N);
    vec3 EE = normalize(E);
    vec3 LL = normalize(L);
    vec3 H = normalize(LL+EE);  //Half vector
    float Kd = dot(LL, NN);
    float Ks = pow(max(dot(NN, H), 0.0), shininess);
    vec4 amb, diff, spec;
    amb = ambient;
    diff = Kd*diffuse;
    if(Kd <= 0.0) spec = vec4(0.0, 0.0, 0.0, 1.0);
    else spec = specular * Ks;

    if(shade) gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); <----------------- THIS LINE
    else gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); <---------------------- THIS LINE

}

Vertext Shader:

#version 120

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
uniform mat4 transform_matrix;
uniform vec4 light_position;

attribute vec2 a_Textcoord;
attribute vec4 a_Vertex;
attribute vec4 a_Normal;

varying vec2 st;
varying vec3 N;
varying vec3 L;
varying vec3 E;

void main(void) {

        vec4 clip_position = transform_matrix * a_Vertex;
        gl_Position = projection_matrix * modelview_matrix * clip_position;

        vec4 nn = transform_matrix * a_Normal;
        N = normalize(nn.xyz);
        L = light_position.xyz - clip_position.xyz;
        if(light_position.w == 0.0) L = light_position.xyz;
        E = clip_position.xyz;
        st = a_Textcoord;

}

As indicated in the Fragment Shader, "<----- THIS LINE", any object I shader, is BLACK instead of Red. I have checked all variable values sending into the shader, they match what's expected and ModelView Matrix and Projection Matrix works fine. Anyone got any ideas why this is the case?

1
Have you checked to see if your shaders compiled correctly? - Nicol Bolas
Passing colour in as a uniform is a bit iffy. You are best passing it into the vertex shader and then from there into the fragment shader. You are also not using the uniform colour param, but I am assuming this is for test purposes. Try moving the glFragColour = vec4(..) calls to after the if statement and replace them inside the if statement with something like vec4 temp = (1.0, 0.0, 0.0, 1.0); and then after the if statements do glFragColor = temp; and see if that works. Alternatively ensure that a simple glFragColor with no if statement works correctly. - const_ref
@Moore91 And what exactly is "iffy" about passing the colour as a uniform? - Christian Rau
@Christian Rau. Its more costly to do manipulations on uniforms in a frag shader than in the vertex shader IIRC. Its been a while so I may be wrong. - const_ref
@Moore91 I would wonder if giving your vertices a bunch of additional attributes and varyings to interpolate just for a value that's constant over the whole batch was in any way better. And of course passing it in as uniform into the vertex shader just to put it out as a varying to be interpolated is plain ridiculous. And in the end there isn't really a difference between vertex and fragment shaders, anyway. - Christian Rau

1 Answers

0
votes

Magically, it worked again ... I did not do anything but restart the computer (Shut-down and wait 15s, turn on). Does it have to do with OpenGL settings?