PDA

View Full Version : Problem with CG



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?

Silkut
02-18-2005, 03:40 AM
I'm a beginner too in Cg, but there is something wrong, you can see the first screenshot, the light reflection appear to 2 vertices, on the second screenshot, you can see that the light reflection appear to an array between 4 vertices, are you sure that your quadric is exactly the same ?
This spherical quadric is just a lot of triangles, maybe the sphere approximation is not exactly the same as in the tutorial.
The lighting appear when the vertex is on front of the camera (or something like that), make sure that the sphere approximation is the same, and try to move the sphere (using dragging by example), maybe the lighting reflection willbe the same

And I've no idea about the specular highlighting..

hope that helps a little

Relic
02-18-2005, 05:18 AM
There are different light model settings in OpenGL. One is LOCAL_VIEWER.
This is not the default, so the fixed function pipeline calculates the half-angle for the specular highlight with an infinite viewer.
But the Cg program does:
// Compute the specular term
float3 V = normalize(eyePosition - P);
float3 H = normalize(L + V);
which is a local viewer.

Set glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1), and re-run the program generating the first image.

I think there are some problems in the Cg program, too. Normally lighting is calculated in eye space, P is in clip space, that seems wrong. Eye positon in world space should be at the origin, not at (0, 0, 5). Maybe this is lighting in model space, but then there are inverse transformations missing. I think this example is not general purpose.
Read the OpenGL specs chapter 2.11 and 2.14.1 about transformations and lighting.

ohms
02-20-2005, 09:30 PM
Thanks, you two help a lot.