Directional Light: Shadowmap

Started by
10 comments, last by ZMaster 18 years, 10 months ago
Hi, I have the following scene setting: A textured Terrain with different 3DS buildings on it. It's an outdoor scene and the camera view is like from a birds eye view. The only light is the sunlight, which I want to implement as a directional light. Do you have suggestions how I could implement shadow mapping, so that the buildings throw shadows onto the terrain and other objects/buildings? Maybe you could point me into some direction... Thanks in advance!
Advertisement
I'm not quite sure what the general solution to shadow mapping terrain is, but you could use a method such as this:

* generate your terrain
* generate an empty texture - your shadow map
* for each pixel in the shadow map, find the corresponding 2d point on the terrain
* calculate the point in 3-d, utilizing a terrain.getHeightAt(x, y) type of method
* create a ray with its origin at the calculated point, and its direction being the negated direction of the sunlight.
* test to see if that ray intersects any geometry (including the terrain)
* if it does, color the current pixel black
* if it doesn't, color the current pixel white
* perform any post-processing on your shadow map if necessary(if you need to give your shadows soft edges, for instance)
* you are done - just apply the image to the terrain

Quote:Original post by odiousangel
I'm not quite sure what the general solution to shadow mapping terrain is, but you could use a method such as this:

* ...


I could easily utilize that, but the only downside is that it will only shadow the terrain, not buildings, that are in the shadow of another.
Any idea on this?!?
i think there would be a lot of problems if u want to draw evey shadow.If u are gonna put them in a lightmap or shadowmap then u must build a large texture to make it precisely enough.In my opinion,there just need a lightmap,and u can draw the shadows of a building using GL_BLEND and i think that would be much easy and seems not bad.
Quote:Original post by ZMaster
Quote:Original post by odiousangel
I'm not quite sure what the general solution to shadow mapping terrain is, but you could use a method such as this:

* ...


I could easily utilize that, but the only downside is that it will only shadow the terrain, not buildings, that are in the shadow of another.
Any idea on this?!?



Doh! Haha, I'm sorry - I forgot you wanted everything shadowed. Well, I imagine the process for the buildings would be the same as for the terrain. The only difference is that finding the 3-d point on the mesh from the 2-d pixel of the shadow map will be a little more difficult. You may have to find the triangle of your mesh that contains the pixel of the shadow map, then find the 3-d point by interpolation of the vertex and texture coordinates at each point. It may be a little complicated, but I'm sure it's possible.
What if I would render the whole scene from the position of the lightsource and create a depth map.
Then render from the camera's position and compare the depth map values to the depth values obtained.
With a fragment shader, I could easily color shadowed fragments dark and lit ones bright, but is there a way to to it without advanced fragment programs? For example on older hardware like the Geforce2 :-)
It can be done without shaders, if you use ARB_depth_texture and ARB_shadow. I'm not entirely certain if it would run on a Geforce 2.
If you want to look at some code we do this and some more in a GPLed RTS game Im working on. However since its a whole game there is a lot of code and some of it quite messy (including the shadow code im afraid). Well mostly you would be interested in the CShadowHandler class. Source code can be found here.

Edit: Ops didnt see the gf2 requirement, this code wont run on that.
Quote:Original post by James Trotter
It can be done without shaders, if you use ARB_depth_texture and ARB_shadow. I'm not entirely certain if it would run on a Geforce 2.


Great, thank you! I read into the ARB_shadow extension and found some literature about creating shadowmaps with it.


Quote:Original post by Isokron
If you want to look at some code we do this and some more in a GPLed RTS game Im working on. However since its a whole game there is a lot of code and some of it quite messy (including the shadow code im afraid). Well mostly you would be interested in the CShadowHandler class. Source code can be found here.


Game looks awesome! Great work. Nevertheless, I'll read into that code, might give me some ideas...
Quote:Original post by Isokron
However since its a whole game there is a lot of code and some of it quite messy (including the shadow code im afraid). Well mostly you would be interested in the CShadowHandler class. Source code can be found here.


Most of the code is quite straight forward, the only thing, I don't really get is, how you create the shadowMatrix. What exactly does CalcMinMaxView() do? Would you mind telling me how the shadowMatrix is calculated :-) Thank you very much!

This topic is closed to new replies.

Advertisement