Game Development Community

Terrain Lighting, Sun Ambient & Sun Color

by Ronald J Nelson · in Torque Game Engine Advanced · 04/17/2008 (3:50 pm) · 9 replies

I have been trying to get any of the Day/Night systems to work without using DRL in TGE 1.03 because it is costly.

I have gotten the sun to move correctly across the scy and shadow and lighting changes occur for all DTS based objects.

However, nothing happens for the terrain and the ambient lighting stays the same. I even added a couple of functions to the Sun code to change the ambient and color values then used the inspectPostApply to update them by calling the consolemethod "apply". Nothing happens.

Here are those functions.

ConsoleMethod( Sun, setSunColor, void, 6, 6, "")
{
	F32 red = dAtof( argv[2] );
	F32 green = dAtof( argv[3] );
	F32 blue = dAtof( argv[4] );
	F32 alpha = dAtof( argv[5] );
	object->setSunColor(red, green, blue, alpha);
}

void Sun::setSunColor(F32 red, F32 green, F32 blue, F32 alpha)
{
	mLight.mColor.set(red, green, blue, alpha);
}

ConsoleMethod( Sun, setSunAmbient, void, 6, 6, "")
{
	F32 red = dAtof( argv[2] );
	F32 green = dAtof( argv[3] );
	F32 blue = dAtof( argv[4] );
	F32 alpha = dAtof( argv[5] );
	object->setSunAmbient(red, green, blue, alpha);
}

void Sun::setSunAmbient(F32 red, F32 green, F32 blue, F32 alpha)
{
	mLight.mAmbient.set(red, green, blue, alpha);
}

Can someone shed some light on this for me and possibly point me in the direction of a solution that does not involve DRL?

#1
04/18/2008 (5:25 am)
Seriously, no one? I am not opposed to doing a bit of code work here, heck I have been. But apparently I have misunderstood how the terrain is lit because while so many areas look the same as lighting a static object, the DTS objects do change lighting with the movement of the sun, but the terrain does not.
#2
04/18/2008 (6:05 am)
As far as I know the terrain uses a static lightmap which is pre-generated upon mission loading or upon a forced re-light (applies for legacy terrain / megaterrain / blended atlas; unique atlas has only a single texture which is intended to include all lighting information pre-rendered in a 3rd party application like Terragen or L3DT).


EDIT: a little clarification; the mission lighting is calculated only once and then the lightmap is saved in an .ml file in the \terrain -directory and that file is used on subsequent loadings of the mission -- unless you change the mission definition in which case the lighting needs to be done again.
#3
04/18/2008 (10:00 am)
OK I am confised then. Why did this method work for TGE? In resources like Day/Night Cycles and Enviro-Torque, what I have done is essentially the same. Additionally the data is saved in the ML file as in TGEA.
#4
04/18/2008 (10:06 am)
Neither Day/Night Cycles or Enviro-Torque actually changed lightmaps--if you watch very carefully, the baked in shadows remained the same.
#5
04/18/2008 (10:16 am)
As you may have noticed neither of my methods do either. I was merely changing the sun characteristics as is done in those resources to update the visible lighting. That is not working.
#6
04/19/2008 (12:54 am)
Well I also got it to update the Sky fog properly which really does help the overall look. However, I still need to find out why the ambient lighting for the scene is not being changed. Can someone tell me where it is updated?
#7
04/19/2008 (7:49 am)
I rewrote the Celestials system awhile back for TGEA. Assuming you have some type of sun positioning update routine, the following may be of some help.
void Celestials::UpdateSunPosition()
{
	if (!mInitialized)
	{
		InitColors();
		mInitialized = true;
	}

    PROFILE_START(UpdateSunPosition);
    // Get the sun vector and put it into our mSunLight object.
    mSunVector.set(0.57f,0.57f,-0.57f); //default
    mAzimuth   = mClampF(mAzimuth,0.0f, 360.0f);
    mElevation = mClampF(mElevation,-90.0f, 90.0f);
    MathUtils::getVectorFromAngles(mSunVector, mAzimuth, mElevation);
    mLightingLevel = mClampF (mLightingLevel, 0.01f, 0.5f);
    F32 ambient = mLightingLevel;

    //  Lighting
	// Tell the lighting system where we are, as this affects shadowing et al
	LightInfo *LightFromSun =    gClientSceneGraph->getLightManager()->getSpecialLight(LightManager::slSunLightType);
	if(LightFromSun != NULL && gCelestials)
	{
		LightFromSun->mDirection = mSunVector;
		LightFromSun->mColor     = mCurrentColor;
		LightFromSun->mAmbient.set(ambient, ambient, ambient, 1);
	}
	// Actual sun object
	Sun * sun = dynamic_cast<Sun*>(Sim::findObject("SunObject"));
	if (sun != NULL)
	{
		sun->mSunElevation   = mElevation;
		sun->mSunAzimuth     = mAzimuth;
                sun->mLight->mColor   = mCurrentColor;
                sun->mLight->mAmbient.set(ambient, ambient, ambient, 1);
		sun->setMaskBits(Sun::UpdateMask);
	}
	PROFILE_END();
}
This handled just about everything. A few cases required access to the new luminence levels, notably the fx functions.
For example, find the first few lines of the following in fxfoliage, and add the changed aspect. This would work pretty much the same for any other fx (or groundcover) files.
// Compute other light parameters
			/*const */F32	LuminanceMidPoint		= (mFieldData.mMinLuminance + mFieldData.mMaxLuminance) / 2.0f;
			const F32	LuminanceMagnitude		= mFieldData.mMaxLuminance - LuminanceMidPoint;

			// Adjust for current ambient lighting.
			if (gCelestials && isClientObject())
				LuminanceMidPoint = gCelestials->mLightingLevel;
This all works (with the rest of the code not posted here) in tgea 1.7
#8
04/19/2008 (5:03 pm)
Thanks Eric. Would be nice if you could just release that Celestial code as a resource. However, as you can tell, I am doing in my functions the same thing you are. There is a difference though, mine doesn't change a thing. Now I know that it is not updating the color and ambient for some reason when I do this, because I can can open the mission file and set them and it works perfectly.
#9
04/19/2008 (6:15 pm)
Well for some reason even though my functions are passing the values, the Sun color and ambient are not updating in real time. I can sure get it's elevation to do so, just not those.