gl-matrix
gl-matrix copied to clipboard
Add note somewhere in the documentation that clarifies the handedness of the coordinate system
Hello!
First off, thanks for the fantastic library! I've been using it to get better acquainted with WebGL.
My request is just that, somewhere in the documentation (right under the part about column-major ordering, maybe?), you add a disclaimer that notes the handedness of the coordinate system being used. Based on my initial messing around with this library, I believe the system is right-handed. But it would be nice to confirm that, and to have it noted in the docs.
Thanks!
Tricky question. I'm a new user too.
Internally to this matrix library, one can work completely without ever referring to handedness of coordinate systems.
Handedness really occurs when the 3d-coords are mapped to the screen. A web search of handedness of OpenGL/WebGL give contradictory answers. My own experience seem to point at a left handed coordinate system: without using any matrix transformation, here is what WebGL did by defaut: it takes the intersection of you objects with the cube [-1,1]x[-1,1]x[-1,1] then projects x to screen horizontal coord (-1 is left, 1 is right) y to vertical one (-1 is bottom, 1 is top) and last z is used for the z-buffer and 1 is behind (away from the guy looking at the screen), -1 is in the front (near the guy).
Now there is a subtlety: rotations. How to decide if a rotation around some axis is of angle +alpha or -alpha. Say we take the X axis. Well, in fact we need an oriented axis so say we take +X axis. Then the angle measuring rotations is measured by looking at a vector in the YZ plane and looking at how much it turned in this plane. So it amounts to orienting the YZ plane (being implied that +X has been chosen). Usually the orientation is the one seen as turning from +Y to +Z. So rotation around +X by +90deg will map +Y to +Z.
In the gl-matrix library, the function mat4.rotateX(out,a,rad) does out <- a.R where R is the matrix of rotation by -alpha. This could be a source of confusion.
This is a bit counterintuitive and not practical for my purpose but I think I have guessed why it is done like that: it is to avoid touching the translation part. In other words if you rotate an object around X you rotate it around the object's own X axis, not around the world X axis, and in the workd coordinate the object indeed has turned of +alpha around its own X axis, not -alpha.
Yes. Handedness comes into play when you transform view/camera/eye-space into clip space, which by convention is usually what the GL_PROJECTION matrix did. With clip space in OpenGL we have by definition a left-handed coordinate system, that is, its +Z axis points into the screen away from the user (so, in the direction a user in front of the screen is looking at). But by convention when we work in the model-/world-/view-coordinate systems we like to do so in a right-handed coordinate system, where +Z points out of the screen towards the user sitting in front of the screen. All that is by convention and in fact, Direct3D uses left-handed model-/view-/world-space and does not do any handedness conversion. And all that directly affects how methods/functions like gluPerspective and glOrtho (which glMatrix and every other matrix library like GLM follow) are implemented, because they invert the Z axis, which effectively makes our right-handed view-space coordinate system into a left-handed clip-space coordinate system. So again, by convention in OpenGL we like to have a right-handed model-/world-/view-coordinate system and by definition we have a left-handed clip space coordinate system.
Got it wrong while reading the code : mat4.rotateX multiplies on the right, true, but by R(alpha), not R(-alpha). Need more thinking here...
Oh, @httpdigest, I see: inverting the handedness at the end is quite an inelegant choice by openGL but we can't change this.
Interestingly enough I wrote all my recent code (this week) with the convention that we have a left handed system (but with the convention for rotations like in a right handed system). I have no multiplication of z by -1 at the end and it all works nicely: I cooked up my own projection matrix instead of using a tool like glOrtho or an equivalent from gl-matrix.js. It had not the minus sign.
I still need to figure out what is the implicit convention for +alpha/-alpha with rotations.
I investigated further and this getting even worse: culling! I oriented all my faces of my sphere in a CCW way. Recall I am using a left handed coordinate system, so they are oriented CCW in the left handed system. Until now I had no culling. I just switched on backface culling with the default setting (meaning front faces are CCW). I was expecting to see the interior of the sphere because of the right handedness convention mentionned above. It turns out I still saw the outside: conclusion the CCW character of the face is decided in the left handed system.
Addition: as a matter of fact it is probably decided after projection on the screen, right?