GridNode.cc 8.58 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
   
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58




//=============================================================================
//
//  CLASS GridNode - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================


#include "GridNode.hh"
#include "SceneGraph.hh"
#include "../GL/gl.hh"
#include <stdio.h>
59
#include <math.h>
Jan Möbius's avatar
   
Jan Möbius committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73

//== NAMESPACES ===============================================================


namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==========================================================


GridNode::
GridNode(BaseNode* _parent, const std::string& _name)
  : MaterialNode(_parent, _name),
74
75
    horizontalLines_(7),
    verticalLines_(7),
76
77
    maxRefinement_(49),
    gridSize_(1500.0)
Jan Möbius's avatar
   
Jan Möbius committed
78
{
79
80
  baseLineColor_ = Vec3f(0.5, 0.5, 0.5);
  midLineColor_  = Vec3f(0.3, 0.3, 0.3);
81
82
  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
  bb_max_ = Vec3f( 0.5*gridSize_, 0.0,  0.5*gridSize_);
Jan Möbius's avatar
   
Jan Möbius committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
}


//-----------------------------------------------------------------------------


unsigned int
GridNode::availableDrawModes() const
{
  return ( DrawModes::WIREFRAME           |
	        DrawModes::SOLID_FLAT_SHADED   );
}


//-----------------------------------------------------------------------------


void
101
GridNode::boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
Jan Möbius's avatar
   
Jan Möbius committed
102
{
103
104
  _bbMin.minimize(bb_min_);
  _bbMax.maximize(bb_max_);
Jan Möbius's avatar
   
Jan Möbius committed
105
106
107
}


Dirk Wilden's avatar
Dirk Wilden committed
108
109
110
111
112
113
114
115
116
//----------------------------------------------------------------

void
GridNode::pick(GLState& _state, PickTarget _target)
{


}

Jan Möbius's avatar
   
Jan Möbius committed
117
118
119
120
121
122
123
124
125
126
127
//-----------------------------------------------------------------------------


