Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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!
 
Last edited:
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!

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.
 
In the Golden Triangle example where did they setup the coordinate system?
 
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.
 
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.
 
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
 

+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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.