Posted: 22nd Dec 2003 0:27
I just had this idea to make fast terrain lightmapping, I just had to try it out. Here is the result. It is so fast, you can do it realtime when using a low resolution for the lightmap. (at least on my comp) It doesn't just do lighting, it also casts shadows. I hope you like it .
Press left,right,up,down do change the direction of the sun.

Here is how a highres lightmap on the terrain looks. It was calculated in about 1,3 secs.


Download here: http://www.dannywartnaby.co.uk/rgt/attachments/terlightmap.zip

Kevil
Posted: 22nd Dec 2003 0:34
nice
Posted: 22nd Dec 2003 18:22
[Edited]
Posted: 23rd Dec 2003 1:13
Nice job Kevil. Reminds me of your memblock world you created quite a while ago.

Posted: 23rd Dec 2003 2:19
Maybe that is because it uses the same terrain .

Kevil
Posted: 23rd Dec 2003 5:23
Well that explains a lot.

Keep up the good work. In the mean time, I'll have to study your brilliant effects.

Posted: 23rd Dec 2003 22:16
Very nice !
Posted: 27th Dec 2003 0:48
Hey Kev - was it you who did the example of a heightbased ocean? Where the ocean ranged from blue to white by height?

If you were, and if you have a link, Id like to see it
Posted: 27th Dec 2003 2:36
Yes! That was me.
No! I don't have a link, I think the topic is all gone now.
Why do you want to see it?

Kevil
Posted: 27th Dec 2003 4:39
I wanted to see the coding of it, see if I could reproduce something similiar to it for ED, as I thought it looked magnifico

RPGamer
Posted: 27th Dec 2003 4:46
Ah! I DLed this example, and noticed that that awesome water was in there

/me starts looking at code

Very nice!!

RPGamer
Posted: 27th Dec 2003 17:35
Ok then.
Tell me if you don't understand the code, I can easily explain it to you.

Kevil
Posted: 27th Dec 2003 17:44
Thx I just have to read up on memblocks, as I never have been a wiz with them
Posted: 27th Dec 2003 22:43
hi kevil i have run the code and the affects are great although complicated to understand so could you make a tutorial on it or maybe an code file that if you include then all you need to do is call one function and it handles the rest, i don't know like makelightmatrix(what ever it needs) and runlightmatrix() for the real time okay
thanks

*edit* sorry i don't know if this is possible but could you also make a function to quickly find the exact height at a passific point like for a matrix
Posted: 27th Dec 2003 23:38
Clear_Coder: Just use Get Ground Height
Posted: 28th Dec 2003 0:17
RPGamer _ i thought it used meshs and objects rather than a matrix
Posted: 28th Dec 2003 16:22
Yeah, it is a mesh (memblock matrix), but there is a similar function to get ground height for this kind of "matrix" and that function can be found in the file "makematrixmem.dba" which also has a lot more functions.

The lightmapping is almost completely done by the two gosubs. First you'll need to calculate the normals and if you have that data and you have the height data and you have all the arrays initialized that I use too, you can simply call the createlightmap function to do the lightmapping. So now you know this, you can make your own functions.

I can also explain the technique used, so you can make your own code for it. I also explained it on the LLRGT forum, so I'll also put it on here.

Now to explain the technique, I can best show how it works in 2D. The same way can be done in 3d. This is only for the shadows. The lighting itself is not so hard to do.

Below you'll see an image of a basic 2d terrain (blue line). You can also see the sunlight direction. This technique works with some sort of 'shadow' value.

Ok. Let's say that P1 is in the light. That means his shadow value is 0. To calculate the shadow value of P2 we don't need all the points that came before him, we only need the shadow value of P1. So how do we calculate this shadow value for P2? Simple! . We go from P2 and the go against the sun direction until we reach the x-position of P1. Then we can calculate the difference of that height with the height of P1 which is called DH1. If we say that in this case DH1 is negative, then we can calculate the shadow value, by subtracting DH1 from the shadow value of P1. So if this new shadow value is positive you are in the shadow. If the shadow value is negative, you should set it to 0 again, because the point will be in direct light. Then you can repeat this process for P3 and P4 and you'll find out that when you get to P4 the shadow value will be 0 again which is correct because P4 should be in the light again.

This same technique can be used in 3d, however it isn't perfect, because a terrain is made out of triangles and not square planes. However this small error is not very noticable, so we just use it, for speed. But still it is harder in 3d. You need to go against the sun direction until you reach one of the sides of the squares where the point is in. This is usually between two points. So then you would need to interpolate the height and the shadow value to those coordinates. The rest will work the same.

This shadow value technique has some advantages. You can use the shadow value to see if you are near the edge of a shadow or not. Say you have an edge variable you could say that when the shadow value is higher than that edge value you give it the shadow color and when it is below the edge variable you can interpolate the lighting from shadow color to light color. This will result in soft edges. Also it is easy to increase the lightmap detail. Since we are dealing with square planes (well, almost) we can just interpolate the shadow values of the points you already have to get the new shadow values, which will create the right effect. I used that technique for the screenshot, which is actually the same as the realtime demo with the only difference being that I increased the resolution to 1024x1024 instead of 64x64. Does it look blurry? No, because instead of interpolating the light colors, I interpolate the shadow values.

And that's how it basically works. I hope I made some sense .



Kevil
Posted: 28th Dec 2003 22:12
it all made sense until you got into 3d this is probally because i don't really understand how you get the terrain in the first place have you got an explaination of the orginal heightmap to mesh using memblock anywhere if you can find one could you post it here , thx

cheers
Posted: 1st Jan 2004 3:28
@Kevil: That's really good code and a good explanation too.
Now I understand the algorithm.
Thanks.
Posted: 6th Jan 2004 19:41
Clever! Thanks for explaining ...