GlutExaminer.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include "GlutExaminer.hh"
00055 #include "../Utils/StopWatch.hh"
00056
00057
00058
00059
00060
00061 namespace ACG {
00062
00063
00064
00065
00066
00067 GlutExaminer::
00068 GlutExaminer(const char* _title, int _width, int _height)
00069 : GlutViewer(_title, _width, _height),
00070 trackball_(glstate_)
00071 {
00072 init();
00073 }
00074
00075
00076
00077
00078
00079 void
00080 GlutExaminer::init()
00081 {
00082 trackball_.set_center(Vec3f(0,0,0));
00083 }
00084
00085
00086
00087
00088
00089 void
00090 GlutExaminer::draw()
00091 {
00092 glEnable(GL_LIGHTING);
00093 glShadeModel(GL_SMOOTH);
00094 glutSolidTeapot(0.5);
00095 }
00096
00097
00098
00099
00100
00101 void
00102 GlutExaminer::mouse(int button, int state, int x, int y)
00103 {
00104 if (state == GLUT_DOWN) trackball_.mouse_press(button, x, y);
00105 else trackball_.mouse_release(button, x, y);
00106 glutPostRedisplay();
00107 }
00108
00109
00110
00111
00112
00113 void
00114 GlutExaminer::motion(int x, int y)
00115 {
00116 trackball_.mouse_move(x, y);
00117 glutPostRedisplay();
00118 }
00119
00120
00121
00122
00123
00124 void
00125 GlutExaminer::keyboard(int key, int x, int y)
00126 {
00127 switch (key)
00128 {
00129 case 'f':
00130 {
00131 std::cerr << "Performance test: ";
00132 double fps = measure_fps();
00133 std::cerr << fps << " FPS\n";
00134 break;
00135 }
00136
00137 default:
00138 {
00139 GlutViewer::keyboard(key, x, y);
00140 break;
00141 }
00142 }
00143 }
00144
00145
00146
00147
00148
00149 void
00150 GlutExaminer::setup_scene(const Vec3f& _center, float _radius)
00151 {
00152 center_ = _center;
00153 radius_ = _radius;
00154
00155 trackball_.set_center(_center);
00156
00157 near_ = 0.01 * radius_;
00158 far_ = 10.0 * radius_;
00159 update_projection();
00160
00161 view_all();
00162 }
00163
00164
00165
00166
00167
00168 void
00169 GlutExaminer::view_all()
00170 {
00171 ACG::Vec3f t = (-(glstate_.modelview().transform_point(center_))
00172 - Vec3f(0.0, 0.0, 3.0*radius_));
00173 glstate_.translate(t[0], t[1], t[2], MULT_FROM_LEFT);
00174 }
00175
00176
00177
00178
00179
00180 double
00181 GlutExaminer::measure_fps()
00182 {
00183 const Vec3f t = glstate_.modelview().transform_point(center_);
00184
00185 glMatrixMode(GL_MODELVIEW);
00186 glPushMatrix();
00187
00188 StopWatch timer; timer.start();
00189
00190 for (int i=0; i<72; ++i)
00191 {
00192 glstate_.translate(-t[0], -t[1], -t[2], MULT_FROM_LEFT);
00193 glstate_.rotate(5.0f, 0.0f, 1.0f, 0.0f, MULT_FROM_LEFT);
00194 glstate_.translate( t[0], t[1], t[2], MULT_FROM_LEFT);
00195 display();
00196 }
00197 glFinish();
00198
00199 double elapsed = timer.stop();
00200
00201 glPopMatrix();
00202 glutPostRedisplay();
00203
00204 return (1000.0 / elapsed * 72.0);
00205 }
00206
00207
00208
00209 }
00210