Jump to content
  • Advertisement
Sign in to follow this  

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.

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

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 this post

Link to post
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 this post

Link to post
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.


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!