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

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
Dirk Wilden's avatar
Dirk Wilden committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57




//=============================================================================
//
//  CLASS SkeletonNodeT - IMPLEMENTATION
//
//=============================================================================

#define ACG_SKELETONNODET_C

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

#include "SkeletonNodeT.hh"
#include <ACG/GL/gl.hh>
58
#include <ACG/GL/IRenderer.hh>
Dirk Wilden's avatar
Dirk Wilden committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <vector>
#include <deque>

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

namespace ACG {
namespace SceneGraph {

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


/** \brief Constructor
 * 
 */
73
74
template <class SkeletonType>
SkeletonNodeT<SkeletonType>::SkeletonNodeT(SkeletonType &_skeleton, BaseNode *_parent, std::string _name) :
Dirk Wilden's avatar
Dirk Wilden committed
75
76
77
78
               BaseNode(_parent, _name),
               bCoordFramesVisible_(false),
               skeleton_(_skeleton),
               hAni_(),
79
80
81
               fFrameSize_(0.0),
               slices_(6),
               stacks_(1)
Dirk Wilden's avatar
Dirk Wilden committed
82
83
{
  this->multipassNodeSetActive(3, true);
84
85

  sphere_ = new ACG::GLSphere(10, 10);
86
  cone_ = new ACG::GLCone(slices_, stacks_, 1.0f, 0.0f, false, false);
87
  cylinder_ = new ACG::GLCylinder(10, 2, 1.0f, true, true);
Dirk Wilden's avatar
Dirk Wilden committed
88
89
90
91
92
93
94
95
}


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

/** \brief Destructor
 * 
 */
96
97
template <class SkeletonType>
SkeletonNodeT<SkeletonType>::~SkeletonNodeT()
Dirk Wilden's avatar
Dirk Wilden committed
98
{
99
100
  if (sphere_)
    delete sphere_;
Matthias Möller's avatar
Matthias Möller committed
101

102
103
  if (cone_)
    delete cone_;
104
105
106

  if (cylinder_)
    delete cylinder_;
Dirk Wilden's avatar
Dirk Wilden committed
107
108
109
110
111
112
113
114
}


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

/** \brief Returns a pointer to the skeleton
 * 
 */
115
116
template <class SkeletonType>
SkeletonType& SkeletonNodeT<SkeletonType>::skeleton()
Dirk Wilden's avatar
Dirk Wilden committed
117
118
119
120
121
122
123
124
125
126
{
  return skeleton_;
}


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

/** \brief Returns the bounding box of this node
 * 
 */
127
128
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
Dirk Wilden's avatar
Dirk Wilden committed
129
130
131
132
133
{  
  if(skeleton_.jointCount() == 0)
    return; // no joints, no contribution to the bound box

  Pose *pose = skeleton_.pose(hAni_);
134
  typename SkeletonType::Iterator it;
Dirk Wilden's avatar
Dirk Wilden committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
  {
    _bbMin.minimize( (Vec3d)pose->globalTranslation((*it)->id()) );
    _bbMax.maximize( (Vec3d)pose->globalTranslation((*it)->id()) );
  }
  
  //choose a proper size for the coordinate frames
  fFrameSize_ = (_bbMin - _bbMax).norm() * 0.07;
}


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

/** \brief Returns available draw modes
 * 
 */
151
152
template <class SkeletonType>
DrawModes::DrawMode SkeletonNodeT<SkeletonType>::availableDrawModes() const
Dirk Wilden's avatar
Dirk Wilden committed
153
154
155
156
157
158
159
160
{
  return (DrawModes::WIREFRAME           | DrawModes::POINTS | DrawModes::SOLID_FLAT_SHADED
        | DrawModes::SOLID_FACES_COLORED | DrawModes::SOLID_FACES_COLORED_FLAT_SHADED);
}


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

161
162
163
164
165
166
167
168
169
170

template <class SkeletonType>
double SkeletonNodeT<SkeletonType>::frameSize() {
	return fFrameSize_;
}


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


Dirk Wilden's avatar
Dirk Wilden committed
171
172
173
/** \brief Normalizes a coordinate frame defined by the given matrix
 * 
 */
174
175
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::NormalizeCoordinateFrame(Matrix &_mat)
Dirk Wilden's avatar
Dirk Wilden committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
  ACG::Vec3d vec;
  for(int i = 0; i < 3; ++i)
  {
    for(int x = 0; x < 3; ++x)
      vec[x] = _mat(x, i);
    vec.normalize();
    for(int x = 0; x < 3; ++x)
      _mat(x, i) = vec[x];
  }
}


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

/**
 * @brief Converts the color from HSV color space to RGB
 *
 * H is in range [0, 360], S and V in [0, 1].
 *
 * @param[in] _HSV An array of 3 GLfloats, holding H, S and V
 * @param[out] _RGB An array of 3 GLfloats, holding R, G and B
 */
199
200
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::HSVtoRGB(const Vec4f& _HSV, Vec4f& _RGB)
Dirk Wilden's avatar
Dirk Wilden committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{
  int h;
  float f, p, q, t;
  h = int(floor(_HSV[0] / 60.0));
  f = _HSV[0] / 60.0 - float(h);
  p = _HSV[2] * (1 - _HSV[1]);
  q = _HSV[2] * (1 - _HSV[1] * f);
  t = _HSV[2] * (1 - _HSV[1] * (1 - f));

  switch(h)
  {
  case 0:
  case 6:
    _RGB[0] = _HSV[2];
    _RGB[1] = t;
    _RGB[2] = p;
    break;
  case 1:
    _RGB[0] = q;
    _RGB[1] = _HSV[2];
    _RGB[2] = p;
    break;
  case 2:
    _RGB[0] = p;
    _RGB[1] = _HSV[2];
    _RGB[2] = t;
    break;
  case 3:
    _RGB[0] = p;
    _RGB[1] = q;
    _RGB[2] = _HSV[2];
    break;
  case 4:
    _RGB[0] = t;
    _RGB[1] = p;
    _RGB[2] = _HSV[2];
    break;
  case 5:
    _RGB[0] = _HSV[2];
    _RGB[1] = p;
    _RGB[2] = q;
    break;
  }
  
  //always set alpha to 1.0
  _RGB[3] = 1.0;
}


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

/**
 * @brief Converts a color from RGB color space to HSV
 *
 * Red, green and blue are in the range [0, 1].
 *
 * @param[out] _RGB An array of 3 GLfloats, holding R, G and B
 * @param[in] _HSV An array of 3 GLfloats, holding H, S and V
 */
260
261
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::RGBtoHSV(const Vec4f& _RGB, Vec4f& _HSV)
Dirk Wilden's avatar
Dirk Wilden committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{
  double maxC = _RGB[2];
  if(maxC < _RGB[1])
    maxC = _RGB[1];
  if(maxC < _RGB[0])
    maxC = _RGB[0];
  double minC = _RGB[2];
  if(minC > _RGB[1])
    minC = _RGB[1];
  if(minC > _RGB[0])
    minC = _RGB[0];

  double delta = maxC - minC;

  double V = maxC;
  double S = 0;
  double H = 0;

  if(delta == 0)
  {
    H = 0;
    S = 0;
  }else{
    S = delta / maxC;
    double dR = 60 * (maxC - _RGB[0]) / delta + 180;
    double dG = 60 * (maxC - _RGB[1]) / delta + 180;
    double dB = 60 * (maxC - _RGB[2]) / delta + 180;
    if (_RGB[0] == maxC)
      H = dB - dG;
    else if (_RGB[1] == maxC)
      H = 120 + dR - dB;
    else
      H = 240 + dG - dR;
  }

  if(H < 0)
    H += 360;
  if(H >= 360)
    H -= 360;

  _HSV[0] = H;
  _HSV[1] = S;
  _HSV[2] = V;
}

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

/** \brief Renders the nodes contents using the given render state and draw mode
 * 
 */
312
313
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
Dirk Wilden's avatar
Dirk Wilden committed
314
315
{
  
316
  ACG::GLState::disable(GL_LIGHTING);
Dirk Wilden's avatar
Dirk Wilden committed
317
318
319
320
  
  glPushAttrib(GL_ENABLE_BIT);
  
  Pose *pose = skeleton_.pose(hAni_);
321
  typename SkeletonType::Iterator it;
Dirk Wilden's avatar
Dirk Wilden committed
322
  
323
324


Dirk Wilden's avatar
Dirk Wilden committed
325
326
  // draw points
  //
Matthias Möller's avatar
Matthias Möller committed
327
  if (_drawMode & DrawModes::POINTS)
Dirk Wilden's avatar
Dirk Wilden committed
328
329
  {
    Vec4f jointColor;
330
    getJointColor(_state.diffuse_color(), jointColor);
Dirk Wilden's avatar
Dirk Wilden committed
331
332
333
    
    // draw all joint positions
    glPointSize(12);
334
335
336

    //we will set the specular color, otherwise the color cannot be seen
    ACG::Vec4f oldSpecular = _state.specular_color();
337
    ACG::Vec4f oldDiffuse  = _state.diffuse_color();
Dirk Wilden's avatar
Dirk Wilden committed
338
339
340
                
    for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
    {
Jan Möbius's avatar
Jan Möbius committed
341
342

      // If the vertex is selected, it will be always red
Dirk Wilden's avatar
Dirk Wilden committed
343
      if ( (*it)->selected() )
Matthias Möller's avatar
Matthias Möller committed
344
      {
Jan Möbius's avatar
Jan Möbius committed
345
346
347
        _state.set_color(ACG::Vec4f(1.0f, 0.0f, 0.0f ,1.0f));
      	_state.set_diffuse_color(ACG::Vec4f(1.0f, 0.0f, 0.0f ,1.0f));
      	_state.set_specular_color(ACG::Vec4f(1.0f, 0.0f, 0.0f ,1.0f));
Matthias Möller's avatar
Matthias Möller committed
348
      }
Jan Möbius's avatar
Jan Möbius committed
349
350
351
352
      else {
        // If it is the root joint, it will get some kind of orange color
        // Otherwise the the Base color is used
        if ( (*it)->isRoot() )
Matthias Möller's avatar
Matthias Möller committed
353
        {
Jan Möbius's avatar
Jan Möbius committed
354
        	ACG::Vec4f root_color = ACG::Vec4f(1.0f, 0.66f, 0.0f ,1.0f);
355
        	_state.set_color(root_color);
356
        	_state.set_diffuse_color(root_color);
357
        	_state.set_specular_color(root_color);
Matthias Möller's avatar
Matthias Möller committed
358
        }
359
        else {
360
361
        	_state.set_color(ACG::Vec4f(jointColor[0], jointColor[1] , jointColor[2], 1.0));
        	_state.set_ambient_color(ACG::Vec4f(jointColor[0], jointColor[1] , jointColor[2], 1.0));
Matthias Möller's avatar
Matthias Möller committed
362
        	_state.set_diffuse_color(ACG::Vec4f(jointColor[0], jointColor[1] , jointColor[2], 1.0));
363
        	_state.set_specular_color(ACG::Vec4f(jointColor[0], jointColor[1] , jointColor[2], 1.0));
364
        }
Dirk Wilden's avatar
Dirk Wilden committed
365
366
      }

367

368
      // Simulate glPointSize() with a sphere
369

370
371
      const double sphereSize = unprojectPointSize((double)_state.point_size(),
                  pose->globalTranslation( (*it)->id() ), _state);
Jan Möbius's avatar
Jan Möbius committed
372

373
      sphere_->draw(_state,sphereSize,ACG::Vec3f(pose->globalTranslation( (*it)->id() )));
374

Dirk Wilden's avatar
Dirk Wilden committed
375
376
    }

377
    _state.set_specular_color(oldSpecular);
378
    _state.set_diffuse_color(oldDiffuse);
379

Dirk Wilden's avatar
Dirk Wilden committed
380
381
382
383

    // draw the local coordinate frames
    if(bCoordFramesVisible_)
    {
384
      ACG::GLState::enable(GL_DEPTH_TEST);
Dirk Wilden's avatar
Dirk Wilden committed
385
386
387
388
      glPointSize(6.0);
      glBegin(GL_POINTS);
      for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
      {
389
        typename SkeletonType::Matrix global = pose->globalMatrix( (*it)->id() );
Dirk Wilden's avatar
Dirk Wilden committed
390
        NormalizeCoordinateFrame(global);
Jan Möbius's avatar
Jan Möbius committed
391
        glColor3f(0.8f, 0.2f, 0.2f);
Dirk Wilden's avatar
Dirk Wilden committed
392
        glVertex( global.transform_point(Point(fFrameSize_, 0, 0)) );
Jan Möbius's avatar
Jan Möbius committed
393
        glColor3f(0.2f, 0.8f, 0.2f);
Dirk Wilden's avatar
Dirk Wilden committed
394
        glVertex( global.transform_point(Point(0, fFrameSize_, 0)) );
Jan Möbius's avatar
Jan Möbius committed
395
        glColor3f(0.2f, 0.2f, 0.8f);
Dirk Wilden's avatar
Dirk Wilden committed
396
397
398
399
400
401
402
403
404
405
        glVertex( global.transform_point(Point(0, 0, fFrameSize_)) );
      }
      glEnd();
      glPointSize(_state.point_size());
    }
  }


  // draw bones
  //
Matthias Möller's avatar
Matthias Möller committed
406
407
408
409
  if ( (_drawMode & DrawModes::WIREFRAME)
    || (_drawMode & DrawModes::SOLID_FLAT_SHADED)
    || (_drawMode & DrawModes::SOLID_FACES_COLORED)
    || (_drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) )
Dirk Wilden's avatar
Dirk Wilden committed
410
411
  {
    
412
    Vec4f baseColor = _state.ambient_color();
Dirk Wilden's avatar
Dirk Wilden committed
413
    
Matthias Möller's avatar
Matthias Möller committed
414
415
    if ( (_drawMode & DrawModes::SOLID_FLAT_SHADED)
      || (_drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) ){
Dirk Wilden's avatar
Dirk Wilden committed
416

417
418
      ACG::GLState::shadeModel(GL_FLAT);
      ACG::GLState::enable(GL_LIGHTING);
Dirk Wilden's avatar
Dirk Wilden committed
419
420
    }
    
Matthias Möller's avatar
Matthias Möller committed
421
    if (_drawMode & DrawModes::SOLID_FACES_COLORED)
422
      ACG::GLState::shadeModel(GL_FLAT);
Dirk Wilden's avatar
Dirk Wilden committed
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
    
    
    glLineWidth(1.5);
    
    // draw the bones
    for(it = skeleton_.begin(); it != skeleton_.end(); ++it) {

      //joint is the (unique) tail joint of the bone
      Joint* joint  = *it;
      Joint* parent = joint->parent();

      // root can be ignored
      // we only want to draw bones from (parent -> joint)
      if (parent == 0)
        continue;

      //select joint color
      glColor4fv( &baseColor[0] );

      Vec3d parentPos = pose->globalTranslation(parent->id());
      Vec3d jointPos  = pose->globalTranslation(joint->id());

      Vec3d boneVector = (jointPos - parentPos);

      draw_bone(_state, _drawMode, parentPos, boneVector);
    }


    // draw the local coordinate frames
    if(bCoordFramesVisible_)
    {
454
455
      ACG::GLState::disable(GL_COLOR_MATERIAL);
      ACG::GLState::disable(GL_LIGHTING);
Dirk Wilden's avatar
Dirk Wilden committed
456
457
458
459
460
461

      glLineWidth(3.0);
      glBegin(GL_LINES);
      for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
      {
        unsigned int index = (*it)->id();
462
        typename SkeletonType::Matrix global = pose->globalMatrix(index);
Dirk Wilden's avatar
Dirk Wilden committed
463
        NormalizeCoordinateFrame(global);
Jan Möbius's avatar
Jan Möbius committed
464
        glColor3f(0.8f, 0.2f, 0.2f);
Dirk Wilden's avatar
Dirk Wilden committed
465
466
        glVertex( pose->globalTranslation(index));
        glVertex( global.transform_point(Point(fFrameSize_, 0, 0)) );
Jan Möbius's avatar
Jan Möbius committed
467
        glColor3f(0.2f, 0.8f, 0.2f);
Dirk Wilden's avatar
Dirk Wilden committed
468
469
        glVertex( pose->globalTranslation(index));
        glVertex( global.transform_point(Point(0, fFrameSize_, 0)) );
Jan Möbius's avatar
Jan Möbius committed
470
        glColor3f(0.2f, 0.2f, 0.8f);
Dirk Wilden's avatar
Dirk Wilden committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
        glVertex( pose->globalTranslation(index));
        glVertex( global.transform_point(Point(0, 0, fFrameSize_)) );
      }
      glEnd();
      glLineWidth(_state.line_width());
    }
  }


  glColor(_state.base_color());

  glPopAttrib();
}

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

/** \brief get suitable color for the joint
 * 
 */
490
491
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::getJointColor( const Vec4f& _baseColor, Vec4f& _result )
Dirk Wilden's avatar
Dirk Wilden committed
492
493
494
495
{
  Vec4f hsv;
  RGBtoHSV(_baseColor, hsv);
  
Jan Möbius's avatar
Jan Möbius committed
496
  hsv[0] += 0.4f;
Dirk Wilden's avatar
Dirk Wilden committed
497
498
  if (hsv[0] > 1.0) hsv[0] -= 1.0;
  
Jan Möbius's avatar
Jan Möbius committed
499
  hsv[1] += 0.1f;
Dirk Wilden's avatar
Dirk Wilden committed
500
501
502
503
504
505
506
507
508
509
510
  if (hsv[1] > 1.0) hsv[1] = 1.0;
  
  HSVtoRGB(hsv, _result);
}


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

/** \brief Renders the node in picking mode, restricted to the node components given by \e _target
 * 
 */
511
512
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::pick(GLState& _state, PickTarget _target)
Dirk Wilden's avatar
Dirk Wilden committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
{
  unsigned int n_of_vertices = skeleton_.jointCount();

  switch (_target)
  {
  case PICK_VERTEX:
    _state.pick_set_maximum(n_of_vertices);
    pick_vertices(_state);
    break;

  case PICK_EDGE:
    _state.pick_set_maximum(n_of_vertices);
    pick_edges(_state);
    break;

  case PICK_ANYTHING:
    _state.pick_set_maximum(n_of_vertices);
    pick_vertices(_state);
    pick_edges(_state);
    break;

  default:
    break;
  }
}


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

/** \brief Pick method for vertices
 * 
 */
545
546
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::pick_vertices(GLState &_state)
Dirk Wilden's avatar
Dirk Wilden committed
547
548
{
  glPointSize(16.0);
549
550
  typename SkeletonType::Pose* pose = skeleton_.pose(hAni_);
  for(typename SkeletonType::Iterator it = skeleton_.begin(); it != skeleton_.end(); ++it)
Dirk Wilden's avatar
Dirk Wilden committed
551
  {
552
    typename SkeletonType::Joint *joint = *it;
Dirk Wilden's avatar
Dirk Wilden committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    _state.pick_set_name(joint->id());

    Vec3d p = pose->globalTranslation(joint->id());

    glBegin(GL_POINTS);
    glVertex(p);
    glEnd();
  }
}


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

/** \brief Pick method for edges
 * 
 */
569
570
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::pick_edges(GLState &_state)
Dirk Wilden's avatar
Dirk Wilden committed
571
572
{
  glLineWidth(10);
573
574
  typename SkeletonType::Pose* pose = skeleton_.pose(hAni_);
  for(typename SkeletonType::Iterator it = skeleton_.begin(); it != skeleton_.end(); ++it)
Dirk Wilden's avatar
Dirk Wilden committed
575
576
577
578
579
580
581
582
583
  {
    Joint *joint = *it;
    for(typename Joint::ChildIter it_ch = joint->begin(); it_ch != joint->end(); ++it_ch)
    {
      Joint *child = *it_ch;

      _state.pick_set_name(child->id());

      Vec3d p0 = pose->globalTranslation(joint->id()),
584
            p1 = pose->globalTranslation(child->id());
Dirk Wilden's avatar
Dirk Wilden committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

      glBegin(GL_LINES);
      glVertex(p0);
      glVertex(p1);
      glEnd();
    }
  }
}


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

/** \brief Helper function to draw the bones
 * 
 */
600
601
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::draw_bone(GLState &_state, DrawModes::DrawMode _drawMode, const Point& _parent, const Point& _axis)
Dirk Wilden's avatar
Dirk Wilden committed
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
{
 
  _state.push_modelview_matrix();

  Point midPoint = _parent + 0.1 * _axis;
  
  _state.translate(midPoint[0], midPoint[1], midPoint[2]);

  Point  direction = _axis;
  Point  z_axis(0,0,1);
  Point  rot_normal;
  double rot_angle;

  direction.normalize();
  rot_angle  = acos((z_axis | direction))*180/M_PI;
  rot_normal = ((z_axis % direction).normalize());


  if(fabs(rot_angle) > 0.0001 && fabs(180-rot_angle) > 0.0001)
    _state.rotate(rot_angle, rot_normal[0], rot_normal[1], rot_normal[2]);
  else
    _state.rotate(rot_angle, 1, 0, 0);

  double boneLength = _axis.norm();
  double radius     = boneLength * 0.07;
  
  //draw the large cone from midPoint to the end of the bone
629
630
631
  cone_->setBottomRadius(radius);
  cone_->setTopRadius(0.0f);
  cone_->draw(_state,boneLength*0.9);
Dirk Wilden's avatar
Dirk Wilden committed
632
633
634

  //rotate 180.0 and draw the the small cone from midPoint to the start
  _state.rotate(180.0, 1, 0, 0);  
635
  cone_->draw(_state,boneLength*0.1);
Dirk Wilden's avatar
Dirk Wilden committed
636
637
638
639
640
  
  _state.pop_modelview_matrix();
}


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
//----------------------------------------------------------------------------


/** \brief Helper function to create a renderobject for bones
 * 
 */
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::addBoneToRenderer(IRenderer* _renderer, RenderObject& _base, const Point& _parent, const Point& _axis)
{
  // save previous modelview transform
  GLMatrixf prevTransform = _base.modelview;

  Point midPoint = _parent + 0.1 * _axis;
  
  _base.modelview.translate(midPoint[0], midPoint[1], midPoint[2]);

  Point  direction = _axis;
  Point  z_axis(0,0,1);
  Point  rot_normal;
  double rot_angle;

  direction.normalize();
  rot_angle  = acos((z_axis | direction))*180/M_PI;
  rot_normal = ((z_axis % direction).normalize());


  if(fabs(rot_angle) > 0.0001 && fabs(180-rot_angle) > 0.0001)
    _base.modelview.rotate(rot_angle, rot_normal[0], rot_normal[1], rot_normal[2]);
  else
    _base.modelview.rotate(rot_angle, 1, 0, 0);

  double boneLength = _axis.norm();
  double radius     = boneLength * 0.07;
  
  //draw the large cone from midPoint to the end of the bone
  cone_->setBottomRadius(1.0f);
  cone_->setTopRadius(0.0f);

  _base.modelview.scale(radius, radius, 1.0f);
  cone_->addToRenderer(_renderer, &_base, boneLength*0.9);

  //rotate 180.0 and draw the the small cone from midPoint to the start
  _base.modelview.rotate(180.0, 1, 0, 0);  
  cone_->addToRenderer(_renderer, &_base, boneLength*0.1);


  _base.modelview = prevTransform;
}


Dirk Wilden's avatar
Dirk Wilden committed
691
692
693
694
695
696
697
698
699
700
701
702
703
//----------------------------------------------------------------------------

/**
 * @brief Selects the active pose
 *
 * The active pose is the pose that is drawn to the viewport. If the animation handle is invalid (-1),
 * the default pose will be drawn.
 *
 * @warning Do not call this method directly, call SkeletonObject::setActivePose instead. The object has
 * to be notified, in order to properly update the text node positions.
 *
 * @param _hAni A handle to an animation, or an invalid handle for the default pose.
 */
704
705
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::setActivePose(const AnimationHandle &_hAni)
Dirk Wilden's avatar
Dirk Wilden committed
706
707
708
709
710
711
712
713
714
715
{
  hAni_ = _hAni;
}


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

/**
 * @brief Returns the active pose set with SkeletonNodeT::setActivePose
 */
716
717
template <class SkeletonType>
AnimationHandle SkeletonNodeT<SkeletonType>::activePose()
Dirk Wilden's avatar
Dirk Wilden committed
718
719
720
721
722
723
724
725
726
727
{
  return hAni_;
}


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

/** \brief Toggle visibility of coordinate frames for all joints
 * 
 */
728
729
template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::showCoordFrames(bool _bVisible)
Dirk Wilden's avatar
Dirk Wilden committed
730
731
732
733
{
	bCoordFramesVisible_ = _bVisible;
}

734
735
template <class SkeletonType>
bool SkeletonNodeT<SkeletonType>::coordFramesVisible()
Dirk Wilden's avatar
Dirk Wilden committed
736
737
738
739
{
  return bCoordFramesVisible_;
}

740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//----------------------------------------------------------------------------

/** \brief Compute the radius of a sphere simulating glPointSize after projection.
 * 
 */
template <class SkeletonType>
double SkeletonNodeT<SkeletonType>::unprojectPointSize(double _pointSize, const Vec3d& _point, GLState& _state)
{
  // 1. Project point to screen
  ACG::Vec3d projected = _state.project( _point );

  // 2. Shift it by the requested point size
  //    glPointSize defines the diameter but we want the radius, so we divide it by two
  ACG::Vec3d shifted = projected;
  shifted[0] = shifted[0] + _pointSize / 2.0 ;

  // 3. un-project into 3D
  ACG::Vec3d unProjectedShifted = _state.unproject( shifted );

  // 4. The difference vector defines the radius in 3D for the sphere
  ACG::Vec3d difference = unProjectedShifted - _point;

  return difference.norm();
}

Dirk Wilden's avatar
Dirk Wilden committed
765

766
767
768
769
770
771
772
773
774
//----------------------------------------------------------------------------

/**
 * @brief Generates renderobjects instead of direct draw calls for better rendering management.
 */

template <class SkeletonType>
void SkeletonNodeT<SkeletonType>::getRenderObjects(IRenderer* _renderer, 
                                                   GLState& _state,
775
776
                                                   const DrawModes::DrawMode& _drawMode,
                                                   const Material* _mat)
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
{
  RenderObject ro;
  ro.initFromState(&_state);

  // render states
  ro.depthTest = true;
  ro.depthWrite = true;
  
  ro.culling = true;
  ro.blending = false;


  Pose *pose = skeleton_.pose(hAni_);
  typename SkeletonType::Iterator it;

792
  Vec4f jointColor;
793
  getJointColor(_mat->diffuseColor(), jointColor);
794

795

796
797
  // draw points
  for (unsigned int i = 0; i < _drawMode.getNumLayers(); ++i)
798
  {
799
    const DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);
800

801
802
    switch (props->primitive())
    {
803

804
805
    case DrawModes::PRIMITIVE_POINT:
      {
806
        ro.debugName = "SkeletonNode.point";
807
        ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
808

809
810
811
812
813
        for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
        {
          // If the vertex is selected, it will be always red
          // If it is not selected,
          if ( (*it)->selected() )
814
            ro.emissive = Vec3f(1.0f, 0.0f, 0.0f);
815
816
817
818
          else {
            // If it is the root joint, it will get some kind of orange color
            // Otherwise the the Base color is used
            if ( (*it)->isRoot() )
819
              ro.emissive = Vec3f(1.0f,0.66f, 0.0f);
820
            else
821
              ro.emissive = Vec3f(jointColor[0], jointColor[1] , jointColor[2]);
822
823
824
          }


825
          // simulate glPointSize( ) with sphere
826

827
828
829
          const double sphereSize = unprojectPointSize((double)_state.point_size(), 
            pose->globalTranslation( (*it)->id() ),
            _state);
830
831
832

          sphere_->addToRenderer(_renderer, &ro, sphereSize, ACG::Vec3f(pose->globalTranslation( (*it)->id() )));
        }
833

834
      } break;
835
836


837
    case DrawModes::PRIMITIVE_POLYGON:
838
      {
839
840
        ro.debugName = "SkeletonNode.bone"; 

841
        ro.setupShaderGenFromDrawmode(props);
842

843
        ro.setMaterial(_mat);
844

845
846
847
848
849
850
851
852
853
854
855
        // draw the bones
        for(it = skeleton_.begin(); it != skeleton_.end(); ++it) {

          //joint is the (unique) tail joint of the bone
          Joint* joint  = *it;
          Joint* parent = joint->parent();

          // root can be ignored
          // we only want to draw bones from (parent -> joint)
          if (parent == 0)
            continue;
856

857
858
859
860
861
862
863
          Vec3d parentPos = pose->globalTranslation(parent->id());
          Vec3d jointPos  = pose->globalTranslation(joint->id());

          Vec3d boneVector = (jointPos - parentPos);

          addBoneToRenderer(_renderer, ro, parentPos, boneVector);
        }
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
      } break;

    default: break;
    }
  }