void
GridNode::draw(GLState&  _state  , unsigned int /* _drawMode */ )
{

  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
  glDisable(GL_LIGHTING);

  glPushAttrib( GL_DEPTH_TEST );
128
  glEnable( GL_DEPTH_TEST );
Jan Möbius's avatar
   
Jan Möbius committed
129

130
  glLineWidth(0.1);
Jan Möbius's avatar
   
Jan Möbius committed
131
132
  _state.push_modelview_matrix();

133
134
135
  //first we calculate the hitpoint of the camera direction with the grid
  GLMatrixd modelview = _state.modelview();

136
137
  Vec3d viewDirection(0.0, 1.0, 0.0); // = _state.viewing_direction();
  Vec3f eye = (Vec3f) _state.eye();
138
139
140
141
142
143
144
145
146
147
148

  double lambda = - (eye[1] / viewDirection[1]);

  Vec3f hitPoint;
  hitPoint[0] = eye[0] + lambda * viewDirection[0];
  hitPoint[1] = 0;
  hitPoint[2] = eye[2] + lambda * viewDirection[2];

  //the distance between camera and hitpoint defines a factor that controls
  //how many lines are drawn
  double distance = _state.modelview().transform_point( hitPoint ).norm();
Jan Möbius's avatar
   
Jan Möbius committed
149

150
151
152
153
154
  int factor =  floor(2000.0 / distance) - 1;

  int vertical = verticalLines_;
  int horizontal = horizontalLines_;

155
156
157
158
159
  if (factor > 20){
    vertical = maxRefinement_;
    horizontal = maxRefinement_;

  } else if (factor > 0){
160
161
162
163
    // 'factor' times split the grid (split every cell into 4 new cells)
    int rest = 0;

    for (int i=0; i < factor; i++)
Jan Möbius's avatar
Jan Möbius committed
164
      rest -= floor( pow(double(2.0), i));
165

Jan Möbius's avatar
Jan Möbius committed
166
167
    vertical   = vertical   * floor( pow(double(2.0),factor)) + rest;
    horizontal = horizontal * floor( pow(double(2.0),factor)) + rest;
168

169
170
    vertical   = std::min(vertical, maxRefinement_ );
    horizontal = std::min(vertical, maxRefinement_ );
171
172
173
  }

  //now start drawing
174
  _state.translate(-0.5*gridSize_, 0, -0.5*gridSize_);
Jan Möbius's avatar
   
Jan Möbius committed
175
176
177

  glBegin(GL_LINES);

178
179
    //red line (x-axis)
    glColor3f( 0.7, 0.0, 0.0 );
180
181
    glVertex3f(   0.0, 0.0, gridSize_*0.5);
    glVertex3f(gridSize_, 0.0, gridSize_*0.5);
182
183
184

    //blue line (z-axis)
    glColor3f( 0.0, 0.0, 0.7 );
185
186
    glVertex3f(gridSize_*0.5, 0.0,     0.0);
    glVertex3f(gridSize_*0.5, 0.0, gridSize_);
187
188
189
190
191
192
193

    //remaining vertical lines
    for ( int i = 0 ; i <  vertical ; ++i ) {

      //first draw a baseLine
      glColor3fv( &baseLineColor_[0] );

194
195
      double big  = gridSize_ / (vertical-1) * i;
      double next = gridSize_ / (vertical-1) * (i+1);
196
197

      glVertex3f( big, 0.0, 0.0);
198
      glVertex3f( big, 0.0, gridSize_);
199
200
201
202
203
204
205

      if ( i+1 < vertical)
        for (int j=1; j < 10; j++){

          //then draw 9 darker lines in between
          glColor3fv( &midLineColor_[0] );
  
Jan Möbius's avatar
Jan Möbius committed
206
207
208
          double smallPos = big + (next - big) / 10.0 * j;
          glVertex3f( smallPos, 0.0, 0.0);
          glVertex3f( smallPos, 0.0, gridSize_);
209
        }
Jan Möbius's avatar
   
Jan Möbius committed
210
211
    }

212
213
214
215
216
217
    //remaining horizontal lines
    for ( int i = 0 ; i <  horizontal; ++i ) {

      //first draw a baseline
      glColor3fv( &baseLineColor_[0] );

218
219
      double big  = gridSize_ / (vertical-1) * i;
      double next = gridSize_ / (vertical-1) * (i+1);
220

221
222
      glVertex3f(   0.0, 0.0, gridSize_ / (horizontal-1) * i);
      glVertex3f(gridSize_, 0.0, gridSize_ / (horizontal-1) * i);
223
224
225
226
227
228
229

      if ( i+1 < vertical)
        for (int j=1; j < 10; j++){

          //then draw 9 darker lines in between
          glColor3fv( &midLineColor_[0] );
  
Jan Möbius's avatar
Jan Möbius committed
230
231
232
          double smallPos = big + (next - big) / 10.0 * j;
          glVertex3f(   0.0, 0.0, smallPos);
          glVertex3f(gridSize_, 0.0, smallPos);
233
234
        }   
    }
Jan Möbius's avatar
   
Jan Möbius committed
235
236
237
238
  glEnd();

  _state.pop_modelview_matrix();

239
240
241
242
  glLineWidth(1.0);

  glPopAttrib( ); // ->Depth test
  glPopAttrib( ); // ->Lighting
Jan Möbius's avatar
   
Jan Möbius committed
243
244
}

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//-----------------------------------------------------------------------------

void
GridNode::gridSize(float _size){
  gridSize_ = _size;

  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
  bb_max_ = Vec3f( 0.5*gridSize_, 0.0,  0.5*gridSize_);
}

//-----------------------------------------------------------------------------

float
GridNode::gridSize(){
  return gridSize_;
}
Jan Möbius's avatar
   
Jan Möbius committed
261
262
263
264
265

//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================