user8221752
user8221752

Reputation:

Negative values for gl_Position.w?

Is the w component of gl_Position required to be greater than zero? Because when I set it to a negative number nothing is drawn but positive numbers are fine.

gl_Position = vec4(vPos,0,-1);

Face culling is not enabled btw.

Upvotes: 7

Views: 3433

Answers (1)

Rabbid76
Rabbid76

Reputation: 211176

Has the w component of gl_Position required to be greater than zero?

No, but the result of gl_Position.xyz / gl_Position.w has to be in the range (-1,-1,-1) to (1,1,1), the normalized device space. This means each component (x, y and z) of the result, has to be >= -1.0 and <= 1.0.

enter image description here

But, if the w component is negative, nothing is draw anyway. Because gl_Position defines the clip space. The condition for a homogeneous coordinate to be in clip space is

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

If w = -1 this would mean:

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

and that can never be fulfilled. (see Why does GL divide gl_Position by W for you rather than letting you do it yourself?)

Explanation:

The coordinates which are set to gl_Position are Homogeneous coordinates. If the w component of a Homogeneous coordinate is 1, it is equal to the Cartesian coordinate built of the components xyz.

Homogeneous coordinates are used for the representation of the perspective projection.

In a rendering, each mesh of the scene usually is transformed by the model matrix, the view matrix and the projection matrix.

The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The projection matrix transforms from view space to the clip space, and the coordinates in the clip space are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, 1) by dividing with the w component of the clip coordinates.
Every geometry which is out of the NDC is clipped.


At Orthographic Projection the coordinates in the eye space are linearly mapped to normalized device coordinates. (TCommonly the w component is 1.0)

enter image description here


At Perspective Projection the projection matrix describes the mapping from 3D points in the world as they are seen from of a pinhole camera, to 2D points of the viewport.
The eye space coordinates in the camera frustum (a truncated pyramid) are mapped to a cube (the normalized device coordinates).

enter image description here

Upvotes: 10

Related Questions