6

Say I am using an Identity Matrix for my modelViewTransformation Matrix on an OpenGL ES 2.0 program. The Co-ordinate system in this case is the canonical OpenGL co-ordinate system which extends from (-1,-1,-1) to (1,1,1).

Is this coordinate system right-handed or left-handed?

A broader question: Is there a document with OpenGL which can list all the mathematical conventions followed by the API?

4 Answers 4

7

ALWAYS REMEMBER!! OpenGL only knows about Normalized Display Coordinates (NDC), and it is LEFT_HANDED!

The illusion OpenGL is right-handed is because in the fixed pipeline, the fixed functions are right-handed, like glOrtho(...), glFrustrum(...). All these functions have been deprecated in programmable-pipeline times.

OpenGL doesn`t care what handed coordinate system you use for intermediate matrix processes. You can use any axis coordinate system if you want, as long as you translate that to NDC.

Forget about the camera!

Because our screen is a 2D plane. Imagine this, your viewport is a yellow rubber square. Pin the four corners of that square to the -Z side of the NDC, something like this:

A wireframe image of an orthonormal cube with corner vertices being all combinations of -1 and +1 in (X, Y, Z), with a visible origin/axes marker in its center that has +X pointing right, +Y pointing up, and +Z pointing away from the viewer, with the -Z cube side facing the viewer colored yellow to highlight it.

All vertices in the -Z axis in NDC space are on, and define, the yellow rubber square. The yellow rubber plane is then stretched to be fitted to, and shown on, your actual screen.

6

My question is, is this coordinate system right-handed or left-handed?

By default, OpenGL is always right-handed. You can observe this by the automatic normal creation. You can force a left-handed normal creation by specifying it per point but, in general, right hand rule applies all the time. See 9.150 in the OpenGL FAQ for more discussion of the right-hand-only nature of OpenGL.

... all the mathematical conventions followed by the API?

It's not clear what you're asking for. The math is basic linear algebra with a strong focus on matrix math and linear transformations.

EDIT to respond to comment question:

REMEMBER, however, that if you are using the uniform matrix calls rather than the older glRotates, etc, that you must specify whether you are using row-major or column-major matrices. In this case (from the code mentioned in the comment):

glUniformMatrix4fv(uniforms[UNIFORM_MVP], 16, GL_FALSE, mvpMatrixZRotation);

In this call, GL_FALSE is telling the call that this is a column-major matrix and, as such, the rotation that results will be the transpose of what was intended. Therefore, the rotation will be inverted, looking like a left-handed coordinate system.

Change that to GL_TRUE and all will be well.

Here is a very simple example from the OpenGL discussion board that's relevant to this specific topic.

Yet another edit to respond to the request for comment: Here is another detailed explanation of the matrix pipeline in OpenGL. Notice the GL_MODELVIEW diagram with the three axes: they illustrate the right-handed coordinate system. The above citation in the FAQ also still applies.

13
  • Lets say OpenGL is right-handed. Say I draw the x,y axis on the screen with the transformation matrix being Identity and then rotate them about z-axis by an angle theta = 30 Degrees. So if the system is right handed the rotation should occur counter-clockwise. However, the rotation happens to be clockwise indicating a left-handed system. So this is confusing me.
    – praveen
    Mar 3, 2011 at 7:44
  • @praveen, I think you're misunderstanding the order of operations. If you post actual code, the reason will become more clear. Check 9.070 in the OpenGL FAQ: opengl.org/resources/faq/technical/transformations.htm
    – Bob Cross
    Mar 3, 2011 at 13:21
  • @BobCross Hi Bob. I am trying to learn OpenGL ES on iOS. I believe the canonical view volume has a left-handed coordinate system. The code from my program can be found here : link. [To try this code just replace the render function in the iOS OpenGL template project, generated by XCode, with this one] Given the angle is positive and the system is right-handed, the rotation should happen in anti-clockwise direction. Contrary to this, the rotation occurs in clockwise direction.
    – praveen
    Mar 3, 2011 at 13:55
  • @praveen, your code has a FALSE where it should have a TRUE: you're passing in column-major rather than row-major. See the updated answer for details.
    – Bob Cross
    Mar 3, 2011 at 15:40
  • @bobcross hi Bob, the transpose parameter is supposed to be GL_False if the matrix is column major which is the case in my code. So I don't think that parameter is the problem.
    – praveen
    Mar 3, 2011 at 19:51
6

The canonical view volume aka normalized device coordinates is/are left-handed. This is easy to test by passing the identity matrix and drawing 2 triangles say

float points[] = { -1,  1,  -0.5,
                   -1, -1,  -0.5,
                    1, -1,  -0.5,

                    1,  1,   0.5,
                   -1, -1,   0.5,
                    1, -1,   0.5 };

and looking at the results (with glEnable(GL_DEPTH_TEST))

So your vertex shader will look like this:

uniform mat4 mvp_mat;

// Incoming per vertex... position (fills in 1 for w if from vec3 buf)
layout (location = 0) in vec4 v_vertex;


void main(void) 
{ 
    // multiplying by identity doesn't transform the geometry
    gl_Position = mvp_mat * v_vertex;
}

or you could even test it more simply like this since the identity matrix does nothing

layout (location = 0) in vec4 v_vertex;

void main(void) 
{
    //no transformation, no confusion, just straight to OpenGL
    gl_Position = v_vertex;
}

Obviously you'll have to make the colors of the triangles different so you can see how they overlap.

Also see this post and my comment/question in it: plus.google.com/114825651948330685771/posts/AmnfvYssvSe

I don't know where all the incorrect information comes from all over the internet and even in textbooks. OpenGL is only right handed by default if you're talking about triangle winding for determining the front face (CCW by default = right handed). World space, object space and eye space don't exist to OpenGL, only to the graphics programmer. OpenGL just takes points, clips anything outside of the canonical view volume [-1, -1, -1] x [1, 1, 1] and transforms them to screenspace/window coordinates which by default is [0, w) x [0, h) x [0, 1]. If the depth test is enabled the default behavior is left-handed, looking down +z.

There are several ways to account for OpenGL's left-handedness. Most people handle it in their matrices (though they don't realize it). I think you can also change it using glDepthFunc and setting it to GL_GREATER.

http://www.opengl.org/sdk/docs/man3/xhtml/glDepthFunc.xml

Further proof that it is left handed is here http://www.opengl.org/sdk/docs/man3/xhtml/glDepthRange.xml

"After clipping and division by w, depth coordinates range from -1 to 1, corresponding to the near and far clipping planes" ie positive z goes into the screen = left-handed. You can also use DepthRange to change to right-handed behavior.

EDIT: In Response to Bob Cross's insistence that I'm wrong, here's a single source file program that shows that I'm right. https://github.com/rswinkle/opengl_reference/blob/master/src/left_handed.c

You can see the screenshot in the README https://github.com/rswinkle/opengl_reference

Final Edit: In September 2020 I released PortableGL. If anyone wants to see the internals/math of the OpenGL pipeline and as some evidence I know what I'm talking about here: http://portablegl.com/

7
  • Wow!!! This is a very good explanation. So my stuff is still wrong though some matrix math may have compensated for the wrong assumption.
    – praveen
    Mar 27, 2014 at 10:07
  • Thanks. Well you know us programmers, if it looks like it's running right, it doesn't matter how. ha. But really I added the link to the Google+ thread to show another aspect of confusion. The whole row-major vs column-major debacle which further obscures the issue.
    – rswinkle
    Mar 29, 2014 at 3:09
  • Dang sorry, this is the first time really using StackOverflow. I meant there to be more to the above comment but hit enter and apparently I can't edit it after 5 minutes. Anyway I'll let it rest but this whole OpenGL handedness and matrix row vs col-major rats nest is a pet peeve of mine.
    – rswinkle
    Mar 29, 2014 at 3:20
  • Hey Praveen, I know it's been almost a year and I should have asked then, but can you change the accepted answer? Or maybe comment on Bob's poking him into responding to my answer? I don't want to edit his post and I don't have the required rep to comment on his.
    – rswinkle
    Feb 2, 2015 at 18:13
  • Removing accepted answer as I am currently not in a to verify this and there seems to be some disagreement with this thing. So I will let the community judge for itself.
    – praveen
    Feb 7, 2015 at 8:55
2

Opengl ES coordinate system is indeed Right Handed System including the Canonical Volume of (-1, -1, -1) to (1,1,1). I have verified this through code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.