Game Development Community

Enabling projectile shadows

by Faraz Ahmed · in Torque Game Engine · 01/26/2006 (4:02 am) · 7 replies

(moving this post from general to the private TLK forums as per suggestion)

Hello, just to keep a running thread in case someone figures it out, I have a requirement for my projectile ball to cast a shadow above the ground which it travels. I modified the lighting kit code to include a renderShadow function for projectiles. The code changes are working and projectiles are casting shadows in both the release and debug builds of the code.

There is a strange bug though which is happening only in the release build. If you quit a mission and then re-enter it, launching the projectile crashes the engine without any clues as to why.. no messages, nothing in the console log. I have isolated (via printfs) the crash point in the new renderShadow funtion but I have no idea why that operation would crash the engine. Here are the code changes and the crash point:

Projectile.h
// add to class ProjectileData
// ----- SHADOW ENABLING BLOCK -----
F32 noShadowLevel;
F32 genericShadowLevel;
S32 shadowNode;  
// ----- SHADOW ENABLING BLOCK -----

// add to class Projectile
// ----- SHADOW ENABLING BLOCK -----
void renderShadow(F32 dist, F32 fogAmount);
// ----- SHADOW ENABLING BLOCK -----

Projectile.cc
// add to ProjectileData constructor
// ----- SHADOW ENABLING BLOCK -----
genericShadowLevel = 0.4f;
noShadowLevel = 0.01f;
// ----- SHADOW ENABLING BLOCK -----

// update renderObject method
void Projectile::renderObject(SceneState* state, SceneRenderImage *)
{
   .
   .
   .
   glScalef( mDataBlock->scale.x, mDataBlock->scale.y, mDataBlock->scale.z );
	
	// ----- SHADOW ENABLING BLOCK -----
	this->sgSceneObjectData.installLights(this);
   // ----- SHADOW ENABLING BLOCK -----

   if(mProjectileShape)
   {
     .
     .
     .
    
   }
   glDisable(GL_BLEND);
   glDisable(GL_TEXTURE_2D);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();

   // ----- SHADOW ENABLING BLOCK -----
   F32 fogAmount = 0.0f;
   F32 dist = 0.0f;
   Point3F cameraOffset;
   getRenderTransform().getColumn(3,&cameraOffset);
   cameraOffset -= state->getCameraPosition();
   dist = cameraOffset.len();
   fogAmount = state->getHazeAndFog(dist,cameraOffset.z);
	
   Projectile::renderShadow(dist,fogAmount);
	
    this->sgSceneObjectData.uninstallLights(this);
   // ----- SHADOW ENABLING BLOCK -----
   .
   .
   .

sgShadow.cc
// added to includes
// ----- SHADOW ENABLING BLOCK -----
#include "game/projectile.h"
// ----- SHADOW ENABLING BLOCK -----


// created new function, same as ShapeBase:renderShadow except removed the rendering code for mounted objects
// ----- SHADOW ENABLING BLOCK -----
void Projectile::renderShadow(F32 dist, F32 fogAmount)
{
	
	if(sgSceneObjectData.sgDisableDynamicShadows)
		return;
	if(Shadow::getGlobalShadowDetailLevel()<mDataBlock->noShadowLevel)
		return;
	if(mProjectileShape->getShape()->subShapeFirstTranslucentObject.empty() ||
		mProjectileShape->getShape()->subShapeFirstTranslucentObject[0] == 0)
		return;

	Vector<LightInfo *> lights;
	LightManager *lm = gClientSceneGraph->getLightManager();

	lm->sgGetValidLights(lights);

	// ambient must be last!
	for(U32 i=0; i<lights.size(); i++)
	{
		LightInfo *light = lights[i];  // ** CRASHES HERE IN RELEASE MODE 

		if(light->mType != LightInfo::Ambient)
			continue;

		U32 il = lights.size() - 1;
		if(i == il)
			continue;

		// swap...
		LightInfo *last = lights[il];
		lights[il] = light;
		lights[i] = last;
		break;
	}
        .
        .
        .

}
// ----- SHADOW ENABLING BLOCK -----

#1
01/26/2006 (4:54 am)
I've seen this type of crash before (on restarting the mission), but always related to particles.

Does the ball emit particles? If not try removing the ball from the game and then try to get the crash to happen again. If it continues to crash I can give you some pointers on how to avoid the particle crash (it's been in Torque for a while and it's nasty :/ ).

Also are you using 1.3.5 or 1.4?
#2
01/26/2006 (8:05 am)
Ok I think I was wrong to say that the crash is happening after a mission restart because just now the engine crashed on me during a first time a mission load as well. It happens more often during mission restart however. Its intermittent.

The ball has no particle emitters.

I'm using TLK 1.4 and the torque pre-merged SDK that came with it.

It would be nice to know in general about what kind of things go wrong in release builds that work correctly in debug builds.

No solution to the above problem yet...
#3
01/26/2006 (9:09 am)
Serious clues to the problem on offer:

(1) The crash does not happen if I delete the .ml (mission lighting) file in the missions folder. This was tested successfully like 100 times!

(2) There IS a pattern to the crashes, start the .exe & enter mission & launch projectile it works, start the .exe & enter mission & launch projectile it crashes... and so on, it alternates.. If I delete the .ml file before launching the .exe it never crashes and the pattern breaks.

I think we're getting closer now.
#4
01/26/2006 (12:22 pm)
It sounds like something a lot more involved is going there, like memory corruption that is eventually leading to the crash.

Is this TLK 1.4 right out of the box, did you add any code to it?

I also wanted to mention that you might want to look into the rigid shape resource, it could make a much better ball. This isn't going to solve your current problem, but I think it might be easier to work with regarding collision, physics, ....
#5
01/26/2006 (10:46 pm)
This was TLK 1.4 right out of the box and the only code modifications were as above.

I'll also look at your suggestion of rigid shapes, meanwhile I'll also try and mess with the projectile light parameters and see if these can be used to generate a shadow like effect on the ground. Boy I really want that shadow :p

By the way from your profile it seems that you may be a part of the TLK development team? I'd just like to mention that I was completely blown away by how my simulation came to life when I launched it with the TLK and also thanks for your time spent & close attention to the issues we go through.
#6
01/27/2006 (12:31 am)
The rigid shape class is doing its thing and generating shadows as well. This seems to be the right direction and with a lot more ready-made physics functions.

Thanks & Cheers.
#7
01/27/2006 (7:29 am)
I'm glad the rigid shape resource helped out.

I'm actually the guy that originally designed a wrote the Torque Lighting Kit. :) Thanks for the compliments!

-John