Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fourth dimension in a GLSL gl_Position?

I'm learning OpenGL, and currently I'm writing a shader, but I'm confused as to why the gl_Position variable is a vec4 instead of a vec3, as I'd expect. I'd expect this of course because it uses 3D space, not 4D.

I hope I'm at least right in assuming that the first three fields of gl_Position are indeed the x, y, and z ordinates of the position.

In case my question isn't clear enough: what's the fourth field of gl_Position.

By the way, I'm using OpenGL 3.2 and GLSL 1.5.

like image 860
Jacob Garby Avatar asked Mar 06 '23 11:03

Jacob Garby


1 Answers

gl_Positionis a Homogeneous coordinates. At orthographic projection the 4th component w is 1. As a result of a perspective projection, the component assumes another value than 1.

gl_Position can be transformed to a Cartesian coordinate in normalized device sapce by a Perspective divide.

vec3 ndc = gl_Position.xyz / gl_Position.w;

gl_Position is the clip space coordinate. In clip space the clipping of the scene is performed.
A point is in clip space if the x, y and z components are in the range defined by the inverted w component and the w component of the homogeneous coordinates of the point:

-w <=  x, y, z  <= w.

See also Transform the modelMatrix

like image 130
Rabbid76 Avatar answered Mar 12 '23 20:03

Rabbid76