grunge fightr
grunge fightr

Reputation: 1370

chamging lights while drawing in open gl

I am curioust about one theme: Say that I have complex scene and one light, I can draw scene in parts by sending draw commands sequentuially (for example in 100 steps).

Can I move the light between sequential calls ? For example i would draw table vith light0 close to it then move light0 to wall then draw wall then move light0 to chair then draw chair. It would be then somewhat like assembly of it all It is legal ot it will not work and each whole frame should have one light in one position? (Same question would also be for other attributes ) tnx

Upvotes: 1

Views: 95

Answers (1)

JAre
JAre

Reputation: 4756

Of corse you can. Light position is just a variable that you pass to object shader like this:

uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform float fSpecularPower;

uniform sampler2D baseMap;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection; <------  here is your light position
varying vec3 Normal;

void main( void )
{
   vec3  fvLightDirection = normalize( LightDirection );
   vec3  fvNormal         = normalize( Normal );
   float fNDotL           = dot( fvNormal, fvLightDirection ); 

   vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection ); 
   vec3  fvViewDirection  = normalize( ViewDirection );
   float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) );

   vec4  fvBaseColor      = texture2D( baseMap, Texcoord );

   vec4  fvTotalAmbient   = fvAmbient * fvBaseColor; 
   vec4  fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor; 
   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) );

   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );

}

Its a shader with texture and one infinitely distant poin light.

If you need to move a light source between objects render its easy like changing variable value.

Upvotes: 1

Related Questions