PDA

View Full Version : NDC to Screenspace



xyzwrgba
06-04-2011, 11:59 AM
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/

RefleX
06-04-2011, 01: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.

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

RefleX
06-05-2011, 11:52 AM
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.