issue when passing world matrix to shader

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
phixuannhat
Posts: 15
Joined: Tue Jun 29, 2010 9:52 am

issue when passing world matrix to shader

Post by phixuannhat »

I implemented a simple per-fragment lighting shader with fixed light position and models' positions.

The render is correct until I rotate one of the model 180deg around Y-axis using setRotation(..). Then the shading on the rotated model is flipped as if the light was rotated as well.

I'm using irrCg to implement shader. The only matrix I pass to shader is ETS_PROJECTION * ETS_VIEW * ETS_WORLD, which transforms vertex position to clip plane position.

I have tried passing different matrices like inverse/transpose of ETS_WORLD but still have issue as mentioned. I have a feeling that somehow I miss passing some matrices.

Anyone having this issue?

These are screenshots

Normal shading

Image

Rotated shading
Image


This is the Cg vertex prog, which is from irrCg example, LightingFrag.cg

Code: Select all

void main_v( float4 position 	: POSITION,
	   float3 normal	: NORMAL,

	      out float4 oPosition	: POSITION,
	      out float3 objectPos: TEXCOORD0,
	      out float3 oNormal: TEXCOORD1,

                  uniform float4x4 modelViewProj)
{
  oPosition = mul(modelViewProj, position);
  objectPos = position.xyz;
  oNormal = normal;
}
phixuannhat
Posts: 15
Joined: Tue Jun 29, 2010 9:52 am

Post by phixuannhat »

Never mind. I fixed it.

I pass the inverse of ETS_WORLD and use it to transform light position from world space back to object space. Now shading is correct :D
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Nice, a more common method is to transform the Normals from object space to world space using the (non-inverse) world matrix. This is more efficient if you get multiple lights.

Cheers,

BlindSide
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply