Game Development Community

dev|Pro Game Development Curriculum

Polished up daylight cycles

by Chris \"Hobbiticus\" Weiland · 03/31/2003 (11:10 am) · 71 comments

Download Code File

Basically, I've rewritten and torque-ized Josh Ritter's day/night cycles to be a lot more robust. Instead of keeping everything in ugly and "unstable" externs, I've put everything into it's own class with good stuff like information hiding. It's still a little ugly because of all of the statics, however.

All you should have to do is apply the patch to a head version of torque, add the game/fx/daylightCycle.h/cc to your project file, compile and run.

Also, IF YOU GET MY TERRAIN BUMP MAPPING CODE (now available), you can go change this:
VectorF sunVector;
		//if you use my updated day/night cycles you can use this for dynamic bumps
		//sunVector = DaylightCycle::getTrueSunVector();
		static Vector<LightInfo*> lights;
		lights.clear();
		gClientSceneGraph->getLightManager()->getLights(lights);
		sunVector = -lights[0]->mDirection; //first light is always sun
		
		//find s and t tangents
to this:
VectorF sunVector = DaylightCycle::getTrueSunVector();
		
		//find s and t tangents
in terrRender.cc

Then you will get dynamically generated bumps that change with the sun vector!

Added benefits:
Ability to get sunlight vector/color easily, only runs when sky is being rendered, modular, scalable, toggleable.

Have fun!
#41
10/31/2003 (1:53 pm)
Yes i found that :p thanks anyway..

However, it would be good if when clients joined in muliplayer their dayligth cycle was at the same stage as the servers cycle rather that at their own stag eof the cycle..

Because some clients will have a sunny well lit day while others can be at opposite end of the cycle and be pitch black..
#42
11/20/2003 (11:13 pm)
Is there a reason the trees are a nice bright green at night?

I told my girlfriend they were magical trees, but she didn't buy that as an excuse =P
#43
12/07/2003 (11:49 pm)
I just applied the patch and when I first builit everything compiled fine but the sun did not move. The patch file says to add these lines.
mUseDayNightCycle = false;
mTimeScale = 96000;

I set mUseDayNightCycle to true and the lighting seemed to work but there were two suns (one red on yellow one) and the red one seemed to move as it should but the yellow one moved eratically. I asume this variable is supposed to be set to true but does anyone know what may be going wrong?
#44
01/02/2004 (11:48 am)
If you are using Release_1_2_0 or the HEAD it has it's own fxSunlight in missions by default. The day/night cycle *I believe* creates its own instance, which is confusing the effects.

Try removing your existing fxSunlight and see if things don't calm down for you.
#45
01/14/2004 (9:15 pm)
#46
01/17/2004 (8:02 pm)
The resource grabs a fxSunLight (at random if there are more than one, it would seem) and moves it across the sky. As for setting mUseDayNightCycle = false, that's a DEFAULT value. Look in the mission editor under sky->misc for a scripted hook to that variable. It's meant to be toggleable by mission.

It also does not create it's own instance. There are just two in the demo mission.
#47
02/18/2004 (4:26 pm)
For some reason, the sun doesnt get rendered when looking directly at it.. it only gets rendered when i'm looking at it in the corner of the cam.. and i think it has something to do with scripting.. not the engine.. any thoughts?
#48
02/18/2004 (4:57 pm)
Did you add the detailed options resource?

Im asking this because it uses visibility modifer, and if this is not full, then u cannt see the sun (set the slider to full). This was the problem in my cass anyway.
#49
02/29/2004 (4:40 pm)
How did you get this patch with the recent head? I always get errors applying the patch.

For example - from a clean tip:

Quote:
|--- engine/terrain/fluidRender.cc 2002/11/24 14:07:05 1.6
|+++ engine/terrain/fluidRender.cc 2003/03/29 02:56:22
--------------------------
File to patch:
Skip this patch? [y]
Skipping patch.
8 out of 8 hunks ignored
can't find file to patch at input line 937
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|Index: engine/terrain/sky.cc
|===================================================================
|RCS file: /cvs/torque/torque/engine/terrain/sky.cc,v
|retrieving revision 1.16
|diff -u -r1.16 sky.cc
|--- engine/terrain/sky.cc 2002/09/08 14:59:58 1.16
|+++ engine/terrain/sky.cc 2003/03/29 02:56:22
--------------------------
File to patch:
Skip this patch? [y]
Skipping patch.
21 out of 21 hunks ignored
can't find file to patch at input line 1184
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|Index: engine/terrain/sky.h
|===================================================================
|RCS file: /cvs/torque/torque/engine/terrain/sky.h,v
|retrieving revision 1.5
|diff -u -r1.5 sky.h
|--- engine/terrain/sky.h 2002/08/16 20:49:08 1.5
|+++ engine/terrain/sky.h 2003/03/29 02:56:22
--------------------------
File to patch:
Skip this patch? [y]
Skipping patch.
2 out of 2 hunks ignored
can't find file to patch at input line 1211
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------

Basically the patch skipped most everything and then got an application error and aborted.

What can I be doing wrong? Seems some have been able to patch it recently.

I followed the "patch -p0 < ..\patchname.patch" from the docs (so I did use the -p0 option.
#50
03/01/2004 (3:57 am)
This was made back in March.. the patch will likely not work with current code..
I suggest you make the changes manually.. .patch files are pretty straight forward to understand.

e.g.

===================================================================
RCS file: /cvs/torque/torque/engine/audio/audio.cc,v
^file to change
retrieving revision 1.22
diff -u -r1.22 audio.cc
--- engine/audio/audio.cc 2003/09/30 19:31:38 1.22
+++ engine/audio/audio.cc 2004/02/14 19:40:36

@@ -48,6 +48,7 @@ +line number

Point3F mPosition;
Point3F mDirection;
^Current code
+ Point3F mVelocity;code to add

F32 mPitch;
F32 mScore;
U32 mCullTime;
^more existing code
#51
03/01/2004 (2:45 pm)
I can do that.

Is there any worth to me submitting an updated patch or will be out of date so quickly that its not worth my time?
#52
03/09/2004 (3:13 pm)


I manually applied the patch to TGE 1.2.1 and it works :D

I didn't seem to have the same problem with fog (not using fractal sky patch).

There are still the issues that need to be worked out. For example, the baked in shadows don't move. I know it would be hard to fix this, but I'm hoping there is a way. A quick hack would be to simply relight the scene every x minutes, but relighting freezes the game for a few seconds.

[edit]
um. actually there are alot of problems. I have most of the problems discribed above.

Also, I noticed that the sun rises and sets at the fog distance. So if you want close fog, it will look very cheesy.

I guess this patch is a good starting point, but not good enough for realistic day/night cycles.

BTW, I think the glow in the dark trees is a modeling issue, not a problem with this patch.
#53
03/10/2004 (12:25 pm)
Hi All

I have played around with this ressource and Fractal Sky..
When I zoom out a lot I get to see these ugly sharp edges as seen on this picture.. I didn't use to get these earlier on..
Any suggestions to what might have gone wrong..

http://www.mushy.dk/flux/ugly.gif -> look at the top of the picture..

- Rasmus
#54
03/18/2004 (6:54 pm)
By the way, I found a better way to remove the hardcoding for the shadow of ShapeBase objects.

Instead of changing
Point3F lightDir(0.57f,0.57f,-0.57f);
in shapeBase.cc
to
Point3F lightDir = DaylightCycle::getSunVector();

you can use the same method that other TGE code already uses to calculate the sun vector. This is more robost and doesn't rely and the patch for those of you not using this patch :)

In terrain/fluidRender.cc they get the sun vector this way
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 );
}
#55
04/25/2004 (5:27 pm)
Cross refernence to Daynight with Seasons see:
click here
#56
05/09/2004 (3:07 pm)
I was having a few problems with the day-night cycles when having several fxSunLight's..

so i added this code to DaylightCycle::calcColors()

if(fxSunLight* SunFlare = (fxSunLight*)Sim::findObject("Sun"))
mSunFlare = SunFlare;

that way if there is a fxSunLight named "Sun" it uses that fxSunLight to do the day-night cycles..

but for some reason i dont think its grabbing the right object..

fxSunLight - Sunflare = 142257232 <-- the sunflare object..
Sunflare = 22504512 <-- what Sim::Findobject() is returning

in fxSunLight::unpackUpdate i added

DaylightCycle::mSunFlare = this; <-- existing code
Con::printf("fxSunLight - Sunflare = %i", DaylightCycle::mSunFlare); <-- added this..

and i only have 1 sunflare in the mission right now.. so 'fxSunLight - Sunflare' is what 'Sunflare' should equal..

not sure what the problem is.. any help would be appreciated..

Thx,
RedCore
#57
09/08/2004 (5:59 pm)
this is from a while ago, so im sorry, but im getting conflicts in shapebase.cc

Point3F lightDir=DaylightCycle::getSunVector();

// Get a real light dir...
Point3F lightDir = gClientSceneGraph->getLightManager()->getShadowLightDirection(); <-- with this line

if i take either of the 2 above lines out i get 23 very nasty errors about unresolved external symbols.
im still pretty new to this, and ended up adding some stuff by hand. got any ideas what is up with this?

this is the only error im getting with both lines in:

cl : Command line warning D9002 : ignoring unknown option '/YXstdafx.h'
shapeBase.cc
..\engine\game\shapeBase.cc(2518) : error C2374: 'lightDir' : redefinition; multiple initialization
..\engine\game\shapeBase.cc(2514) : see declaration of 'lightDir'

i need help figuring this out.
thx in advance.
#58
09/13/2004 (8:41 am)
Without looking at the rest of the code and only what you've pasted, you have done just what it says, redeclared it...

needs to look more like...

Point3F lightDir=DaylightCycle::getSunVector();
lightDir = gClientSceneGraph->getLightManager()->getShadowLightDirection(); <-- with this line

I'd assume your 23 errors are from lightDir not being defined.
#59
10/06/2004 (1:38 pm)
I implmented this patch by hand, and compiled without many erros. The terrain, sky, and object shadows all work as they should. However none of my interiors or any of the baked in shadows get lighted/updated. Not sure what the problems is.
#60
10/13/2004 (3:10 am)
Can someone Please convert the Patch file to a resource that can be used with 1.3? I an not the best programmer in the world with C++, and I would not know what changes to make. I know + is add and - is remove in the patch file, but I would not know what to do if there was a conflict. I would very much apreciate it, and I know lots of programmers out there would honor the person who does it.

Marrion