Archived

This topic is now archived and is closed to further replies.

Vertex Light Shader stops working on rotation...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I got a Vertex & Pixel Shader combo to render a teapot. Everything works fine... until I rotate the teapot, which screws something up, as the lighting stops working correctly... Heres the VShader Code:
struct sOut
{
  float4 pos  : POSITION;
  float4 col  : COLOR;
  float2 tuv  : TEXCOORD0;
};

sOut main(	float4 pos		: POSITION,
			float2 tuv		: TEXCOORD0,			
			float3 normal   : NORMAL,
			
            uniform float3   lp1,   // Light Position 1

            uniform float4x4 ModelViewProj )
{
  sOut OUT;
    
  OUT.pos		= mul(ModelViewProj, pos);
  
  float3 P		= pos.xyz;
  float3 N		= normal;
  
  float3 L		= normalize( lp1 - P );
  float  D		= max(dot(N, L), 0);  
  
  OUT.tuv		= tuv; 
  
  OUT.col[0]	= D;
  OUT.col[1]	= D;
  OUT.col[2]	= D;  
  
  return OUT;	
}
What stops working exactly? Well, the teapot remains EXACTLY the same when I rotate him, as if its orientation to the lightsource hadnt changed a bit... so I''m guessing the vertex shader isnt receiving information on the ModelViewProj that the model got rotated... So, do I have to setup another matrix? A vector? What?.... Salsa cooked it, your eyes eat it!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][My DevDiary]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
                          Prozak - The GameDever formally known as pentium3id

Share this post


Link to post
Share on other sites
When you do the N.L calculation, the normal and the light vector need to be in the same space.
It looks to me like you''re using the object space position and the world space light position to calculate the light vector. You can either transform your light position into object space (by an ''inverseModelView'' matrix), or you can move the position into world space (where the light is) by transforming it by the the ''model'' matrix (not modelViewProj, you don''t want the view/projection bit!).
I''d recommend moving the light position into object space, because then the rest of your code doesn''t need changing (if you do it in another space, you would have to transform the normal as well).

-Mezz

Share this post


Link to post
Share on other sites
Everything is ok with the script, it calculates lighting on the surface of the model perfectly.

The problem is that rotation information isnt geting fed into the lighting calculations.

In pseudo-code, this is more or less what Im doing:

glPushMatrix();

Translate_Model();
Rotate_Model();

cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

Draw_Model();

glPopMatrix();

Maybe I need to pass another matrix to the shader, a rotation matrix? no?

Salsa cooked it, your eyes eat it!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][My DevDiary]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
                          Prozak - The GameDever formally known as pentium3id

Share this post


Link to post
Share on other sites
*bump*

How would you guys write a simple vertex shader to take into account angle of light position with normal of model, also taking into acount not only the position of the model, but its rotation too?

vertices are passed onto the shader in object-space, and light coordinates are passed in world-space coordinates...

Salsa cooked it, your eyes eat it!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][My DevDiary]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
                          Prozak - The GameDever formally known as pentium3id

Share this post


Link to post
Share on other sites
So either transform the vertices into world space, or transform the light into object space. And like was mentioned before, if you opt to trasform the vertices into world space, you must also transform the normals into world space. So it is better to transform the light into object space with an inverse model matrix.

Share this post


Link to post
Share on other sites