OpenGL on OSX with GLUT and G++

There’s no doubt that programming with an IDE saves you loads of time with convenience, and ease-of-use. As programmers, we don’t always have the luxury of having a program do all the nasty bits for us, so it’s always good to learn how to program in a text editor (Emacs is a great choice) and how to compile from the command line.

Personally, I like programming in an environment that doesn’t require a lot of overhead, gives me a large amount of control, and lets me see all the little details. I’m going to give a startup tutorial for programming OpenGL with GLUT on OSX (Version 10.5.7), and I’ll be using nothing but GNU Emacs, and g++ (which is an optional install on your OSX Install DVD). More after the jump.

Getting Started…

Here are three easy steps to get yourself all set up:

  1. Get Emacs and install it.
  2. Find your OSX Install DVD and fire it up. Look for “Optional Installs” then go to “Xcode tools” and finally double-click on “XcodeTools.mpkg” to install it. (This will install the g++ compiler)
  3. Open up a terminal window and type “g++ -v” If you don’t get a “g++ not found” error, you’re good to go.

Writing some OpenGL code!

Now for the fun stuff. Fire up Emacs, and type/copy-paste the following in:

#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#endif

#define kWindowWidth 400
#define kWindowHeight 300

GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);

int main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize (kWindowWidth, kWindowHeight);
 glutInitWindowPosition (100, 100);
 glutCreateWindow (argv[0]);

 InitGL();

 glutDisplayFunc(DrawGLScene);
 glutReshapeFunc(ReSizeGLScene);

 glutMainLoop();

 return 0;
}

GLvoid ReSizeGLScene(int width, int height)
{
 if (height==0)
 {
 height=1;
 }

 glViewport(0, 0, width, height);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}

GLvoid InitGL(GLvoid)
{
 glShadeModel(GL_SMOOTH);
 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 glClearDepth(1.0f);
 glEnable(GL_DEPTH_TEST);
 glDepthFunc(GL_LEQUAL);
 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

GLvoid DrawGLScene(GLvoid)
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glLoadIdentity();

 glTranslatef(-1.5f, 0.0f, -6.0f);
 glColor3f(1.0, 0.0, 0.0);

 glBegin(GL_TRIANGLES);
 glVertex3f( 0.0f, 1.0f, 0.0f);
 glVertex3f(-1.0f,-1.0f, 0.0f);
 glVertex3f( 1.0f,-1.0f, 0.0f);
 glEnd();

 glTranslatef(1.0, 1.0, 1.0);
 glColor3f(0.0, 1.0, 0.0);

 glBegin(GL_QUADS);
 glVertex3f(-1.0f, 1.0f, 0.0f);
 glVertex3f( 1.0f, 1.0f, 0.0f);
 glVertex3f( 1.0f,-1.0f, 0.0f);
 glVertex3f(-1.0f,-1.0f, 0.0f);
 glEnd();

 glutSwapBuffers();
}

To actually compile the code from the command line, you’ll need to do the following:

g++ GlutTest.cpp -framework OpenGL -framework GLUT

Note that this is the g++ line within OSX. This is because OSX uses frameworks to include libraries and headers, This means that instead of using a bunch of command line options like “-lopengl32 -lGLUT -lglu32”, you only need to tell the compiler what framework you want to use, and it will include all the required files on its own.

More about GLUT/the above source code:

If you want to know more about the source code above, or maybe just GLUT in general, I’d recommend checking out the tutorials at Lighthouse 3D. Not much will be different except for the headers you include at the top.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s