Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating world space coordinates in the pixel shader

I have a pixel shader and I want to calculate the position of each pixel in terms of my world space coordinates. How would I do this? What would I need?

I have a ps_input structure which has a float4 position : SV_POSITION. I'm assuming this is important, but the values stored inside seems to be kind of funny.

I can't seem to figure out what they relate to. For instance, if a pixel is 2d, how come it has a w component, or a z component for that matter?

I'm using DirectX and the pixel shader is in HLSL. C++ as my programing language on the CPU.

Any help would be appreciated.

like image 856
l3utterfly Avatar asked Oct 09 '12 06:10

l3utterfly


1 Answers

If i remember this correctly, SV_POSITION is only 0->W 0->H, so it describes where it is on the screen. this site contains more information : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx

To get the pixel to world space, you transform your pixelcordinate back to worlspace. it´s a bit tricky with the math and all ( i think it is! ) but this article should help you out! http://jcoluna.wordpress.com/2011/01/27/reconstructing-view-space-position-from-depth/

else wise search for pixel to viewspace. i know you wanted worldspace, but converting everything to viewspace is probably faster.

like image 176
Tordin Avatar answered Sep 30 '22 02:09

Tordin