CoordsysNode.cc 21.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
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
 *                           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




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

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

#include "CoordsysNode.hh"
55 56
#include <ACG/GL/gl.hh>

Jan Möbius's avatar
 
Jan Möbius committed
57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <math.h>


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

namespace ACG {
namespace SceneGraph {


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


70 71 72 73 74 75 76 77 78 79 80 81
CoordsysNode::CoordsysNode(BaseNode* _parent, std::string _name, CoordsysMode _mode, ProjectionMode _projectionMode) :
        BaseNode(_parent, _name),
        mode_(_mode),
        projectionMode_(_projectionMode)
{
  const double bodyRadius = 0.004;
  const double topRadius = 0.01;
  const int slices = 10;
  const int stacks = 10;

  sphere_   = new ACG::GLSphere(slices,stacks);
  cylinder_ = new ACG::GLCylinder(slices, stacks, bodyRadius,false,false);
Jan Möbius's avatar
Jan Möbius committed
82
  cone_     = new ACG::GLCone(slices, stacks, 0, topRadius , false,true);
Jan Möbius's avatar
Jan Möbius committed
83
  disk_     = new ACG::GLDisk(slices, 10, 0.0f, bodyRadius);
84 85
}

Jan Möbius's avatar
Jan Möbius committed
86 87 88 89 90 91 92 93 94 95 96 97 98
CoordsysNode::~CoordsysNode() {
  if (sphere_)
    delete sphere_;

  if (cylinder_)
    delete cylinder_;

  if (cone_)
    delete cone_;

  if (disk_)
    delete disk_;
}
99

Jan Möbius's avatar
 
Jan Möbius committed
100 101
void
CoordsysNode::
102
boundingBox(Vec3d& /*_bbMin*/, Vec3d& /*_bbMax*/)
Jan Möbius's avatar
 
Jan Möbius committed
103 104 105 106 107 108 109 110
{
	//_bbMin.minimize( Vect3f  )
}


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


Jan Möbius's avatar
Jan Möbius committed
111
DrawModes::DrawMode
Jan Möbius's avatar
 
Jan Möbius committed
112
CoordsysNode::
Jan Möbius's avatar
Jan Möbius committed
113
availableDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
114 115 116 117 118 119 120 121 122 123 124 125 126
{
  return ( DrawModes::POINTS |
	        DrawModes::POINTS_SHADED |
	        DrawModes::POINTS_COLORED );
}


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

void
CoordsysNode::
drawCoordsys( GLState&  _state) {

127 128 129
  const double arrowLength  = 0.03;
  const double bodyLength   = 0.06;
  const double sphereRadius = 0.01;
Jan Möbius's avatar
 
Jan Möbius committed
130

131 132 133

  ACG::Vec4f matCol(0.5f, 0.5f, 0.5f, 1.0f);

Jan Möbius's avatar
 
Jan Möbius committed
134
  // Origin
135 136 137
  glColor4f(0.5, 0.5, 0.5 , 1.0);
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
138
  sphere_->draw(_state,sphereRadius);
139
  
Jan Möbius's avatar
 
Jan Möbius committed
140
  // X-Axis
141 142 143 144
  glColor4f(1.0, 0.0, 0.0, 1.0);
  matCol[0] = 1.0f;  matCol[1] = 0.0f; matCol[2] = 0.0f;
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
145 146 147
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
148
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
149
  _state.translate ( 0, 0, -arrowLength );
150
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
151
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
152 153

  // Y-Axis
154 155 156 157
  glColor4f(0.0, 1.0, 0.0, 1.0);
  matCol[0] = 0.0f;  matCol[1] = 1.0f; matCol[2] = 0.0f;
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.2f).data());
  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
158 159 160
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
161
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
162
  _state.translate ( 0, 0, -arrowLength );
163
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
164
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
165 166

  // Z-Axis
167 168 169 170
  glColor4f(0.0, 0.0, 1.0, 1.0);
  matCol[0] = 0.0f;  matCol[1] = 0.0f; matCol[2] = 1.0f;
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
171 172 173
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
174
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
175
  _state.translate ( 0, 0, -arrowLength );
176
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
177
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
178 179 180 181 182 183 184 185

}

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

