GlutExaminer.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS GlutExaminer - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 //== INCLUDES =================================================================
00052 
00053 
00054 #include "GlutExaminer.hh"
00055 #include "../Utils/StopWatch.hh"
00056 
00057 
00058 //== NAMESPACES ===============================================================
00059 
00060 
00061 namespace ACG {
00062 
00063 
00064 //== IMPLEMENTATION ========================================================== 
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 } // namespace ACG
00210 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .