Developer Documentation
CoordsysNode.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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS CoordsysNode - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 //== INCLUDES =================================================================
60 
61 #include "CoordsysNode.hh"
62 #include <ACG/GL/IRenderer.hh>
63 
64 
65 //== NAMESPACES ===============================================================
66 
67 namespace ACG {
68 namespace SceneGraph {
69 
70 
71 //== IMPLEMENTATION ==========================================================
72 
73 
74 CoordsysNode::CoordsysNode(BaseNode* _parent, std::string _name, CoordsysMode _mode, ProjectionMode _projectionMode) :
75  BaseNode(_parent, _name),
76  mode_(_mode),
77  projectionMode_(_projectionMode)
78 {
79  const double bodyRadius = 0.004;
80  const double topRadius = 0.01;
81  const int slices = 10;
82  const int stacks = 10;
83 
84  sphere_ = new ACG::GLSphere(slices,stacks);
85  cylinder_ = new ACG::GLCylinder(slices, stacks, bodyRadius,false,false);
86  cone_ = new ACG::GLCone(slices, stacks, 0, topRadius , false,true);
87  disk_ = new ACG::GLDisk(slices, 10, 0.0f, bodyRadius);
88 }
89 
91  if (sphere_)
92  delete sphere_;
93 
94  if (cylinder_)
95  delete cylinder_;
96 
97  if (cone_)
98  delete cone_;
99 
100  if (disk_)
101  delete disk_;
102 }
103 
104 void
106 boundingBox(Vec3d& /*_bbMin*/, Vec3d& /*_bbMax*/)
107 {
108  //_bbMin.minimize( Vect3f )
109 }
110 
111 
112 //----------------------------------------------------------------------------
113 
114 
118 {
119  return ( DrawModes::POINTS |
122 }
123 
124 
125 //----------------------------------------------------------------------------
126 
127 void
128 CoordsysNode::
129 drawCoordsys( GLState& _state) {
130 
131  const double arrowLength = 0.03;
132  const double bodyLength = 0.06;
133  const double sphereRadius = 0.01;
134 
135 
136  ACG::Vec4f matCol(0.5f, 0.5f, 0.5f, 1.0f);
137 
138  // Origin
139  glColor4f(0.5, 0.5, 0.5 , 1.0);
140  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
141  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
142  sphere_->draw(_state,sphereRadius);
143 
144  // X-Axis
145  glColor4f(1.0, 0.0, 0.0, 1.0);
146  matCol[0] = 1.0f; matCol[1] = 0.0f; matCol[2] = 0.0f;
147  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
148  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
149  _state.push_modelview_matrix ();
150  _state.rotate (-90, 0, 1, 0);
151  _state.translate ( 0, 0, -bodyLength );
152  cylinder_->draw(_state,bodyLength);
153  _state.translate ( 0, 0, -arrowLength );
154  cone_->draw(_state,arrowLength);
155  _state.pop_modelview_matrix ();
156 
157  // Y-Axis
158  glColor4f(0.0, 1.0, 0.0, 1.0);
159  matCol[0] = 0.0f; matCol[1] = 1.0f; matCol[2] = 0.0f;
160  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.2f).data());
161  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
162  _state.push_modelview_matrix ();
163  _state.rotate (90, 1, 0, 0);
164  _state.translate ( 0, 0, -bodyLength );
165  cylinder_->draw(_state,bodyLength);
166  _state.translate ( 0, 0, -arrowLength );
167  cone_->draw(_state,arrowLength);
168  _state.pop_modelview_matrix ();
169 
170  // Z-Axis
171  glColor4f(0.0, 0.0, 1.0, 1.0);
172  matCol[0] = 0.0f; matCol[1] = 0.0f; matCol[2] = 1.0f;
173  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (matCol * 0.5f).data());
174  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matCol.data());
175  _state.push_modelview_matrix ();
176  _state.rotate (180, 0, 1, 0);
177  _state.translate ( 0, 0, -bodyLength );
178  cylinder_->draw(_state,bodyLength);
179  _state.translate ( 0, 0, -arrowLength );
180  cone_->draw(_state,arrowLength);
181  _state.pop_modelview_matrix ();
182 
183 }
184 
185 void CoordsysNode::drawCoordsys(IRenderer* _renderer, RenderObject* _baseRO)
186 {
187  // save model view matrix
188  const GLMatrixd mModelView = _baseRO->modelview;
189 
190 
191  const double arrowLength = 0.03;
192  const double bodyLength = 0.06;
193  const double sphereRadius = 0.01;
194 
195 
196  // Origin
197  _baseRO->debugName = "coordsys.sphere";
198  _baseRO->emissive = Vec3f(0.4f, 0.4f, 0.4f);
199  _baseRO->diffuse = Vec3f(0.3f, 0.3f, 0.3f);
200  _baseRO->specular = Vec3f(0.2f, 0.2f, 0.2f);
201  _baseRO->ambient = Vec3f(0.1f, 0.1f, 0.1f);
202  sphere_->addToRenderer(_renderer, _baseRO, sphereRadius);
203 
204 
205  // X-Axis
206  _baseRO->debugName = "coordsys.x.axis";
207  _baseRO->emissive = Vec3f(0.5f, 0.0f, 0.0f);
208  _baseRO->diffuse = Vec3f(0.3f, 0.0f, 0.0f);
209  _baseRO->specular = Vec3f(0.1f, 0.0f, 0.0f);
210  _baseRO->ambient = Vec3f(0.1f, 0.0f, 0.0f);
211  _baseRO->modelview = mModelView;
212  _baseRO->modelview.rotate (-90, 0, 1, 0);
213  _baseRO->modelview.translate ( 0, 0, -bodyLength );
214  cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
215 
216  _baseRO->debugName = "coordsys.x.head";
217  _baseRO->modelview.translate ( 0, 0, -arrowLength );
218  cone_->addToRenderer(_renderer, _baseRO, arrowLength);
219 
220 
221  // Y-Axis
222  _baseRO->debugName = "coordsys.y.axis";
223  _baseRO->emissive = Vec3f(0.0f, 0.5f, 0.0f);
224  _baseRO->diffuse = Vec3f(0.0f, 0.3f, 0.0f);
225  _baseRO->specular = Vec3f(0.0f, 0.1f, 0.0f);
226  _baseRO->ambient = Vec3f(0.0f, 0.1f, 0.0f);
227  _baseRO->modelview = mModelView;
228  _baseRO->modelview.rotate (90, 1, 0, 0);
229  _baseRO->modelview.translate ( 0, 0, -bodyLength );
230  cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
231 
232  _baseRO->debugName = "coordsys.y.head";
233  _baseRO->modelview.translate ( 0, 0, -arrowLength );
234  cone_->addToRenderer(_renderer, _baseRO, arrowLength);
235 
236 
237  // Z-Axis
238  _baseRO->debugName = "coordsys.z.axis";
239  _baseRO->emissive = Vec3f(0.0f, 0.0f, 0.5f);
240  _baseRO->diffuse = Vec3f(0.0f, 0.0f, 0.3f);
241  _baseRO->specular = Vec3f(0.0f, 0.0f, 0.1f);
242  _baseRO->ambient = Vec3f(0.0f, 0.0f, 0.1f);
243  _baseRO->modelview = mModelView;
244  _baseRO->modelview.rotate (180, 0, 1, 0);
245  _baseRO->modelview.translate ( 0, 0, -bodyLength );
246  cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
247 
248  _baseRO->debugName = "coordsys.z.head";
249  _baseRO->modelview.translate ( 0, 0, -arrowLength );
250  cone_->addToRenderer(_renderer, _baseRO, arrowLength);
251 }
252 
253 //============================================================================
254 
255 void
256 CoordsysNode::drawCoordsysPick( GLState& _state) {
257 
258  const double arrowLength = 0.03;
259  const double bodyLength = 0.06;
260  const double sphereRadius = 0.01;
261 
262  // Origin
263  _state.pick_set_name (1);
264  sphere_->draw(_state,sphereRadius);
265 
266  // X-Axis
267  _state.pick_set_name (2);
268  _state.push_modelview_matrix ();
269  _state.rotate (-90, 0, 1, 0);
270  _state.translate ( 0, 0, -bodyLength );
271  cylinder_->draw(_state,bodyLength);
272  _state.translate ( 0, 0, -arrowLength );
273  cone_->draw(_state,arrowLength);
274  _state.pop_modelview_matrix ();
275 
276 
277  // Y-Axis
278  _state.pick_set_name (3);
279  _state.push_modelview_matrix ();
280  _state.rotate (90, 1, 0, 0);
281  _state.translate ( 0, 0, -bodyLength );
282  cylinder_->draw(_state,bodyLength);
283  _state.translate ( 0, 0, -arrowLength );
284  cone_->draw(_state,arrowLength);
285  _state.pop_modelview_matrix ();
286 
287  // Z-Axis
288  _state.pick_set_name (4);
289  _state.push_modelview_matrix ();
290  _state.rotate (180, 0, 1, 0);
291  _state.translate ( 0, 0, -bodyLength );
292  cylinder_->draw(_state,bodyLength);
293  _state.translate ( 0, 0, -arrowLength );
294  cone_->draw(_state,arrowLength);
295  _state.pop_modelview_matrix ();
296 
297 }
298 
299 
300 //============================================================================
301 
302 
303 void
305 draw(GLState& _state , const DrawModes::DrawMode& /*_drawMode*/)
306 {
307  GLenum prev_depth = _state.depthFunc();
308 
309  GLboolean colorMask[4];
310  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);
311 
312  // Push Modelview-Matrix
313  _state.push_modelview_matrix();
314 
315  Vec4f lastBaseColor = _state.base_color();
316 
317  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
318  ACG::GLState::enable(GL_LIGHTING);
319  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
320  ACG::GLState::enable(GL_COLOR_MATERIAL);
321  ACG::GLState::shadeModel(GL_SMOOTH);
322 
323  GLfloat zeroVec[4] = {0.0f};
324  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zeroVec);
325  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zeroVec);
326 
327  // Init state - changes when mode_ != POSITION
328  Vec3d pos3D(0.0,0.0,0.0);
329 
330  if ( mode_ == SCREENPOS ) {
331 
332  int left, bottom, width, height;
333  double aspect = _state.aspect();
334 
335  _state.get_viewport(left, bottom, width, height);
336 
337  // Projection reset
338  _state.push_projection_matrix();
339  _state.reset_projection();
340 
341  if (projectionMode_ == PERSPECTIVE_PROJECTION)
342  _state.perspective(45.0, aspect, 0.8, 20.0);
343  else
344  _state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
345 
346  _state.push_modelview_matrix();
347  _state.reset_modelview();
348 
349  float rel_size = 50.0;
350  float projdist = sqrt ( (width*height) / rel_size );
351 
352  float posx = left + width - projdist ;
353  float posy = bottom + height - projdist ;
354 
355  // get our desired coordsys position in scene coordinates
356  pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
357  _state.pop_modelview_matrix();
358 
359  // reset scene translation
360  // we want only the scene rotation to rotate the coordsys
361  GLMatrixd modelview = _state.modelview();
362 
363  modelview(0,3) = 0.0;
364  modelview(1,3) = 0.0;
365  modelview(2,3) = 0.0;
366 
367  _state.set_modelview (modelview);
368  _state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
369 
370 
371  // clear the depth buffer behind the coordsys
372  ACG::GLState::depthRange (1.0, 1.0);
373  ACG::GLState::depthFunc (GL_ALWAYS);
374 
375  drawCoordsys(_state);
376 
377  ACG::GLState::depthRange (0.0, 1.0);
378  ACG::GLState::depthFunc (GL_LESS);
379 
380  // draw coordsys
381  drawCoordsys(_state);
382 
383  // set depth buffer to 0 so that nothing can paint over cordsys
384  ACG::GLState::depthRange (0.0, 0.0);
385  ACG::GLState::depthFunc (GL_ALWAYS);
386  glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
387 
388  // Koordinatensystem zeichnen
389  drawCoordsys(_state);
390 
391  ACG::GLState::depthRange (0.0, 1.0);
392  ACG::GLState::depthFunc (prev_depth);
393  glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
394 
395  // Projection reload
396  _state.pop_projection_matrix();
397 
398 
399  } else if (mode_ == POSITION) { /* mode_ == POSITION */
400 
401  GLMatrixd modelview = _state.modelview();
402 
403  modelview(0,3) = 0.0;
404  modelview(1,3) = 0.0;
405  modelview(2,3) = 0.0;
406 
407  _state.set_modelview (modelview);
408 
409  // clear depth buffer in coordsys region
410  ACG::GLState::depthRange (1.0, 1.0);
411  ACG::GLState::depthFunc (GL_ALWAYS);
412 
413  // Koordinatensystem zeichnen
414  drawCoordsys(_state);
415 
416  // draw coordsys in normal mode
417  ACG::GLState::depthRange (0.0, 1.0);
418  ACG::GLState::depthFunc (GL_LESS);
419 
420  // Koordinatensystem zeichnen
421  drawCoordsys(_state);
422 
423  // set depth buffer to 0 so that nothing can paint over cordsys
424  ACG::GLState::depthRange (0.0, 0.0);
425  ACG::GLState::depthFunc (GL_ALWAYS);
426  glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
427 
428  // Koordinatensystem zeichnen
429  drawCoordsys(_state);
430 
431  // reset to default
432  ACG::GLState::depthRange (0.0, 1.0);
433  ACG::GLState::depthFunc (prev_depth);
434  glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
435  }
436 
437  glPopAttrib();
438 
439  glColor4fv(lastBaseColor.data());
440 
441  // Reload old configuration
442  _state.pop_modelview_matrix();
443 }
444 
445 
446 
447 
448 
449 
450 void CoordsysNode::getRenderObjects( IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode, const Material* _mat )
451 {
452  // Init state - changes when mode_ != POSITION
453  Vec3d pos3D(0.0,0.0,0.0);
454 
455  _state.push_modelview_matrix();
456 
457 
458  // init base renderobject
459  RenderObject ro;
460  ro.initFromState(&_state);
461  ro.setMaterial(_mat);
462 
463 
464  ro.depthTest = true;
465  ro.depthWrite = true;
466  ro.inZPrePass = false;
467  ro.overlay = true;
468 
469  if ( mode_ == SCREENPOS ) {
470 
471  int left, bottom, width, height;
472  double aspect = _state.aspect();
473 
474  _state.get_viewport(left, bottom, width, height);
475 
476  // Projection reset
477  _state.push_projection_matrix();
478  _state.reset_projection();
479 
480  if (projectionMode_ == PERSPECTIVE_PROJECTION)
481  _state.perspective(45.0, aspect, 0.8, 20.0);
482  else
483  _state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
484 
485  _state.push_modelview_matrix();
486  _state.reset_modelview();
487 
488  float rel_size = 50.0;
489  float projdist = sqrt ( (width*height) / rel_size );
490 
491  float posx = left + width - projdist ;
492  float posy = bottom + height - projdist ;
493 
494  // get our desired coordsys position in scene coordinates
495  pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
496  _state.pop_modelview_matrix();
497 
498  // reset scene translation
499  // we want only the scene rotation to rotate the coordsys
500  GLMatrixd modelview = _state.modelview();
501 
502  modelview(0,3) = 0.0;
503  modelview(1,3) = 0.0;
504  modelview(2,3) = 0.0;
505 
506  _state.set_modelview (modelview);
507  _state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
508 
509 
510  // grab new transforms
511  ro.proj = _state.projection();
512  ro.modelview = _state.modelview();
513 
514  // colored by emission only
515  ro.shaderDesc.shadeMode = SG_SHADE_PHONG;
517  ro.shaderDesc.vertexColors = false;
518 
519 
520 
521  // regrab of transforms needed, drawCoordsys overwrites this
522  ro.modelview = _state.modelview();
523 
524  ro.priority = 3;
525  ro.depthRange = Vec2f(0.0f, 1.0f);
526  ro.depthFunc = GL_LESS;
527 
528  // draw coordsys
529  drawCoordsys(_renderer, &ro);
530 
531 
532  // Projection reload
533  _state.pop_projection_matrix();
534 
535  } else if (mode_ == POSITION) { /* mode_ == POSITION */
536 
537  GLMatrixd modelview = _state.modelview();
538 
539  modelview(0,3) = 0.0;
540  modelview(1,3) = 0.0;
541  modelview(2,3) = 0.0;
542 
543  _state.set_modelview (modelview);
544 
545  // clear depth buffer in coordsys region
546  ACG::GLState::depthRange (1.0, 1.0);
547  ACG::GLState::depthFunc (GL_ALWAYS);
548 
549  // Koordinatensystem zeichnen
550  drawCoordsys(_renderer, &ro);
551 
552  // draw coordsys in normal mode
553  ACG::GLState::depthRange (0.0, 1.0);
554  ACG::GLState::depthFunc (GL_LESS);
555 
556  // Koordinatensystem zeichnen
557  drawCoordsys(_renderer, &ro);
558 
559  // set depth buffer to 0 so that nothing can paint over cordsys
560  ACG::GLState::depthRange (0.0, 0.0);
561  ACG::GLState::depthFunc (GL_ALWAYS);
562  glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
563 
564  // Koordinatensystem zeichnen
565  drawCoordsys(_renderer, &ro);
566  }
567 
568 
569  _state.pop_modelview_matrix();
570 }
571 
572 
573 
574 void
576 setMode(const CoordsysMode _mode)
577 {
578  mode_ = _mode;
579 }
580 
581 void
584 {
585  projectionMode_ = _mode;
586 }
587 
588 void
590 setPosition(const Vec3f& _pos)
591 {
592  pos3f_ = _pos;
593 }
594 
597 getMode() const
598 {
599  return mode_;
600 }
601 
605 {
606  return projectionMode_;
607 }
608 
609 void
611 {
612  GLenum prev_depth = _state.depthFunc();
613 
614  if (_target == PICK_ANYTHING) {
615 
616  GLdouble mat[16];
617 
618  // Push Modelview-Matrix
619  _state.push_modelview_matrix();
620  _state.pick_set_maximum (5);
621  _state.pick_set_name (0);
622 
623  // Init state - changes when mode_ != POSITION
624  Vec3d pos3D(0.0,0.0,0.0);
625 
626  if ( mode_ == SCREENPOS ) {
627 
628  int left, bottom, width, height;
629  double aspect = _state.aspect();
630 
631  _state.get_viewport(left, bottom, width, height);
632 
633  // Projection reset
634  _state.push_projection_matrix();
635  _state.reset_projection();
636 
637  if (projectionMode_ == PERSPECTIVE_PROJECTION)
638  _state.perspective(45.0, aspect, 0.8, 20.0);
639  else
640  _state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
641 
642  _state.push_modelview_matrix();
643  _state.reset_modelview();
644 
645  float rel_size = 50.0;
646  float projdist = sqrt ( (width*height) / rel_size );
647 
648  float posx = left + width - projdist ;
649  float posy = bottom + height - projdist ;
650 
651  // get our desired coordsys position in scene coordinates
652  pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
653  _state.pop_modelview_matrix();
654 
655  // reset scene translation
656  GLMatrixd modelview = _state.modelview();
657 
658  modelview(0,3) = 0.0;
659  modelview(1,3) = 0.0;
660  modelview(2,3) = 0.0;
661 
662  _state.set_modelview (modelview);
663  _state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
664 
665  // We don't have access to the pick matrix used during selection buffer picking
666  // so we can't draw our pick area circle in this case
667  if (_state.color_picking ())
668  {
669  // clear depth buffer behind coordsys node
670  clearPickArea(_state, true, 1.0);
671 
672  // Koordinatensystem zeichnen
673  drawCoordsysPick(_state);
674 
675  // set depth buffer to 0.0 so that nothing can paint above
676  clearPickArea(_state, false, 0.0);
677  }
678  else
679  {
680  // clear depth buffer in coordsys region
681  ACG::GLState::depthRange (1.0, 1.0);
682  ACG::GLState::depthFunc (GL_ALWAYS);
683 
684  // Koordinatensystem zeichnen
685  drawCoordsys(_state);
686 
687  // draw coordsys in normal mode
688  ACG::GLState::depthRange (0.0, 1.0);
689  ACG::GLState::depthFunc (GL_LESS);
690 
691  // Koordinatensystem zeichnen
692  drawCoordsys(_state);
693 
694  // set depth buffer to 0 so tah nothing can paint over cordsys
695  ACG::GLState::depthRange (0.0, 0.0);
696  ACG::GLState::depthFunc (GL_ALWAYS);
697  glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
698 
699  // Koordinatensystem zeichnen
700  drawCoordsys(_state);
701 
702  // reset to default
703  ACG::GLState::depthRange (0.0, 1.0);
704  ACG::GLState::depthFunc (prev_depth);
705  glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
706  }
707 
708  // Projection reload
709  _state.pop_projection_matrix();
710 
711  } else if (mode_ == POSITION) { /* mode_ == POSITION */
712 
713  // The selection buffer picking method might have set a
714  // pick matrix that has been multiplied with the projection matrix.
715  // This is the only way to get the gl pick matrix again
716  glMatrixMode(GL_PROJECTION);
717 
718  glPushMatrix ();
719  glMultMatrixd( _state.inverse_projection().get_raw_data());
720 
721  glGetDoublev(GL_PROJECTION_MATRIX, mat);
722 
723  glPopMatrix ();
724 
725  GLMatrixd pickMat (mat);
726 
727  glMatrixMode(GL_MODELVIEW);
728 
729  GLMatrixd modelview = _state.modelview();
730 
731  modelview(0,3) = 0.0;
732  modelview(1,3) = 0.0;
733  modelview(2,3) = 0.0;
734 
735  // We don't have access to the pick matrix used during selection buffer picking
736  // so we can't draw our pick area circle in this case
737  if (_state.color_picking ())
738  {
739  // clear depth buffer behind coordsys node
740  clearPickArea(_state, true, 1.0);
741 
742  // Koordinatensystem zeichnen
743  drawCoordsysPick(_state);
744 
745  // set depth buffer to 0.0 so that nothing can paint above
746  clearPickArea(_state, false, 0.0);
747  }
748  else
749  {
750  // clear depth buffer in coordsys region
751  ACG::GLState::depthRange (1.0, 1.0);
752  ACG::GLState::depthFunc (GL_ALWAYS);
753 
754  // Koordinatensystem zeichnen
755  drawCoordsys(_state);
756 
757  // draw coordsys in normal mode
758  ACG::GLState::depthRange (0.0, 1.0);
759  ACG::GLState::depthFunc (GL_LESS);
760 
761  // Koordinatensystem zeichnen
762  drawCoordsys(_state);
763 
764  // set depth buffer to 0 so tah nothing can paint over cordsys
765  ACG::GLState::depthRange (0.0, 0.0);
766  ACG::GLState::depthFunc (GL_ALWAYS);
767  glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
768 
769  // Koordinatensystem zeichnen
770  drawCoordsys(_state);
771 
772  // reset to default
773  ACG::GLState::depthRange (0.0, 1.0);
774  ACG::GLState::depthFunc (prev_depth);
775  glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
776  }
777  }
778  // Reload old configuration
779  _state.pop_modelview_matrix();
780 
781  }
782 }
783 
784 //----------------------------------------------------------------------------
785 
786 void CoordsysNode::clearPickArea(GLState& _state, bool _draw, GLfloat _depth)
787 {
788  GLenum prev_depth = _state.depthFunc();
789 
790  std::vector<Vec2f> points;
791  Vec2f center;
792  float radius;
793 
794  int left, bottom, width, height;
795  _state.get_viewport(left, bottom, width, height);
796 
797  GLboolean colorMask[4];
798  glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);
799 
800  // respect sphere radius
801  Vec3d proj = _state.project (Vec3d (-0.01, -0.01, -0.01));
802  points.push_back (Vec2f (proj[0], proj[1]));
803 
804  proj = _state.project (Vec3d (0.1, 0.0, 0.0));
805  points.push_back (Vec2f (proj[0], proj[1]));
806 
807  proj = _state.project (Vec3d (0.0, 0.1, 0.0));
808  points.push_back (Vec2f (proj[0], proj[1]));
809 
810  proj = _state.project (Vec3d (0.0, 0.0, 0.1));
811  points.push_back (Vec2f (proj[0], proj[1]));
812 
813 
814  // get bounding circle of projected 4 points of the coord node
815  boundingCircle(points, center, radius);
816 
817  _state.push_projection_matrix();
818  _state.reset_projection();
819 
820  _state.ortho (left, left + width, bottom, bottom + height, 0.0, 1.0);
821 
822  _state.push_modelview_matrix();
823  _state.reset_modelview();
824  ACG::GLState::depthFunc (GL_ALWAYS);
825  ACG::GLState::depthRange (_depth, _depth);
826  _state.translate (center[0], center[1], -0.5);
827 
828  if (_draw)
829  _state.pick_set_name (0);
830  else
831  glColorMask(false, false, false, false);
832 
833  // 10% more to ensure everything is in
834  disk_->setInnerRadius(0.0f);
835  disk_->setOuterRadius(radius * 1.1f);
836  disk_->draw(_state);
837 
838  ACG::GLState::depthFunc (prev_depth);
839  _state.pop_modelview_matrix();
840  _state.pop_projection_matrix();
841 
842  ACG::GLState::depthRange (0.0, 1.0);
843 
844  if (!_draw)
845  glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
846 }
847 
848 //----------------------------------------------------------------------------
849 
850 void CoordsysNode::boundingCircle(std::vector<Vec2f> &_in, Vec2f &_center, float &_radius)
851 {
852  if (_in.empty())
853  return;
854  if (_in.size () < 2)
855  {
856  _center = _in[0];
857  _radius = 0.0f;
858  return;
859  }
860  bool found = false;
861 
862  // try all circumcircles of all possible lines
863  for (unsigned int i = 0; i < _in.size () - 1; i++)
864  for (unsigned int j = i + 1; j < _in.size (); j++)
865  {
866  Vec2f cen = (_in[i] + _in[j]) * 0.5;
867  float rad = (_in[i] - cen).length ();
868  bool allin = true;
869 
870  for (unsigned int k = 0; k < _in.size (); k++)
871  if (k != i && k != j && (_in[k] - cen).length () > rad)
872  {
873  allin = false;
874  break;
875  }
876 
877  if (!allin)
878  continue;
879 
880  if (found)
881  {
882  if (rad < _radius)
883  {
884  _center = cen;
885  _radius = rad;
886  }
887  }
888  else
889  {
890  found = true;
891  _center = cen;
892  _radius = rad;
893  }
894  }
895 
896  if (found)
897  return;
898 
899  // try all circumcircles of all possible triangles
900  for (unsigned int i = 0; i < _in.size () - 2; i++)
901  for (unsigned int j = i + 1; j < _in.size () - 1; j++)
902  for (unsigned int k = j + 1; k < _in.size (); k++)
903  {
904  float v = ((_in[k][0]-_in[j][0])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
905  ((_in[i][0]-_in[k][0])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
906  ((_in[j][0]-_in[i][0])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
907  float u = ((_in[j][1]-_in[k][1])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
908  ((_in[k][1]-_in[i][1])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
909  ((_in[i][1]-_in[j][1])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
910  float d = (_in[i][0]*_in[j][1])+(_in[j][0]*_in[k][1])+(_in[k][0]*_in[i][1]) -
911  (_in[i][0]*_in[k][1])-(_in[j][0]*_in[i][1])-(_in[k][0]*_in[j][1]);
912  Vec2f cen(0.5 * (u/d), 0.5 * (v/d));
913  float rad = (_in[i] - cen).length ();
914  bool allin = true;
915 
916  for (unsigned int l = 0; l < _in.size (); l++)
917  if (l != i && l != j && l != k && (_in[l] - cen).length () > rad)
918  {
919  allin = false;
920  break;
921  }
922 
923  if (!allin)
924  continue;
925 
926  if (found)
927  {
928  if (rad < _radius)
929  {
930  _center = cen;
931  _radius = rad;
932  }
933  }
934  else
935  {
936  found = true;
937  _center = cen;
938  _radius = rad;
939  }
940  }
941 }
942 
943 //=============================================================================
944 } // namespace SceneGraph
945 } // namespace ACG
946 //=============================================================================
VectorT< float, 2 > Vec2f
Definition: VectorT.hh:108
Vec3f diffuse
material definitions
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
Definition: DrawModes.cc:80
void setPosition(const Vec3f &_pos)
set position of the coordsys
void clearTextures()
disables texture support and removes all texture types
void push_projection_matrix()
push projection matrix
Definition: GLState.cc:967
Draws the Coordsys at the upper right position on the screen.
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat)
add renderobjects for shader pipeline renderer
void pop_projection_matrix()
pop projection matrix
Definition: GLState.cc:985
void setProjectionMode(const ProjectionMode _mode)
set mode to either ORTHOGRAPHIC_PROJECTION or PERSPECTIVE_PROJECTION
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.cc:102
bool overlay
Layer based rendering.
bool color_picking() const
Is color picking active?
Definition: GLState.cc:1124
ProjectionMode getProjectionMode() const
get current projection mode
void pick(GLState &_state, PickTarget _target)
draw Coordsys for object picking
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1006
void perspective(double _fovY, double _aspect, double _near_plane, double _far_plane)
perspective projection
Definition: GLState.cc:446
int priority
Priority to allow sorting of objects.
Draws the Coordsys at the coordsys origin.
ProjectionMode
projection mode
Definition: CoordsysNode.hh:93
void ortho(double _left, double _right, double _bottom, double _top, double _near_plane, double _far_plane)
orthographic projection
Definition: GLState.cc:400
DrawMode POINTS
draw unlighted points using the default base color
Definition: DrawModes.cc:79
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
Definition: GLState.cc:638
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.cc:161
PickTarget
What target to use for picking.
Definition: BaseNode.hh:99
double aspect() const
get aspect ratio
Definition: GLState.cc:873
GLMatrixd modelview
Modelview transform.
static void enable(GLenum _cap)
replaces glEnable, but supports locking
const GLenum & depthFunc() const
get glDepthFunc() that is supposed to be active
Definition: GLState.cc:937
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:125
const Vec4f & base_color() const
get base color (used when lighting is off)
Definition: GLState.hh:929
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
draw Coordsys
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:531
bool pick_set_maximum(unsigned int _idx)
Set the maximal number of primitives/components of your object.
Definition: GLState.cc:1047
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
Definition: GLState.cc:649
void set_modelview(const GLMatrixd &_m)
set modelview
Definition: GLState.hh:731
ACG::SceneGraph::DrawModes::DrawMode availableDrawModes() const
return available draw modes
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:262
static void depthRange(GLclampd _zNear, GLclampd _zFar)
replaces glDepthRange, supports locking
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
Definition: DrawModes.cc:81
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1022
void pick_set_name(unsigned int _idx)
sets the current name/color (like glLoadName(_idx))
Definition: GLState.cc:1057
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
Definition: GLState.cc:562
void reset_projection()
reset projection matrix (load identity)
Definition: GLState.cc:332
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
GLenum depthFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
const GLMatrixd & projection() const
get projection matrix
Definition: GLState.hh:789
const GLMatrixd & inverse_projection() const
get inverse projection matrix
Definition: GLState.hh:809
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
Definition: RenderObject.cc:69
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:794
void setMode(const CoordsysMode _mode)
set mode to either POSITION or SCREENPOS
CoordsysNode(BaseNode *_parent=0, std::string _name="<TextNode>", CoordsysMode _mode=SCREENPOS, ProjectionMode _projectionMode=PERSPECTIVE_PROJECTION)
Definition: CoordsysNode.cc:74
bool inZPrePass
Specify whether this object should be rendered in a z-prepass.
pick any of the prior targets (should be implemented for all nodes)
Definition: BaseNode.hh:110
GLMatrixd proj
Projection transform.
Interface class between scenegraph and renderer.
CoordsysMode getMode() const
get current mode
void get_viewport(int &_left, int &_bottom, int &_width, int &_height) const
get viewport
Definition: GLState.hh:819
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void reset_modelview()
reset modelview matrix (load identity)
Definition: GLState.cc:368
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:193
Vec2f depthRange
glDepthRange: (znear, zmax)
const GLenum & depthFunc() const
get glDepthFunc() that is supposed to be active