0

In OpenGL your viewpoint is always at [0,0,0]. Say you have a vertex at this point as a part of a cube or some other object. Is that vertex in front of or behind the camera/viewpoint? After projection I always end up with w=1 when z==0, which also (as expected) happens to vertices with z==-1. So practically vertices with z=0 and z=-1 ends up at equal distance after projection.

Look how vec(2,2,0) and vec(2,2,-1) ends up with same screen coordinates here: https://jsfiddle.net/sf4dspng/1/

Result:

vec1:  x=2.0000, y=2.0000, z= 0.0000, w=1
proj1: x=0.9474, y=2.0000, z=-0.2002, w=1
norm1: x=0.9474, y=2.0000, z=-0.2002, w=1
view1: x=1.0000, y=0.0000, z= 0.3999, w=1

vec2:  x=2.0000, y=2.0000, z=-1.0000, w=1
proj2: x=0.9474, y=2.0000, z= 0.8018, w=1
norm2: x=0.9474, y=2.0000, z= 0.8018, w=1
view2: x=1.0000, y=0.0000, z= 0.9009, w=1

Why is that?

Arve Waltin
  • 4,098
  • 1
  • 15
  • 12
  • What if you skip clipping? Is w supposed to be 1 after projection if z value is 0? – Arve Waltin Sep 09 '16 at 12:37
  • Depends on what space you're talking about: [opengl spaces](http://learnopengl.com/#!Getting-started/Coordinate-Systems). – BeyelerStudios Sep 09 '16 at 12:38
  • This line in your fiddle `this.w = w || 1` is wrong. If you pass in `w=0` (which should happen for the first point), `w` will be changed to `1` and you get wrong results. In fact, points that lie on the eye plane (zero z-coordinate) do not have images, which is expressed by the division by zero. – Nico Schertler Sep 09 '16 at 14:04
  • @NicoSchertler If w == 1, vector is a position in space. If w == 0, vector is a direction. True? Thats why Im operating with w=1. – Arve Waltin Sep 09 '16 at 14:11
  • That is correct. But if you actually pass a 4D vector (as a result of the projection) that has a zero w-component, you will change the vector. Better use `typeof w === "undefined"`. – Nico Schertler Sep 09 '16 at 14:12
  • Where exactly is w switched to zero? As my log output shows, w is equal to 1 throughout the whole pipeline. – Arve Waltin Sep 09 '16 at 16:03
  • That's exactly the problem. The multiplication yields wrong results. Check the last row of the perspective matrix. It is `0, 0, -1, 0`, which means that the result's w-component will be the input's negative z-component. So if you feed in `z=0`, the result should have `w=0`. But your `Vector3D` constructor tampers with the `w` component and makes it `1`. – Nico Schertler Sep 09 '16 at 16:07
  • Gaah, that stupid zero is false. :) Thank you very much! But what to do with the "perspective divide" when w is zero? Skipping a vertex will cause shapes to deform. – Arve Waltin Sep 09 '16 at 17:20
  • That's why there is z-near clipping. This avoids dividing by zero. – Nico Schertler Sep 09 '16 at 17:37

1 Answers1

0

The coordinate system after all of your transformations are applied and the implicit perspective divide occurs after your vertex shader has been ran is called normalized device coordinates. In this space, the visible range of coordinates is from (-1,-1,-1) to (1,1,1); everything else gets clipped. (0,0,0) would be in the center of the screen, and halfway into the scene.

In clip coordinates, the coordinate system of values returned by your vertex shader, the visible range of XYZ values is (-w,-w,-w) to (w,w,w), since NDC is computed as clip coordinate's XYZ values divided by the W value.

Beyond that, it's up to what your vertex shader implements. Things like "object" and "view" space are commonly implemented by vertex shaders, but OpenGL is not actually aware of their existence. Visible values for those spaces are dependent on how you implement it in the vertex shader, and since the space transitions are usually defined using matrices, the visible coordinates depend on the matrix you use.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Are you then considering Clip Space (NPC) and NDC Space as the same space? Before making the visible area range from (-1,-1,-1) to (1,1,1), I think you have to do normalization which brings you into NDC space. – Arve Waltin Sep 09 '16 at 12:50
  • @ArveWaltin Edited. According to the spec, clip space is the space before the perspective divide, and NDC is after. My answer applies to NDC. – Colonel Thirty Two Sep 09 '16 at 12:55
  • Ok good, then we have a common understanding about spaces. Still, this does not actually answer the question. I think I will have to clarify with an example, give me some minutes. – Arve Waltin Sep 09 '16 at 13:05