void
CoordsysNode::drawCoordsysPick( GLState&  _state) {

186 187 188
  const double arrowLength  = 0.03;
  const double bodyLength   = 0.06;
  const double sphereRadius = 0.01;
Jan Möbius's avatar
 
Jan Möbius committed
189 190

  // Origin
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
191
  _state.pick_set_name (1);
192
  sphere_->draw(_state,sphereRadius);
Jan Möbius's avatar
 
Jan Möbius committed
193 194

  // X-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
195 196 197 198
  _state.pick_set_name (2);
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
199
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
200
  _state.translate ( 0, 0, -arrowLength );
201
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
202
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
203

204

Jan Möbius's avatar
 
Jan Möbius committed
205
  // Y-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
206 207 208 209
  _state.pick_set_name (3);
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
210
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
211
  _state.translate ( 0, 0, -arrowLength );
212
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
213
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
214 215

  // Z-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
216 217 218 219
  _state.pick_set_name (4);
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
220
  cylinder_->draw(_state,bodyLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
221
  _state.translate ( 0, 0, -arrowLength );
222
  cone_->draw(_state,arrowLength);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
223 224
  _state.pop_modelview_matrix ();

Jan Möbius's avatar
 
Jan Möbius committed
225 226 227 228 229 230 231 232
}


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


void
CoordsysNode::
233
draw(GLState&  _state  , const DrawModes::DrawMode& /*_drawMode*/)
Jan Möbius's avatar
 
Jan Möbius committed
234
{
235 236
  GLenum prev_depth = _state.depthFunc();
    
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
237 238 239
  GLboolean colorMask[4];
  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);
  
Jan Möbius's avatar
 
Jan Möbius committed
240 241 242 243 244 245
  // Push Modelview-Matrix
  _state.push_modelview_matrix();

  Vec4f lastBaseColor = _state.base_color();

  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
246
  ACG::GLState::enable(GL_LIGHTING);
Jan Möbius's avatar
 
Jan Möbius committed
247
  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
248 249
  ACG::GLState::enable(GL_COLOR_MATERIAL);
  ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
 
Jan Möbius committed
250

251 252 253 254
  GLfloat zeroVec[4] = {0.0f};
  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zeroVec);
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zeroVec);

Jan Möbius's avatar
 
Jan Möbius committed
255 256 257 258 259 260
  // Init state - changes when mode_ != POSITION
  Vec3d pos3D(0.0,0.0,0.0);

  if ( mode_ == SCREENPOS ) {

    int left, bottom, width, height;
261
    double aspect = _state.aspect();
262

Jan Möbius's avatar
 
Jan Möbius committed
263 264 265 266
    _state.get_viewport(left, bottom, width, height);

    // Projection reset
    _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
267
    _state.reset_projection();
Jan Möbius's avatar
 
Jan Möbius committed
268

269 270 271
    if (projectionMode_ == PERSPECTIVE_PROJECTION)
        _state.perspective(45.0, aspect, 0.8, 20.0);
    else
272
        _state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
273

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
274 275
    _state.push_modelview_matrix();
    _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
276

277 278 279 280 281 282
    float rel_size = 50.0;
    float projdist = sqrt ( (width*height) / rel_size );

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

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
287
    // reset scene translation
288
    // we want only the scene rotation to rotate the coordsys
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
289
    GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
290

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
291 292 293
    modelview(0,3) = 0.0;
    modelview(1,3) = 0.0;
    modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
294

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
295
    _state.set_modelview (modelview);
296
    _state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
297

298 299

    // clear the depth buffer behind the coordsys
Jan Möbius's avatar
Jan Möbius committed
300
    ACG::GLState::depthRange (1.0, 1.0);
301
    ACG::GLState::depthFunc (GL_ALWAYS);
302 303

    drawCoordsys(_state);
304

Jan Möbius's avatar
Jan Möbius committed
305
    ACG::GLState::depthRange (0.0, 1.0);
306
    ACG::GLState::depthFunc (GL_LESS);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
307

308
    // draw coordsys
Jan Möbius's avatar
 
Jan Möbius committed
309 310
    drawCoordsys(_state);

311
    // set depth buffer to 0 so that nothing can paint over cordsys
Jan Möbius's avatar
Jan Möbius committed
312
    ACG::GLState::depthRange (0.0, 0.0);
313
    ACG::GLState::depthFunc (GL_ALWAYS);
314
    glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
315

316 317 318
    // Koordinatensystem zeichnen
    drawCoordsys(_state);

Jan Möbius's avatar
Jan Möbius committed
319
    ACG::GLState::depthRange (0.0, 1.0);
320
    ACG::GLState::depthFunc (prev_depth);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
321
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
322

Jan Möbius's avatar
 
Jan Möbius committed
323 324
    // Projection reload
    _state.pop_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
325

Jan Möbius's avatar
 
Jan Möbius committed
326 327 328

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
329 330 331 332 333 334 335
    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
336

337
    // clear depth buffer in coordsys region
Jan Möbius's avatar
Jan Möbius committed
338
    ACG::GLState::depthRange (1.0, 1.0);
339
    ACG::GLState::depthFunc (GL_ALWAYS);
340 341 342 343 344

    // Koordinatensystem zeichnen
    drawCoordsys(_state);

    // draw coordsys in normal mode
Jan Möbius's avatar
Jan Möbius committed
345
    ACG::GLState::depthRange (0.0, 1.0);
346
    ACG::GLState::depthFunc (GL_LESS);
347

348
    // Koordinatensystem zeichnen
Jan Möbius's avatar
 
Jan Möbius committed
349 350
    drawCoordsys(_state);

351
    // set depth buffer to 0 so that nothing can paint over cordsys
Jan Möbius's avatar
Jan Möbius committed
352
    ACG::GLState::depthRange (0.0, 0.0);
353
    ACG::GLState::depthFunc (GL_ALWAYS);
354 355 356 357
    glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

    // Koordinatensystem zeichnen
    drawCoordsys(_state);
358

359
    // reset to default
Jan Möbius's avatar
Jan Möbius committed
360
    ACG::GLState::depthRange (0.0, 1.0);
361
    ACG::GLState::depthFunc (prev_depth);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
362
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
Jan Möbius's avatar
 
Jan Möbius committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  }

  glPopAttrib();

  glColor4fv(lastBaseColor.data());

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


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

