Game Development Community

Melv's shadows

by Andy Hawkins · in Torque Game Engine · 09/25/2005 (10:12 pm) · 13 replies

After pluging in the code Melv has provided for shadowing DTS objects, it all works, except the shadow comes from the wrong direction.

How can I make sure it always checks the position of the sun to cast it's shadows from?

#1
09/26/2005 (8:05 am)
After that resource was posted, Ben Garney recognized the problem and as far as I am aware abstracted out a call for the sun vector so that all shadow rendering would use the same reference vector.

I would suggest looking in the code for how other shadow rendering code gets the sun's position, and see if you are using the same.
#2
09/26/2005 (4:11 pm)
I did find an alternative light vector in the tut but when I went to paste it in, I found this in the code. Is there a reason why this code would fail because it seems to do the right calls.

( in void ShapeBase::renderShadow(F32 dist, F32 fogAmount) )
   // Get a real light dir...
   Point3F lightDir = gClientSceneGraph->getLightManager()->getShadowLightDirection(); 

   F32 shadowLen = 10.0f * mShapeInstance->getShape()->radius;
   Point3F pos = mShapeInstance->getShape()->center;
#3
09/26/2005 (5:45 pm)
Maybe this'll help..i did this a while back, but this is from a very old head

Point3F	lastlPos;
bool inRadius;
	LightManager * mLightManager =gClientSceneGraph->getLightManager();
    // Get a real light dir...
    Point3F alightDir;
    Point3F lightDirSum;
	// default to zero
	lightDirSum = Point3F(0,0,0);
U32 iLightCount=0;
U32 iCount;
for (U32 lightIdx=0; lightIdx < mLightManager->getNumLights(); lightIdx++)
{
	LightInfo alight = mLightManager->getLight(lightIdx);
	// Is the object within the light's radius?
	if (alight.mType == LightInfo::Point)
	{
		SphereF lightSphere(alight.mPos, alight.mRadius);
		if (mWorldSphere.isIntersecting(lightSphere)){
			//inRadius = true;
     	alightDir = Point3F(mWorldSphere.center.x,mWorldSphere.center.y,mWorldSphere.center.z)- Point3F(alight.mPos.x,alight.mPos.y,alight.mPos.z);
			alightDir.normalize();
                                               
			lightDirSum += alightDir;
			iLightCount++;
		}
	}
}

   F32 shadowLen = 10.0f * mShapeInstance->getShape()->radius;
   Point3F pos = mShapeInstance->getShape()->center;

then down here
getRenderTransform().mulP(pos);
   // pos is where shadow will be centered (in world space)

   mShadow->setRadius(mShapeInstance, mObjScale);
   U32 iShadowCastingLights;
   if (!iLightCount){
		iShadowCastingLights = 1;
		//the sun
	//	Con::printf("no dynamic lights using sun..");
		lightDirSum= gClientSceneGraph->getLightManager()->getShadowLightDirection();
   }else 
   {
   iShadowCastingLights=iLightCount;
   }
 
   //Ben said average em..
   lightDirSum.normalize();
   if (!mShadow->prepare(pos, lightDirSum / iShadowCastingLights, shadowLen, mObjScale, dist, fogAmount, mShapeInstance))
      return;
#4
09/26/2005 (11:55 pm)
Thanks I'll give it a go - It looks like the code tests all the lights and finds an average vector for all lights near the object? Or does it pick the strongest light to cast the shadow if there are many lights? Any way I'll try this and see what happens.
#5
09/27/2005 (12:37 am)
Okay - I tried that and it made no difference. Thinking I screwed up the sun pos I loaded up a simple version of starter.fps. This is what's wrong with my shadows. The DTS cast shadows the wrong way to everything else. The tree is a DTS static object

www.drewfx.com/Torque/shadows.jpg
#6
09/27/2005 (2:03 am)
Don't worry about it - I got it working :)

I forgot to change tsStatic.cc as well. I included the changes above in both shapebase.cc and tsstatic.cc and the fix worked ( for anyone reading this thread ). The pic above is what happens when you don't change both bits of code.
#7
09/27/2005 (9:21 am)
Yah, the code just averages the relavent lights near the object, i guess the light distance could also be taken into account, but for a quick and dirty it seems to werk..

glad i could be of help.
#8
10/02/2005 (11:03 pm)
Thanks for the followup, guys. :)

Nice shot. Really illustrates the problem.
#9
10/03/2005 (4:27 am)
As an aside, when I dropped the elevation of the sun down to get really long shadows, and looked away from the objects casting them, they were clipped - they were effectively switched off even though from my point of view I should still be able to see the shadows. I think it must be an optimisation to only render what can be seen in the view frustum, but I would like to mod the code to leave long shadow visible when close to objects off-screen.

Where can this be done - which bit of code?
#10
10/27/2005 (3:32 pm)
Can somebody please point me to the ressource? I cannot seem to find it ;)
#11
10/27/2005 (4:24 pm)
Here's Melv's thread on shadows...

www.garagegames.com/index.php?sec=mg&mod=forums&page=result.thread&qt=11168

For the rest, do a search using the "dts shadows"
#12
10/27/2005 (5:00 pm)
Any reason why this isn't going into head? Seems like it's been around long enough, and universally praised enough, to go in.
#13
10/28/2005 (3:46 am)
Is this for TSE only? I don't have access to that forum.

EDIT: It's working now. thanks