I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why.

If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates?

// a struct for the vertex shader return value
struct VSOut
float4 Col : COLOR;    // vertex color
float4 Pos : SV_POSITION;    // vertex screen coordinates

// the vertex shader
VSOut VS(float4 Col : COLOR, float4 Pos : POSITION)
VSOut Output;
Output.Pos = Pos;    // set the vertex position to the input's position
Output.Col = Col;    // set the vertex color to the input's color

return Output;    // send the modified vertex data to the Rasterizer Stage

// the pixel shader
float4 PS(float4 Col : COLOR) : SV_TARGET
return Col;    // set the pixel color to the color passed in by the Rasterizer Stage

// the primary technique
technique10 Technique_0
// the primary pass
pass Pass_0
    SetVertexShader(CompileShader(vs_4_0, VS()));
    SetPixelShader(CompileShader(ps_4_0, PS()));
  • \$\begingroup\$ I'm pretty sure that texture coordinates work on a scale of 0-1 so if you divide the coordinates by the size of the screen (in each axis) you can get a number from 0-1 (and even if it's 0-2 you can just multiply by 2 to get the correct range) \$\endgroup\$ Jan 11, 2012 at 0:17
  • \$\begingroup\$ probably worth mentioning that if I put in 0,0 as the coords, that point draws dead centre. This puzzles me. @annonymously I tried, it didn't work unfortunately. \$\endgroup\$ Jan 11, 2012 at 0:24
  • \$\begingroup\$ Do you mean that the line draws from the center of the screen? \$\endgroup\$ Jan 11, 2012 at 0:28
  • \$\begingroup\$ yes. well one end of it does anyway. \$\endgroup\$ Jan 11, 2012 at 0:28
  • 1
    \$\begingroup\$ Oh I just realised that the coordinates are [-1 - 1]: riemers.net/eng/Tutorials/XNA/Csharp/Series1/World_space.php \$\endgroup\$ Jan 11, 2012 at 0:30

1 Answer 1


Screen coordinates go from -1 to +1 along each axis. You can remap from pixel coordinates by doing

x = (x / screenWidthInPixels) * 2.0 - 1.0
y = (y / screenHeightInPixels) * 2.0 - 1.0

This could be done in the vertex shader, in which case you'd have to pass in the screen width and height as parameters, or you could do it on the app side when you fill the vertex buffer.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .