// Developed by Neil Kemp: http://neilkemp.us // Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php // GL includes // ---------------------------------------------------------------- #include #include #include #include // Window defines // ---------------------------------------------------------------- #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #ifndef BOOL #define BOOL int #define FALSE 0 #define TRUE 1 #endif // Function prototypes // ---------------------------------------------------------------- BOOL initialize_gl(void); BOOL initialize_resources(void); void free_resources(void); BOOL on_render_frame(void); void on_size_window(int width, int height); // GLUT callback unction prototypes // ---------------------------------------------------------------- void draw_glut_frame(void); void resize_glut(int width, int height); void process_normal_keys(unsigned char key, int x, int y); void process_special_keys(int key, int x, int y); // Entry point // ---------------------------------------------------------------- int main(int argc, char** argv) { // Initialize GLUT glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE); glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutSetWindowTitle("Project Name (OSX:OpenGL) - ESC to Exit"); // Initialize GL settings if(!initialize_gl()) { return -1; } // Initialize resources if(!initialize_resources()) { free_resources(); return 0; } // Set GLUT callbacks glutDisplayFunc(draw_glut_frame); glutIdleFunc(draw_glut_frame); glutReshapeFunc(resize_glut); glutKeyboardFunc(process_normal_keys); glutSpecialFunc(process_special_keys); // Start GLUT loop glutMainLoop(); // Shutdown free_resources(); return 0; } // Initialize OpenGL settings BOOL initialize_gl(void) { // Local data float gl_float4[4]; // Setup perspective matrix on_size_window(WINDOW_WIDTH, WINDOW_HEIGHT); // Set OpenGL defaults glClearColor (0.05f, 0.1f, 0.2f, 1.0f); glClearDepth (1.0f); glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL); glShadeModel (GL_SMOOTH ); glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST); glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); glEnable (GL_CULL_FACE); glFrontFace (GL_CCW); glCullFace (GL_BACK); glEnable (GL_TEXTURE_2D); glEnable (GL_LIGHTING); glEnable (GL_MULTISAMPLE); // Set ambient lighting gl_float4[0] = 0.9f; gl_float4[1] = 0.9f; gl_float4[2] = 0.9f; gl_float4[3] = 1.0f; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, gl_float4); return TRUE; } // Initialize application resources BOOL initialize_resources(void) { // TODO return TRUE; } // Free resources void free_resources(void) { // TODO } // Called once per render frame BOOL on_render_frame(void) { // Clear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // TODO Draw // Swap buffers glutSwapBuffers(); return TRUE; } // Called when window is resized void on_size_window(int width, int height) { if(height <= 0) { height = 1; } // Change viewport size glViewport(0,0, width, height); // Set projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 1000.0f); // TODO Setup camera /* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera_position.x, camera_position.y, camera_position.z, camera_look_at.x, camera_look_at.y, camera_look_at.z, 0, 1, 0); */ } // GLUT callback functions // ---------------------------------------------------------------- // Draw frame void draw_glut_frame(void) { if(!on_render_frame()) { exit(0); } } // Resize GLUT window void resize_glut(int width, int height) { on_size_window(width, height); } // Process normal GLUT keys void process_normal_keys(unsigned char key, int x, int y) { // Exit on ESC if(key == 27) { exit(0); } } // Process special GLUT keys void process_special_keys(int key, int x, int y) { // Check for left arrow if(key == GLUT_KEY_LEFT) // LEFT { } // Check for right arrow if(key == GLUT_KEY_RIGHT) // RIGHT { } // Check for up arrow if(key == GLUT_KEY_UP) // UP { } // Check for down arrow if(key == GLUT_KEY_DOWN) // DOWN { } }