CoordsysNode.cc 20 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 59 60 61 62 63 64 65 66 67 68 69 70




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

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

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


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

namespace ACG {
namespace SceneGraph {


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


void
CoordsysNode::
71
boundingBox(Vec3f& /*_bbMin*/, Vec3f& /*_bbMax*/)
Jan Möbius's avatar
 
Jan Möbius committed
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
{
	//_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();

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

  // X-Axis
  glColor3f(1.0, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
113 114 115
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
116 117
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
118
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
119
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
120
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
121 122 123

  // Y-Axis
  glColor3f(0.0, 1.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
124 125 126
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
127 128
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
129
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
130
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
131
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
132 133 134

  // Z-Axis
  glColor3f(0.0, 0.0, 1.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
135 136 137
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
138 139
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
140
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
141
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
142
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
143 144 145 146 147 148 149 150 151

  gluDeleteQuadric(quadric);
}

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

void
CoordsysNode::drawCoordsysPick( GLState&  _state) {

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
152 153 154 155 156 157 158 159
  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;
Jan Möbius's avatar
 
Jan Möbius committed
160

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
161
  GLUquadricObj *quadric = gluNewQuadric();
Jan Möbius's avatar
 
Jan Möbius committed
162 163

  // Origin
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
164 165
  _state.pick_set_name (1);
  gluSphere( quadric, sphereRadius, slices, stacks );
Jan Möbius's avatar
 
Jan Möbius committed
166 167

  // X-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
168 169 170 171 172 173 174 175 176
  _state.pick_set_name (2);
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
177 178

  // Y-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
179 180 181 182 183 184 185 186 187
  _state.pick_set_name (3);
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
188 189

  // Z-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
190 191 192 193 194 195 196 197 198 199 200 201
  _state.pick_set_name (4);
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();


  gluDeleteQuadric(quadric);
Jan Möbius's avatar
 
Jan Möbius committed
202 203 204 205 206 207 208 209 210 211 212

}


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


void
CoordsysNode::
draw(GLState&  _state  , unsigned int /*_drawMode*/)
{
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
213 214 215
  GLboolean colorMask[4];
  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);
  
Jan Möbius's avatar
 
Jan Möbius committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  // 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;
233 234
    double aspect = 1.0;

Jan Möbius's avatar
 
Jan Möbius committed
235 236
    _state.get_viewport(left, bottom, width, height);

237
    if (width && height)
238
      aspect = (double)width / (double)height;
239

Jan Möbius's avatar
 
Jan Möbius committed
240 241
    // Projection reset
    _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
242
    _state.reset_projection();
243
    _state.perspective(45.0, aspect, 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
244 245 246 247 248 249

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

    Vec3d screenposCenterPoint( posx , posy , 0.0);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
250 251
    _state.push_modelview_matrix();
    _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
252

253
    // get our desired coordsys position in scene coordinates
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
254 255
    pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
    _state.pop_modelview_matrix();
Jan Möbius's avatar
 
Jan Möbius committed
256

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
257
    // reset scene translation
258
    // we want only the scene rotation to rotate the coordsys
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
259
    GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
260

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
261 262 263
    modelview(0,3) = 0.0;
    modelview(1,3) = 0.0;
    modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
264

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
265 266
    _state.set_modelview (modelview);
    _state.translate (pos3D[0], pos3D[1], pos3D[2]-0.3, MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
267

268 269

    // clear the depth buffer behind the coordsys
270 271 272 273
    glDepthRange (1.0, 1.0);
    glDepthFunc (GL_ALWAYS);

    drawCoordsys(_state);
274

275 276
    glDepthRange (0.0, 1.0);
    glDepthFunc (GL_LESS);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
277

278
    // draw coordsys
Jan Möbius's avatar
 
Jan Möbius committed
279 280
    drawCoordsys(_state);

281 282 283 284
    // set depth buffer to 0 so tah nothing can paint over cordsys
    glDepthRange (0.0, 0.0);
    glDepthFunc (GL_ALWAYS);
    glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
285

286 287 288 289 290
    // Koordinatensystem zeichnen
    drawCoordsys(_state);

    glDepthRange (0.0, 1.0);
    glDepthFunc (GL_LESS);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
291
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
292

Jan Möbius's avatar
 
Jan Möbius committed
293 294
    // Projection reload
    _state.pop_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
295

Jan Möbius's avatar
 
Jan Möbius committed
296 297 298

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
299 300 301 302 303 304 305
    GLMatrixd modelview = _state.modelview();

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

    _state.set_modelview (modelview);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
306

307 308 309 310 311 312 313 314 315 316
    // clear depth buffer in coordsys region
    glDepthRange (1.0, 1.0);
    glDepthFunc (GL_ALWAYS);

    // Koordinatensystem zeichnen
    drawCoordsys(_state);

    // draw coordsys in normal mode
    glDepthRange (0.0, 1.0);
    glDepthFunc (GL_LESS);
317

318
    // Koordinatensystem zeichnen
Jan Möbius's avatar
 
Jan Möbius committed
319 320
    drawCoordsys(_state);

321 322 323 324 325 326 327
    // set depth buffer to 0 so tah nothing can paint over cordsys
    glDepthRange (0.0, 0.0);
    glDepthFunc (GL_ALWAYS);
    glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

    // Koordinatensystem zeichnen
    drawCoordsys(_state);
328

329 330 331
    // reset to default
    glDepthRange (0.0, 1.0);
    glDepthFunc (GL_LESS);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
332
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
333

Jan Möbius's avatar
 
Jan Möbius committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  }

  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
369 370
  if (_target == PICK_ANYTHING) {

Jan Möbius's avatar
Jan Möbius committed
371 372
    GLdouble mat[16];

Jan Möbius's avatar
 
Jan Möbius committed
373
    // Push Modelview-Matrix
Jan Möbius's avatar
Jan Möbius committed
374
    _state.push_modelview_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
375
    _state.pick_set_maximum (5);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
376
    _state.pick_set_name (0);
Jan Möbius's avatar
 
Jan Möbius committed
377 378

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

Jan Möbius's avatar
Jan Möbius committed
381
    if ( mode_ == SCREENPOS ) {
Jan Möbius's avatar
 
Jan Möbius committed
382

Jan Möbius's avatar
Jan Möbius committed
383
      int left, bottom, width, height;
384 385
      double aspect = 1.0;

Jan Möbius's avatar
Jan Möbius committed
386
      _state.get_viewport(left, bottom, width, height);
Jan Möbius's avatar
 
Jan Möbius committed
387

388
      if (width && height)
389
        aspect = (double)width / (double)height;
390

Jan Möbius's avatar
Jan Möbius committed
391 392
      // Projection reset
      _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
393
      _state.reset_projection();
394
      _state.perspective(45.0, aspect, 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
395

Jan Möbius's avatar
Jan Möbius committed
396 397
      float posx = left + width - 30.0 ;
      float posy = bottom + height - 30.0 ;
Jan Möbius's avatar
 
Jan Möbius committed
398

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
401 402
      _state.push_modelview_matrix();
      _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
403

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
404 405
      pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
      _state.pop_modelview_matrix();
Jan Möbius's avatar
 
Jan Möbius committed
406

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
407 408
      // reset scene translation
      GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
409

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
410 411 412
      modelview(0,3) = 0.0;
      modelview(1,3) = 0.0;
      modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
413

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
414 415
      _state.set_modelview (modelview);
      _state.translate (pos3D[0], pos3D[1], pos3D[2]-0.3, MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
      // We don't have access to the pick matrix used during selection buffer picking
      // so we can't draw our pick area circle in this case
      if (_state.color_picking ())
      {
        // clear depth buffer behind coordsys node
        clearPickArea(_state, true, 1.0);

        // Koordinatensystem zeichnen
        drawCoordsysPick(_state);

        // set depth buffer to 0.0 so that nothing can paint above
        clearPickArea(_state, false, 0.0);
      }
      else
      {
        // clear depth buffer in coordsys region
        glDepthRange (1.0, 1.0);
        glDepthFunc (GL_ALWAYS);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // draw coordsys in normal mode
        glDepthRange (0.0, 1.0);
        glDepthFunc (GL_LESS);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // set depth buffer to 0 so tah nothing can paint over cordsys
        glDepthRange (0.0, 0.0);
        glDepthFunc (GL_ALWAYS);
        glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // reset to default
        glDepthRange (0.0, 1.0);
        glDepthFunc (GL_LESS);
        glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
      }
Jan Möbius's avatar
Jan Möbius committed
459

460 461
      // Projection reload
      _state.pop_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
462

Jan Möbius's avatar
Jan Möbius committed
463 464
    } else if (mode_ == POSITION) { /* mode_ == POSITION */

465 466 467
      // The selection buffer picking method might have set a 
      // pick matrix that has been multiplied with the projection matrix.
      // This is the only way to get the gl pick matrix again
Jan Möbius's avatar
Jan Möbius committed
468 469 470 471 472 473 474 475 476 477 478 479 480
      glMatrixMode(GL_PROJECTION);

      glPushMatrix ();
      glMultMatrixd( _state.inverse_projection().get_raw_data());

      glGetDoublev(GL_PROJECTION_MATRIX, mat);

      glPopMatrix ();

      GLMatrixd pickMat (mat);

      glMatrixMode(GL_MODELVIEW);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
481 482 483 484 485 486
      GLMatrixd modelview = _state.modelview();

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

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
      // We don't have access to the pick matrix used during selection buffer picking
      // so we can't draw our pick area circle in this case
      if (_state.color_picking ())
      {
        // clear depth buffer behind coordsys node
        clearPickArea(_state, true, 1.0);

        // Koordinatensystem zeichnen
        drawCoordsysPick(_state);

        // set depth buffer to 0.0 so that nothing can paint above
        clearPickArea(_state, false, 0.0);
      }
      else
      {
        // clear depth buffer in coordsys region
        glDepthRange (1.0, 1.0);
        glDepthFunc (GL_ALWAYS);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // draw coordsys in normal mode
        glDepthRange (0.0, 1.0);
        glDepthFunc (GL_LESS);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // set depth buffer to 0 so tah nothing can paint over cordsys
        glDepthRange (0.0, 0.0);
        glDepthFunc (GL_ALWAYS);
        glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // reset to default
        glDepthRange (0.0, 1.0);
        glDepthFunc (GL_LESS);
        glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
      }
Jan Möbius's avatar
 
Jan Möbius committed
529
    }
Jan Möbius's avatar
Jan Möbius committed
530 531 532 533
    // Reload old configuration
    _state.pop_modelview_matrix();

  }
Jan Möbius's avatar
 
Jan Möbius committed
534 535
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
536 537
//----------------------------------------------------------------------------

538
void CoordsysNode::clearPickArea(GLState&  _state, bool _draw, GLfloat _depth)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
539 540 541 542 543 544 545 546
{
  std::vector<Vec2f> points;
  Vec2f center;
  float radius;

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
547 548 549
  GLboolean colorMask[4];
  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  GLUquadricObj *quadric = gluNewQuadric();

  // respect sphere radius
  Vec3d proj = _state.project (Vec3d (-0.01, -0.01, -0.01));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.1, 0.0, 0.0));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.0, 0.1, 0.0));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.0, 0.0, 0.1));
  points.push_back (Vec2f (proj[0], proj[1]));


  // get bounding circle of projected 4 points of the coord node
  boundingCircle(points, center, radius);

  _state.push_projection_matrix();
  _state.reset_projection();
Jan Möbius's avatar
Jan Möbius committed
571

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
572 573 574 575 576
  _state.ortho (left, left + width, bottom, bottom + height, 0.0, 1.0);

  _state.push_modelview_matrix();
  _state.reset_modelview();
  glDepthFunc (GL_ALWAYS);
577 578
  glDepthRange (_depth, _depth);
  _state.translate (center[0], center[1], -0.5);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
579

580
  if (_draw)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
581 582 583 584 585 586 587 588 589 590 591
    _state.pick_set_name (0);
  else
    glColorMask(false, false, false, false);

  // 10% more to ensure everything is in
  gluDisk( quadric, 0, radius * 1.1, 10, 10 );

  glDepthFunc (GL_LESS);
  _state.pop_modelview_matrix();
  _state.pop_projection_matrix();

592 593 594
  glDepthRange (0.0, 1.0);

  if (!_draw)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
595
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
596

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  gluDeleteQuadric(quadric);
}

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

void CoordsysNode::boundingCircle(std::vector<Vec2f> &_in, Vec2f &_center, float &_radius)
{
  if (_in.size () == 0)
    return;
  if (_in.size () < 2)
  {
    _center = _in[0];
    _radius = 0.0f;
    return;
  }
  bool found = false;

  // try all circumcircles of all possible lines
  for (unsigned int i = 0; i < _in.size () - 1; i++)
    for (unsigned int j = i + 1; j < _in.size (); j++)
    {
      Vec2f cen = (_in[i] + _in[j]) * 0.5;
      float rad = (_in[i] - cen).length ();
      bool allin = true;

      for (unsigned int k = 0; k < _in.size (); k++)
        if (k != i && k != j && (_in[k] - cen).length () > rad)
        {
          allin = false;
          break;
        }

      if (!allin)
        continue;

      if (found)
      {
        if (rad < _radius)
        {
          _center = cen;
          _radius = rad;
        }
      }
      else
      {
        found = true;
        _center = cen;
        _radius = rad;
      }
    }

  if (found)
    return;

  // try all circumcircles of all possible triangles
  for (unsigned int i = 0; i < _in.size () - 2; i++)
    for (unsigned int j = i + 1; j < _in.size () - 1; j++)
      for (unsigned int k = j + 1; k < _in.size (); k++)
      {
        float v = ((_in[k][0]-_in[j][0])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
                  ((_in[i][0]-_in[k][0])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
                  ((_in[j][0]-_in[i][0])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
        float u = ((_in[j][1]-_in[k][1])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
                  ((_in[k][1]-_in[i][1])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
                  ((_in[i][1]-_in[j][1])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
        float d = (_in[i][0]*_in[j][1])+(_in[j][0]*_in[k][1])+(_in[k][0]*_in[i][1]) -
                  (_in[i][0]*_in[k][1])-(_in[j][0]*_in[i][1])-(_in[k][0]*_in[j][1]);
        Vec2f cen(0.5 * (u/d), 0.5 * (v/d));
        float rad = (_in[i] - cen).length ();
        bool allin = true;

        for (unsigned int l = 0; l < _in.size (); l++)
          if (l != i && l != j && l != k && (_in[l] - cen).length () > rad)
          {
            allin = false;
            break;
          }

        if (!allin)
          continue;

        if (found)
        {
          if (rad < _radius)
          {
            _center = cen;
            _radius = rad;
          }
        }
        else
        {
          found = true;
          _center = cen;
          _radius = rad;
        }
      }
}

Jan Möbius's avatar
 
Jan Möbius committed
695 696 697 698
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================