OpenGL Coordinate System

Discussion in 'Mac Programming' started by Blakeasd, Sep 4, 2011.

  1. Blakeasd, Sep 4, 2011
    Last edited: Sep 4, 2011

    Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am starting OpenGL and I was looking a the Golden Triangle example on Apple's developer website. In the example the triangle is drawn like this:
    Code:
    glBegin(GL_TRIANGLES);{
            glVertex2f(0.0, 0.6);
            glVertex2f(-0.2, -0.3);
            glVertex2f(0.2, -0.3);
        
        
        }
    
    The triangle appears in the center of the window. I can't figure out why it appears in the center. If the OpenGL coordinates begin in the lower left corner and they are using measurements less than a pixel, then how does it get to the center. What am I missing?? :confused:
    In other words what unit of measurement is being used in "x" and "y"?
    Thanks!
     
  2. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #2
    It's not in pixels, it is "units" which are whatever you want them to be. In that tutorial 0,0 is setup to be in the center of the screen, and it's about -5,-5 to 5,5 to the edges of the viewport (depending on the aspect ratio). In OpenGL you can make the coordinate system whatever you like, and even change it when drawing different things by using matrices. You can examine the methods updateProjection and updateModelView in the file BasicOpenGLView.m to see how this is setup.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    In the Golden Triangle example where did they setup the coordinate system?
     
  4. szymczyk macrumors regular

    Joined:
    Mar 5, 2006
    #4
    The initial OpenGL setup has screen coordinates ranging from -1 to 1. The lower left corner of the screen is (-1, -1). The upper right corner is (1, 1). The center is (0, 0). I haven't seen the Golden Triangle example, but it probably didn't have to do anything to set the center of the screen to (0, 0).

    As rossipoo mentioned, you can change the coordinate system to whatever you want.
     
  5. SidBala macrumors 6502a

    Joined:
    Jun 27, 2010
    #5
    Who said that the coordinates start at the lower left corner?

    The default viewing volume in OpenGL has the origin at the center of the screen.

    However it is true that the numbers decrease as you go more to the bottom left.
     
  6. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #6
    There's a lot of setup that you need to do in OpenGL before you draw anything out. This is abstracted out by the NSOpenGLView used initially in the programming guide, hence how they get straight to drawing stuff.

    If you read a little further, in the section "Drawing OpenGL Content to a Custom View", you'll see they refer to preparing your OpenGL view without mentionning how to do so.

    A tutorial explaining the viewport and matrix setup needed is not hard to find though, as this is the very first chapter of any good OpenGL book. NeHe has a good one :

    http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/

    It's made for Win32, but there's sample code for a lot of platforms, including OS X. You could browse through that. Or look into the following functions :

    glViewport
    glLoadIdentity
    glMatrixMode
     
  7. adrian.oconnor macrumors 6502

    Joined:
    Jan 16, 2008
    Location:
    Nottingham, England
    #7
    +1 for NeHe. You should really go through all of the lessons if you want to learn OpenGL. I bet all of us here who have replied so far used NeHe to learn the basics.

    I struggled at first to get the code running on Mac (and it wasn't plain sailing on Windows either, to be honest), because it's a bit out of date. In particular, the Mac version of the tutorials uses glut, and from what I read glut is considered a bit antiquated now (it just didn't work well for me for some reason -- I kept getting a black screen). In the end I got it working on Windows (using the alternative texture loading function from NeHe's homepage) and from there it was easy to follow the tutorials.
     

Share This Page