Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
|
00001 /* 00002 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 00003 * 00004 * This software is provided 'as-is', without any express or implied 00005 * warranty. In no event will the authors be held liable for any damages 00006 * arising from the use of this software. 00007 * Permission is granted to anyone to use this software for any purpose, 00008 * including commercial applications, and to alter it and redistribute it 00009 * freely, subject to the following restrictions: 00010 * 1. The origin of this software must not be misrepresented; you must not 00011 * claim that you wrote the original software. If you use this software 00012 * in a product, an acknowledgment in the product documentation would be 00013 * appreciated but is not required. 00014 * 2. Altered source versions must be plainly marked as such, and must not be 00015 * misrepresented as being the original software. 00016 * 3. This notice may not be removed or altered from any source distribution. 00017 */ 00018 00019 /* 00020 * Base code for CS 296 Software Systems Lab 00021 * Department of Computer Science and Engineering, IIT Bombay 00022 * Instructor: Parag Chaudhuri 00023 */ 00024 00027 #include<iostream> 00028 #include "render.hpp" 00029 #include "cs296_base.hpp" 00030 #include "callbacks.hpp" 00031 #include <sys/time.h> //Timer Functions 00032 00036 #ifndef __APPLE__ 00037 #include "GL/glui.h" 00038 #else 00039 #include "GL/glui.h" 00040 #endif 00041 00045 #include <cstdio> 00046 00047 00049 namespace cs296 00050 { 00051 extern int32 test_index; 00052 extern int32 test_selection; 00053 extern int32 test_count; 00054 extern cs296::sim_t* entry; 00055 extern cs296::base_sim_t* test; 00056 extern cs296::settings_t settings; 00057 extern const int32 frame_period; 00058 extern float settings_hz; 00059 extern int32 width; 00060 extern int32 height; 00061 extern int32 main_window; 00062 }; 00063 00066 using namespace cs296; 00067 00068 00070 void create_glui_ui(void) 00071 { 00072 GLUI *glui = GLUI_Master.create_glui_subwindow( main_window, GLUI_SUBWINDOW_BOTTOM ); 00073 00074 glui->add_statictext("Simulation Timesteps"); 00075 GLUI_Spinner* velocityIterationSpinner = 00076 glui->add_spinner("Velocity Iterations", GLUI_SPINNER_INT, &settings.velocity_iterations); 00077 velocityIterationSpinner->set_int_limits(1, 500); 00078 00079 GLUI_Spinner* positionIterationSpinner = 00080 glui->add_spinner("Position Iterations", GLUI_SPINNER_INT, &settings.position_iterations); 00081 positionIterationSpinner->set_int_limits(0, 100); 00082 00083 GLUI_Spinner* hertzSpinner = 00084 glui->add_spinner("Sim steps per frame", GLUI_SPINNER_FLOAT, &settings_hz); 00085 hertzSpinner->set_float_limits(5.0f, 200.0f); 00086 00087 00088 00089 new GLUI_Column( glui, false ); 00090 glui->add_statictext("Simulation Parameters"); 00091 glui->add_checkbox("Warm Starting", &settings.enable_warm_starting); 00092 glui->add_checkbox("Time of Impact", &settings.enable_continuous); 00093 glui->add_checkbox("Sub-Stepping", &settings.enable_sub_stepping); 00094 00095 00096 00097 new GLUI_Column( glui, false ); 00098 glui->add_statictext("Display Options"); 00099 GLUI_Panel* drawPanel = glui->add_panel("Draw"); 00100 glui->add_checkbox_to_panel(drawPanel, "Shapes", &settings.draw_shapes); 00101 glui->add_checkbox_to_panel(drawPanel, "Joints", &settings.draw_joints); 00102 glui->add_checkbox_to_panel(drawPanel, "AABBs", &settings.draw_AABBs); 00103 glui->add_checkbox_to_panel(drawPanel, "Statistics", &settings.draw_stats); 00104 glui->add_checkbox_to_panel(drawPanel, "Profile", &settings.draw_profile); 00105 00106 new GLUI_Column( glui, false ); 00107 glui->add_button("Pause", 0, callbacks_t::pause_cb); 00108 glui->add_button("Single Step", 0, callbacks_t::single_step_cb); 00109 glui->add_button("Restart", 0, callbacks_t::restart_cb); 00110 00111 glui->add_button("Quit", 0,(GLUI_Update_CB)callbacks_t::exit_cb); 00112 glui->set_main_gfx_window( main_window ); 00113 } 00114 00116 void average_time(int32 no_of_steps){ 00117 // settings_hz from callbacks.cpp 00118 float32 step_size=1/settings_hz; 00119 00120 b2World* world = test->get_world(); 00121 00123 const b2Profile& world_profile = world->GetProfile(); 00124 int32 step_Count=0; 00125 00126 float32 time_taken_for_step=0.0000; 00127 float32 time_taken_for_velocity_update=0.0000; 00128 float32 time_taken_for_collisions=0.0000; 00129 float32 time_taken_for_position_update=0.0000; 00130 00131 struct timeval start, end; 00132 gettimeofday(&start,NULL); 00133 double t1 = 1000*(start.tv_sec) + start.tv_usec/1000.0; 00134 00135 while (step_Count<no_of_steps) 00136 { 00137 // Step function for Simulation in Box2D world. 00138 world->Step(step_size, settings.velocity_iterations, settings.position_iterations); 00139 time_taken_for_step +=world_profile.step; 00140 time_taken_for_velocity_update += world_profile.solveVelocity; 00141 time_taken_for_collisions += world_profile.collide; 00142 time_taken_for_position_update += world_profile.solvePosition; 00143 step_Count++; 00144 } 00145 00146 gettimeofday(&end, NULL); 00147 double t2 = 1000*(end.tv_sec) + end.tv_usec/1000.0; 00148 double t= t2 - t1; 00149 //[Lab07] 00150 std::cout<<"Total Iterations: "<< no_of_steps<<"\n"; 00151 std::cout<<"Average time per step is "<< time_taken_for_step/no_of_steps<<" ms\n"; 00152 std::cout<<"Average time for collisions is "<<time_taken_for_collisions/no_of_steps<<" ms\n"; 00153 std::cout<<"Average time for velocity updates is "<<time_taken_for_velocity_update/no_of_steps<<" ms\n"; 00154 std::cout<<"Average time for position updates is "<<time_taken_for_position_update/no_of_steps<<" ms\n\n"; 00155 std::cout<<"Total time for loop is "<<t<<" ms"<<std::endl; 00156 //std::cout <<t/1000 00157 //<<std::endl; 00158 } 00159 00160 00162 int main(int argc, char** argv) 00163 { 00164 test_count = 1; 00165 test_index = 0; 00166 test_selection = test_index; 00167 00168 entry = sim; 00169 test = entry->create_fcn(); 00170 00172 glutInit(&argc, argv); 00173 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 00174 glutInitWindowSize(800, 500); 00175 00176 char title[50]; 00177 sprintf(title, "CS296 Base Code. Running on Box2D %d.%d.%d", b2_version.major, b2_version.minor, b2_version.revision); 00178 main_window = glutCreateWindow(title); 00179 00182 GLUI_Master.set_glutReshapeFunc(callbacks_t::resize_cb); 00183 GLUI_Master.set_glutKeyboardFunc(callbacks_t::keyboard_cb); 00184 GLUI_Master.set_glutSpecialFunc(callbacks_t::keyboard_special_cb); 00185 GLUI_Master.set_glutMouseFunc(callbacks_t::mouse_cb); 00187 glutDisplayFunc(callbacks_t::display_cb); 00188 glutMotionFunc(callbacks_t::mouse_motion_cb); 00189 glutKeyboardUpFunc(callbacks_t::keyboard_up_cb); 00190 glutTimerFunc(frame_period, callbacks_t::timer_cb, 0); 00191 00192 /* 00193 // Command Line argument for number of iterations 00194 int no_of_steps = 0; 00195 if(argc == 1) no_of_steps = 100; //Default Value 00196 else no_of_steps = atoi(argv[1]); 00198 average_time(no_of_steps); 00199 */ 00201 create_glui_ui(); 00202 00204 glutMainLoop(); 00205 00206 return 0; 00207 }