Torque's dynamic shadows
by Gerald Fishel · in Torque Game Engine · 04/16/2004 (2:33 pm) · 56 replies
Hi all,
I'm interested in learning about how Torque handles the dynamic shadows, and if it is a reasonable task to apply the shadows to new renderable types. Would I need to handle the shadow casting within the new types, or is there some type of container that holds polygons which the shadows can be cast on to?
Also, what about adding new polygons to the static lighting, as far as them casting static shadows on the terrain?
Any advice on where to begin in these ventures would be most appreciated.
Thanks
I'm interested in learning about how Torque handles the dynamic shadows, and if it is a reasonable task to apply the shadows to new renderable types. Would I need to handle the shadow casting within the new types, or is there some type of container that holds polygons which the shadows can be cast on to?
Also, what about adding new polygons to the static lighting, as far as them casting static shadows on the terrain?
Any advice on where to begin in these ventures would be most appreciated.
Thanks
#2
To get the player's shadow onto your new objects is pretty simple: add the object type to the shadowmask at the top of shadow.cc and implement the function buildPolyList() for your new object...done!
I am writing a new tutorial resource that demonstrates getting the player shadow to cast on your object, getting your object to cast a shadow on other objects (like the player), and getting your object to cast a static shadow onto the terrain/interiors. Unfortunately, I am probably 3 or 4 weeks from having that done.
04/16/2004 (7:33 pm)
Game/shadow.cc is where the shadow actually gets cast for tsshapes.To get the player's shadow onto your new objects is pretty simple: add the object type to the shadowmask at the top of shadow.cc and implement the function buildPolyList() for your new object...done!
I am writing a new tutorial resource that demonstrates getting the player shadow to cast on your object, getting your object to cast a shadow on other objects (like the player), and getting your object to cast a static shadow onto the terrain/interiors. Unfortunately, I am probably 3 or 4 weeks from having that done.
#3
Peace
04/16/2004 (8:15 pm)
Thanks guys, I think I have something to go on now. Very helpful, as usual.Peace
#4
Thanks
04/16/2004 (10:59 pm)
Okay, I got the player shadows to cast onto my objects now. Is it possible to dynamically modify the position of the light that casts the shadow for each individual player model? Such as... when the player goes inside of one of my new interior types, I would like to have his shadow cast by the closest lightsource to the player inside of the interior instead of by the sun. Is this something that the engine will support without too much trouble or do all of the shadows need to be cast from the same light?Thanks
#5
Thanks
04/16/2004 (11:12 pm)
Also, what about the dynamic lights, such as what you get when a projectile explodes? Is there a similar way to get those to work?Thanks
#6
In ShapeBase::renderShadow() Ben very recently changed the Point3F lightDir from a static position to:
Point3F lightDir = gClientSceneGraph->getLightManager()->getShadowLightDirection();
* this is in the HEAD
What you could do is use LightManager::addLight() to add the lights in the interior and edit getShadowLightDirection() so that it will use the most relevant light (closest and/or brightest maybe?) to return the light direction. You might have to change getShadowLightDirection() to pass in a position and let it determine if the sun is visible (gClientContainer.castRay()) or if we are inside an interior (gClientContainer.findObjects()).
I haven't had a chance to implement this yet so I can't tell you if there are any drawbacks to this approach or if it will even work =)
04/17/2004 (8:53 am)
Actually Gerald, I was just about to take a look at the very same thing...here is what I was thinking:In ShapeBase::renderShadow() Ben very recently changed the Point3F lightDir from a static position to:
Point3F lightDir = gClientSceneGraph->getLightManager()->getShadowLightDirection();
* this is in the HEAD
What you could do is use LightManager::addLight() to add the lights in the interior and edit getShadowLightDirection() so that it will use the most relevant light (closest and/or brightest maybe?) to return the light direction. You might have to change getShadowLightDirection() to pass in a position and let it determine if the sun is visible (gClientContainer.castRay()) or if we are inside an interior (gClientContainer.findObjects()).
I haven't had a chance to implement this yet so I can't tell you if there are any drawbacks to this approach or if it will even work =)
#7
04/17/2004 (9:20 am)
Matthew's got a good idea.
#8
Peace
04/17/2004 (9:39 am)
Thanks, that does make sense. And I just happened to have gotten the HEAD last night, so I am going to give that a try today. I'll let you know how it works out. Peace
#9
Thanks
04/17/2004 (11:51 am)
Just in case somebody that knows reads this before I get it figured out, where would be a good place to add the lights to the light manager? Is the list cleared every frame, or at some other point during mission load? I add them when I first load the interior and I see that the number of lights in the light manager is correct right after that, but when getShadowLightDirection is called only the sun is in the list of lights.Thanks
#10
In onAdd you need to add Sim::getLightSet()->addObject(this); and then implement the registerLights method.
Now I have no shadows at all, lol, but it is probably because of my math when figuring out the actual direction that the closest light should have, since I am using point lights. Back to work :p
Peace
04/17/2004 (12:04 pm)
Nevermind, I figured it out.In onAdd you need to add Sim::getLightSet()->addObject(this); and then implement the registerLights method.
Now I have no shadows at all, lol, but it is probably because of my math when figuring out the actual direction that the closest light should have, since I am using point lights. Back to work :p
Peace
#11
As I figured would happen, the shadows pop when switching between light influences. Will need to figure out a way to make it change smoothly, but I am well on my way. Also need to modify it take into account power of the light and whether the light is obstructed like you suggested, but that shouldnt be too hard now.
Thanks again
Peace
04/17/2004 (12:12 pm)
Yes, it is working now. Had the directions backwards.As I figured would happen, the shadows pop when switching between light influences. Will need to figure out a way to make it change smoothly, but I am well on my way. Also need to modify it take into account power of the light and whether the light is obstructed like you suggested, but that shouldnt be too hard now.
Thanks again
Peace
#12
My solution was to just add another LightInfoList to LightManager, then in getShadowLightDirection I first check to see if the sun is blocked, if not I just use the sun's direction. If the sun is blocked, then I go through my second LightInfoList and find the closest light that is not blocked. So I can add the interior lights to that list, and it no longer affects the terrain.
My next step is to have it render multiple shadows if there are additional lights within a certain radius, or to just gradually fade one out when moving between regions of influence, to keep things looking smooth. Not sure which way will be best, I'll just have to experiment.
Peace
04/17/2004 (3:00 pm)
Actually there turned out to be a problem with that. I'm not sure exactly why, but the terrain would periodically flash white, more frequently while shooting.My solution was to just add another LightInfoList to LightManager, then in getShadowLightDirection I first check to see if the sun is blocked, if not I just use the sun's direction. If the sun is blocked, then I go through my second LightInfoList and find the closest light that is not blocked. So I can add the interior lights to that list, and it no longer affects the terrain.
My next step is to have it render multiple shadows if there are additional lights within a certain radius, or to just gradually fade one out when moving between regions of influence, to keep things looking smooth. Not sure which way will be best, I'll just have to experiment.
Peace
#13
I assume you're passing a location (or better, a bounding box) to getShadowDirection?
04/17/2004 (3:24 pm)
You could average the N nearest lights. That might look goofy, but might not.I assume you're passing a location (or better, a bounding box) to getShadowDirection?
#14
I am also at the moment doing something similar to what you mentioned. I am only using the 2 closest unblocked light sources that are within a range threshold based on their power, and doing some funky averaging with the distance and power of the light. And it actually looks pretty good. There is a little glitchiness when moving in areas where there are many lights, but that can probably be solved by taking more lights into the equation.
Meanwhile I still haven't figured out how the dynamic lights work. Any tips on how to get the dynamic lights to apply to my objects?
Thanks
04/17/2004 (5:14 pm)
I was originally passing a location, but now I am passing a bounding box. The problem with the location, as you probably guessed, was that the sun would be blocked by the tiniest polygon, causing special havoc when my trees were between the object and the sun, lol.I am also at the moment doing something similar to what you mentioned. I am only using the 2 closest unblocked light sources that are within a range threshold based on their power, and doing some funky averaging with the distance and power of the light. And it actually looks pretty good. There is a little glitchiness when moving in areas where there are many lights, but that can probably be solved by taking more lights into the equation.
Meanwhile I still haven't figured out how the dynamic lights work. Any tips on how to get the dynamic lights to apply to my objects?
Thanks
#15
04/17/2004 (7:10 pm)
You have to register/unregister them with OpenGL using the LightManager.
#16
04/18/2004 (2:03 pm)
Is this something you are interested in submitting back as a resource? I think this would be very useful and I know would save me a night of coding =) Maybe even worth getting into the HEAD?
#17
Peace
04/18/2004 (3:07 pm)
Yes, I am working on putting a few things together for resources that I have been working on. This will probably be the most immediately useful, so I will try to get it up first. Maybe tonight, more likely tomorrow. Peace
#18
ps it is not using the datablock
04/19/2004 (2:05 pm)
I didn't something like this over the weekend (its so friggin weird that whatever i work on others are working on the same thing.. :P ) but here is my prob..I used triggers to move the shadow - this is mainly for spot-type lights..but it moves every player shadows..so should i move this to like sim object or something?ps it is not using the datablock
#19
04/19/2004 (2:26 pm)
How are you doing it? Are you passing a position or bounding volume to getShadowLightDirection from within the ShapeBase::renderShadow method?
#20
renderShadow. (i used setDamagelevel as an example and made a setLightDir)And it seemed to werk great until i loaded a client..on the client i saw NO shadows.. and on the server i saw both shadows..but both (players') shadows moved when only the server's player was standing in the light.. I just patched shapebase from the head so maybe i can just add lights to the light manager..but my question is how do you make it affect each player's shadow independantly? do i need to move it into some other place?
04/19/2004 (9:19 pm)
Yes I was passing lightDir from the trigger to renderShadow. (i used setDamagelevel as an example and made a setLightDir)And it seemed to werk great until i loaded a client..on the client i saw NO shadows.. and on the server i saw both shadows..but both (players') shadows moved when only the server's player was standing in the light.. I just patched shapebase from the head so maybe i can just add lights to the light manager..but my question is how do you make it affect each player's shadow independantly? do i need to move it into some other place?
Associate Kyle Carter