//----------------------------------------------------------------------------- // name: moons.cpp // desc: simple OpenGL example involving // basic initialization of GLUT and GL // basic perspective projection + camera // single light source // hierarchy of objects // // to compile (OS X): // g++ -o moons moons.cpp -framework OpenGL -framework GLUT // // to run: // ./moons // // Music 256a | Stanford University | Ge Wang // http://ccrma.stanford.edu/courses/256a/ //----------------------------------------------------------------------------- #include #include #include #include // Mac OS X #include // other platforms // #include //----------------------------------------------------------------------------- // function prototypes //----------------------------------------------------------------------------- void idleFunc( ); void displayFunc( ); void reshapeFunc( GLsizei width, GLsizei height ); void keyboardFunc( unsigned char, int, int ); void mouseFunc( int button, int state, int x, int y ); void initialize( ); //----------------------------------------------------------------------------- // global variables and #defines //----------------------------------------------------------------------------- #define __PI 3.1415926 #define ABS(x) (float) (x < 0 ? -x : x) #define COS(x) (float) cos( (double) (x) * __PI / 180.0 ) #define SIN(x) (float) sin( (double) (x) * __PI / 180.0 ) // width and height of the window GLsizei g_width = 800; GLsizei g_height = 600; // light 0 position GLfloat g_light0_pos[4] = { 2.0f, 8.2f, 4.0f, 1.0f }; //----------------------------------------------------------------------------- // Name: main( ) // Desc: starting point //----------------------------------------------------------------------------- int main( int argc, char ** argv ) { // initialize GLUT glutInit( &argc, argv ); // double buffer, use rgb color, enable depth buffer glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); // initialize the window size glutInitWindowSize( g_width, g_height ); // set the window postion glutInitWindowPosition( 100, 100 ); // create the window glutCreateWindow( "The New File" ); // set the idle function - called when idle glutIdleFunc( idleFunc ); // set the display function - called when redrawing glutDisplayFunc( displayFunc ); // set the reshape function - called when client area changes glutReshapeFunc( reshapeFunc ); // set the keyboard function - called on keyboard events glutKeyboardFunc( keyboardFunc ); // set the mouse function - called on mouse stuff glutMouseFunc( mouseFunc ); // do our own initialization initialize(); // let GLUT handle the current thread from here glutMainLoop(); return 0; } //----------------------------------------------------------------------------- // Name: initialize( ) // Desc: sets initial OpenGL states // also initializes any application data //----------------------------------------------------------------------------- void initialize() { // set the GL clear color - use when the color buffer is cleared glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // set the shading model to 'smooth' glShadeModel( GL_SMOOTH ); // enable depth glEnable( GL_DEPTH_TEST ); // set the front faces of polygons glFrontFace( GL_CCW ); // draw in wireframe glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); // enable lighting glEnable( GL_LIGHTING ); // enable lighting for front glLightModeli( GL_FRONT_AND_BACK, GL_TRUE ); // material have diffuse and ambient lighting glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ); // enable color glEnable( GL_COLOR_MATERIAL ); // enable light 0 glEnable( GL_LIGHT0 ); // set the position of the lights glLightfv( GL_LIGHT0, GL_POSITION, g_light0_pos ); } //----------------------------------------------------------------------------- // Name: reshapeFunc( ) // Desc: called when window size changes //----------------------------------------------------------------------------- void reshapeFunc( GLsizei w, GLsizei h ) { // save the new window size g_width = w; g_height = h; // map the view port to the client area glViewport( 0, 0, w, h ); // set the matrix mode to project glMatrixMode( GL_PROJECTION ); // load the identity matrix glLoadIdentity( ); // create the viewing frustum gluPerspective( 90.0, (GLfloat) w / (GLfloat) h, .1, 50.0 ); // set the matrix mode to modelview glMatrixMode( GL_MODELVIEW ); // load the identity matrix glLoadIdentity( ); // position the view point gluLookAt( 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f ); } //----------------------------------------------------------------------------- // name: rand2f() // desc: generate a random float //----------------------------------------------------------------------------- GLfloat rand2f( float a, float b ) { return a + (b-a)*(rand() / (GLfloat)RAND_MAX); } //----------------------------------------------------------------------------- // Name: keyboardFunc( ) // Desc: key event //----------------------------------------------------------------------------- void keyboardFunc( unsigned char key, int x, int y ) { switch( key ) { case 'Q': case 'q': exit(1); break; } glutPostRedisplay( ); } //----------------------------------------------------------------------------- // Name: mouseFunc( ) // Desc: handles mouse stuff //----------------------------------------------------------------------------- void mouseFunc( int button, int state, int x, int y ) { if( button == GLUT_LEFT_BUTTON ) { // when left mouse button is down, move left if( state == GLUT_DOWN ) { } else { } } else if ( button == GLUT_RIGHT_BUTTON ) { // when right mouse button down, move right if( state == GLUT_DOWN ) { } else { } } glutPostRedisplay( ); } //----------------------------------------------------------------------------- // Name: idleFunc( ) // Desc: callback from GLUT //----------------------------------------------------------------------------- void idleFunc( ) { // render the scene glutPostRedisplay( ); // reshape // reshapeFunc( g_width, g_height ); } //----------------------------------------------------------------------------- // Name: displayFunc( ) // Desc: callback function invoked to draw the client area //----------------------------------------------------------------------------- void displayFunc( ) { static GLfloat x = 0, y = 0; // clear the color and depth buffers glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // push the current matrix glPushMatrix(); // color glColor3f( .5, 1.0, .5 ); // sphere glutSolidSphere( 2.0, 10, 10 ); // rotate glRotatef( x, 0, 1, 0 ); x += .5; // translate glTranslatef( 4.0, 0, 0 ); // color glColor3f( 1.0, 1.0, .5 ); // sphere 2 (the moon) glutSolidTeapot( .25 ); // rotate glRotatef( y, 1, 0, 0 ); y += 1; // translate glTranslatef( 0, 1, 0 ); // color glColor3f( 1.0, .5, .5 ); // sphere 2 (the moon) glutSolidSphere( .1, 10, 10 ); // restore the previous matrix glPopMatrix(); // flush and swap glFlush( ); glutSwapBuffers( ); }