CoordsysNode.cc 10.9 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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
Jan Möbius's avatar
 
Jan Möbius committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
//
//=============================================================================




//=============================================================================
//
//  CLASS CoordsysNode - IMPLEMENTATION
//
//=============================================================================

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

#include "CoordsysNode.hh"
#include "../GL/gl.hh"
#include <iostream>
#include <math.h>


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

namespace ACG {
namespace SceneGraph {


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


void
CoordsysNode::
boundingBox(Vec3f& _bbMin, Vec3f& _bbMax)
{
	//_bbMin.minimize( Vect3f  )
}


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


unsigned int
CoordsysNode::
availableDrawModes() const
{
  return ( DrawModes::POINTS |
	        DrawModes::POINTS_SHADED |
	        DrawModes::POINTS_COLORED );
}


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

void
CoordsysNode::
drawCoordsys( GLState&  _state) {

  double topRadius = 0.01;
  double arrowLength = 0.04;
  double bodyRadius = 0.004;
  double bodyLength = 0.06;
  int slices = 10;
  int stacks = 10;
  int loops = 10;
  double sphereRadius = 0.01;

  GLUquadricObj *quadric = gluNewQuadric();

  GLMatrixd modelview = _state.modelview();

  modelview(0,3) = 0.0;
  modelview(1,3) = 0.0;
  modelview(2,3) = 0.0;

  // Origin
  glColor3f(0.5, 0.5, 0.5);
  glLoadIdentity();
  gluSphere( quadric, sphereRadius, slices, stacks );

  // X-Axis
  glColor3f(1.0, 0.0, 0.0);
  glLoadIdentity();

  glMultMatrixd(modelview.get_raw_data());
  glRotatef(-90, 0, 1, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  glLoadIdentity();
  glMultMatrixd(modelview.get_raw_data());
  glRotatef(-90, 0, 1, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluDisk( quadric, 0, topRadius, slices, loops );
  glTranslatef( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

  // Y-Axis
  glColor3f(0.0, 1.0, 0.0);
  glLoadIdentity();
  glMultMatrixd(modelview.get_raw_data());
  glRotatef(90, 1, 0, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  glLoadIdentity();
  glMultMatrixd(modelview.get_raw_data());
  glRotatef(90, 1, 0, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluDisk( quadric, 0, topRadius, slices, loops );
  glTranslatef( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

  // Z-Axis
  glColor3f(0.0, 0.0, 1.0);
  glLoadIdentity();
  glMultMatrixd(modelview.get_raw_data());
  glRotatef(180, 0, 1, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  glLoadIdentity();
  glMultMatrixd(modelview.get_raw_data());
  glRotatef(180, 0, 1, 0);
  glTranslatef( 0, 0, -bodyLength );
  gluDisk( quadric, 0, topRadius, slices, loops );
  glTranslatef( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

  gluDeleteQuadric(quadric);
}

//============================================================================

void
CoordsysNode::drawCoordsysPick( GLState&  _state) {

    double topRadius = 0.01;
    double arrowLength = 0.04;
    double bodyRadius = 0.004;
    double bodyLength = 0.06;
    int slices = 10;
    int stacks = 10;
    int loops = 10;
    double sphereRadius = 0.01;

    GLUquadricObj *quadric = gluNewQuadric();
    GLMatrixd modelview = _state.modelview();

    modelview(0,3) = 0.0;
    modelview(1,3) = 0.0;
    modelview(2,3) = 0.0;

  // Origin
    glLoadIdentity();
179
    glLoadName(0);
Jan Möbius's avatar
 
Jan Möbius committed
180 181 182 183
    gluSphere( quadric, sphereRadius, slices, stacks );

  // X-Axis
    glLoadIdentity();
184
    glLoadName(1);
Jan Möbius's avatar
 
Jan Möbius committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(-90, 0, 1, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
    glLoadIdentity();
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(-90, 0, 1, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluDisk( quadric, 0, topRadius, slices, loops );
    glTranslatef( 0, 0, -arrowLength );
    gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

  // Y-Axis
    glLoadIdentity();
199
    glLoadName(2);
Jan Möbius's avatar
 
Jan Möbius committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(90, 1, 0, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
    glLoadIdentity();
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(90, 1, 0, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluDisk( quadric, 0, topRadius, slices, loops );
    glTranslatef( 0, 0, -arrowLength );
    gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

  // Z-Axis
    glLoadIdentity();
214
    glLoadName(3);
Jan Möbius's avatar
 
Jan Möbius committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(180, 0, 1, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
    glLoadIdentity();
    glMultMatrixd(modelview.get_raw_data());
    glRotatef(180, 0, 1, 0);
    glTranslatef( 0, 0, -bodyLength );
    gluDisk( quadric, 0, topRadius, slices, loops );
    glTranslatef( 0, 0, -arrowLength );
    gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );

    gluDeleteQuadric(quadric);

}


//============================================================================


void
CoordsysNode::
draw(GLState&  _state  , unsigned int /*_drawMode*/)
{
  // Push Modelview-Matrix
  _state.push_modelview_matrix();

  Vec4f lastBaseColor = _state.base_color();

  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
  glEnable(GL_LIGHTING);
  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
  glEnable(GL_COLOR_MATERIAL);
  glShadeModel(GL_SMOOTH);

  // Init state - changes when mode_ != POSITION
  Vec3d pos3D(0.0,0.0,0.0);

  if ( mode_ == SCREENPOS ) {

    int left, bottom, width, height;
    _state.get_viewport(left, bottom, width, height);

    // Projection reset
    _state.push_projection_matrix();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, _state.aspect(), 0.8, 20.0);

    glMatrixMode(GL_MODELVIEW);

    float posx = left + width - 30.0 ;
    float posy = bottom + height - 30.0 ;

    Vec3d screenposCenterPoint( posx , posy , 0.0);

    double pm[16], mvm[16];
    double x,y,z;
    GLint viewport[4];

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glGetDoublev( GL_MODELVIEW_MATRIX, mvm);
    glPopMatrix();
    glGetDoublev( GL_PROJECTION_MATRIX, pm);
281 282 283 284 285

    viewport[0] = left;
    viewport[1] = bottom;
    viewport[2] = width;
    viewport[3] = height;
Jan Möbius's avatar
 
Jan Möbius committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343


    gluUnProject(posx, posy, 0.5, mvm, pm, viewport, &x, &y, &z);

    pos3D = Vec3d(x, y, z);

    // Projection Translation
    glMatrixMode(GL_PROJECTION);
    // Translation in rechte obere Ecke
    glTranslatef(pos3D[0], pos3D[1], pos3D[2]-0.3);
    glMatrixMode(GL_MODELVIEW);

    // Koordinatensystem zeichnen
    drawCoordsys(_state);

    // Projection reload
    _state.pop_projection_matrix();
    glMatrixMode(GL_MODELVIEW);

  } else if (mode_ == POSITION) { /* mode_ == POSITION */

    drawCoordsys(_state);

  }

  glPopAttrib();

  glColor4fv(lastBaseColor.data());

  // Reload old configuration
  _state.pop_modelview_matrix();
}


void
CoordsysNode::
setMode(const CoordsysMode _mode)
{
  mode_ = _mode;
}

void
CoordsysNode::
setPosition(const Vec3f& _pos)
{
  pos3f_ = _pos;
}

CoordsysNode::CoordsysMode
CoordsysNode::
getMode() const
{
  return mode_;
}

void
CoordsysNode::pick(GLState& _state, PickTarget _target)
{
Jan Möbius's avatar
Jan Möbius committed
344 345
  if (_target == PICK_ANYTHING) {

Jan Möbius's avatar
 
Jan Möbius committed
346
    // Push Modelview-Matrix
Jan Möbius's avatar
Jan Möbius committed
347 348
    _state.push_modelview_matrix();
    glLoadName(0);
Jan Möbius's avatar
 
Jan Möbius committed
349 350

    // Init state - changes when mode_ != POSITION
Jan Möbius's avatar
Jan Möbius committed
351
    Vec3d pos3D(0.0,0.0,0.0);
Jan Möbius's avatar
 
Jan Möbius committed
352

Jan Möbius's avatar
Jan Möbius committed
353
    if ( mode_ == SCREENPOS ) {
Jan Möbius's avatar
 
Jan Möbius committed
354

Jan Möbius's avatar
Jan Möbius committed
355 356
      int left, bottom, width, height;
      _state.get_viewport(left, bottom, width, height);
Jan Möbius's avatar
 
Jan Möbius committed
357

Jan Möbius's avatar
Jan Möbius committed
358 359 360 361 362 363
      // Projection reset
      _state.push_projection_matrix();
      glMatrixMode(GL_PROJECTION);
      glPushMatrix ();
      glLoadIdentity();
      gluPerspective(45.0, _state.aspect(), 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
364

Jan Möbius's avatar
Jan Möbius committed
365
      glMatrixMode(GL_MODELVIEW);
Jan Möbius's avatar
 
Jan Möbius committed
366

Jan Möbius's avatar
Jan Möbius committed
367 368
      float posx = left + width - 30.0 ;
      float posy = bottom + height - 30.0 ;
Jan Möbius's avatar
 
Jan Möbius committed
369

Jan Möbius's avatar
Jan Möbius committed
370
      Vec3d screenposCenterPoint( posx , posy , 0.0);
Jan Möbius's avatar
 
Jan Möbius committed
371

Jan Möbius's avatar
Jan Möbius committed
372 373 374
      double pm[16], mvm[16];
      double x,y,z;
      GLint viewport[4];
Jan Möbius's avatar
 
Jan Möbius committed
375

Jan Möbius's avatar
Jan Möbius committed
376 377 378 379 380 381
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadIdentity();
      glGetDoublev( GL_MODELVIEW_MATRIX, mvm);
      glPopMatrix();
      glGetDoublev( GL_PROJECTION_MATRIX, pm);
Jan Möbius's avatar
 
Jan Möbius committed
382

383 384 385 386
      viewport[0] = left;
      viewport[1] = bottom;
      viewport[2] = width;
      viewport[3] = height;
Jan Möbius's avatar
 
Jan Möbius committed
387

Jan Möbius's avatar
Jan Möbius committed
388
      gluUnProject(posx, posy, 0.5, mvm, pm, viewport, &x, &y, &z);
Jan Möbius's avatar
 
Jan Möbius committed
389

Jan Möbius's avatar
Jan Möbius committed
390
      pos3D = Vec3d(x, y, z);
Jan Möbius's avatar
 
Jan Möbius committed
391

Jan Möbius's avatar
Jan Möbius committed
392 393
      // Projection Translation
      glMatrixMode(GL_PROJECTION);
Jan Möbius's avatar
 
Jan Möbius committed
394

Jan Möbius's avatar
Jan Möbius committed
395 396 397 398 399 400 401 402
      // go back to gluPickMatrix
      glPopMatrix ();
      glMultMatrixd( _state.inverse_projection().get_raw_data());
      // add our matrix
      gluPerspective(45.0, _state.aspect(), 0.8, 20.0);
      // Translation in rechte obere Ecke
      glTranslatef(pos3D[0], pos3D[1], pos3D[2]-0.3);
      glMatrixMode(GL_MODELVIEW);
Jan Möbius's avatar
 
Jan Möbius committed
403

Jan Möbius's avatar
Jan Möbius committed
404 405 406 407 408 409 410 411 412
      // Koordinatensystem zeichnen
      drawCoordsysPick(_state);

      // Projection reload
      _state.pop_projection_matrix();

    } else if (mode_ == POSITION) { /* mode_ == POSITION */

      drawCoordsysPick(_state);
Jan Möbius's avatar
 
Jan Möbius committed
413 414

    }
Jan Möbius's avatar
Jan Möbius committed
415 416 417 418
    // Reload old configuration
    _state.pop_modelview_matrix();

  }
Jan Möbius's avatar
 
Jan Möbius committed
419 420 421 422 423 424
}

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