Archive for August, 2008

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

The other night I saw Transformers with the roomies. It was that or Lord of War, and having never seen Transformers, I thought I would give it a shot. For those of you who have no idea what the movie is about, it basically tells the story of two futuristic robots who have an epic battle on earth, in which the final outcome rests in the hands (quite literally) of some random kid.

As far as the making of the movie goes, the visual effects stole the show. There were only a small handful of shots where you could definitively tell there was CGI (except for the fact that these robots obviously don’t exist… yet). Now, as far as the writing goes… wow, I was stunned. I didn’t know that Transformers was supposed to be a comedy, and apparently it was because I spent half the movie laughing. I kind of felt like someone went to the Disney lot, pulled the good looking teenage stars, gave them something a four year old wrote as their first screenplay, and then started shooting rubber bullets at them telling them to read the script as it happened. As you can tell, that kind of ruined the movie for me as far as something I would want to own or see again (except for the comedic value of course).

But given better actors and writing, I think the film would have a lot more potential. I like Michael Bay’s work, but I think his writer and casting director could have done a much better job.

Kevin’s movie rating:

Good, but only time I am seeing it

Movie Rating Key:

  1. Best movie I have ever seen
  2. Absolutely incredible
  3. Pretty darn good
  4. I would watch it again
  5. Good, but only time I am seeing it
  6. Disappointing
  7. Not very good
  8. Horrendous
  9. Shouldn’t have been made

Image courtesy of spectrasonics.net

Review: Apple Time Capsule

I usually do not do reviews of hardware, but in this case, I feel the urge to.

For my apartment, I purchased the Apple Time Capsule, which is an Airport Extreme Base Station (wireless router) with a 500 GB for backing up. So, let’s start with the wireless router. Setting it all up was a cinch. I plugged it in, and all my computers were on. One of the great things about this router is that it broadcasts at 802.11n, so all of you with 802.11n cards in your laptop would love the transfer speed we get. Also, the back of the router has 3 additional Ethernet jacks, so you can run any other computers and what not off Ethernet. And it works like a charm.

The second main feature I love about the Time Capsule is the hard drive. Not only is it a shared hard drive (so anyone I give access to can see it and put files on it), but I can use it as my Time Machine volume (a back up disk that is). I can do wireless backup seamlessly whenever I am in the apartment. That is terrific considering the drive doesn’t have to be hooked up to my machine, etc. And because the router is 802.11n, it’s pretty darn fast too.

And finally, the Time Capsule also has a USB port, so you can plug in a printer or extra hard drive, or both (given you plug in a hub). Setting up the printer to work with my Macbook Pro was literally as easy as plugging in the printer.

I can be sitting in my living room, surfing the net, printing out pictures from a weekend adventure, and backing up my computer, all at the same time, wirelessly. And given the fact the cost is pretty good and the device looks pretty, I would say that is a tremendous deal.

If you’re thinking of getting a wireless router, get the Time Capsule.