Code archives/3D Graphics - Misc/New Terrain LightMapper

This code has been declared by its author to be Public Domain code.

Download source code

New Terrain LightMapper by TartanTangerine (was Indiepath)2004
New and Improved and does exactly what it says on the tin.

Download the files from here as there are some includes.

; ******************************* Terrain Shadow Lab ******************************

Include ""

Global gWidth 		= 	640
Global gHeight 		= 	480
Global InMap$		=	"levelterrain123.bmp"			; Your Height Map
Global OutMap$		=	"shadowmap.bmp"					; The Output File
Global Sun.Vector	=	Vector(1,2,-1)					; Position of the Sun IN VECTORS!
Global InImage
Global OutImage

Graphics gWidth,gHeight,32,2

InImage		=	LoadImage(InMap$)
MapSize		=	ImageWidth(InImage)

LEV_SHAD_Initialise(InImage,MapSize)					; Put all map info into an array
CalcNormalMap(MapSize)									; calculate Map Normals for precise lighting

Start		= 	MilliSecs()
OutImage	=	LEV_SHAD_RenderLMAP(Sun,MapSize)		; Render LightMap According to Sun Position
Timenow 	= 	MilliSecs() - Start

SaveImage (OutImage,Outmap$)

SetBuffer BackBuffer()

DrawImage 	InImage,0,0
DrawImage	OutImage,MapSize,0

Text		0,Mapsize+10,"Time : " + Timenow




Great !

What am I seeing there in that screenshot?

Is it the lightmap combined with another image?

Cause what I'm seeing looks like it is not just computing what is occluded, but also taking into account the angle of the light source, and light reflected from nearby terrain, ala radiosity.

Perhaps though it is just multiple light soruces combined to simulate the effects of a bright sun, and a sky which also disperses light?

Btw, your zip file is not in that directory.

angel martinez2004
The link does not work.

TartanTangerine (was Indiepath)2004
Apologies, I will upload again this evening.

@sswift : The image is the lightmap, it has not been combined with any other images. It does take account of the light source angles, occluded areas but no reflections ;)

TartanTangerine (was Indiepath)2004
If you want to see this code in action then download the latest version of TerraEd

But if the image takes into account occluded areas, then how come areas that should be completely in shadow have variation in their lighting? For exmaple, the middle left hand side of the image. Clearly a big shadow is cast from the hill, yet there is variation in the shadow as if the sun is reaching that area and lighting the polygons.

I'm not saying it's neccessarily a bad thing mind you. I might actually look good, artistically. I'd have to see it on an actual terrain. But if that's not reflections of light, or light from a secondary light source, like the sky, then it's not physically accurate. :-)

TartanTangerine (was Indiepath)2004
:) I've spent quite a bit of time thinking about the shadows.. In real-life you get a variation in the shadows cast from the sun since some light is still getting to the objects - if not then shadows would be 100% black.

Yes, you do get variation in shadows in real life, because light is bounced around the atmopshere, and reflected off the ground. But light doesn't pass through hills like in your version. :-)

But that's okay, like I said, it may be noticeable from this point of view, but down on the ground I'm sure you can't tell and the variation will make it look better.

But for a much more realistic simulation you'd want a directional light for the sun, and for each point of the terrain you'd want to see how much sky it could see and use that to determine how much of the sky color it takes on, and then you'd want to also try to calculate how much each point can see of the ground.

Of course at that point it would probably be worthwhile to just try to simulate radiosity in maybe two passes. One pass for the sun and sky illumination, and then a second pass taking into account only the light each pixel can see reflected from the terrain. And ideally you'd take into account the color of the terrain to adjust the color of the reflected light so that green grass reflects green light.

Not trying to belittle your effort or anyhting, it's the best terrain lighting I've seen so far. Better than the stark lighting I did using 3DS max... God that was slow trying to render a heightmap like that. Odd that it was so slow just to render one mesh but it was. And I didn't have radioisty... not that I'd have tried that.

TartanTangerine (was Indiepath)2004
interesting....I've already played around with coloration of the light map and I think that code is used in TerraEd. I'll see what I can Develop.

Oh and the files are back online. :)

Rob Farley2004
It looks a lot like the way I do terrain shading.

Stand at each point on the terrain and look at the sun... if you can see it light it, if you can't don't.

Hmmm... I've always done this mathematically, however, I wonder how well it would work actually rendering the terrain and putting a sun sprite in the sky, then picking the colour of the sun from the what is visible to how much light it's getting... Might have to have a play this evening...

With regards to procedual texturing, I've been working on getting this nailed and I've found the best way to sort this is by first writing a steep map, working out what parts are steep and whats not, this way you end up with 2 grey scale images, the height map and the steep map, armed with these 2 maps you can then easily texture however you like.

TartanTangerine (was Indiepath)2004
You could get a steep map from the data generated in the application. I calculate the normals for each point on the map and by measuring the angle of the normal I could generate a map to represent the steepness.

I tried the render terrain thing and it took ages to render the lightmap, I prefer this way.

Hi Tim :) i have not integrate shadow coloring because
there is a problem when you take particular color !

But the rest of the code work very fine !!! great piece of code !

What do you think about celebrity (TerraEd about) :) ???
The girls, the drug, the money ! ??? :) lol

TartanTangerine (was Indiepath)2004
Great to be famous ;) The money would be nice.

Filax, Why don't you use the normal calcutations in the code to help you with coloring the terrain. ie. Steep = Rock and flat = grass ????

A don't know ! mayber in the future when i have made the rest !
So i have made really hard work to do the current stuff :)

Would it be possible to add meshes that would cast shadows on the lightmap? This would truly rock.

TartanTangerine (was Indiepath)2004
Not really possible with the meshes. I could add a routine that allowed you to do this but then you might as well use gile[s], and it would be dead slow.

Well, the slow side doesn't affect me much since it would be pregenerated.
Because what I want to do is create a terrain from a .BMP, I've got my object placer/map editor, and would like to render a lightmap after this. I would love to have to objects cast a shadow on the lightmap to give it a better look.
And, tell me if I'm wrong, but I don't think gile[s] could help me on that field.
So, I would be really glad to have a function to add meshes to cast shadows :D

TartanTangerine (was Indiepath)2004
I'll put some work into this. What I am thinking is that I render the objects from the sun's position (one at a time) and overlay the resulting images on the lightmap - that should be very fast.

Watch this space.

Pete Rigz2004
I'm using this in my little terrain engine im mucking about with, great stuff :)

TartanTangerine (was Indiepath)2004
Rigz, Good stuff, I'd like to see your engine at some point.

Code Archives Forum