Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Shadow matrix calculation

Started by parroteye Mar 5, 2017 at 7:31 PM 4 replies 2.7k views
Original Post
parroteye
parroteye

Hey,

I am writing my first renderer, so sorry if the question is question is stupid :(

I am trying to write my first attempt at shadows, but I am stuck with nothing rendered on my shadow map. Looking at the orthographic projection I am using, the values generated look odd, very small:


_11 0.00210536504  _12 0.000000000  _13 0.000000000  _14 0.000000000 
_21 0.000000000  _22 0.00331832888  _23 0.000000000 _24 0.000000000 
_31 0.000000000 _32 0.000000000  _33 0.00166944903  _34 0.000000000
_41 0.000000000 _42 0.000000000 _43 -0.00166944903 _44 1.00000000

So I am assuming this kind of matrix will crush any value to almost zero right?

The way I am generating it is:


float near = mainCamera.nearPlane(); // It's 1.0f
float far = mainCamera.farPlane(); // It's 600.0f
vec3 sceneExtent = 0.5*(sceneMax-sceneMin); 
float orthowidth = sceneExtent.x; // It's about 950.0f
float orthoheight = sceneExtent.z; // it's about 600.0f


orthoProjM = XMMatrixOrthographicLH(orthowidth, orthoheight, near, far);

Just for sake of completeness my shadow view matrix is computed as:


vec3 center = 0.5f * (sceneMax+sceneMin);
vec3 cameraPos = center - LightDir * 50.0f;


viewM = XMMatrixLookAtLH(cameraPos, cameraPos + LightDir, vec3(0.0f, 0.0f, 1.0f));

So I obviously trust d3d functions, but can anybody explain to me what I am doing wrong?

Thank you very much!!

Sorry if my english is bad, but it's not my first language :)

Jacques

P.S.

Just for the sake of completeness, I am rendering with a depth test of less or equal, setting the viewport to (0,0,shadowRes, shadowRes) and the vertex shader just transform world postions using orthoProjM*viewM

Krypt0n
Krypt0n
is the problem the shadow mapping or the rendering of the shadow map? Have you made a capture of your rendering and checked your shadow map?

if you make a capture, check the vertexbuffer before and after the vertex shader (for shadowmap rendering). this might already show the issue. (you can use e.g. NSight)
vinterberg
vinterberg

Your up-vector, shouldn't it be vec3(0.0f, 1.0f, 0.0f) and not vec3(0.0f, 0.0f, 1.0f) ?

.:vinterberg:.  
parroteye
parroteye

EDIT:

I am finding other issues with the code, will update when and if I actually track down the issue to the same problem I thought when I started... Thank you and sorry foir the bother

___

is the problem the shadow mapping or the rendering of the shadow map? Have you made a capture of your rendering and checked your shadow map?

if you make a capture, check the vertexbuffer before and after the vertex shader (for shadowmap rendering). this might already show the issue. (you can use e.g. NSight)

My problem is at rendering time of the shadow map, I submit the draw calls and nothing renders...

I will check, however I know for a fact that the vertex buffer contains good data, I will check the output, however, with the matrix I posted, wouldn't all get crushed to basically zero?

Your up-vector, shouldn't it be vec3(0.0f, 1.0f, 0.0f) and not vec3(0.0f, 0.0f, 1.0f) ?

Should just be a matter of conventions, right? In any case I tried with both same results.

EDIT:

Yup, like I assumed the output is zero for all vertices {x,y}

(from render doc)

WnNdrY0.png

EDIT2:

I changed my scales a bit (weren't accounting for scale in the scnee extents) and realized I needed to transpose the matrices... so I am not getting zeros anymore, but still nothing renders :(

CDProp
CDProp

I only checked a couple values, but the ortho matrix in your original post looks correct. The top left element is going to be 2/width, and so if your width is 950, then small numbers like 0.002105 are expected. Now that you're taking scale into account, these numbers might have changed a bit, but I doubt your ortho matrix is a problem.

Concerning the up vector, it depends on the LightDir. You'll just want to make sure that your up vector and LightDir are not in the same or opposite directions, as this will result in a singular matrix. Given your choice of coordinate system (where x and z are horizontal axes and y is vertical, and z seems to be north/south), I think that <0,0,1> is a wise choice.

Do you have a world matrix for these verts, or are they already in world space?

How big is shadowRes? And how large are your objects in these units, typically?

_WeirdCat_
_WeirdCat_

i think he messed projection and perspective mats, you should use perspective matrix for shadomap comparsion:

in eg

fragment shader

vec3 imo = shadow_perspective_matrix * vertex;

imo = imo / imo.w;

imo = imo + 0.5 + 0.5;

now if you are really using ortho but for what then

you mulsiply

vec3 test_v = view_ortho * vertex;

test_v = test_v / test_v.w;

test_v = test_v * 0.5 + 0.5;

0.00005 - this is more like float point inaccuracy thing 0.0001 should be max what float could define

theres always half of it (0.0001 / 2 ) which defines if fragment is in shadow, try to debug float and see if it will show 0.0001 - its more likely to get 0.00012323 or something smaller like 0.00009999997999999 but i dont have time to figure it out

if ( (test_v >= imo.z + 0.00005) || (dot(light_vec, face_normal) >= 0.0 ) )

fragment is in shadow

https://www.gamedev.net/topic/505893-orthographic-projection-for-shadow-mapping/

i may even say theres no such thing liek ortho shadow projection, maybe i dont know and dont have wasttime to search for it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.