orthogonal projection and clip space

24 Mar 2013

In OpenGL clip space coordinate system is between:[-1, 1].

attribute vec2 a_position;
uniform vec2 u_resolution;

void main(){
	// [0, 1] coordinate system
	vec2 clipSpace = a_position/a_resolution;
	// [0, 2] coordinate system
	clipSpace *= 2.0;
	// [-1, 1] finally clip space coordinate system
	clipSpace -= 1.0;
	gl_Position = vec4(clipSpace, 0, 1);
}

However, the origin of the clip space will be end up at bottom left corner. We can flip the y axis to achieve the top left origin, like Adobe Flash and other design tool.

attribute vec2 a_position;
uniform vec2 u_resolution;

void main(){
	// [0, 1] coordinate system
	vec2 clipSpace = a_position/a_resolution;
	// [0, 2] coordinate system
	clipSpace *= 2.0;
	// [-1, 1] finally clip space coordinate system
	clipSpace -= 1.0;
	// flip y axis
	clipSpace *= vec2(1, -1);
	gl_Position = vec4(clipSpace, 0, 1);
}

The OpenGL use column-major matrix. Using a 4x4 matrix represent the projection:

Use the above matrix in the GLSL:

1
2
3
4
5
6
7
8
attribute vec3 a_position;

// orthogonal projection matrix
uniform mat4 u_pMatrix;

void main(){
  gl_Position = u_pMatrix * vec4(a_position, 1);
}

Liy at 12:05