# hmm... what am I doing wrong in my lighting?

This topic is 4723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, this has made me scratch my head for too long now, and I am thinking I must have something really, really messed up: I am _attempting_ to do just a simple point light via CG: the vertex shader code is:
struct vertexIn
{
float4 pos0:POSITION;
float4 pos1:COLOR0;
float4 normal:NORMAL;
float2 texCoord:TEXCOORD0;
};

struct vertexOut
{
float4 hpos:POSITION;
float2 texCoord:TEXCOORD0;
float4 color:COLOR0;
};

vertexOut main(vertexIn IN,
uniform float4x4 ModelView,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewIT,
uniform float3 lightsource,
uniform float time)
{
float3 realNormal,vect;
float4 ll;
vertexOut OUT;
float dotpr;

OUT.hpos=lerp(IN.pos0,IN.pos1, time);

vect.xyz=lightsource-mul(ModelView,OUT.hpos);
normalize(vect);
realNormal=mul( (float3x3)ModelViewIT, IN.normal.xyz);
//normalize(realNormal);

dotpr=(dot(vect,realNormal));
ll=lit(dotpr,1,1);

OUT.color.xyzw=ll.y;
OUT.hpos=mul(ModelViewProj,OUT.hpos);
OUT.texCoord=IN.texCoord;

return OUT;
}


now this is an outline of the rendering code:

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
gluPerspective( 45.0f, ( GLfloat )GetWidth() / ( GLfloat )GetHeight(),1.0f, 1000.0f );

glMatrixMode(GL_MODELVIEW);

glMultMatrixf( m_Camera->GetMatrix() );

//apply other transforms to place the model...

cgGLSetStateMatrixParameter(m_modelViewProj,
CG_GL_MODELVIEW_PROJECTION_MATRIX,
CG_GL_MATRIX_IDENTITY);

cgGLSetStateMatrixParameter(m_modelViewIT,
CG_GL_MODELVIEW_MATRIX,
CG_GL_MATRIX_INVERSE_TRANSPOSE);

vec4 ll;    ll=m_Camera->ApplyMatrixToVector(m_lightPosition,GenericCameraClass::IDENTITY);

//here ll.elements is just the actual array of 4 floats
//note that I am using 3fv, as I do not use the 4th component of the vector
cgGLSetParameter3fv(m_lightsource, ll.elements);

//set textures

//issue model drawing commands/set arrays for the model


now when I run this code, the lighting is a touch miffed, namely when the light source is in fromt me(the camera) the lighting goes away, and when it is behing me it is ok.. so what gives?? I've checked that my m_Camera->ApplyMatrixToVector by drawing a blue cube before the transformation centterd at the return value of m_Camera->ApplyMAtrixToTransformation and a white cube centered at m_lightPosition after the call to glMatrixMult(m_Camera->GetMAtrix()), the blue cube is always screen alighned (duh) and the white cube is algined to the "world"(as expected) and they have the same center... any help would be much appreciated... edit: oh my goodness... I am such a nicompoop! I forgot to add the line

cgGLSetStateMatrixParameter(m_modelView,
CG_GL_MODELVIEW_MATRIX,
CG_GL_MATRIX_IDENTITY);


to the part that sets the CG variables... I can not beliweve I missed that.. so many hours wasted right there... woe is me.... well now atleast it works. [Edited by - kRogue on March 16, 2005 7:17:36 AM]