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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With