Game Development Community

Shadows

by Daniel Brown · in Torque Game Engine · 04/09/2004 (4:01 am) · 9 replies

I've just noticed this, the shadow of my character doesnt change, so if im facing the sun the shadow should go behind me, but it doesnt. Is there a way to change this?

#1
04/09/2004 (7:34 am)
Yes, there are some constants in the code that control this.
#2
04/09/2004 (8:02 am)
Where abouts are they?
#3
04/09/2004 (8:43 am)
Ah, that's the tricky bit. They are scattered hither and yon. I don't know where they are at the moment; I do know that in TSE we're gonna fix this issue.

Look for hardcoded vectors with names like lightDir, sunDir, sunPos, etc. near the shadow calculation code for player and such.
#4
04/09/2004 (9:05 am)
IIRC theres really only two you need to fix.

shapeBase.cc (ShapeBase::renderShadow)
// JWV begin Fix for light direction to use actual direction rather than hardcoded
   //Point3F lightDir(0.57f,0.57f,-0.57f);
	Point3F lightDir;
	// Do we have any lights?
	if ( gClientSceneGraph->getLightManager()->getNumLights() > 0 )
	{
	   // Yes, so fetch sunlight ( always first light ).
	   // calculate real sun elevation/azimuth.
	   LightInfo sunLight = gClientSceneGraph->getLightManager()->getLight(0);
	   lightDir = sunLight.mDirection; //first light is always sun
	}
	else
	{
	   // No, so set default light.
	   lightDir.set( 0.5, 0.5, -0.5 );
	}
	// Normalise light direction.
	lightDir.normalize();
	// JWV End Fix for light direction to use actual direction rather than hardcoded

tsStatic.cc (TSStatic::renderShadow)
// Change to resource.  Change this to use actual light direction
   //Point3F lightDir(0.57f,0.57f,-0.57f);
	Point3F lightDir;
	// Do we have any lights?
	if ( gClientSceneGraph->getLightManager()->getNumLights() > 0 )
	{
	   // Yes, so fetch sunlight ( always first light ).
	   // calculate real sun elevation/azimuth.
	   LightInfo sunLight = gClientSceneGraph->getLightManager()->getLight(0);
	   lightDir = sunLight.mDirection; //first light is always sun
	}
	else
	{
	   // No, so set default light.
	   lightDir.set( 0.5, 0.5, -0.5 );
	}
	// Normalise light direction.
	lightDir.normalize();
#5
04/09/2004 (11:05 am)
As John posted, making those two modifs will get you sun aligned shadows, according to it's position. That will only work while your .dts shapes or your players is on the terrain/landscape : as soon as you step on or in a .dif interior, you'll revert back to a fixed light position, because of the way interiors are lit, and how they calculate ambient lighting, etc.
See SceneObject::installLights, in the LightingInfo::Interior case of the mLightingInfo.mLightSource switch statement (around line 1726 in sceneObject.cc) for a third spot where the light vector is hardcoded.
You can hack it so it uses the Sun light vector, like when on Terrain, or, if you're using fxLights, get the nearest/strongest, and use that one as your light vector source when on or in interiors.
Better yet, would be to make the difference between being on a .dif interior, and inside a .dif interior, most likely using the zone system/portals to see if you're really inside, or still outside, albeit on a .dif : in the first case, you'd have to decide on a appropriate light source (ie when really inside), and in the other, you'd use the sun.
Torque already has some support lighting functions to look for best lights, etc. Well, it gives you a basis to start with, but there is quite a bit of work involved :)
#6
04/09/2004 (11:09 am)
Ah sorry I didn't know about the interior lighting. We don't use interiors, so I hadn't run into it.
#7
04/09/2004 (6:36 pm)
Thanks for the update Nicolas, at least that location allows an easy fix for 'exterior' difs like roads etc. Couldn't find it for the life of me.
#8
04/09/2004 (8:52 pm)
Got sick of seeing people having trouble with this, so I did a quick, simple fix. :)

A new method on LightManager, getShadowLightDirection, is called wherever a light direction for shadowcasting is needed. It basically wraps the fix above in a method that can be called wherever.

getShadowLightDirection could be expanded, for instance, to accept a query point or BoxF, to determine the appropriate light vectors. For now, it's simple, but it should fix a lot of visual incongruities.
#9
04/13/2004 (12:18 pm)
Awesome! Thanks Ben. :-)