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