5

I am learning openGL from this scratchpixel, and here is a quote from the perspective project matrix chapter:

Cameras point along the world coordinate system negative z-axis so that when a point is converted from world space to camera space (and then later from camera space to screen space), if the point is to left of the world coordinate system y-axis, it will also map to the left of the camera coordinate system y-axis. In other words, we need the x-axis of the camera coordinate system to point to the right when the world coordinate system x-axis also points to the right; and the only way you can get that configuration, is by having camera looking down the negative z-axis.

I think it has something to do with the mirror image? but this explanation just confused me...why is the camera's coordinate by default does not coincide with the world coordinate(like every other 3D objects we created in openGL)? I mean, we will need to transform the camera coordinate anyway with a transformation matrix (whatever we want with the negative z set up, we can simulate it)...why bother?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
watashiSHUN
  • 9,684
  • 4
  • 36
  • 44
  • that is only for `gluPerspective` projection matrix... if you use Identity instead then you will looking at Z+ direction ... you can also construct your own perspective projection facing to Z+ or any other axis ... see http://stackoverflow.com/a/28084380/2521214 and http://stackoverflow.com/a/21100338/2521214 – Spektre Oct 29 '15 at 07:14

3 Answers3

7

It is totally arbitrary what to pick for z direction.

But your pick has a lot of deep impact.

One reason to stick with the GL -z way is that the culling of faces will match GL constant names like GL_FRONT. I'd advise just to roll with the tutorial.

Flipping the sign on just one axis also flips the "parity". So a front face becomes a back face. A znear depth test becomes zfar. So it is wise to pick one early on and stick with it.

starmole
  • 4,974
  • 1
  • 28
  • 48
5

By default, yes, it's "right hand" system (used in physics, for example). Your thumb is X-axis, index finger Y-axis, and when you make those go to right directions, Z-points (middle finger) to you. Why Z-axis has been selected to point inside/outside screen? Because then X- and Y-axes go on screen, like in 2D graphics.

But in reality, OpenGL has no preferred coordinate system. You can tweak it as you like. For example, if you are making maze game, you might want Y to go outside/inside screen (and Z upwards), so that you can move nicely at XY plane. You modify your view/perspective matrices, and you get it.

MaKo
  • 746
  • 3
  • 11
2

What is this "camera" you're talking about? In OpenGL there is no such thing as a "camera". All you've got is a two stage transformation chain:

  1. vertex position → viewspace position (by modelview transform)
  2. viewspace position → clipspace position (by projection transform)

To see why be default OpenGL is "looking down" -z, we have to look at what happens if both transformation steps do "nothing", i.e. full identity transform.

In that case all vertex positions passed to OpenGL are unchanged. X maps to window width, Y maps to window height. All calculations in OpenGL by default (you can change that) have been chosen adhere to the rules of a right hand coordinate system, so if +X points right and +Y points up, then Z+ must point "out of the screen" for the right hand rule to be consistent.

And that's all there is about it. No camera. Just linear transformations and the choice of using right handed coordinates.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • You missed the view transform. Which in GL also has "good defaults" but otherwise is quite strange in z by mapping -1..1 to window z 0..1. – starmole Oct 29 '15 at 10:58
  • 1
    @starmole: No, I didn't miss that, because there is no such thing as a a "view transform". What you mean is probably the perspective divide followed by mapping into NDC space. However I deliberately left this out, as this is irrelevant to OPs problem; unlike the vertex setup (modelview and projection in fixed function) the NDC mapping is outside the control of the programmer; it just happens, and the best you can do is setting the depth value range and viewport in window space; but there are strict constraints on the values and their choices; so strict, that IMHO it's not worth discussing them – datenwolf Oct 29 '15 at 11:18
  • No attack intended. I think it's the one peculiar thing though. In classic GL you can actually flip z freely. Classic DX would map view z from clip as identity. In GL one could fix a wrong z sign by flipping the depth test. Not so in DX. Your answer just reminded me of that and default choices. – starmole Oct 29 '15 at 11:44
  • @starmole: That is indeed the case and you can also flip the depth range values. Used to be a popular technique to save on depth buffer clears, which in ancient times used to be a rather expensive operation (due to hardware shortcomings). – datenwolf Oct 29 '15 at 13:27
  • The default (NDC) coordinate system is actually left-handed, with the z-axis pointing into the screen. It's the commonly used projection matrix that flips the coordinate system from the right-handed system that most people prefer to use to the left-handed NDC coordinate system. – Reto Koradi Oct 30 '15 at 04:38
  • @RetoKoradi: Indeed that is the case, due to the way the Z values are mapped (0…1 for increasing depth). There are few other inconsistencies of that kind in OpenGL, like the way cube map coordinates are mapped. – datenwolf Oct 30 '15 at 09:47