OpenGL shader: a spotlight and a directional light

懵懂的女人 提交于 2019-12-06 11:09:18

Here is what I came up with:

The vertex shader:

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

And the fragment shader:

varying vec3 N;
varying vec3 v;
#define MAX_LIGHTS 2 
void main (void)  
{  
   vec4 finalColour;

   for (int i=0; i<MAX_LIGHTS; i++)
   {
       vec3 L = normalize(gl_LightSource[i].position.xyz - v);   
       vec3 E = normalize(-v);
       vec3 R = normalize(-reflect(L,N));  
       vec4 Iamb = gl_FrontLightProduct[i].ambient;    
       vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);
       Idiff = clamp(Idiff, 0.0, 1.0);     
       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);
       finalColour += Iamb + Idiff + Ispec;
   }
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColour;
}

Which give this image:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!