Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GlutExaminer.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS GlutExaminer - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 //== INCLUDES =================================================================
59 
60 
61 #include "GlutExaminer.hh"
62 #include "../Utils/StopWatch.hh"
63 
64 
65 //== NAMESPACES ===============================================================
66 
67 
68 namespace ACG {
69 
70 
71 //== IMPLEMENTATION ==========================================================
72 
73 
74 GlutExaminer::
75 GlutExaminer(const char* _title, int _width, int _height)
76  : GlutViewer(_title, _width, _height),
77  trackball_(glstate_),
78  center_(Vec3f(0.0,0.0,0.0)),
79  radius_(1.0)
80 {
81  init();
82 }
83 
84 
85 //-----------------------------------------------------------------------------
86 
87 
88 void
89 GlutExaminer::init()
90 {
91  trackball_.set_center(Vec3f(0,0,0));
92 }
93 
94 
95 //-----------------------------------------------------------------------------
96 
97 
98 void
99 GlutExaminer::draw()
100 {
101  ACG::GLState::enable(GL_LIGHTING);
102  ACG::GLState::shadeModel(GL_SMOOTH);
103  glutSolidTeapot(0.5);
104 }
105 
106 
107 //-----------------------------------------------------------------------------
108 
109 
110 void
111 GlutExaminer::mouse(int button, int state, int x, int y)
112 {
113  if (state == GLUT_DOWN) trackball_.mouse_press(button, x, y);
114  else trackball_.mouse_release(button, x, y);
115  glutPostRedisplay();
116 }
117 
118 
119 //-----------------------------------------------------------------------------
120 
121 
122 void
123 GlutExaminer::motion(int x, int y)
124 {
125  trackball_.mouse_move(x, y);
126  glutPostRedisplay();
127 }
128 
129 
130 //-----------------------------------------------------------------------------
131 
132 
133 void
134 GlutExaminer::keyboard(int key, int x, int y)
135 {
136  switch (key)
137  {
138  case 'f':
139  {
140  std::cerr << "Performance test: ";
141  double fps = measure_fps();
142  std::cerr << fps << " FPS\n";
143  break;
144  }
145 
146  default:
147  {
148  GlutViewer::keyboard(key, x, y);
149  break;
150  }
151  }
152 }
153 
154 
155 //-----------------------------------------------------------------------------
156 
157 
158 void
159 GlutExaminer::setup_scene(const Vec3f& _center, float _radius)
160 {
161  center_ = _center;
162  radius_ = _radius;
163 
164  trackball_.set_center(_center);
165 
166  near_ = 0.01f * radius_;
167  far_ = 10.0f * radius_;
168  update_projection();
169 
170  view_all();
171 }
172 
173 
174 //-----------------------------------------------------------------------------
175 
176 
177 void
178 GlutExaminer::view_all()
179 {
180  ACG::Vec3d t = (-(glstate_.modelview().transform_point(center_))
181  - Vec3d(0.0f, 0.0f, 3.0f*radius_));
182  glstate_.translate(t[0], t[1], t[2], MULT_FROM_LEFT);
183 }
184 
185 
186 //-----------------------------------------------------------------------------
187 
188 
189 double
190 GlutExaminer::measure_fps()
191 {
192  const Vec3d t = glstate_.modelview().transform_point(center_);
193 
194  glMatrixMode(GL_MODELVIEW);
195  glPushMatrix();
196 
197  StopWatch timer; timer.start();
198 
199  for (int i=0; i<72; ++i)
200  {
201  glstate_.translate(-t[0], -t[1], -t[2], MULT_FROM_LEFT);
202  glstate_.rotate(5.0f, 0.0f, 1.0f, 0.0f, MULT_FROM_LEFT);
203  glstate_.translate( t[0], t[1], t[2], MULT_FROM_LEFT);
204  display();
205  }
206  glFinish();
207 
208  double elapsed = timer.stop();
209 
210  glPopMatrix();
211  glutPostRedisplay();
212 
213  return (1000.0 / elapsed * 72.0);
214 }
215 
216 
217 //=============================================================================
218 } // namespace ACG
219 //=============================================================================
static void enable(GLenum _cap)
replaces glEnable, but supports locking
Definition: GLState.cc:1490
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:531
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
Definition: GLState.cc:562
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:127
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:794
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
Definition: GLState.cc:1700
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:125
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
Definition: Matrix4x4T.cc:202