Sign in to follow this  
Striken

diffuse lighting Q. (gl matrices) - [self solved]

Recommended Posts

Striken    110
Hi all, Trying to implement basic diffuse lighting with OGL and CG shader. Seen many examples but I'm getting a frustrating problem. The lighting on my object is fine if the camera is completely stationary, but as soon as I move the camera, the direction of the light remains the same, relative to the object (as if I have not moved the camera at all). But as I said, any transformations to the object from a static camera is fine. I'm missing a multiplication somewhere? Here's my code, from frame start to end of object rendering... can anyone help me out? Please ignore the fact that the class is actually called normalmapshader!
//First call - frame start.
void IRenderer::iStartFrame(void) const
{
  glViewport(0, 0, icg::mainWindow->Width(), icg::mainWindow->Height());
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

//Second call - setting up the camera
void  ICCamera::PrepareView(void)
{
  if(!m_orthographic)
  {
    const float aspectRatio = (float)icg::mainWindow->Width() / (float)icg::mainWindow->Height();

    glMatrixMode(GL_PROJECTION);						
    glLoadIdentity();
    gluPerspective(m_fieldOfView, aspectRatio, m_nearClip, m_farClip);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(m_position[X], m_position[Y], m_position[Z], 
              m_position[X]+m_forward[X], m_position[Y]+m_forward[Y], 
              m_position[Z]+m_forward[Z], m_up[X], m_up[Y], m_up[Z]);

    glGetFloatv( GL_PROJECTION_MATRIX, m_projectionMatrix );
    glGetFloatv( GL_MODELVIEW_MATRIX, m_viewMatrix );
    MatrixMultiply( m_viewProjectionMatrix, m_projectionMatrix, m_viewMatrix );
  }
}


//Third call - object diffuse shader
void IRNormalMapShader::Enable(imVector const lightPos, imMatrix const objectMatrix)
{
    glPushMatrix();
    glMultMatrixf(objectMatrix);
    cgSetParameter4fv(m_lightPos, lightPos);
    cgGLSetStateMatrixParameter(m_modelviewIT, 
                                CG_GL_MODELVIEW_MATRIX,
                                CG_GL_MATRIX_INVERSE_TRANSPOSE);
}

//Render the Object itself
void ICGameObject::RenderMain(void)
{
  if(m_mainModel)
  {
    static_cast<ICModel*>(m_mainModel->m_resource)->RenderMain();
  }
  ICObject::RenderMain();
}

//End the render
void IRNormalMapShader::Disable(void)
{
    glPopMatrix();
}

// ***********************************************
// and of course the CG code: (vertex shader)
// ***********************************************

struct vertex
{
  float4 position	: POSITION;
  float3 normal		: NORMAL;
  float2 texcoord0      : TEXCOORD0;
};
struct fragment
{
  float4 position	 : POSITION;
  float3 colour		 : COLOR0;
  float2 texcoord0	 : TEXCOORD0;
  float3 lightVec        : TEXCOORD1;
};

uniform float4x4 modelViewProj : state.matrix.mvp;

fragment main(vertex IN, uniform float4 lightPosition, uniform float3x3 mvIT)
{
  fragment OUT;
  OUT.lightVec        = normalize(lightPosition.xyz - IN.position.xyz);
  float3 n            = normalize(mul(mvIT, IN.normal));
  float diffuseTerm   = dot( n, OUT.lightVec );
	
  // Pass the final position on to the fragment shader
  OUT.position		= mul(modelViewProj, IN.position);
  OUT.texcoord0		= IN.texcoord0;
  OUT.colour		= diffuseTerm;
  
  return OUT;
}

// *************************************************
// fragment shader just does OUT.colour = IN.colour;
// *************************************************





Please, any suggestions? [Edited by - Striken on July 10, 2007 8:21:39 AM]

Share this post


Link to post
Share on other sites
Striken    110
A couple of problems -

Firstly my normal coming in was a float4, I think then the normal value got to the shader, it seemed like the w component was undefined, and forcing it to 0 solved one part.

Also appeared to be using the incorrect inverse object matrix from the OGL state, so I took my own object matrix and inverted it. Seemed to do the trick.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this