Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
Ok...

I'm reading Open GL Programming Guide...

this is going to sound stupid now... but... how do I do Example 1-2? What I've done so far...

started with the Cocoa application template in XCode (is that the wrong template?) and then added the OpenGL and GLUT frameworks. At this point... I've got no idea what to do. It doesn't seem to be written in Objective-C which is what I've gotten used too... instead it appears to all be C which I haven't used for a while (and was told that I should never have to use again since Objective-C/Cocoa provide higher level commands that can do it all.) What do I do with the XIB (known as NIBs in previous versions of Mac OS X?) Should I just stick all the code in a new object? Should the object be based off an NSObject or an NSView or what?

Sorry that I'm so confused... hopefully once I cover this gap of going from the examples in the book to working examples in Cocoa (or whatever API I end up having to use,) once I won't have to do it again for the rest of the book...
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
started with the Cocoa application template in XCode (is that the wrong template?)

Nope, that's right.

instead it appears to all be C which I haven't used for a while (and was told that I should never have to use again since Objective-C/Cocoa provide higher level commands that can do it all.)

Objective-C is a superset of C, meaning everything you can do in C you can do in Objective-C. So you will use C, whether you know it or not ;)

What do I do with the XIB (known as NIBs in previous versions of Mac OS X?)

XIB is the newer XML-based version of the old NIB file format.

Should I just stick all the code in a new object? Should the object be based off an NSObject or an NSView or what?

You could, and probably will if you want to write a Cocoa-based application, but since OpenGL/GLUT are C-based APIs you don't need Cocoa objects.

So to get started just edit the main.m file (under the Other Sources group in Xcode).

Also check out this tutorial (and the others) which might help:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Mac_OS_X
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

To start a new GLUT project, select either a C or C++ command line project, and add the OpenGL and GLUT frameworks. There will be no NIBs or XIBs etc in the project and the demo code from the book will work. The only difference will be the #includes. For the Mac you should use,

Code:
#include <GLUT/glut.h>

and perhaps

Code:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

b e n
 

psingh01

macrumors 68000
Apr 19, 2004
1,590
633
Here is a template you can use. Whenever I wanted to do a quick and dirty test I would just use this template and start right away with OpenGL calls in the display method. Like the previous poster said, you don't need windows and nibs/xibs. Just a command line app is all you need with the frameworks linked.

Note you might have to edit the #include line as I think I used this with windows and the mac header might be different.

Code:
/*
Simple template for a GLUT/OpenGL 3D application

This requires the GLUT library.
*/

#include <GL/glut.h>

// window height & width
float W = 500.0f;
float H = 500.0f;

// this function handles events for special keys such as the arrow keys or function keys.
void special(int key, int x, int y) {
	switch(key) {
	case GLUT_KEY_UP:
		// do something
		break;
	case GLUT_KEY_DOWN:
		// do something
		break;
	case GLUT_KEY_LEFT:
		// do something
		break;
	case GLUT_KEY_RIGHT:
		// do something
		break;
	default:
		break;
	}
}

// this function handles events for regular keys
void keyboard(unsigned char key, int x, int y) {
	if(key == 27) exit(0); // exit program when escape key is pressed
//	if(key == '+') ;	   // do something when the + key is pressed
}

// reshape
void reshape(int w, int h) {
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	W = w; 
	H = h;
}

// use this to initialize some opengl states
void init() {
	glClearColor(1.0,1.0,1.0,0.0); // set the clear color to white
	glColor3f(1.0,0.0,0.0); // draw in color red
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
	glEnable(GL_DEPTH_TEST);
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	
	gluPerspective(60.0, // angle
				   W/H,  // aspect ratio
				   1,	 // near plane distance
				   2000);// far plane distance

	gluLookAt(0, 0, 200,  // eye position
			  0, 0, -100, // target position
			  0, 1, 0);	  // up vector

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}


// this function does the actual drawing
void display() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// clears the screen

	
	/*
		Your OpenGL commands go here
	*/


	glFlush(); 	// forces all opengl commands to execute
	
	glutSwapBuffers(); // need this when in double buffered mode. (see main)

//	glutPostRedisplay(); // call display function again
}


int main( int argc, char *argv[] )
{
	// let glut eat any command line args that it owns
	glutInit( &argc, argv );
	// full color, double buffered
	glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
	glutInitWindowSize(W,H);
	glutInitWindowPosition(0,0);
	glutCreateWindow( "Simple example" );
	
	// setup remaining state
	init();
	
	// set call back functions (there are more callback functions, be sure to look at GLUT documentation for the others)
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutSpecialFunc(special);
	glutKeyboardFunc(keyboard);
	
	// start processing events
	glutMainLoop();

	return 0;
}



This one is slightly different, but setup to do 2D graphics (in opengl everything is 3D, but if you always look at it from one side then it's 2D ;))


Code:
/*
Simple template for a GLUT/OpenGL 2D application

This requires the GLUT library.
*/

#include <GL/glut.h>

// this function handles events for special keys such as the arrow keys or function keys.
void special(int key, int x, int y) {
	switch(key) {
	case GLUT_KEY_UP:
		// do something
		break;
	case GLUT_KEY_DOWN:
		// do something
		break;
	case GLUT_KEY_LEFT:
		// do something
		break;
	case GLUT_KEY_RIGHT:
		// do something
		break;
	default:
		break;
	}
}

// this function handles events for regular keys
void keyboard(unsigned char key, int x, int y) {
	if(key == 27) exit(0); // exit program when escape key is pressed
}

// use this to initialize some opengl states
void init() {
	glClearColor(1.0,1.0,1.0,0.0); // set the clear color to white
	glColor3f(1.0,0.0,0.0); // draw in color red
	
	glPolygonMode(GL_FRONT, GL_LINE); 
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();

	// your vertices must be within these coordinates to appear in your window.
	gluOrtho2D( -100.0, 100.0, -100.0, 100.0 ); 
	
	// back to the model matrix
	glMatrixMode( GL_MODELVIEW );
}


// this function does the actual drawing
void display() {
	glClear(GL_COLOR_BUFFER_BIT);	// clears the screen

	
	/*
		Your OpenGL commands go here
	*/


	glFlush(); 	// forces all opengl commands to execute
	
	glutSwapBuffers(); // need this when in double buffered mode. (see main)
}


int main( int argc, char *argv[] )
{
	// let glut eat any command line args that it owns
	glutInit( &argc, argv );
	// full color, double buffered
	glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);
	glutCreateWindow( "Simple example" );
	
	// setup remaining state
	init();
	
	// set call back functions (there are more callback functions, be sure to look at GLUT documentation for the others)
	glutDisplayFunc(display);
	glutSpecialFunc(special);
	glutKeyboardFunc(keyboard);
	
	// start processing events
	glutMainLoop();

	return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.