light rotation (not direction) needed in shader

Minor issues with the Ogre API that can be trivial to fix
cyrfer
Orc
Posts: 424
Joined: Wed Aug 01, 2007 8:13 pm
Location: Venice, CA, USA
x 7

light rotation (not direction) needed in shader

Post by cyrfer »

This one cut me today:
I am trying to make a demo with deferred shading using textured lights, but it seems it is not possible to get the (derived) rotation of a light in a shader. OGRE already offers similar auto-constant parameters for shaders. For example, when rendering an object, the "world_matrix" is available in the shader. The 'direction' of a light is also available as an automatic-shader-parameter, but the direction is not enough information for effects that need the orientation of the light.

Can this be added before the 1.8.x release series so it is possible for better light effects with the upcoming release series? Thank you.
cyrfer
Orc
Posts: 424
Joined: Wed Aug 01, 2007 8:13 pm
Location: Venice, CA, USA
x 7

Re: light rotation (not direction) needed in shader

Post by cyrfer »

I was able to use the auto-parameter "spotlight_viewproj_matrix" to get the full rotation matrix to my shader that projects a texture based on the Light's rotation. However, there is a bug with rotating a Light in general that causes this shader parameter to have the wrong value...

By default, OGRE will use -Z_AXIS for the Light "direction", and (usually) +Y_AXIS for the UP vector, which defines the full rotation basis. In my application I specify the "direction" of the Light to be +X_AXIS (when the light has IDENTITY rotation). I rotate my Lights relative to the +X_AXIS using a parent SceneNode (so I can use the same interface as I would when rotating an Entity). I had incorrectly assumed the Light's UP vector would be correct.
WARNING TO OTHERS THAT WANT TO SET THE LIGHT DIRECTION:
There is no API to set the UP or RIGHT vectors for a Light. You may encounter errors.

I have discovered the source code involved with calculating the Light's "spotlight_viewproj_matrix" shader parameter will assume an UP vector of +Y_AXIS and even +Z_AXIS depending on the rotation of the Light. IMO it is really dangerous that the definition of the UP vector is conditional.

I believe one can work around the bug by just using the default Light "direction", which is -Z_AXIS. You will then need to rotate your Light's parent SceneNode to get a different rotation convention. This seems like a good work around until API is added to set the full rotation basis of a Light. I think OGRE should remove the ability to set the "direction" vector. This will reduce the amount of code (and bugs) in that class and it will eliminate the redundant rotation you can already do with a SceneNode.


Currently, the "spotlight_viewproj_matrix" auto-parameter is not documented well. For those interested, this matrix actually contains the following concatenated transformations:
PROJECTIONCLIPSPACE2DTOIMAGESPACE_PERSPECTIVE *
frust.getProjectionMatrixWithRSDepth() *
frust.getViewMatrix();

...where "frust" is the transformation (view) and projection state of the spot light.