Mac OS X Glut build instructions(在Mac上用glut库编写OpenGL程序)
Wentao Sun, Autodesk, Inc.
1. Building GLUT apps under Mac OS X
There are only a few modifications you need to make to the
robot.c sample to get it compiled on your Mac. These
instructions were tested under OS X 10.3 (Panther) with XCode 1.1.
XCode comes with everything you need to build GL apps, including
stable versions of OpenGL, GLUT, gcc and all the related make tools.
XCode by default places the include files for GLUT in a different
location than MESA, so to build robot.c you need to change line
46 from
#include <GL/glut.h>
to
#include <GLUT/glut.h>
After making this change you are ready to build from the command
line:
gcc -framework GLUT -framework OpenGL -framework Cocoa
robot.c -o robot
Note the use of the -framework option to make use
of OS X's packaged libraries. The Cocoa framework provides the OS
services required by GLUT, such as interfacing with the window
manager (Aqua).
I will post a sample makefile here which can be used to compile your files under OS X or Linux as necessary without modification; as soon as my Sutherland account is activated.
There are only a few modifications you need to make to the
robot.c sample to get it compiled on your Mac. These
instructions were tested under OS X 10.3 (Panther) with XCode 1.1.
XCode comes with everything you need to build GL apps, including
stable versions of OpenGL, GLUT, gcc and all the related make tools.
XCode by default places the include files for GLUT in a different
location than MESA, so to build robot.c you need to change line
46 from
#include <GL/glut.h>
to
#include <GLUT/glut.h>
After making this change you are ready to build from the command
line:
gcc -framework GLUT -framework OpenGL -framework Cocoa
robot.c -o robot
Note the use of the -framework option to make use
of OS X's packaged libraries. The Cocoa framework provides the OS
services required by GLUT, such as interfacing with the window
manager (Aqua).
I will post a sample makefile here which can be used to compile your files under OS X or Linux as necessary without modification; as soon as my Sutherland account is activated.
Source code:
#include <iostream>
using namespace std;
#include <math.h>
float myAngle;
float myTime;
//codefragmentbegin,headerInclude
#include <GLUT/glut.h>
//codefragmentend,headerInclude
//codefragmentbegin,app
void prepareOpenGL()
{
myAngle = 0;
myTime = 0;
}
void draw()
{
glClearColor( 0, .5, .8, 1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glRotatef( myAngle, 0, 0, 1 );
glTranslatef( 0, 0, 1 );
glColor3f( 0, 1, 0 );
glBegin( GL_QUADS );
float ww = .9;
float hh = .9;
glTexCoord2f( 0, 0 );
glVertex3f( -ww, -hh, 0 );
glTexCoord2f( 1, 0 );
glVertex3f( ww, -hh, 0 );
glTexCoord2f( 1, 1 );
glVertex3f( ww, hh, 0 );
glTexCoord2f( 0, 1 );
glVertex3f( -ww, hh, 0 );
glEnd();
glutSwapBuffers();
}
void angleUpdate( int delay )
{
float twopi = 2 * M_PI;
myTime = ( myTime > twopi ) ? 0 : myTime + .03;
myAngle = sinf( twopi * myTime );
glutTimerFunc( delay, angleUpdate, delay );
glutPostRedisplay();
}
int main( int argc,
char * argv[] )
{
glutInit( &argc, argv );
// choose a visual and create a window
glutInitDisplayString( "stencil>=2 rgb~8 double depth>=16 samples" );
// glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 450, 300 );
glutCreateWindow( "GLUT Configuration Example" );
// initalize our opengl (context is now valid)
prepareOpenGL();
// register callback functions
int delay = 50;
glutTimerFunc( delay, angleUpdate, delay );
glutDisplayFunc( draw );
glutMainLoop();
}
//codefragmentend,app
void altinit()
{
//codefragmentbegin,stringinit
glutInitDisplayString( "stencil>=2 rgb~8 double depth>=16 samples" );
//codefragmentend,stringinit
}
using namespace std;
#include <math.h>
float myAngle;
float myTime;
//codefragmentbegin,headerInclude
#include <GLUT/glut.h>
//codefragmentend,headerInclude
//codefragmentbegin,app
void prepareOpenGL()
{
myAngle = 0;
myTime = 0;
}
void draw()
{
glClearColor( 0, .5, .8, 1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glRotatef( myAngle, 0, 0, 1 );
glTranslatef( 0, 0, 1 );
glColor3f( 0, 1, 0 );
glBegin( GL_QUADS );
float ww = .9;
float hh = .9;
glTexCoord2f( 0, 0 );
glVertex3f( -ww, -hh, 0 );
glTexCoord2f( 1, 0 );
glVertex3f( ww, -hh, 0 );
glTexCoord2f( 1, 1 );
glVertex3f( ww, hh, 0 );
glTexCoord2f( 0, 1 );
glVertex3f( -ww, hh, 0 );
glEnd();
glutSwapBuffers();
}
void angleUpdate( int delay )
{
float twopi = 2 * M_PI;
myTime = ( myTime > twopi ) ? 0 : myTime + .03;
myAngle = sinf( twopi * myTime );
glutTimerFunc( delay, angleUpdate, delay );
glutPostRedisplay();
}
int main( int argc,
char * argv[] )
{
glutInit( &argc, argv );
// choose a visual and create a window
glutInitDisplayString( "stencil>=2 rgb~8 double depth>=16 samples" );
// glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 450, 300 );
glutCreateWindow( "GLUT Configuration Example" );
// initalize our opengl (context is now valid)
prepareOpenGL();
// register callback functions
int delay = 50;
glutTimerFunc( delay, angleUpdate, delay );
glutDisplayFunc( draw );
glutMainLoop();
}
//codefragmentend,app
void altinit()
{
//codefragmentbegin,stringinit
glutInitDisplayString( "stencil>=2 rgb~8 double depth>=16 samples" );
//codefragmentend,stringinit
}
Build commands:
g++ -framework GLUT -framework OpenGL main.cpp -o test
Run:
./test
Notice:
(1) Framework 类型提供的lib和其他.a & .dylib的库文件不同;
(2) 编译时使用 -framework GLUT的编译选项。