7

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

10

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks for the explanation but I still wonder why I get nothing on the screen when w is negative even with all the coordinates within [-1,1] range. Do you think it could be a driver problem or something? I use intel HD graphics with Haswell architecture if it helps –  Nov 11 '17 at 20:01
  • I see but they still remain inside [-1,1] after division. Would you do me a favor and test it on your own system please? Just draw a triangle with its w coordinates set to -1 and see if anything is shown. –  Nov 11 '17 at 20:12
  • @ You are right, if `w = -1` the vertex is clipped, see the adds to the answer – Rabbid76 Nov 11 '17 at 20:47
  • That's interesting. I never knew -w must be less than w –  Nov 11 '17 at 21:12