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.

DarkBASIC Professional Discussion / Any tutorials on directional lighting with shadow mapping?

Author
Message
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 2nd Aug 2013 15:40
Like the title suggests I'm currently looking for a comprehensible tutorial on how to accomplish per-pixel directional lighting along with generating a shadow map to go with such a light source. The shadowing seems to be the general problem as just directional lighting tutorials are relatively plentiful on the internet.

Googling for it yields surprisingly unhelpful results for the most part, all from forum posts describing it merely as "the easiest type of lighting / shadowing - you can figure that out yourself if you're worth your salt", to tutorials that are based on a huge framework that has been built throughout 40+ previous tutorials in a series. Or ones that use GLSL or DirectX 11-specific functionality.
Ideally I'd like to see a to-the-point tutorial using DirectX 9 since that's what I have to work with in DBP. If there are any functions required that aren't exposed in DBP I'm fairly confident I could hack something together to access those anyway, but I can't add OpenGL or DirectX 10 / 11 support to a DirectX 9 engine.
So I was wondering if anybody around here knew of any such texts?

Oh, and I'm well aware that Evolved "has already solved all this" - but I want a somewhat lighter solution that I can understand and thus later on modify myself to my suiting.


Thanks for any suggestions,
Rudolpho


"Why do programmers get Halloween and Christmas mixed up?"
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 7th Aug 2013 16:08
I've eventually managed to end up with this:


It looks decent I guess (lots of surface acne though) but I was thinking that there must be a way to get more detail for the shadow map by only rendering the area that can currently be seen by the main camera. Or am I wrong?
Currently I'm rendering the whole scene from a imagined sun's position to a 4096x4096 image and utilizing 25-texel percentage closer filtering. Still so the shadows are very blocky.

Also I know how to create an orthographic projection matrix for the depth-rendering camera, but it is a mess to align that properly.
Do anybody know how to position such a camera so that it points in the same direction as the directional light from any given position?
Basically all I've managed to do with it is move it a certain distance from (0, 0, 0) along one axis and then rotate it in some "panorama-like" way.
Or should I just use a standard camera (as was done in the above screenshot)? Maybe increase the fov a bit?


"Why do programmers get Halloween and Christmas mixed up?"
Inflictive
15
Years of Service
User Offline
Joined: 16th Jun 2009
Location: Altis
Posted: 7th Aug 2013 20:06
It looks like you've pretty much got it. As for the position of the camera, it can be positioned just about anywhere as long as it is looking in the same direction as the light source. In your pixel shader you need to have a case where no shadow is rendered if the pixel is outside of the shadow camera.

You can sort of only render shadows in view by putting the shadow camera in front of the main camera, then moving it in the direction of the light source (so it "looks down on" the part of the scene which is in front of and close to the main camera, not the entire scene). Then to keep it from flickering with small movements of the main camera you round the shadow camera's position to the nearest 10,10,10 or something.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Aug 2013 21:27
Quote: "In your pixel shader you need to have a case where no shadow is rendered if the pixel is outside of the shadow camera."


A better way to do it is to use the BorderColor option in the shadow map sampler. Something like this (you'll need to choose appropriate names and values for your application of course):





Powered by Free Banners
Inflictive
15
Years of Service
User Offline
Joined: 16th Jun 2009
Location: Altis
Posted: 7th Aug 2013 22:10
Is it possible to use the depth map directly when rendering the shadow camera? I saw a "D32" image format and you can use the ": DEPTH" semantic rather than ": COLOR" in the pixel shader.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Aug 2013 02:35
I'm not sure whether that is possible in DBPro.



Powered by Free Banners
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 8th Aug 2013 15:03
Quote: "You can sort of only render shadows in view by putting the shadow camera in front of the main camera, then moving it in the direction of the light source"

That sounds reasonable, however wouldn't that call for an orthographic camera as well? Otherwise the shadows cast would be huge / all-encompassing if the camera managed to end up just behind some object. Even if in theory the main camera is also in shadow in that case, light would realistically seep through to light up areas further in front of it if the light source was "infinitely" far away.
I just can't figure out how to position these ortho cameras in 3d space; essentially you can set how many units across and down will be seen by it and that's it. I'm trying out the more advanced D3DXMatrixOrthoOffCenterLH function at the moment, which sort of seems to allow movement along the X and Y axis but it is irradic at best... I don't suppose you happen to know anything about how to manage that?
Anyway, thanks for the suggestion; I will keep tinkering with it to see if I can come up with anything


@Green Gandalf: I was not aware of the border attribute for samplers. I've been doing this for now:

But that sounds like it could be both more readable and efficient; I'll switch my custom check for that, thanks.

Quote: "Is it possible to use the depth map directly when rendering the shadow camera? I saw a "D32" image format and you can use the ": DEPTH" semantic rather than ": COLOR" in the pixel shader."

I tried that and no, it didn't work (actually it crashed the engine).
Using the D3DFMT_R32F format and only writing to the red channel works swell however.


"Why do programmers get Halloween and Christmas mixed up?"
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 11th Aug 2013 14:19
I'm finally starting to get results from this, after hunting the web for instructions for days.
It seems it's not as complicated as I made it out to be, however you need to access some functions that are not in DBPro by default.

I'll try to write down my solution if anybody else is interested once I get it to work more flawlessly. At the time being the program freezes on exiting...

The basic steps are to create a orthographic projection matrix, which basically describes a box containing the area to be rendered, then continually update a view matrix which is constructed using the build lookat lhmatrix4 function and override the camera's matrix using that (this can be done using the GetCameraData function in DBProCameraDebug.dll and IanM's pointer functions).

Current looks (very simple test scene, I should probably try it with a terrain for more realistic lighting later on):



"Why do programmers get Halloween and Christmas mixed up?"
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Aug 2013 02:59
Looks very promising. I recently needed an orthographic camera for shadows but didn't finish it so I'll be very interested to see the details of your solution.



Powered by Free Banners

Login to post a reply

Server time is: 2024-09-08 05:58:02
Your offset time is: 2024-09-08 05:58:02