Jump to content
  • Advertisement
Sign in to follow this  
mike_ix

Terrain Shadow Map Tutorial With Math In DirectX C++

This topic is 4424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

http://www.gamedev.net/reference/articles/article1817.asp http://gpwiki.org/index.php/Faster_Ray_Traced_Terrain_Shadow_Maps So I've been entering the realm of shadows and how they apply to terrains and I've discovered that there are a few key concepts missing from my understanding of this whole business. I've looked over the two best tutorials I could find on the Internet (links above) and they are great at explaining general concepts, but I'm not sure how to implement it. For starters, when doing the ray tracing method, the tutorial refers to a "working point" from which to calculate the ray to the light source. I understand that by checking all the points of the height map along the ray will determine if the "working point" is in shadow or not. What I don't understand is if the "working point" is a pixel or a vertex? And regardless if it is a pixel or vertex, how exactly am I changing the color of the pixel on the screen? Am I doing this with vertex/pixel shaders? If anyone knows of some good sample code that can show me excatly how to implement any kind of shadow map in DirectX 9.0 C++ .Net please tell me!

Share this post


Link to post
Share on other sites
Advertisement
Alright, after much deliberation and a pair of sore eyes, I found quite a few more links to help me on my way to understanding shadow mapping. It appears that ATI and MSDN both have examples of shadow mapping in DirectX C++ to look at. However I'm not really excitied about downloading the entire June DXSDK to get the MS example, so if anyone knows where I can download this example by itself, it would be greatly appreciated!

MSDN Shadow Map Example:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/ShadowMap_Sample.asp

ATI Shadow Map Example:
http://www.ati.com/developer/samples/shadowmap.html

Apprently both examples use vertex/pixel shaders to do the job, so I've been doing some research on shaders. I'm beginning to realize that the question I originally asked was a bit too broad, so to narrow down the scope of this discussion, I'm mainly interested in how shaders are utilized conceptually to produce the shadow map. I already understand the math, but I'm not sure how shaders fit in to the Big Picture, so to speak. Anyway, for those who are interested, here are all the links I've found on the Internet so far that give good exlpanations of the theories that are involved.


DevMaster.net - Shadow Mapping and Shadow Volumes
Faster Ray Traced Terrain Shadow Maps - GPWiki
Gamasutra - Features - Real-time Shadow Casting Using Shadow Volumes [11.15.99]
GameDev.net - An Example of Shadow Rendering in Direct3D 9
GameDev.net - Ray Lighting
GameDev.net - Soft-Edged Shadows
GameDev.net - The Theory of Stencil Shadow Volumes
http--www.nealen.com-projects-ibr-shadows.pdf
Simple Terrain Shadow Algorithm (source code) - GameDev.Net Discussion Forums
Soft Shadow Mapping using Percentage Closer Filter

Share this post


Link to post
Share on other sites
To give an example of what I'm talking about, here's a screenshot of what I have so far:

http://www.artopium.com/worldsim/screenshots/tigercarship.jpg

I have loaded three mesh objects (tiger.x, car.x and spaceship 5.x from the DXSDK samples) using D3DXLoadMeshFromX and placed them over a mesh I created from a height map array using D3DXCreateMeshFVF. Notice that the three mesh objects all have shadows, but my terrain mesh does not! Can't I just recompute the normals of my terrain mesh so that it renders with shadows like the other objects? Is this what the above tutorials are showing, how to recompute the normals of each vertex? Would I then use shaders to cast shadows from the objects onto the terrain? And if so, how (conceptually)?

Share this post


Link to post
Share on other sites
OK, so nobody's responded to this topic but myself, and I can see why. After doing a lot of research I've discovered this question is too broad to answer in a discussion forum. :) Silly me. Or maybe everyone is sick to death of shadow discussions and it's just new to me. Whatever. One question I have been able to answer is that, yes, you can compute normals on a mesh created by D3DXCreateMeshFVF to get shadows that look like this (click to see screenshot). Because I have realized that my real problem is a lack of understanding shaders, I have begun a crusade to scour the Internet for any and all information relating to shader programming. So far, these are the links I have found that are worth while:

ATI Developer RenderMonkey™ Toolsuite - Overview
ATI Developer Source Code
Direct3D 8.1 Shaders
DirectXDirect3DTutorialsVertex Shaders Introduction - GPWiki
flipcode - Pixel Shader 2.0 Example
GameDev.net - Introduction to Shader Programming Part II Programming Vertex Shaders
GameDev.net - Shader Programming Part I Fundamentals of Vertex Shaders
GameDev.net -- Shader Programming Part III Fundamentals of Pixel Shaders
HLSL Shaders
Pieter Germishuys Graphics-HLSL Tutorials
Pixel Shader Reference
Programmable Shaders for DirectX 8.0
Shader Compilers


Unfortunately, after reading all this and much more, there are a few concepts I'm still missing for which I have created a new topic here. although these articles and tutorials have great technical information, I guess I just need to express some of these ideas in laymens terms so I can get a grip on them.

Share this post


Link to post
Share on other sites
I'm sorry, I can't help you - I haven't started looking at shadows just yet. I've been busy working on getting my terrain and skydome looking good. I can tell you that if you want to learn about shaders, a good book to get is Frank Luna's newest "Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach." Everything in the book uses shaders as opposed to the FFP as in the first book "Introduction to 3D Game Programming with DirectX 9." It was only around $33 shipped, so it's not very expensive and is an excellent learning tool.

Share this post


Link to post
Share on other sites
Here are a few notes to consider. First, what is it you want to accomplish? As in: lighting, shading, shadows, other? Lighting and Shadows work well together, but aren't necesarily the same thing. Calculating the normals and performing the lighting equation isn't shadowing, it's getting the ammount of light that hits a surface depending on how it faces, with the assumption that nothing is blocking it. Shadows don't care about the ammount of light, they simply determine if a point is hit by a light source, or if something blocks the light. These two methods work well together to produce shadowed AND lit scenes.

That's the first question, what is it that you want to accomplish? After that, you already seem to have plenty of links to useful papers, now it's just a matter of absorbing that material and implementing it. You have a basic model for lighting down (which is easy to reproduce in shaders since it's just NdotL), now onto shadows. Shadow mapping is simply a matter of rendering the scene from the lights point of view, then projecting that out onto the scene from the cameras point of view; checking to see if the visible pixel is behind whatever pixel the light sees.

Share this post


Link to post
Share on other sites
It seems you're mixing a bunch of different concepts here. From your first post, I think that you want to light your terrain in a more realistic way.

Those two first articles are about pre-generating a lightmap for the whole terrain. AFAIK it's not a realtime technique. So what you do is preprocess the terrain, generate the "shadow map", and then blend it when rendering the scene.

This will get you a result such as the pictures in the first article you posted. The terrain will be smoothly lit, and it will cast shadows into itself. Just that.

That's not the same as "shadow mapping", in the sense of rendering the scene from the light POV and then projecting it. You'd need to use it if, for example, you wanted the car to also cast its shadow on the terrain.

Share this post


Link to post
Share on other sites
You can also get your terrain lighting "smoother" by doing per pixel lighting using pixel shaders.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!