Sign in to follow this  
ZMaster

Directional Light: Shadowmap

Recommended Posts

ZMaster    240
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!

Share this post


Link to post
Share on other sites
extralongpants    704
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

Share this post


Link to post
Share on other sites
ZMaster    240
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?!?

Share this post


Link to post
Share on other sites
dopo    122
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.

Share this post


Link to post
Share on other sites
extralongpants    704
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.

Share this post


Link to post
Share on other sites
ZMaster    240
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 :-)

Share this post


Link to post
Share on other sites
Isokron    196
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.

Share this post


Link to post
Share on other sites
ZMaster    240
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...

Share this post


Link to post
Share on other sites
ZMaster    240
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!

Share this post


Link to post
Share on other sites
Isokron    196
CalcMinMaxView calculates the minimum and maximum values in a coordinate system where the camera is the center and the axises are ortogonal to the light direction where the camera frustum can see objects given the assumption that all objects has y (height) coordinates between 0 and 700 in world space and only have a certain view distance. To be frank the code look kind of strange to me also now but it made sense when i wrote it :)

The matrix is then calculated so that this min/max values will be at the edge of the shadow matrix to get as much resolution as possible from it. The process is complicated by the fact that i also run an non linear transform (sqrt) on the the coordinates to get higher res close to the camera.

If you just have a top down camera and the sun somewhat high over the horizon you can probably skip these steps more or less.

Share this post


Link to post
Share on other sites
ZMaster    240
I found a detailed paper including source code, that uses a similiar technique to yours.
If someone else is also interested in this, here is the url:

http://www.cg.tuwien.ac.at/research/vr/lispsm/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this