  // draw coordframes

  if (bCoordFramesVisible_)
  {
    ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;

    for(it = skeleton_.begin(); it != skeleton_.end(); ++it)
    {
      unsigned int index = (*it)->id();
      typename SkeletonType::Matrix global = pose->globalMatrix(index);
      NormalizeCoordinateFrame(global);

      // color for each arrow
      Vec3f colors[] = {
        Vec3f(0.8f, 0.2f, 0.2f),
        Vec3f(0.2f, 0.8f, 0.2f),
        Vec3f(0.2f, 0.2f, 0.8f)
      };

      Point points[] = {
        Point(fFrameSize_, 0, 0),
        Point(0, fFrameSize_, 0),
        Point(0, 0, fFrameSize_)
      };
895
896


897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
      // simulate glLineWidth(3) with cylinder
      float lineWidth = (float)unprojectPointSize(3.0f, pose->globalTranslation(index), _state);

      // glPointSize(6)
      float sphereSize = (float)unprojectPointSize(6.0f, pose->globalTranslation(index), _state);

      // draw coordframe arrows
      for (int i = 0; i < 3; ++i)
      {
        ro.emissive = colors[i];

        // cylinder start and end points
        Vec3f vstart = (Vec3f)pose->globalTranslation(index);
        Vec3f vend = (Vec3f)global.transform_point(points[i]);

        Vec3f vdir = vend - vstart;
        float height = vdir.length();

        cylinder_->addToRenderer(_renderer, &ro, height, vstart, vdir, lineWidth);
        sphere_->addToRenderer(_renderer, &ro, sphereSize, vend);
917
918
      }
    }
919
920


921

922
923
924
  }


925
926
927
928
929
930
931
}






Dirk Wilden's avatar
Dirk Wilden committed
932
933
934
935
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================