Game Development Community

Sneaky LightInfo initialization

by Jeff Faust · in Torque Game Engine Advanced · 06/28/2007 (12:45 pm) · 2 replies

I suppose this might not merit a "Bug" designation but it should at least be filed under "Bad and Confusing Coding Practice".

I've been attempting to do some minor customization of lighting internals and this bit of code caused much head scratching because members I have added to LightInfo were incorrectly being initialized to zero although they are properly initialized in the LightInfo constructor.

The culprit is this bit of code in LightManager::sgSetupLights() in engine/lightingSystem/sgLightManager.cc:
dMemset(&obj->mLightingInfo.smAmbientLight, 0, sizeof(obj->mLightingInfo.smAmbientLight));

    LightInfo &light = obj->mLightingInfo.smAmbientLight;
    light.mType = LightInfo::Ambient;
    light.mDirection = VectorF(0.0, 0.0, -1.0);
    light.sgCastsShadows = sun->sgCastsShadows;
The dMemset() call is used here to wipe a static instance of LightInfo to zero which is then copied over to another instance LightInfo. The problem is that not all of LightInfo's members default to zero. The code here fixes a few members, but any changes made to LightInfo may result in hard-to-explain behavior due to the way all of LightInfo's members are forced to zero.

This same issue exists in matching TGE LightManager code.

About the author

Jeff Faust creates special effects indie middleware and games for Faust Logic. --- Blog: Effectronica.com --- Twitter: @FaustLogic


#1
06/28/2007 (1:46 pm)
@Jeff - thanks for the heads up. Yeah, we're trying to clean some of that stuff up. I assume you figured out a work around in your own codebase.
#2
06/28/2007 (1:52 pm)
Thanks for the reply Clark. A work around is fairly easy, since I just have to do the initializations in place, where the others are done here. I just wanted to post this so there was a record of it.