Game Development Community

Terrain Lighting

by Chris Labombard · in Torque Game Engine · 03/10/2005 (9:48 am) · 8 replies

Ok, I have been trying to find where, in the source code, that the terrain is being lit. I came to realize that it doesn't relight unless you delete the ml files...

I have been altering the
void TerrainBlock::relight(const ColorF &lightColor, const ColorF &ambient, const Point3F &lightDir)
function... specifically this section:

if(height >= intHeight)// non shadowed
{
U32 idx = (xmask + (ymask << blockShift)) << 2;

Point3F normal;
normal = pNormals[0][bi] * normTable[idx++];
normal += pNormals[0][binext] * normTable[idx++];
normal += pNormals[1][binext] * normTable[idx++];
normal += pNormals[1][bi] * normTable[idx];
normal.normalize();

nextHeightArray[y] = height;
F32 colorScale = 0;//mDot(normal, lightDir);
if(colorScale >= 0)// // facing away from light
col += ambient;
else // facing the light
col += (ambient + lightColor * -colorScale);
}
else // shadowed
{
nextHeightArray[y] = intHeight;
col += ambient;
}

which appears to be the section that calculate the dot product of the normal to the light source.... but when I change it (or even comment out the contents of the entire method) and rebuild the exe and run torquedemo.exe the terrain still lights exactly the same...

I have looked through all the terr*.cc files as well as teh light manager stuff ... I cant find the right block of code.

Can anyne tell me where the lighting code is being called from?

About the author

I have been a professional game programmer for over 5 years now. I've worked on virtually every platform, dozens of games and released a few of my own games, including 2 iPhone titles and a title waiting release on Big Fish Games.


#1
03/10/2005 (10:23 am)
I may be wrong, but I think terrain lighting is done from blender.cc/blender.asm

Do a search in blender for "copy in the lighting info" and it should be around there.
#2
03/10/2005 (10:25 am)
I found it... it is in scenelighting.cc
#3
03/11/2005 (7:06 am)
I have another question... on topic: I want to do my own lighting calculations on objects like trees and players...

I can't find, in the source, where it is lighting the player... Does Torque light the player each rendering pass? I know it has a shadow. So it must do something... unless the shadow is just a decal or something. If Torque does do lighting (or shadowing) on the player each pass, where is the lighting code at?
#4
03/11/2005 (7:17 am)
Chris,

Yes, the shadow is rendered every frame (because it changes). Check shapeBase.cc

void ShapeBase::renderShadow(F32 dist, F32 fogAmount)


HIH
#5
03/11/2005 (7:19 am)
Just a thought here, but has anyone tried to get it to render ONLY the players shadow and not the player? Would be kinda kewl for a cloaking device effect. :)
#6
03/11/2005 (8:08 am)
THat would be really easy. In the renderXFCache method just cull everytihgn... then no shapes will be drawn. I'm sure you could use a variable tomask it so it is just the player.

As for the SHADOW... Does it do lighting calculations on the player mesh, or just draw the shadow? I wlil check for the code when I get home, but I couldn't find it earlier.
#7
03/11/2005 (10:20 am)
That shadow is a sort of decal with an alpha-blended texture on it. The texture is generated by rendering the mesh into a bitmap using a software render, and then uploading the texture to the video card.

The lighting for mesh objects is actually calculated by OpenGL (or Direct3D, if enabled). TGE simply makes some calls to register the relevant lights with OpenGL before rendering a mesh, and the mesh will react to it according to it's material.

in ShapeBase::renderObject() you'll see this line right at the beginning:

installLights();

That function will then start the process of scoring the closest lights (the amount of lights will depend on the maxHardwareLights pref variable - defaults to 3, but any 5 years old vid card can handle 8), and registers them with openGL.
#8
03/11/2005 (11:01 am)
Ok...... So is it possible to add my own, software lighting for shapes? I am going to look into this when I get home tonight.