Jump to content
  • Advertisement
Sign in to follow this  
speciesUnknown

OpenGL Learning about GLSL, need explanation of this shader

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been playing around with shaders recently, and have been experimenting with this fragment shader:
/* -------------------------------------------------------

This shader implements a spotlight per pixel using the 
diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" 
as found in the book "OpenGL Programming Guide" (aka the Red Book)

Antonio Ramires Fernandes

--------------------------------------------------------- */

varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;


void main()
{
    vec3 n,halfV;
    float NdotL,NdotHV;
    vec4 color = ambientGlobal;
    float att,spotEffect;

    /* a fragment shader can't write a verying variable, hence we need
    a new variable to store the normalized interpolated normal */
    n = normalize(normal);

    /* compute the dot product between normal and ldir */
    NdotL = max(dot(n,normalize(lightDir)),0.0);

    if (NdotL > 0.0) {
        spotEffect = dot(normalize(gl_LightSource[0].spotDirection), normalize(-lightDir));
        if (spotEffect > gl_LightSource[0].spotCosCutoff) {
            spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);
            att = spotEffect / (gl_LightSource[0].constantAttenuation +
                    gl_LightSource[0].linearAttenuation * dist +
                    gl_LightSource[0].quadraticAttenuation * dist * dist);

            color += att * (diffuse * NdotL + ambient);


            halfV = normalize(halfVector);
            NdotHV = max(dot(n,halfV),0.0);
            color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);
        }
    }

    gl_FragColor = color;
}


It is clear to me that its doing a dot product spotlight function, the principle of which I vaguely understand. There are a few things I am unsure of: 1) how often does this shader get executed? is it once per texel of a fragment, or more often? e.g, is it done once per pixel that is written to the display buffer? supposing I had a quad that filled exactly 100 pixels on the screen, how many times would the shader code be executed? The current idea I have is that its done with a kind of "foreach" system. 2) How could I pass in other data? In fact, how does data in general get into a shader? I only see it reading LIGHT_0 data. Could it read a c++ variable, for example? 3) Am i correct in thinking that this shader is reading the values of OPenGL light number 0? in this case, how would I modify this to allow more than GL_MAX_LIGHTS number of lights? The standard limit of 8 is too low.

Share this post


Link to post
Share on other sites
Advertisement
1) It's run once per fragment written to the back-buffer. Ignore texels. The GL specs should have a better description of exactly what a fragment is.
Often there is a one-to-one mapping between fragments and pixels, but AFAIK things like FSAA would increase the number of fragments per pixel.

2) uniform variables are set from your C++ code.

3) Use your own uniforms instead of the built-in GL variables.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
1) It's run once per fragment written to the back-buffer. Ignore texels. The GL specs should have a better description of exactly what a fragment is.
Often there is a one-to-one mapping between fragments and pixels, but AFAIK things like FSAA would increase the number of fragments per pixel.

2) uniform variables are set from your C++ code.

3) Use your own uniforms instead of the built-in GL variables.


Great, this gives me a few things to search for and study.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!