381 382 383 384 385 386 387
void
CoordsysNode::
setProjectionMode(const ProjectionMode _mode)
{
  projectionMode_ = _mode;
}

Jan Möbius's avatar
 
Jan Möbius committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401
void
CoordsysNode::
setPosition(const Vec3f& _pos)
{
  pos3f_ = _pos;
}

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

402 403 404 405 406 407 408
CoordsysNode::ProjectionMode
CoordsysNode::
getProjectionMode() const
{
  return projectionMode_;
}

Jan Möbius's avatar
 
Jan Möbius committed
409 410 411
void
CoordsysNode::pick(GLState& _state, PickTarget _target)
{
412 413
  GLenum prev_depth = _state.depthFunc();  
    
Jan Möbius's avatar
Jan Möbius committed
414 415
  if (_target == PICK_ANYTHING) {

Jan Möbius's avatar
Jan Möbius committed
416 417
    GLdouble mat[16];

Jan Möbius's avatar
 
Jan Möbius committed
418
    // Push Modelview-Matrix
Jan Möbius's avatar
Jan Möbius committed
419
    _state.push_modelview_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
420
    _state.pick_set_maximum (5);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
421
    _state.pick_set_name (0);
Jan Möbius's avatar
 
Jan Möbius committed
422 423

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

Jan Möbius's avatar
Jan Möbius committed
426
    if ( mode_ == SCREENPOS ) {
Jan Möbius's avatar
 
Jan Möbius committed
427

Jan Möbius's avatar
Jan Möbius committed
428
      int left, bottom, width, height;
429
      double aspect = _state.aspect();
430

Jan Möbius's avatar
Jan Möbius committed
431
      _state.get_viewport(left, bottom, width, height);
432
      
Jan Möbius's avatar
Jan Möbius committed
433 434
      // Projection reset
      _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
435
      _state.reset_projection();
Jan Möbius's avatar
 
Jan Möbius committed
436

437 438 439
      if (projectionMode_ == PERSPECTIVE_PROJECTION)
        _state.perspective(45.0, aspect, 0.8, 20.0);
      else
440
        _state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
441

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
442 443
      _state.push_modelview_matrix();
      _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
444

445 446 447 448 449 450 451
      float rel_size = 50.0;
      float projdist = sqrt ( (width*height) / rel_size );

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

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
455 456
      // reset scene translation
      GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
457

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
458 459 460
      modelview(0,3) = 0.0;
      modelview(1,3) = 0.0;
      modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
461

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
462
      _state.set_modelview (modelview);
463
      _state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
      // 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
Jan Möbius's avatar
Jan Möbius committed
481
        ACG::GLState::depthRange (1.0, 1.0);
482
        ACG::GLState::depthFunc (GL_ALWAYS);
483 484 485 486 487

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // draw coordsys in normal mode
Jan Möbius's avatar
Jan Möbius committed
488
        ACG::GLState::depthRange (0.0, 1.0);
489
        ACG::GLState::depthFunc (GL_LESS);
490 491 492 493 494

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // set depth buffer to 0 so tah nothing can paint over cordsys
Jan Möbius's avatar
Jan Möbius committed
495
        ACG::GLState::depthRange (0.0, 0.0);
496
        ACG::GLState::depthFunc (GL_ALWAYS);
497 498 499 500 501 502
        glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // reset to default
Jan Möbius's avatar
Jan Möbius committed
503
        ACG::GLState::depthRange (0.0, 1.0);
504
        ACG::GLState::depthFunc (prev_depth);
505 506
        glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
      }
Jan Möbius's avatar
Jan Möbius committed
507

508 509
      // Projection reload
      _state.pop_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
510

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

513 514 515
      // 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
516 517 518 519 520 521 522 523 524 525 526 527 528
      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
529 530 531 532 533 534
      GLMatrixd modelview = _state.modelview();

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

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      // 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
Jan Möbius's avatar
Jan Möbius committed
551
        ACG::GLState::depthRange (1.0, 1.0);
552
        ACG::GLState::depthFunc (GL_ALWAYS);
553 554 555 556 557

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // draw coordsys in normal mode
Jan Möbius's avatar
Jan Möbius committed
558
        ACG::GLState::depthRange (0.0, 1.0);
559
        ACG::GLState::depthFunc (GL_LESS);
560 561 562 563 564

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // set depth buffer to 0 so tah nothing can paint over cordsys
Jan Möbius's avatar
Jan Möbius committed
565
        ACG::GLState::depthRange (0.0, 0.0);
566
        ACG::GLState::depthFunc (GL_ALWAYS);
567 568 569 570 571 572
        glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);

        // Koordinatensystem zeichnen
        drawCoordsys(_state);

        // reset to default
Jan Möbius's avatar
Jan Möbius committed
573
        ACG::GLState::depthRange (0.0, 1.0);
574
        ACG::GLState::depthFunc (prev_depth);
575 576
        glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
      }
