GLSL Two sided Lighting

Ok, I understand. Waiting for the test code, try to replace the main function in your fragment shader by this one:


void main()
{   
    // Normalize the normal. A varying variable CANNOT
    // be modified by a fragment shader. So a new variable
    // needs to be created.
    vec3 n = normalize(normal);
   
    vec4 ambient, diffuse, specular, color;

    // Initialize the contributions.
    ambient  = vec4(0.0);
    diffuse  = vec4(0.0);
    specular = vec4(0.0);
   
    
    if(n.z > 0) // front face
    {
		// In this case the built in uniform gl_MaxLights is used
		// to denote the number of lights. A better option may be passing
		// in the number of lights as a uniform or replacing the current
		// value with a smaller value.
		calculateLighting(1, n, vertex, gl_FrontMaterial.shininess,
		                  ambient, diffuse, specular);
	   
		color  = gl_FrontLightModelProduct.sceneColor  +
		         (ambient  * gl_FrontMaterial.ambient) +
		         (diffuse  * gl_FrontMaterial.diffuse) +
		         (specular * gl_FrontMaterial.specular);
    }
    else // back face
    {
          
		// Now caculate the back contribution. All that needs to be
		// done is to flip the normal.
		calculateLighting(1, -n, vertex, gl_BackMaterial.shininess,
		                  ambient, diffuse, specular);

		color = gl_BackLightModelProduct.sceneColor  +
		         (ambient  * gl_BackMaterial.ambient) +
		         (diffuse  * gl_BackMaterial.diffuse) +
		         (specular * gl_BackMaterial.specular);
    }

    color = clamp(color, 0.0, 1.0);
	
    gl_FragColor = color;

}

My explaination was not complete. I did not talk about the case you have a front face that is back lighted. In this configuration, with your code, dark areas would became bright.

Thats it! Thank you very much! I was confused how there should be dark areas if you simply add the values of back and front face, but since the orange book told me thats the way two sided lighting works I simply did it like that. Do you still want a glut version to check it?

Thank you very much!

Great! :slight_smile: You are welcome. If it runs correctly now, I do not need the glut code anymore.

… wrong window/thread

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.