This post is intended as an easy step-by-step guide to get things started in OpenGL land for the complete laymen. It is not an OpenGL or GLUT tutorial but just a guide how to set things up, creating a window in which we can draw stuff.
Although you might want to dive further into C or C++ and OpenGL, this tutorial will provide you with some sort of sandbox in which to further experiment or at least allow you to copy/paste examples you find online and run them. Hopefully it is of any use to someone.

There are a lot of other libraries out there and some of you may argue that GLUT is buggy and outdated. One thing it does do though is ‘being there’ in the system folder and work.

As I just downloaded the new Xcode 4 and had to figure out a few things because of the new lay-out, I figured I might as well note everything down and possibly help someone trying to set it all up themselves.

First of: Get the latest Xcode from Apple.

If you’ve got everything installed, open Xcode from the applications folder or the lauchpad.

When you open the application you will be greeted by this window:

Select ‘Create a new Xcode project’

Xcode will open and present you with the following smörgåsbord of possibilities:

Make the same selection as in the screenshot above: under Max OS X select ‘Application’ and from the options which will appear on the right select ‘Command Line Tool’, click next.

Name the ‘product’ and set your ‘company identifier’ as per request of next window. Under ‘Type’ select C++, click next. (The automatic reference counting option is up to you.)

Next save the project in your prefered location.

Ok. So far so good. Next stop you’ll see this window (click to enlarge):

On the left hand side you can see a column in which you can find all the files that are currently in the new project. Two folders, the first containing a file named ‘main.cpp’ and a second which is a ‘man’ file. (i.e. a manual for our command line tool; you can leave it there, delete it or change it, it’s not important in the context of this tutorial)
The second folder contains our ‘products’, references to our command line tool, once compiling is succesfull.

What we want to do next is add the GLUT and OpenGL framework to our project. Simply right-click the project icon and select ‘Add Files to…’

Navigate to *YourHD*/System/Library/Frameworks, select GLUT.framework. Scroll further down and command-click OpenGL.framework.
In the dialogue you have the option to select which targets to add the framework to, but as we only have one target it is automatically selected for us.
Click ‘Add’. This should add both frameworks to your project, the file tree of which should now look something like this:

Now before we dive into the code, there’s one thing I like to do; by default Xcode will store the built program in a ‘unique subfolder of Xcode’s Derived Data Location’. I prefer to have the file somewhere I can easily get to. If you are also one of these people who rather has things where you decide you can change this:

In the XCode menu, select preferences. In the preferences go to the ‘Locations’ tab. Click the ‘Advanced’ button underneath ‘Derived Data’ and change the build location to ‘Legacy’. This will use the folder where you selected to save your ‘product’ to also place the compiled program.

Next thing we want to do is of course add code!
Select the main.cpp file, it should contain a block of comments at the top (starting with //) containing the filename, your name and creation date, your company name. You can leave all that there, as they are comments they don’t have any influence on our final program anyway.

Next you will see code for a simple ‘Hello World’ program. If you click the run arrow in the left-top of the window you will see what the program does in the output window in the bottom.

Great stuff! Now delete all that and paste the following code:
There are a lot of comments in the code explaining what everything does. You can leave it in or strip it out, either way it will not affect your final program.


#include 
//C Standard General Utilities Library, more info here: http://www.cplusplus.com/reference/clibrary/cstdlib/
#include 
//GLUT library, take note that when trying out examples you find on the web you might need to change this as on windows and linux
//this is called 

//I'm declaring my function prototypes here, otherwise the compiler will complain we use an 'Undeclared Identifier' in the main function.
//Another way to solve this is to have your display, reshape, idle,etc...functions before the main function. I just prefer it this way.
//We will use the boolean fullScreen to toggle between fullscreen and windowed mode.
void display();
void reshape(int width, int height);
void idle();
void keyb(unsigned char key, int x, int y);
bool fullScreen = false;

//Main Loop
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
    //argc is the number of variables passed to our program, argv is a pointer to those arguments,
    //they are passed to glut here. There's no need to know more about this right now.
    
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    /*As the name says, this line initializes the display mode.
     -GLUT_RGBA states we want color and alpha buffers for our window
     -GLUT_DOUBLE means we want a Double Buffer, this means that images are completely drawn before they are sent
     to the screen. This will prevent flickering images.
     -GLUT_DEPTH means that we want a Depth Buffer. This will allow for the images to be drawn correctly with respect
     to their distance: i.e. objects closeby will overlap objects further away.*/
    
	glutInitWindowSize(640, 480);
    //Define the size we would like the window to be
    
	glutCreateWindow("window_name");
    //This command creates the window with the specifications we've set before and gives it a name. 
    
	glutDisplayFunc(display);
    //this function is called whenever GLUT decides it's time to redraw the contents of the current window, it's also
    //the function where we'll do our drawing.
    glutReshapeFunc(reshape);
    //this function is called whenever the user changes the size of the frame
    glutIdleFunc(idle);
    //this function is called whenever glut is not doing anything
    glutKeyboardFunc(keyb);
    //this function is called when a key is pressed
	glutMainLoop();
    //glutMainLoop() goes on forever and ever until you quit the program.
}

//Idle Function
void idle(void)
{
}

//Reshape Function
void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.,(GLfloat) width/(GLfloat) height,0.1, 800.0);
	gluLookAt(0.0,0.0,5.0, 0.0, 0.0, 0.0, 0,1,0);
    glMatrixMode(GL_MODELVIEW);
	
}
//Display Function, this function simply clears the color and depth buffers and then swaps buffers (remember the double buffered
//display mode we set)
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutWireSphere(0.5,100,100);//draw a wireframe sphere.
    glutSwapBuffers();
}

//Keyboard Function
//When pressing the 'esc' key, the boolean changes from false to true or from true to false. The if statement
//checks what the value of fullScreen is, and changes to fullscreen-mode if true and back to windowed mode if false.
void keyb(unsigned char key, int x, int y){
	switch (key){
		case 27:
            fullScreen = !fullScreen;
            if (fullScreen) {
                glutFullScreen();
            }
            else {
                glutReshapeWindow(640, 480);
                glutPositionWindow(100, 100);
            }
            break;
	}
}

Hopefully this is all more or less clear. Now if you want to create more programs, or find sources online and want to try those out, you do not necessarily need to start a new project. It simply suffices to create a new target within the existing project.

Go to the file menu and select new > target.

Once again you will be presented this window:

Select the command line tool again, give it a name, etc. Like we did in the beginning. A new folder will appear in the file tree on the left containing a new main.c file and a new manual file. In the ‘Products’ folder, a new target will have appeared in red, meaning we have not yet successfully compiled it.

In order to run this new program we need to make sure it can make use of the OpenGL and GLUT frameworks we’ve added before. To do so, select both the frameworks. On the right hand side of Xcode, a window named ‘Target Membership’ will be shown.

Check the box next to your newly created target.

Now that you have several targets in your project, you need to tell Xcode which one you want to compile. Next to the play and stop buttons in the top left corner you will see the name of your current active target. Click it to select which target you wish to build.

Et voila! It’s that simple. Now you can scavenge the internet for opengl/glut code and paste it in you project, or make a new target and try it out.

Try it out with this code to make a Harmonograph!

You can start your compiled programs by double-clicking them or by browsing to their containing folder in the terminal and type ./nameofyourprogram