#include #include #include #if defined(__APPLE__) #include #include #else #include #endif #include "plane_cloud.h" #include "plane_renderers.h" #include "timing.h" #define CAMERA_ROTATION_SPEED 1.0 #define CAMERA_XZ_DISTANCE 150.0 #define CAMERA_Y_HEIGHT 50.0 unsigned renderer_index = ~0u; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); static unsigned frame_count = 0; static double last_fps_time = -1.0; static double last_frame_time = -1.0; if (last_frame_time < 0.0) { last_frame_time = time_now(); last_fps_time = last_frame_time; } double now = time_now(); double dt = now - last_frame_time; last_frame_time = now; double dt_fps = now - last_fps_time; if (dt_fps > 1.0) { printf("%0.2f fps\n", frame_count / dt_fps); frame_count = 0; last_fps_time = now; } ++frame_count; static double angle = 0.0; angle += CAMERA_ROTATION_SPEED * dt; if (angle > M_PI) { angle -= 2.0 * M_PI; } glLoadIdentity(); gluLookAt( CAMERA_XZ_DISTANCE * cos(angle), CAMERA_Y_HEIGHT, CAMERA_XZ_DISTANCE * sin(angle), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); update_plane_cloud(dt); RENDERERS[renderer_index].update_and_render(dt); glutSwapBuffers(); glutReportErrors(); } void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)width / (double)height, 1.0, 500.0); glMatrixMode(GL_MODELVIEW); } void idle(void) { glutPostRedisplay(); } void key_pressed(unsigned char key, int x, int y) { if (key >= '1' && key <= '9') { unsigned index = key - '1'; if (index < NUM_RENDERERS && RENDERERS[index].ok) { renderer_index = index; glutSetWindowTitle(RENDERERS[index].name); } } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(640, 480); glutCreateWindow("Kiloplane"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); glutKeyboardFunc(key_pressed); glEnable(GL_DEPTH_TEST); glClearColor(0.5, 0.0, 0.5, 0.0); initialize_plane_cloud(); unsigned i; for (i = 0; i < NUM_RENDERERS; ++i) { RENDERERS[i].ok = RENDERERS[i].initialize(); } for (i = 0; i < NUM_RENDERERS; ++i) { if (RENDERERS[i].ok) { renderer_index = i; glutSetWindowTitle(RENDERERS[i].name); break; } } if (renderer_index == ~0u) { fprintf(stderr, "No working renderers\n"); return EXIT_FAILURE; } #if defined(__APPLE__) && 0 CGLError error = CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine); if (error != kCGLNoError) { fprintf( stderr, "Error: %lu enabling MP engine\n", (unsigned long)error); } #endif glutMainLoop(); return EXIT_SUCCESS; }