Developer Documentation
GridNode.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 
45 
46 
47 //=============================================================================
48 //
49 // CLASS GridNode - IMPLEMENTATION
50 //
51 //=============================================================================
52 
53 //== INCLUDES =================================================================
54 
55 
56 #include "GridNode.hh"
57 #include "SceneGraph.hh"
58 #include <cstdio>
59 
60 //== NAMESPACES ===============================================================
61 
62 
63 namespace ACG {
64 namespace SceneGraph {
65 
66 
67 //== IMPLEMENTATION ==========================================================
68 
69 
71 GridNode(BaseNode* _parent, const std::string& _name)
72  : MaterialNode(_parent, _name),
73  horizontalLines_(7),
74  verticalLines_(7),
75  maxRefinement_(49),
76  minRefinementDistance_(10),
77  gridSize_(10.0),
78  baseLineColor_( Vec3f(0.5f, 0.5f, 0.5f) ),
79  midLineColor_( Vec3f(0.3f, 0.3f, 0.3f) ),
80  autoResize_(false),
81  orientation_(XZ_PLANE)
82 {
83 
84 }
85 
86 
87 //-----------------------------------------------------------------------------
88 
89 
92 {
93  return ( DrawModes::WIREFRAME |
95 }
96 
97 
98 //-----------------------------------------------------------------------------
99 
100 
101 void
103 {
104  // If we do not autoresize, we set our bounding box here
105  // otherwise we set the size of the grid according to the
106  // real scene geometry from the state
107 // if ( !autoResize_ ) {
108 
109  // Only return a bounding box, if something will be drawn
110  if ( orientation_ != NONE) {
111  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
112  bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
113 
114  _bbMin.minimize(bb_min_);
115  _bbMax.maximize(bb_max_);
116  }
117 
118 }
119 
120 
121 //----------------------------------------------------------------
122 
123 void
124 GridNode::pick(GLState& /*_state*/, PickTarget /*_target*/)
125 {
126 
127 
128 }
129 
130 //-----------------------------------------------------------------------------
131 
132 
133 void
134 GridNode::draw(GLState& _state , const DrawModes::DrawMode& /* _drawMode */ )
135 {
136 
137  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
138  ACG::GLState::disable(GL_LIGHTING);
139 
140  glPushAttrib( GL_DEPTH_TEST );
141  ACG::GLState::enable( GL_DEPTH_TEST );
142 
143  glLineWidth(0.1f);
144 
145  ACG::Vec3d direction1,direction2;
146 
147  // For all orientations :
148  for ( unsigned int i = 0 ; i < 3 ; ++i ) {
149 
150  Vec3d viewDirection(0.0, 1.0, 0.0); // = _state.viewing_direction();
151  Vec3d eye = _state.eye();
152 
153  //first we calculate the hitpoint of the camera direction with the grid
154  GLMatrixd modelview = _state.modelview();
155 
156  // Distance from eye point to the plane
157  double distance = 0.0;
158 
159  if ( i == 0 && ((orientation_ ) & XZ_PLANE)) {
160  direction1 = ACG::Vec3f( 1.0 , 0.0 , 0.0 );
161  direction2 = ACG::Vec3f( 0.0 , 0.0 , 1.0 );
162  distance = fabs( eye | ACG::Vec3d(0.0,1.0,0.0 ) );
163  } else if ( i == 1 && (orientation_ & XY_PLANE)) {
164  direction1 = ACG::Vec3f( 1.0 , 0.0 , 0.0 );
165  direction2 = ACG::Vec3f( 0.0 , 1.0 , 0.0 );
166  distance = fabs( eye | ACG::Vec3d(0.0,0.0,1.0 ) );
167  } else if ( i == 2 && (orientation_ & YZ_PLANE)) {
168  direction1 = ACG::Vec3f( 0.0 , 1.0 , 0.0 );
169  direction2 = ACG::Vec3f( 0.0 , 0.0 , 1.0 );
170  distance = fabs( eye | ACG::Vec3d(1.0,0.0,0.0 ) );
171  } else {
172  continue;
173  }
174 
175  // Remember original Modelview Matrix
176  _state.push_modelview_matrix();
177 
178  // The factor is means the following
179  // If the viewer is farther away than the minRefinementDistance_, the factor is 0 and no refinement will take place
180  // If the viewer goes closer to the grid, the grid will be refined.
181  // Block negative distances (grid behind us)
182  int factor = floor( minRefinementDistance_ / distance) - 1;
183 
184  int vertical = verticalLines_;
185  int horizontal = horizontalLines_;
186 
187  if (factor > 20){
188  vertical = maxRefinement_;
189  horizontal = maxRefinement_;
190 
191  } else if (factor > 0){
192  // 'factor' times split the grid (split every cell into 4 new cells)
193  int rest = 0;
194 
195  for (int j=0; j < factor; j++)
196  rest -= floor( pow(double(2.0), j));
197 
198  vertical = vertical * floor( pow(double(2.0),factor)) + rest;
199  horizontal = horizontal * floor( pow(double(2.0),factor)) + rest;
200 
201  vertical = std::min(vertical, maxRefinement_ );
202  horizontal = std::min(vertical, maxRefinement_ );
203  }
204 
205 
206 
207 // if ( autoResize_ ) {
208 //
209 // // Get the current scene size from the glstate
210 // ACG::Vec3d bb_min,bb_max;
211 // _state.get_bounding_box(bb_min,bb_max);
212 //
213 // double radius = std::max( std::max( fabs(bb_max | direction1) , fabs(bb_min | direction1) ),
214 // std::max( fabs(bb_max | direction2) , fabs(bb_min | direction2) ) ) * 2.0;
215 //
216 // // compute the minimal scene radius from the bounding box
217 // // double radius = std::min( fabs(bb_max[0]-bb_min[0]) * 2,fabs(bb_max[2]-bb_min[2]) * 2);
218 //
219 // // set grid size to the given radius if we autoadjust to the scene size
220 // if ( radius > 10.0 )
221 // gridSize_ = radius;
222 //
223 //
224 // /*
225 // _state.set_line_width(3);
226 // glBegin(GL_LINES);
227 // glVertex(bb_min);
228 // glVertex(bb_max);
229 // glEnd();*/
230 //
231 // // // update the bounding box
232 // // bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
233 // // bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
234 //
235 // std::cerr << "Draw " << bb_min << " " << bb_max << std::endl;
236 //
237 //
238 //
239 // }
240 
241  //now start drawing
242  _state.translate( direction1 * -0.5 * gridSize_ + direction2 * -0.5 * gridSize_ );
243 
244  glBegin(GL_LINES);
245 
246  //red line (x-axis)
247  glColor3f( 0.7f, 0.0f, 0.0f );
248  glVertex( direction2 * gridSize_ * 0.5 );
249  glVertex( direction1 * gridSize_ + direction2 * gridSize_ * 0.5 );
250 
251  //blue line (z-axis)
252  glColor3f( 0.0f, 0.0f, 0.7f );
253  glVertex( direction1 * gridSize_ * 0.5 );
254  glVertex( direction1 * 0.5 * gridSize_ + direction2 * gridSize_);
255 
256  //remaining vertical lines
257  for ( int j = 0 ; j < vertical ; ++j ) {
258 
259  //first draw a baseLine
260  glColor3fv( &baseLineColor_[0] );
261 
262  double big = gridSize_ / (vertical-1) * j;
263  double next = gridSize_ / (vertical-1) * (j+1);
264 
265  glVertex( direction1 * big );
266  glVertex( direction1 * big + direction2 * gridSize_ );
267 
268  if ( j+1 < vertical)
269  for (int k=1; k < 10; k++){
270 
271  //then draw 9 darker lines in between
272  glColor3fv( &midLineColor_[0] );
273 
274  double smallPos = big + (next - big) / 10.0 * k;
275  glVertex( direction1 * smallPos);
276  glVertex( direction1 * smallPos + direction2 * gridSize_);
277  }
278  }
279 
280  //remaining horizontal lines
281  for ( int j = 0 ; j < horizontal; ++j ) {
282 
283  //first draw a baseline
284  glColor3fv( &baseLineColor_[0] );
285 
286  double big = gridSize_ / (vertical-1) * j;
287  double next = gridSize_ / (vertical-1) * (j+1);
288 
289  glVertex( direction2 * gridSize_ / (horizontal-1) * j);
290  glVertex( direction1 * gridSize_ + direction2 * gridSize_ / (horizontal-1) * j);
291 
292  if ( j+1 < vertical)
293  for (int k=1; k < 10; k++){
294 
295  //then draw 9 darker lines in between
296  glColor3fv( &midLineColor_[0] );
297 
298  double smallPos = big + (next - big) / 10.0 * k;
299  glVertex( direction2 * smallPos );
300  glVertex( direction1 * gridSize_ + direction2 * smallPos );
301  }
302  }
303  glEnd();
304 
305  _state.pop_modelview_matrix();
306 
307  }
308 
309  glLineWidth(1.0);
310 
311  glPopAttrib( ); // ->Depth test
312  glPopAttrib( ); // ->Lighting
313 }
314 
315 //-----------------------------------------------------------------------------
316 
317 void
318 GridNode::gridSize(float _size){
319  gridSize_ = _size;
320 
321  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
322  bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
323 }
324 
325 //-----------------------------------------------------------------------------
326 
327 float
329  return gridSize_;
330 }
331 
332 //-----------------------------------------------------------------------------
333 
334 void
335 GridNode::minRefinementDistance( double _distance ) {
336  minRefinementDistance_ = _distance;
337 }
338 
339 //-----------------------------------------------------------------------------
340 
341 double
343 {
344  return minRefinementDistance_;
345 }
346 
347 //-----------------------------------------------------------------------------
348 
349 void GridNode::setOrientation( unsigned int _orientation)
350 {
351  orientation_ = _orientation;
352 }
353 
354 //-----------------------------------------------------------------------------
355 
356 void GridNode::autoResize(bool _auto)
357 {
358  autoResize_ = _auto;
359 }
360 
361 
362 //=============================================================================
363 } // namespace SceneGraph
364 } // namespace ACG
365 //=============================================================================
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
Definition: GLState.cc:1507
double minRefinementDistance()
returns the minimal refinement distance
Definition: GridNode.cc:342
Vec3f baseLineColor_
colors for the grid
Definition: GridNode.hh:162
GridNode(BaseNode *_parent=0, const std::string &_name="<GridNode>")
Default constructor.
Definition: GridNode.cc:71
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
Namespace providing different geometric functions concerning angles.
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
drawing the primitive
Definition: GridNode.cc:134
ACG::SceneGraph::DrawModes::DrawMode availableDrawModes() const override
return available draw modes
Definition: GridNode.cc:91
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:587
Vec3d bb_min_
bounding box
Definition: GridNode.hh:158
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
void pick(GLState &_state, PickTarget _target) override
don&#39;t pick me
Definition: GridNode.cc:124
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
void setOrientation(unsigned int _orientation)
Set the plane orientation.
Definition: GridNode.cc:349
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Definition: GLState.cc:1527
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:119
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:559
void glVertex(const Vec2i &_v)
Wrapper: glVertex for Vec2i.
Definition: gl.hh:95
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
Definition: DrawModes.cc:81
unsigned int orientation_
Contains all orientations to draw.
Definition: GridNode.hh:168
DrawMode WIREFRAME
draw wireframe
Definition: DrawModes.cc:78
float gridSize()
Get GridSize.
Definition: GridNode.cc:328
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:816
int horizontalLines_
initial number of baseLines
Definition: GridNode.hh:144
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
Definition: GridNode.cc:102
float gridSize_
dimensions of the grid
Definition: GridNode.hh:155
Vec3d eye() const
get eye point
Definition: GLState.cc:886
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533