Author Archive for Kev

OpenGL on the Mac (Part 1): Using GLUT

Today in my computer graphics class, my professor was talking about development environments for writing with OpenGL, a graphics framework. He mentioned Linux and Windows, but unfortunately left out the Mac. The CS department has dedicated an entire homework to just being to compile some simple OpenGL, which what I learned from lecture today, is a lot more involved on Windows than on the Mac. So for those who are looking for some insight on how to do this on the Mac, this is the post for you.

I will do two posts on OpenGL environments on the Mac, one for the two most common windowing systems: GLUT and Cocoa. Here’s when you would use each:

  • GLUT: Use the OpenGL Utility Toolkit if you need code that is cross platform. For example, if you are developing on the Mac, but the final code will run on Linux, Windows, or the Mac. I will cover GLUT in this post.
  • Cocoa: If your code will only run on the Mac, then Cocoa is definitely the windowing system you will want to be using. I’ll cover how to do this in another post.

The tools I will be using in this post are Apple’s Developer Tools, and I highly highly highly recommend that for all software development you are doing on the Mac, you use these tools. If you don’t already have them, visit Apple’s Developer site to sign up for a free online ADC membership, and then download the latest Xcode tools. You always install the tools off the disk that came with your Mac, though it’s best to get the latest ones from the site listed above.

So let’s get started: Creating our project

I’m just going to go through this step by step, I hope you don’t mind.

  1. Get the Xcode tools from Apple’s developer site.
  2. Open Xcode.
  3. Create a new project by going under File -> New Project or just hit Command-Shift-N.
  4. From the template chooser that appears, in the sidebar on the left, choose “Command Line Utility”, and then from the area on the right, choose “Standard Tool” and then click the “Choose” button. Save your project where you so please.
    Template chooser
  5. At this point, the project window will come up. This contains everything you need to develop your application. For more information on how to use Xcode, please go here. That link will give you a quick start to using Xcode, since that is not the main focus of this post.
    The project window

Writing our code

We have a project, now we need to write the code.

  1. In the files list, select main.c. The file will appear in the editor below.
  2. In the editor, paste the below code:
    #ifdef __APPLE__
        #include <GLUT/glut.h>
    #else
        #include <GL/glut.h>
    #endif
    
    /*
     *  reshape(w,h) called when window changes size
     *  called with width w and height h of new window size
     */
    void reshape(int w, int h) {
        glViewport(0, 0, w, h);         /* Establish viewing area to cover entire window. */
    }
    
    /*
     *  display() called when window contents need to be refreshed
     */
    void display(void) {
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
    
        glBegin(GL_TRIANGLES);
            glColor3f(1.0, 0.0, 0.0);   /* red */
            glVertex2f(-1.0, -1.0);     /* lower left corner */
            glColor3f(0.0, 1.0, 0.0);   /* green */
            glVertex2f(-1.0, 1.0);      /* upper left corner */
            glColor3f(0.0, 0.0, 1.0);   /* blue */
            glVertex2f(1.0, -1.0);      /* lower right corner */
        glEnd();
        glFlush();                      /* OpenGL is pipelined, and sometimes waits
                                           for a full buffer to execute */
    }
    
    int main(int argc, char **argv) {
        glutInit(&argc, argv);          /* setup GLUT */
        glutCreateWindow(argv[0]);      /* open a window */
        glutDisplayFunc(display);       /* tell GLUT how to fill window */
        glutReshapeFunc(reshape);       /* update shape of window */
        glutMainLoop();                 /* let glut manage i/o processing */
        return 0;                       /* ANSI C requires main to return int */
    }
    

The code here is the code you could use on any platform, but is specific to the GLUT windowing system (note the functions called from main). The first few lines of code are used for portability. On non-Apple systems, the GLUT header is located in the OpenGL framework, unlike on Apple systems, where it’s in a separate framework. I’ll get to this next.

Linking against OpenGL and GLUT

Now that we have code, we have to make sure all the pieces are going to be there when we compile and run our application. To accomplish this, we need to link against the OpenGL and GLUT frameworks (a special kind of dylib).

  1. In the sidebar on the left of our project window, right click on the project icon at the top of the bar. This will bring up a contextual menu. From there, select Add -> Existing Frameworks.
    Add Existing Frameworks
  2. An open panel will come up. Navigate to the Frameworks folder which is located in the Library folder in the System folder at the root of your hard drive.
    Navigate to Frameworks folder in /System/Library/Frameworks
  3. Once inside the Frameworks folder, select the GLUT.framework and click the Add button.
  4. At this point, another sheet will come down. Make sure that the checkbox next to the name of your project (actually your target, but ignore that for now) is checked, under the “Add To Targets” label. Then click Add.
    Add to your target
  5. Repeat the above steps for the OpenGL.framework.

Running our app

Now we are linked against the GLUT and OpenGL libraries, we will be able to run our executable. In Xcode, there are targets and there are executables. You can think of a target as the set of rules and commands that produce an executable. In most cases, you will have one executable (or other product like a framework) for each target. When you build in Xcode, you build a target. And when you run in Xcode, you are running the target’s associated executable.

  1. To build and run our executable, from the Build menu, just select “Build and Run”.

Our project will build, and our executable will be launched, and we will have a dandy app to show for it. And that’s it!
The final result

Conclusion

To review, to get an OpenGL app running on the Mac, it’s really quite easy.

  1. Create a new Xcode project (you don’t just have to make C command line utilities, you could do it in C++, Python, whatever).
  2. Write your OpenGL code.
  3. Build and Run!

Movie Review: Transformers

Review: Apple Time Capsule

An Astrophysicist’s Jackpot

Movie Review: Wall•e

The Apollo Simulation Project begins

New pictures up

Officially a nerd: I edited Wikipedia

I’m going to build it

Back in the Bay