Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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()));
    SetGeometryShader(NULL);
    SetPixelShader(CompileShader(ps_4_0, PS()));
}
}
share|improve this question
    
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) –  annonymously Jan 11 '12 at 0:17
    
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. –  Asher Einhorn Jan 11 '12 at 0:24
    
Do you mean that the line draws from the center of the screen? –  annonymously Jan 11 '12 at 0:28
    
yes. well one end of it does anyway. –  Asher Einhorn Jan 11 '12 at 0:28
1  
Oh I just realised that the coordinates are [-1 - 1]: riemers.net/eng/Tutorials/XNA/Csharp/Series1/World_space.php –  annonymously Jan 11 '12 at 0:30
show 5 more comments

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.