ohms

02-18-2005, 01:09 AM

Hi,

I try to follow through the per vertex lighting code from the Cg tutorial book but the result from my shader is not as I expected. I don't think the vertex program code from the book is wrong. But I don't know where is the problem. Can anyone help me find this answer.

This is the result from OpenGL lighting function.

http://www.cp.eng.chula.ac.th/~g47ntp/GLlight.jpg

This is the result from my lighting shader.

http://www.cp.eng.chula.ac.th/~g47ntp/CGlight.jpg

These is my OpenGL code that send parameters to vertex program.

CGparameter Ke, Ka, Kd, Ks;

CGparameter cgGlobalAmbient, cgModelViewProj, cgLightColor, cgLightPosition;

CGparameter cgShininess, cgEyePosition;

GLUquadricObj *sphere = gluNewQuadric();

GLfloat matSpecular[] = {1.0, 0.3, 1.0, 1.0};

GLfloat matShininess = 50.0;

GLfloat lightPos[] = {10.0, 10.0, 10.0, 1.0};

GLfloat eyePos[] = {0.0, 0.0, 5.0};

GLfloat whiteLight[] = {1.0, 10.0, 1.0, 1.0};

GLfloat ambientLight[] = {0.3, 0.3, 0.3, 1.0};

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

// Set CG's parameters value

cgGLSetParameter3f(Ke, 0.0, 0.0, 0.0);

cgGLSetParameter3fv(Ka, &objectColor[0]);

cgGLSetParameter3fv(Kd, &objectColor[0]);

cgGLSetParameter3fv(Ks, &matSpecular[0]);

cgGLSetParameter3fv(cgLightPosition, &lightPos[0]);

cgGLSetParameter3fv(cgLightColor, &whiteLight[0]);

cgGLSetParameter3fv(cgGlobalAmbient, &ambientLight[0]);

cgGLSetParameter3fv(cgEyePosition, &eyePos[0]);

cgGLSetParameter1f(cgShininess, matShininess);

cgGLSetStateMatrixParameter(cgModelViewProj,

CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );And this is vertex shader from the CG book.

struct CGV_OUT

{

float4 position : POSITION;

float4 color : COLOR;

};

CGV_OUT cgvBasicLight( float4 position : POSITION,

float3 normal : NORMAL,

uniform float4x4 modelViewProj,

uniform float3 globalAmbient,

uniform float3 lightColor,

uniform float3 lightPosition,

uniform float3 eyePosition,

uniform float3 Ke,

uniform float3 Ka,

uniform float3 Kd,

uniform float3 Ks,

uniform float shininess)

{

CGV_OUT OUT;

OUT.position = mul(modelViewProj, position);

float3 P = position.xyz;

float3 N = normal;

// Compute the emissive term

float3 emissive = Ke;

// Compute the ambient term

float3 ambient = Ka * globalAmbient;

// Compute the diffuse term

float3 L = normalize(lightPosition - P);

float diffuseLight = max(dot(N, L), 0);

float3 diffuse = Kd * lightColor * diffuseLight;

// Compute the specular term

float3 V = normalize(eyePosition - P);

float3 H = normalize(L + V);

float specularLight = pow(max(dot(N, H), 0), shininess);

//if(diffuseLight == 0) specularLight = 0;

float3 specular = Ks * lightColor * specularLight;

OUT.color.xyz = emissive + ambient + diffuse + specular;

OUT.color.w = 1;

return OUT;

}Did I do something wrong or this result is what it should be.

PS. In the line

if(diffuseLight == 0) specularLight = 0; If I don't comment this out the specular highlight will not be appeared. But in the book they said that this line prevent specular highlight from appear on the vertex that face away from the light. What wrong with this?

I try to follow through the per vertex lighting code from the Cg tutorial book but the result from my shader is not as I expected. I don't think the vertex program code from the book is wrong. But I don't know where is the problem. Can anyone help me find this answer.

This is the result from OpenGL lighting function.

http://www.cp.eng.chula.ac.th/~g47ntp/GLlight.jpg

This is the result from my lighting shader.

http://www.cp.eng.chula.ac.th/~g47ntp/CGlight.jpg

These is my OpenGL code that send parameters to vertex program.

CGparameter Ke, Ka, Kd, Ks;

CGparameter cgGlobalAmbient, cgModelViewProj, cgLightColor, cgLightPosition;

CGparameter cgShininess, cgEyePosition;

GLUquadricObj *sphere = gluNewQuadric();

GLfloat matSpecular[] = {1.0, 0.3, 1.0, 1.0};

GLfloat matShininess = 50.0;

GLfloat lightPos[] = {10.0, 10.0, 10.0, 1.0};

GLfloat eyePos[] = {0.0, 0.0, 5.0};

GLfloat whiteLight[] = {1.0, 10.0, 1.0, 1.0};

GLfloat ambientLight[] = {0.3, 0.3, 0.3, 1.0};

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

// Set CG's parameters value

cgGLSetParameter3f(Ke, 0.0, 0.0, 0.0);

cgGLSetParameter3fv(Ka, &objectColor[0]);

cgGLSetParameter3fv(Kd, &objectColor[0]);

cgGLSetParameter3fv(Ks, &matSpecular[0]);

cgGLSetParameter3fv(cgLightPosition, &lightPos[0]);

cgGLSetParameter3fv(cgLightColor, &whiteLight[0]);

cgGLSetParameter3fv(cgGlobalAmbient, &ambientLight[0]);

cgGLSetParameter3fv(cgEyePosition, &eyePos[0]);

cgGLSetParameter1f(cgShininess, matShininess);

cgGLSetStateMatrixParameter(cgModelViewProj,

CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );And this is vertex shader from the CG book.

struct CGV_OUT

{

float4 position : POSITION;

float4 color : COLOR;

};

CGV_OUT cgvBasicLight( float4 position : POSITION,

float3 normal : NORMAL,

uniform float4x4 modelViewProj,

uniform float3 globalAmbient,

uniform float3 lightColor,

uniform float3 lightPosition,

uniform float3 eyePosition,

uniform float3 Ke,

uniform float3 Ka,

uniform float3 Kd,

uniform float3 Ks,

uniform float shininess)

{

CGV_OUT OUT;

OUT.position = mul(modelViewProj, position);

float3 P = position.xyz;

float3 N = normal;

// Compute the emissive term

float3 emissive = Ke;

// Compute the ambient term

float3 ambient = Ka * globalAmbient;

// Compute the diffuse term

float3 L = normalize(lightPosition - P);

float diffuseLight = max(dot(N, L), 0);

float3 diffuse = Kd * lightColor * diffuseLight;

// Compute the specular term

float3 V = normalize(eyePosition - P);

float3 H = normalize(L + V);

float specularLight = pow(max(dot(N, H), 0), shininess);

//if(diffuseLight == 0) specularLight = 0;

float3 specular = Ks * lightColor * specularLight;

OUT.color.xyz = emissive + ambient + diffuse + specular;

OUT.color.w = 1;

return OUT;

}Did I do something wrong or this result is what it should be.

PS. In the line

if(diffuseLight == 0) specularLight = 0; If I don't comment this out the specular highlight will not be appeared. But in the book they said that this line prevent specular highlight from appear on the vertex that face away from the light. What wrong with this?