Starting OpenGL on Mac

I am starting to learn OpenGL and trying a simple hello world program, and I am getting errors.

My system has OS 10.3, and I am working with CodeWorrior IDE4.04 under OS 9 on.

Following is the program I am trying (from http://nehe.gamedev.net/ ) When I Run the program it asks for arguments and if I hit OK without typing in anything it ends with error message.

//
// This code was created by Jeff Molofee '99
//
// If you’ve found this code useful, please let me know.
//
// Visit me at www.demonews.com/hosted/nehe
//
/**************************************************************/
// This code was ported to MacOS by Tony Parker.
// I’d also appreciate it if you could drop me a line if you found
// this code useful.
//
// Tony Parker - asp@usc.edu
//
// Have a nice day.

#include <stdio.h> // Header File For Standard Input / Output
#include <stdarg.h> // Header File For Variable Argument Routines
#include <string.h> // Header File For String Management
#include <stdlib.h>
#include <gl.h> // Header File For The OpenGL32 Library
#include <glu.h> // Header File For The GLu32 Library
#include <glut.h> // Header File For The GLUT Library
#include <math.h>

// Constants -----------------------------------------------------------------

#define kWindowWidth 400
#define kWindowHeight 300

// Function Prototypes -------------------------------------------------------

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

// Main ----------------------------------------------------------------------

int main(int argc, char** argv)
{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (kWindowWidth, kWindowHeight); 
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);

InitGL();

glutDisplayFunc(DrawGLScene); 
glutReshapeFunc(ReSizeGLScene); 

glutMainLoop();

return 0;

}

// Init ----------------------------------------------------------------------

GLvoid InitGL(GLvoid)
{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		// This Will Clear The Background Color To Black
glClearDepth(1.0);							// Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS);						// The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST);					// Enables Depth Testing
glShadeModel(GL_SMOOTH);					// Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity();							// Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)kWindowWidth/(GLfloat)kWindowHeight,0.1f,100.0f);	// Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);

}

// DrawGLScene ---------------------------------------------------------------

GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

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

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

glTranslatef(3.0f,0.0f,0.0f);

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();

glFlush();

}

// ReSizeGLScene ------------------------------------------------------------

GLvoid ReSizeGLScene(int Width, int Height)
{
glViewport (0, 0, (GLsizei) Width, (GLsizei) Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0, (GLfloat) Width / (GLfloat) Height, 0.1, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

The glut_error_log file says

GLUT: Fatal Error in a.out: window could not be created.

I suggest you use Xcode and the Cocoa ports of the NeHe tutorials. Xcode is free, doesn’t require Classic, and comes with the OS.

What does glutCreateWindow need?
why is argv[0] being passed in?
Need to find that out.

glutCreateWindow takes a const char* which is used as the window title. argv[0] is being passed in the hopes that the window title will be the same as the application name.

I can’t see why that wouldn’t work, but it might be worth trying a constant string.

I am running OSX10.1 on my PB and this code works fine, once I changed the include files to:

#include <OPENGL/gl.h>
#include <OPENGL/glu.h>
#include <GLUT/glut.h>

And add the GLUT and OPENGL frameworks to a cocoa project.

Hope this helps

BennUK

Technically, it’s OpenGL.framework. OPENGL won’t work if you’re on a case-sensitive file system.

Hello!

First, you mus change the path in the statement #include

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

Now, for compile your program, in the terminal aplication with:

cc -Wall -o exe_name your_program_file.c -lobjc \
-framework OpenGL -framework GLUT

I hope you can compile your programs

Regards.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.