Beginning OpenGL on OS X

First I have to start with a quick explanation for people who don’t know what OpenGL is. OpenGL is NOT a language. In fact, OpenGL is merely a specification which is implemented in various languages. I have always used C++ and the GLUT library, it simplifies some things. Anyway, more info can be found with a quick google search if you are interested.

Today I am going to focus primarily on getting started using OpenGL on OS X instead of doing fancy things. A couple months ago I took a course on Computer Graphics at my university. The problem was that this professor was pretty much Windows only. Needless to say I was in trouble, I didn’t know C++ and had never worked with it. Here is what I learned about setting up a usable environment

Required software:
Mac OS X (This tutorial was written using 10.5.7 but the overall concepts should work in modern versions of OS X)
XCode 3.1

Required Skills:
None, it would probably help to know C++ but if you are just learning the language don’t worry. I don’t assume you know anything about C++, frankly I didn’t when I was first learning how to do this.

First thing is first, start up XCode and create a new Project (File->New Project)

Creating a new Project using OpenGL

You want to select Mac OS X and then Command Line Utility. Don’t worry if your XCode window doesn’t look precisely like that. I named my project Intro_To_OpenGL and saved it somewhere I can find it later.

Starting Your Project

The next step is to bring in the correct frameworks, you will need GLUT and OpenGL. To do this you can right click (Control Click if you do not have a 2 button mouse) on the Intro_To_OpenGL in the Groups and Files browser and select Add->Existing Frameworks. Then select OpenGL.framework and GLUT.framework which are located in /System/Library/Frameworks. You can Command select the two frameworks or you can add them separately. Either way they need to be added to your Project.

After adding the frameworks.

After you have added the frameworks you can now start writing some code. You will see in the above I selected main.cpp. This is the main file of a C++ program. This simple program will just use this one file. Normally this is a pretty bad way of doing things but this program won’t be very long so we can get away with it.

The Code

//A couple includes

#include <iostream>//This is just for basic io, you really DON’T need it

#include <GLUT/GLUT.h>//GLUT Library, will make you life easier

#include <OpenGL/OpenGL.h>//OpenGL Library

//Let us create a function to initialize everything

void myInit(void){

glClearColor(1.0,1.0,1.0,1.0);//This determines the background color, in this case White

glMatrixMode(GL_PROJECTION);//Determines which matrix to apply operations to. Don’t worry about this for now just assume it has to be there

glLoadIdentity();//Loads the Identity matrix

gluOrtho2D(0, 640, 0, 480);//Set the size and projection of the buffer

}

//Now, lets tell it to display some stuff

void myDisplay(void){

glClear(GL_COLOR_BUFFER_BIT);//Clear the buffer

glColor3d(1,0,0);//Set the color for this polygon

glBegin(GL_POLYGON);//Let us begin drawing some points

//Specify the points

glVertex2i(100,50);

glVertex2i(100,130);

glVertex2i(150, 130);

glVertex2i(100,50);

glEnd();//Ok we are done specifying points

glFlush();//Write this out to the screen

}

int main (int argc, char **argv) {

glutInit(&argc,argv);//Init glut passing some args, if you know C++ you should know we are just passing the args straight thru from main

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//Specify the Display Mode, this one means there is a single buffer and uses RGB to specify colors

glutInitWindowSize(640, 480);//Set the window size

glutInitWindowPosition(100,100);//Where do we want to place the window initially?

glutCreateWindow(“My First Window”);//Name the window and create it

glutDisplayFunc(myDisplay);//Set the callback function, will be called as needed

myInit();//Initialize the window

glutMainLoop();//Start the main loop running, nothing after this will execute for all eternity (right now)

return0;//Return from this function.

}

The output of this simple program

I hope this helps you guys, this is just the basics if there is interest I will continue writing tutorials on more advanced things using OpenGL, GLUT, and C++. As a challenge, turn the background blue, draw a black octagon on top of it. Good luck.