abhishek bansal

06-14-2011, 09:00 AM

hello all...

I am trying to learn Open GL lighting pipeline...i am following tutorials from lighthouse3D...in one of there tutorial they have simulated openGL lighting pipeline in a vertex shader which is this..

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

This shader implements a directional light per vertex using the

diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing"

as found in the book "OpenGL Programming Guide" (aka the Red Book)

António Ramires Fernandes

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

void main()

{

vec3 normal, lightDir, viewVector, halfVector;

vec4 diffuse, ambient, globalAmbient, specular = vec4(0.0);

float NdotL,NdotHV;

/* first transform the normal into eye space and normalize the result */

normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that according to the

OpenGL specification, the light is stored in eye space. Also since

we're talking about a directional light, the position field is actually

direction */

lightDir = normalize(vec3(gl_LightSource[0].position));

/* compute the cos of the angle between the normal and lights direction.

The light is directional so the direction is constant for every vertex.

Since these two are normalized the cosine is the dot product. We also

need to clamp the result to the [0,1] range. */

NdotL = max(dot(normal, lightDir), 0.0);

/* Compute the diffuse, ambient and globalAmbient terms */

diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;

/* compute the specular term if NdotL is larger than zero */

if (NdotL > 0.0) {

NdotHV = max(dot(normal, normalize(gl_LightSource[0].halfVector.xyz)),0.0);

specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);

}

gl_FrontColor = globalAmbient + NdotL * diffuse + ambient + specular;

gl_Position = ftransform();

}

now i am sending variable values through this set of command in openGL

glLightfv(GL_LIGHT0, GL_POSITION, lpos);

glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

now a strange thing i am finding is when i put above commands in my init() function i get different results and different when i put them in my render() function for same values of all variables....why is this happening??

i am attaching both results..

here variable values are..

float lpos[4] = {1.0,0.0,1.0,0.0};

GLfloat diffuse[] = { 0.5, 0.0, 1.0, 1.0 };

GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };

GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };

GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };

GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };

GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat mat_specular[] = { 1.0,1.0, 1.0, 1.0 };

GLfloat high_shininess[] = { 100.0 };

Thank You

I am trying to learn Open GL lighting pipeline...i am following tutorials from lighthouse3D...in one of there tutorial they have simulated openGL lighting pipeline in a vertex shader which is this..

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

This shader implements a directional light per vertex using the

diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing"

as found in the book "OpenGL Programming Guide" (aka the Red Book)

António Ramires Fernandes

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

void main()

{

vec3 normal, lightDir, viewVector, halfVector;

vec4 diffuse, ambient, globalAmbient, specular = vec4(0.0);

float NdotL,NdotHV;

/* first transform the normal into eye space and normalize the result */

normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that according to the

OpenGL specification, the light is stored in eye space. Also since

we're talking about a directional light, the position field is actually

direction */

lightDir = normalize(vec3(gl_LightSource[0].position));

/* compute the cos of the angle between the normal and lights direction.

The light is directional so the direction is constant for every vertex.

Since these two are normalized the cosine is the dot product. We also

need to clamp the result to the [0,1] range. */

NdotL = max(dot(normal, lightDir), 0.0);

/* Compute the diffuse, ambient and globalAmbient terms */

diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;

/* compute the specular term if NdotL is larger than zero */

if (NdotL > 0.0) {

NdotHV = max(dot(normal, normalize(gl_LightSource[0].halfVector.xyz)),0.0);

specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);

}

gl_FrontColor = globalAmbient + NdotL * diffuse + ambient + specular;

gl_Position = ftransform();

}

now i am sending variable values through this set of command in openGL

glLightfv(GL_LIGHT0, GL_POSITION, lpos);

glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

now a strange thing i am finding is when i put above commands in my init() function i get different results and different when i put them in my render() function for same values of all variables....why is this happening??

i am attaching both results..

here variable values are..

float lpos[4] = {1.0,0.0,1.0,0.0};

GLfloat diffuse[] = { 0.5, 0.0, 1.0, 1.0 };

GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };

GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };

GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };

GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };

GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat mat_specular[] = { 1.0,1.0, 1.0, 1.0 };

GLfloat high_shininess[] = { 100.0 };

Thank You