GLUT, OpenGL, and MacOSX...

Started by
4 comments, last by choffstein 18 years, 11 months ago
Well, ive finally tried to compile my first GLUT/OpenGL program on my new MacOSX laptop...and it actually compiles. Wierd, right? Well, thats pretty awesome...until it crashes.

/*
When creating your project, uncheck OWL,
uncheck Class Library, select Static
instead of Dynamic and change the target
model to Console from GUI.
Also link glut.lib to your project once its done.
*/

#include <iostream>
#include <GLUT/glut.h>   // The GL Utility Toolkit (Glut) Header
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>

using namespace std;

void init ( GLvoid )     // Create Some Everyday Functions
{
/*

   THE PROGRAM IS CRASHING HERE


*/
  glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
   glEnable ( GL_COLOR_MATERIAL );
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display ( void )   // Create The Display Function
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix
	glTranslatef(-1.5f,0.0f,-6.0f);						// Move Left 1.5 Units And Into The Screen 6.0
	glBegin(GL_TRIANGLES);								// Drawing Using Triangles
		glColor3f(1.0f,0.0f,0.0f);						// Set The Color To Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top
		glColor3f(0.0f,1.0f,0.0f);						// Set The Color To Green
		glVertex3f(-1.0f,-1.0f, 0.0f);					// Bottom Left
		glColor3f(0.0f,0.0f,1.0f);						// Set The Color To Blue
		glVertex3f( 1.0f,-1.0f, 0.0f);					// Bottom Right
	glEnd();											// Finished Drawing The Triangle
	glTranslatef(3.0f,0.0f,0.0f);						// Move Right 3 Units
	glColor3f(0.5f,0.5f,1.0f);							// Set The Color To Blue One Time Only
	glBegin(GL_QUADS);									// Draw A Quad
		glVertex3f(-1.0f, 1.0f, 0.0f);					// Top Left
		glVertex3f( 1.0f, 1.0f, 0.0f);					// Top Right
		glVertex3f( 1.0f,-1.0f, 0.0f);					// Bottom Right
		glVertex3f(-1.0f,-1.0f, 0.0f);					// Bottom Left
	glEnd();											// Done Drawing The Quad


  glutSwapBuffers ( );
  // Swap The Buffers To Not Be Left With A Clear Screen
}

void reshape ( int w, int h )   // Create The Reshape Function (the viewport)
{
  glViewport     ( 0, 0, w, h );
  glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix
  glLoadIdentity ( );                // Reset The Projection Matrix
  if ( h==0 )  // Calculate The Aspect Ratio Of The Window
     gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
  else
     gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
  glMatrixMode   ( GL_MODELVIEW );  // Select The Model View Matrix
  glLoadIdentity ( );    // Reset The Model View Matrix
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
  switch ( key ) {
    case 27:        // When Escape Is Pressed...
      //exit ( 0 );   // Exit The Program
      break;        // Ready For Next Case
    default:        // Now Wrap It Up
      break;
  }
}

void arrow_keys ( int a_keys, int x, int y )  // Create Special Function (required for arrow keys)
{
  switch ( a_keys ) {
    case GLUT_KEY_UP:     // When Up Arrow Is Pressed...
      glutFullScreen ( ); // Go Into Full Screen Mode
      break;
    case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed...
      glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window
      break;
    default:
      break;
  }
}

int main ( int argc, char** argv )   // Create Main Function For Bringing It All Together
{
  glutInit( &argc, argv ); // Erm Just Write It =)
  init();
  glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
  glutInitWindowSize  ( 500, 500 ); // If glutFullScreen wasn't called this is the window size
  glutCreateWindow    ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title)
  glutFullScreen();          // Put Into Full Screen
  glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
  glutReshapeFunc     ( reshape );
  //glutKeyboardFunc    ( keyboard );
  //glutSpecialFunc     ( arrow_keys );
  glutMainLoop();          // Initialize The Main Loop
  return 1;
}

I link GLUT.framework and OpenGL.framework, and the program starts fine. That is, until the init() method is called. The program crashes on glShadeModel(). When I comment it out, it just crashes on the next call. Any thoughts? Thanks.
Advertisement
Did you include the cococa framework? You need to.
Herm. Well, adding the Cocoa framework on its own doesn't work, but what I realized was that if I commented out "init()" it didn't crash, but actually worked. So I placed init() under "glutCreateWindow()" and everything seems to work fine.

Anyone know why?

It also works with Cocoa removed, so I dont think that was it. Thanks anyway, though.
Because you have to setup OpenGL completely before you can make calls to it or it will crash. Its been awhile since I coded on a Mac, but have done quite a bit, sold my Mac for now... Anyway I remember one having to call Cocoa also with GLUT but that may have changed since then...
Quote:Original post by visage
Herm. Well, adding the Cocoa framework on its own doesn't work, but what I realized was that if I commented out "init()" it didn't crash, but actually worked. So I placed init() under "glutCreateWindow()" and everything seems to work fine.

Anyone know why?


Most likely because you were using functions that affect the current rendering context without a rendering context :-). Same applies with registering callbacks - you can set a different callback for different windows, e.g.:

glutCreateWindow( "Window 1" )
glutDisplayFunc( display_window_1 );
glutReshapeFunc( reshape_window_1 );
glutCreateWindow( "Window 2" )
glutDisplayFunc( display_window_2 );
glutReshapeFunc( display_window_2 );

Same applies shading modes, depth modes, what's enabled and what's not, etc...

So, if there's no current context to set, it crashes. To convert this to C++ code psuedo-representing what's happening:

Window * window;
//from init() before glutCreateWindow():
window->setGlShadeModel( GL_SMOOTH );
...

window = glutGreateWindow( "Window 1" );
window->setGlutDisplayFunc( display_window_1 );
...

Really, "window" is actually most likely multiple global variables that depend on the implementation (e.g. for windows this would be things like a global HDC and HRC, dunno about mac)
Ah, that makes sense. I figured that the OpenGL variables represented in the init() section were removed from GLUT -- but now I see that GLUT is more of an ...extension, if you will.

I think I am just going to encapsulate this all into an object. I have grown to dislike C style code.

Thanks for the help.

This topic is closed to new replies.

Advertisement