Jan Möbius's avatar
 
Jan Möbius committed
577
    }
Jan Möbius's avatar
Jan Möbius committed
578 579 580 581
    // Reload old configuration
    _state.pop_modelview_matrix();

  }
Jan Möbius's avatar
 
Jan Möbius committed
582 583
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
584 585
//----------------------------------------------------------------------------

586
void CoordsysNode::clearPickArea(GLState&  _state, bool _draw, GLfloat _depth)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
587
{
588 589
  GLenum prev_depth = _state.depthFunc();
    
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
590 591 592 593 594 595 596
  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
597 598 599
  GLboolean colorMask[4];
  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  // 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
619

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
620 621 622 623
  _state.ortho (left, left + width, bottom, bottom + height, 0.0, 1.0);

  _state.push_modelview_matrix();
  _state.reset_modelview();
624
  ACG::GLState::depthFunc (GL_ALWAYS);
Jan Möbius's avatar
Jan Möbius committed
625
  ACG::GLState::depthRange (_depth, _depth);
626
  _state.translate (center[0], center[1], -0.5);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
627

628
  if (_draw)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
629 630 631 632 633
    _state.pick_set_name (0);
  else
    glColorMask(false, false, false, false);

  // 10% more to ensure everything is in
Jan Möbius's avatar
Jan Möbius committed
634 635 636
  disk_->setInnerRadius(0.0f);
  disk_->setOuterRadius(radius * 1.1f);
  disk_->draw(_state);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
637

638
  ACG::GLState::depthFunc (prev_depth);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
639 640 641
  _state.pop_modelview_matrix();
  _state.pop_projection_matrix();

Jan Möbius's avatar
Jan Möbius committed
642
  ACG::GLState::depthRange (0.0, 1.0);
643 644

  if (!_draw)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
645
    glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
646 647 648 649 650 651
}

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

void CoordsysNode::boundingCircle(std::vector<Vec2f> &_in, Vec2f &_center, float &_radius)
{
Jan Möbius's avatar
Jan Möbius committed
652
  if (_in.empty())
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
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 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    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
743 744 745 746
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================