Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Realtime lowres terrain lightmapping [DBP]

Author
Message
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
Posted: 22nd Dec 2003 00: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
LordSorcre
20
Years of Service
User Offline
Joined: 23rd Aug 2003
Location:
Posted: 22nd Dec 2003 00:34
nice
Damokles
20
Years of Service
User Offline
Joined: 28th May 2003
Location: Belgium
Posted: 22nd Dec 2003 18:22 Edited at: 22nd Dec 2003 18:23
[Edited]

"Begin at the beginning, and go on till you come to the end: then stop." - Lewis Carroll
MikeS
Retired Moderator
21
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: United States
Posted: 23rd Dec 2003 01:13
Nice job Kevil. Reminds me of your memblock world you created quite a while ago.



A book? I hate book. Book is stupid.
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
Posted: 23rd Dec 2003 02:19
Maybe that is because it uses the same terrain .

Kevil
MikeS
Retired Moderator
21
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: United States
Posted: 23rd Dec 2003 05:23
Well that explains a lot.

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



A book? I hate book. Book is stupid.
Ian T
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 23rd Dec 2003 22:16
Very nice !

--Mouse: Famous (Avatarless) Fighting Furball

A very nice %it, indeed.
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 27th Dec 2003 00: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


Dont forget to Join the Forums!
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
Posted: 27th Dec 2003 02: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
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 27th Dec 2003 04: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


Dont forget to Join the Forums!
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 27th Dec 2003 04:46
Ah! I DLed this example, and noticed that that awesome water was in there

/me starts looking at code

Very nice!!

RPGamer


Dont forget to Join the Forums!
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
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
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 27th Dec 2003 17:44
Thx I just have to read up on memblocks, as I never have been a wiz with them


Dont forget to Join the Forums!
Plugged
21
Years of Service
User Offline
Joined: 30th Nov 2002
Location: Hertfordshire
Posted: 27th Dec 2003 22:43 Edited at: 27th Dec 2003 22:44
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

CLear Coder
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 27th Dec 2003 23:38
Clear_Coder: Just use Get Ground Height


Dont forget to Join the Forums!
Plugged
21
Years of Service
User Offline
Joined: 30th Nov 2002
Location: Hertfordshire
Posted: 28th Dec 2003 00:17
RPGamer _ i thought it used meshs and objects rather than a matrix

CLear Coder
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
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
Plugged
21
Years of Service
User Offline
Joined: 30th Nov 2002
Location: Hertfordshire
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

CLear Coder
D Man
21
Years of Service
User Offline
Joined: 3rd Oct 2002
Location: Germany
Posted: 1st Jan 2004 03:28
@Kevil: That's really good code and a good explanation too.
Now I understand the algorithm.
Thanks.

God is real, unless declared integer.
Ian T
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 6th Jan 2004 19:41
Clever! Thanks for explaining ...

--Mouse: Famous (Avatarless) Fighting Furball

I am the chainsaw paladin.
Zeal
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: Colorado Springs, CO
Posted: 26th Jan 2004 03:54
Is there anyway to use such a two layered effect without getting those ugly 'outlines' caused when you look at the matrix from a very low angle? It almost looks like cell shading heh. Can you maybe disable object zdepth ONLY against the other matrix? So the two seem to be drawn right ontop of eachother? This would get rid of the outlines wouldnt it?

All you need is zeal
Zeal
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: Colorado Springs, CO
Posted: 26th Jan 2004 03:59
Follow up question. Is the 'set light mapping on' command just broken? I mean, it seems like such a waste to render TWICE the polys for your terrain, when there is a light mapping command built right into the language.

All you need is zeal
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
Posted: 26th Jan 2004 10:44
Well, the set light mapping on sort of works, but you can't set separate UV coords, so in that way it's useless.
Although you should be able to set multiple UV coords with U6.

Kevil
Zeal
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: Colorado Springs, CO
Posted: 26th Jan 2004 22:40
So when u6 comes along, youll be able to use your texture/lightmap generation system, AND cut the polys in half by using the lightmap command rather than a second matrix? Sounds cool.

Hey Kevil, if you get a second, please check out a thread ive had running on the dbpro forums for several days now. Some matrix/memblock/texture related questions (many about your tutorials). Im sure you know the answers. Really the question thats bugging me the most is, how do you have a blended detail map? You know, so your rocks, sand, ect... dont all look like grass, as they do in your program.

Check out the thread at...

http://darkbasic.thegamecreators.com/?m=forum_view&t=24264&b=1

All you need is zeal

Login to post a reply

Server time is: 2024-04-27 22:53:46
Your offset time is: 2024-04-27 22:53:46