# World Space to Screen Space Conversions

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

## Recommended Posts

Hi,

I'm trying to work with transformed quads, that is to say, quads that have there vertices defined in screen coordinates. Basically im trying to shift those quads to different points on the screen. The trouble is, those points are defined in world coordinates.

I'm attempting to write a shader that will accept transformed vertices (from a 2D quad), and shift those vertices to a point on the screen (lets call the point 'p') which is defined in world coordinates. Point 'p' is passed to the shader along with the quad vertex.

So basically, i need to convert my point 'p' to screen coordinates using the shader, and then translate my quad coordinates (defined in screen space) to 'p'.

I have a base quad that has the vertex coordinates: (0,1,0,1), (0,-1,0,1), (1,-1,0,1), (1,1,0,1). I then scale and shift the quad in the shader as appropriate. For this example, i scaled the x component of the vertices by 100, and the y component by 2. So you basically end up with a 4pixel high quad x 100pixels wide. The quad is built around the screen origin (top-left corner) so all i have to do is shift all the vertices is the quad by 'p' to place the quad in the right spot.

I can get my quads to move around to roughly the right relative positions from each other, but not the correct positions on the screen. It seems the magnitude of the translations is not right (only guessing).

Main shader code is posted below. Any help would be much appreciated.

 struct VSOutput { float4 Pos_ws; float4 Pos_ps; float4 Color; }; VSOutput ComputeVSOutput2DShift(float4 position, float4 col, float4 p) { VSOutput vout; // convert point to clip coordinates float4 pCLIP = mul(p, WVP); // convert point to screen space float3 pNDC = pCLIP.xyz / pCLIP.w; // adjust height position.y *= 2; // adjust width position.x *= 100; // convert vertex to screen space position.xy /= ViewportSize; position.xy *= float2(2, -2); position.xy -= float2(1, -1); // translate vertex position.x += pNDC.x; position.y += pNDC.y; // set output data vout.Pos_ps = position; vout.Color = col; return vout; }  Edited by porters

##### Share on other sites
Well to anyone who was interested, I figured out what the problem was. I just needed to make a minor adjustment to my translate vertex code (see below). The problem was that i had not taken into account that the origin of the NDC system is the centre of the viewport. Since the NDC system ranges from -1 to 1, i just had to add/subtract one in the vertex translation code.

 // translate vertex position.x += spNDC.x + 1; position.y += spNDC.y - 1; 

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015498
×