OpenGL pixel shader (Phong)

Hi All

AFAIK I need to create OpenGL pixel shader to have a OpenGL phong preview.
I’m familiar with phong render but never did it in OpenGL. My task is quite standard - I need just a lighting with surface normal interpolated for every pixel. Is there a standard pixel shader(s) (or other ways to do this) that could be used?

Thanks
Tom

The most canonical way to do this is some variant of the following:

  
  
  
varying vec3 N; // normal  
varying vec3 v; // view  
void main(void)    
{       
   v = vec3(gl_ModelViewMatrix * gl_Vertex);         
   N = normalize(gl_NormalMatrix * gl_Normal);  
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;    
}  
  

  
  
varying vec3 N;  
varying vec3 v;   
#define MAX_LIGHTS 3 // can be something else  
   
void main (void)   
{   
   vec3 N = normalize(vN);  
   vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);  
     
   for (int i=0;i<MAX_LIGHTS;i++)  
   {  
      // get each light position  
      vec3 L = normalize(gl_LightSource[i].position.xyz - v);   
      vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)   
      vec3 R = normalize(-reflect(L,N));   
     
      //Ambient Term, i.e. the color un-tinted by reflected light  
      vec4 Iamb = gl_FrontLightProduct[i].ambient;   
      //Diffuse Term i.e. the light partially tinted by reflect light  
      vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);  
      Idiff = clamp(Idiff, 0.0, 1.0);   
     
      // Specular Term, i.e for the light reflected directly back at the camera  
      vec4 Ispec = gl_FrontLightProduct[i].specular   
             * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);  
      Ispec = clamp(Ispec, 0.0, 1.0);   
     
      finalColor += Iamb + Idiff + Ispec;  
   }  
     
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColor;   
}  
  

There’s lots of faster/cooler ways to do this, but that’s really the base one which I have from some openGL doc (not sure which one)

Ola Joshua,

We are learning and trying, as soon we figured out, I will reply with news!

Thanks a lot

Tom

Hi again

We’ve added a simplest shader to our app and it works fine, thx! Now looking for:

  • is there a way to detect count of lights automatically? Or need to pass uniform variable from app?

  • how to get actual “reference” color. e.g. pixel’s color defined by actual texture or together with material’s diffuse (if texture is semi-transparent)

  • can you please suggest a good book/link? Yes, there is a lot of info in web but it’s either too big/fundamental (with tons of things out of our task) or vice versa, too simple and repeat what’s already learned

Thanks
Tom

I really suggest the Orange Book http://www.3dshaders.com/home/ to start. Also, looking on the OF forum will give you a lot of useful shaders and links to resources aimed at beginners.

i have this book! it is very useful… and @Tommato also you can found a lot of examples in the repository.
cheers

Thanks, I’ll take a look

Tom