View Full Version : NDC to Screenspace

06-04-2011, 12:59 PM
Kinda a semi-newbie question,
Can someone please help me understand this:

vec2 screen_space(vec4 vertex)
return (clamp(vertex.xy, -1.3, 1.3)+1) * (screen_size*0.5);

Its from here: http://codeflow.org/entries/2010/nov/07/opengl-4-tessellation/

06-04-2011, 02:17 PM
I could be wrong but it seems to convert OpenGL screen space positions into application screen positions. When points are rendered they are transformed into the range of (-1 -> 1) for x and y positions. If your screen position is (0;0) then your opengl position is (-1;-1) and if your max resolution is (800;600) then the opengl position will be (1;1) at that point. Basically what the function is doing is taking the opengl co-ordinate in the shader and converts it into a screen space co-ordinate as the function is named.

06-04-2011, 03:45 PM
Ok thanks, but why does it clamp it to some strange values. Makes no sense.

06-05-2011, 12:52 PM
Could be to reduce artifacts caused by the shader, you can test it out by comparing (-1;1) with (-1.3;1.3) clamping to identify the need to clamp over the border of the screen.