OpenGL + GLUT + MAC OS X

Via: nehe.gamedev.net/data/lessons/lesson.asp 

 

Lesson: Mac OS X
So you’ve been wanting to setup OpenGL on Mac OS X? Here’s the place to learn what you need and how you need to do it. This is a direct port from the Mac OS ports, so if something seems familiar, thats why ;) 

What You’ll Need: 

You will need a compiler. Two compilers are currently available, Apple’s "Project Builder" and Metrowerks CodeWarrior. Project Builder is being made free in Mid-October(2000), so this tutorial will demonstrate how to make a GLUT project in Project Builder. 

Getting Started with Project Builder: 

This bit is easy. Just choose "File->New Project" and select a "Cocoa Application." Now choose the name of your project, and your project IDE will pop up. 

Now goto the "Project" Menu and "Add Framework…" to add the GLUT.framework. In 10.1 you need to add the OpenGL.framework as well, so do this now. 

Getting Started with GLUT: 

Remove the default code by one of two ways: 

Delete the main.m file that comes with the default project, and insert a new main.c with the GLUT code or… Select all the code in main.m and replace it with the GLUT code. 

You need 3 headers to start with:

#include <OpenGL/gl.h>		// Header File For The OpenGL32 Library
#include <OpenGL/glu.h>		// Header File For The GLu32 Library
#include <GLUT/glut.h>		// Header File For The GLut Library
The first is the standard OpenGL calls, the other three provide additional calls which we will use in our programs. 

Next, we define some constants:

#define kWindowWidth	400
#define kWindowHeight	300
We use these for the height and width of our window. Next, the function prototypes:
GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);
… and the main() function:
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;
}
glutInit(), glutInitDisplayMode(), glutInitWindowSize(), glutInitWindowPosition(), and glutCreateWindow() all set up our OpenGL program. InitGL() does the same thing in the Mac program as in the Windows program. glutDisplayFunc(DrawGLScene) tells GLUT that we want the DrawGLScene function to be used when we want to draw the scene. glutReshapeFunc(ReSizeGLScene) tells GLUT that we want the ReSizeGLScene function to be used if the window is resized. 

Later, we will use glutKeyboardFunc(), which tells GLUT which function we want to use when a key is pressed, and glutIdleFunc() which tells GLUT which function it will call repeatedly (we’ll use it to spin stuff in space).

Finally, glutMainLoop() starts the program. Once this is called, it will only return to the main() function when the program is quitting. 

All Done! 

Notice the only real difference here is that we are changing the headers. Pretty simple! 

In later tutorials there will be some bigger differences, but for now its just as simple as changing the headers and adding the framework. 

Have fun! 

R.Goff (unreality@mac.com)

No related posts.


About this entry