World to screen conversion

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

Recommended Posts

Hi, How to convert a 3D world position to screen space? I'm trying Cascaded ShadowMaps for directional lights. The last thing that remains is picking which shadowMap split to use in the result shader. The shader gets 4 distances that split up the frustum. From the nVidia example:
	// f.fard is originally in eye space - tell's us how far we can see.
// Here we compute it in camera homogeneous coordinates. Basically, we calculate
// cam_proj * (0, 0, f.fard, 1)^t and then normalize to [0; 1]
far_bound = 0.5f * (-frustum.farD * cam_projMatrix[10] + cam_projMatrix[14]) / frustum.farD + 0.5f;

...
in the shader, do a depth/distance check:
if ( gl_Fragcoord.z < far_Bound[0] ) --> use texture 0   else
if ( gl_Fragcoord.z < far_Bound[1] ) --> use texture 1   else
if ( gl_Fragcoord.z < far_Bound[2] ) --> use texture 2   else

I'm using Cg instead of glsl, but it should have the equivalent; "WPOS" which is basically the screen position of the pixel. However, this doesn't work for me because I'm rendering on a screen filling quad, the Z component is missing. What I do have is the pixel world XYZ (not W) coordinate. I need to convert it somehow so it can be used in the comparison shown in the code above... Greetings, Rick

Share on other sites
Doing what gluProject does might work. If I'm interpreting the documentation correctly:
z = (projection_matrix*model_view_matrix*vec4(vec3_coords,1.0)+vec4(1.0))/2.0;

Share on other sites
Sorry for the late reply. I thought this post starved :)

Is it correct that I can reproduce that line to this shader code:
z = (mul( glstate.matrix.mvp, float4( worldPos.xyz, 1.0f ) ).z + 1.0f) / 2.0f;

Thus using the modelview projection matrix instead of proj * modelview? I tried it, but the Z value still seems to be wrong. Maybe I'm using the wrong matrix? The one above is from the current camera view, not the light perspective.

Thanks

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 12
• 9
• 9
• 9
• 14
• Forum Statistics

• Total Topics
633314
• Total Posts
3011326
• Who's Online (See full list)

There are no registered users currently online

×