GlutPrimitiveNode.cc 14.2 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
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
Jan Möbius's avatar
Jan Möbius committed
8
9
 *                                                                           *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
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
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
42
43
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
44
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
45
46
47
48
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62




//=============================================================================
//
//  CLASS GlutPrimitiveNode - IMPLEMENTATION
//
//=============================================================================


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

#include "GlutPrimitiveNode.hh"
63
64
65

#include <ACG/GL/IRenderer.hh>

Jan Möbius's avatar
 
Jan Möbius committed
66
67
68
69
70
71
72
73
74

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

namespace ACG {
namespace SceneGraph {


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

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
GlutPrimitiveNode::GlutPrimitiveNode( BaseNode*         _parent,
                                      std::string       _name )
  : BaseNode(_parent, _name),
    setColor_(true)
{
  const int slices = 20;
  const int stacks = 20;

  sphere_   = new ACG::GLSphere(slices,stacks);
};

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

GlutPrimitiveNode::GlutPrimitiveNode(GlutPrimitiveType _type,
                                     BaseNode* _parent,
                                     std::string _name) :
        BaseNode(_parent, _name),
        setColor_(true)
{
  const int slices = 20;
  const int stacks = 20;

  // add a single primitive of the given type
  Primitive p(_type);
  primitives_.push_back(p);

  sphere_ = new ACG::GLSphere(slices, stacks);
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
void 
GlutPrimitiveNode::
set_position(const Vec3d& _p, int _idx)
{
  if (_idx > -1 && _idx < (int)primitives_.size())
    primitives_[_idx].position = _p; 
}

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

const Vec3d
GlutPrimitiveNode::
get_position(int _idx) const 
{
  if (_idx > -1 && _idx < (int)primitives_.size())
    return primitives_[_idx].position; 
  
  return Vec3d(-1,-1,-1);
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
125
126
127

void
GlutPrimitiveNode::
128
set_size(double _s, int _idx) 
Jan Möbius's avatar
 
Jan Möbius committed
129
{
130
131
  if (_idx > -1 && _idx < (int)primitives_.size())
    primitives_[_idx].size = _s; 
Jan Möbius's avatar
 
Jan Möbius committed
132
133
}

134
135
136
137
138
139
140
141
142
143
//----------------------------------------------------------------------------

double
GlutPrimitiveNode::
get_size(int _idx) const
{
  if (_idx > -1 && _idx < (int)primitives_.size())
    return primitives_[_idx].size; 
  return -1;
}
Jan Möbius's avatar
 
Jan Möbius committed
144
145
146

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

147
148
149
150
151
152
153
void
GlutPrimitiveNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
  for (int i = 0; i < (int)primitives_.size(); ++i)
  {
    Vec3d sizeVec(primitives_[i].size, primitives_[i].size, primitives_[i].size);
154
155
    _bbMax.maximize(primitives_[i].position + sizeVec);
    _bbMin.minimize(primitives_[i].position - sizeVec);
156
157
158
159
  }
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
160
  
Jan Möbius's avatar
Jan Möbius committed
161
DrawModes::DrawMode
Jan Möbius's avatar
 
Jan Möbius committed
162
GlutPrimitiveNode::
Jan Möbius's avatar
Jan Möbius committed
163
availableDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
164
165
166
167
168
{
  return ( DrawModes::POINTS              |
	   DrawModes::WIREFRAME           |
	   DrawModes::HIDDENLINE          |
	   DrawModes::SOLID_FLAT_SHADED   |
169
170
	   DrawModes::SOLID_SMOOTH_SHADED |
	   DrawModes::SOLID_FACES_COLORED );
Jan Möbius's avatar
 
Jan Möbius committed
171
172
173
174
175
176
}

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

void
GlutPrimitiveNode::
177
draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
178
{  
179
180
181
182
183
184
185
186
187
  float backupColorDiffuse[4];
  float backupColorAmbient[4];
  if ( setColor_ ) {
    glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
    glGetFloatv(GL_CURRENT_COLOR,backupColorDiffuse);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
    glGetFloatv(GL_CURRENT_COLOR,backupColorAmbient);
  }
  for (size_t i = 0; i < primitives_.size(); ++i)
188
  {
189

190
191
192
  
    glPushMatrix();
    glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
Jan Möbius's avatar
 
Jan Möbius committed
193
194


195
196
    if (_drawMode & DrawModes::POINTS)
    {
197
198
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
199
200
201
202
      glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
      draw_obj(i);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }
Jan Möbius's avatar
 
Jan Möbius committed
203
204


205
206
    if (_drawMode & DrawModes::WIREFRAME)
    {
207
208
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
209
210
211
212
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      draw_obj(i);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }
Jan Möbius's avatar
 
Jan Möbius committed
213

214
215
    if (_drawMode & DrawModes::SOLID_FACES_COLORED)
    {
216
217
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
218
219
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      
220
221
222
223
      if ( setColor_ ) {
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        glColor(primitives_[i].color);
      }
224
225
226
227
228

      draw_obj(i);
      
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }
Jan Möbius's avatar
 
Jan Möbius committed
229

230
231
232
    if (_drawMode & DrawModes::HIDDENLINE)
    {
      Vec4f base_color_backup = _state.base_color();
Jan Möbius's avatar
 
Jan Möbius committed
233

234
235
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
236

237
238
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      glColor(_state.clear_color());
Jan Möbius's avatar
Jan Möbius committed
239
      ACG::GLState::depthRange(0.01, 1.0);
240
      draw_obj(i);
Jan Möbius's avatar
 
Jan Möbius committed
241

242
243
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      glColor(base_color_backup);
Jan Möbius's avatar
Jan Möbius committed
244
      ACG::GLState::depthRange(0.0, 1.0);
245
      draw_obj(i);
Jan Möbius's avatar
 
Jan Möbius committed
246

247
248
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }
Jan Möbius's avatar
 
Jan Möbius committed
249
250


251
252
    if (_drawMode & DrawModes::SOLID_FLAT_SHADED)
    {
253
254
255
      ACG::GLState::enable( GL_COLOR_MATERIAL );
      ACG::GLState::enable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
256
      
257
258
259
260
      if ( setColor_ ) {
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        glColor(primitives_[i].color);
      }
261
262
263
      
      draw_obj(i);
    }
Jan Möbius's avatar
 
Jan Möbius committed
264
265


266
267
    if (_drawMode & DrawModes::SOLID_SMOOTH_SHADED)
    {
268
269
270
      ACG::GLState::enable( GL_COLOR_MATERIAL );
      ACG::GLState::enable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_SMOOTH);
271
      
272
273
274
275
      if ( setColor_ ) {
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        glColor(primitives_[i].color);
      }
Jan Möbius's avatar
 
Jan Möbius committed
276

277
278
      draw_obj(i);
    }
Jan Möbius's avatar
 
Jan Möbius committed
279

280
    glPopMatrix();
281
282
283
284
285
286
    if ( setColor_ ) {
      glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
      glColor4fv(backupColorDiffuse);
      glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
      glColor4fv(backupColorAmbient);
    }
287
  } // end of primitives iter
Jan Möbius's avatar
 
Jan Möbius committed
288
289
290
291
}

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

292
size_t
293
GlutPrimitiveNode::
294
add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec4f _color)
295
296
297
{
  Primitive p(_type, _pos, _axis, _color);
  primitives_.push_back(p);
298
  return primitives_.size() - 1;
299
300
301
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
302
303

void
304
GlutPrimitiveNode::draw_obj(int _idx) const
Jan Möbius's avatar
 
Jan Möbius committed
305
{
306
307
  if (_idx < 0 || _idx >= (int)primitives_.size()) // range check
    return;
308
309
310
311
312

  Vec3d axis  = primitives_[_idx].axis;
  double size = axis.norm();

  if (size > 1e-10)
Jan Möbius's avatar
 
Jan Möbius committed
313
  {
314
    glPushMatrix();
315

316
317
318
319
    Vec3d direction = axis;
    Vec3d z_axis(0,0,1);
    Vec3d rot_normal;
    double rot_angle;
Jan Möbius's avatar
 
Jan Möbius committed
320

321
322
323
324
    direction.normalize();
    rot_angle  = acos((z_axis | direction)) * 180 / M_PI;
    rot_normal = ((z_axis % direction).normalize());
  
Jan Möbius's avatar
 
Jan Möbius committed
325

326
327
328
329
    if (fabs(rot_angle) > 0.0001 && fabs(180 - rot_angle) > 0.0001)
      glRotatef(rot_angle,rot_normal[0], rot_normal[1], rot_normal[2]);
    else
      glRotatef(rot_angle,1,0,0);
Jan Möbius's avatar
 
Jan Möbius committed
330

331
332
333
334
335
336
    
    switch (primitives_[_idx].type)
    {
      case CONE: 
        glutSolidCone(primitives_[_idx].size, primitives_[_idx].innersize, primitives_[_idx].slices, primitives_[_idx].stacks);
        break;
337

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
      case CUBE: 
        glutSolidCube(primitives_[_idx].size);
        break;
      
      case DODECAHEDRON: 
        glutSolidDodecahedron();
        break;
      
      case ICOSAHEDRON: 
        glutSolidIcosahedron();
        break;

      case OCTAHEDRON:
        glutSolidOctahedron();
        break;

      case  SPHERE: 
        glutSolidSphere(primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
        break;
        
      case TEAPOT: 
        glutSolidTeapot(primitives_[_idx].size);
        break;

      case TETRAHEDRON: 
        glutSolidTetrahedron();
        break;

      case TORUS: 
        glutSolidTorus(primitives_[_idx].innersize, primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
        break;
    }

    glPopMatrix();
  }
Jan Möbius's avatar
 
Jan Möbius committed
373
374
375
376
377
378
}

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

void
GlutPrimitiveNode::
David Bommes's avatar
   
David Bommes committed
379
pick(GLState& _state , PickTarget _target)
Jan Möbius's avatar
 
Jan Möbius committed
380
{
David Bommes's avatar
   
David Bommes committed
381
  // initialize picking stack
382
  if (!_state.pick_set_maximum (primitives_.size()))
David Bommes's avatar
   
David Bommes committed
383
  {
384
    std::cerr << "Strange pickSetMaximum failed for index " << primitives_.size() << " in GlutPrimitiveNode\n";
David Bommes's avatar
   
David Bommes committed
385
386
387
    return;
  }

Jan Möbius's avatar
 
Jan Möbius committed
388
389
390
391
392
  switch (_target)
  {
    case PICK_ANYTHING:
    case PICK_FACE: 
    { 
393
394
395
396
397
398
399
400
      for (int i = 0; i < (int)primitives_.size(); ++i)
      {
        _state.pick_set_name(i);
        glPushMatrix();
        glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
        draw_obj(i);
        glPopMatrix();
      }
Jan Möbius's avatar
 
Jan Möbius committed
401
402
403
404
405
406
407
408
      break; 
    }

    default:
      break;
  }      
}

409
410
411
412
413
414
415
416
417
418
//----------------------------------------------------------------------------

void
GlutPrimitiveNode::
getRenderObjects(IRenderer* _renderer, GLState&  _state , const DrawModes::DrawMode&  _drawMode , const Material* _mat) {

  // init base render object
  RenderObject ro;
  ro.initFromState(&_state);

419
420
421
422
423
424
425
  // the selection sphere uses alpha blending against scene meshes
  //  set priority-order > 0 to draw this after meshes
  ro.priority = 1;

  // enable depth-test
  ro.depthTest = true;

426
427
428
  for (int i = 0; i < (int)primitives_.size(); ++i)
  {

Jan Möbius's avatar
Jan Möbius committed
429
430
431
432
433
    // Set the right position
    _state.push_modelview_matrix();
    _state.translate(primitives_[i].position);
    ro.modelview = _state.modelview();
    _state.pop_modelview_matrix();
434

Jan Möbius's avatar
Jan Möbius committed
435
    Material localMaterial = *_mat;
436
437
    if (setColor_)
    {
438
439
      //localMaterial.color(primitives_[i].color);
      //localMaterial.ambientColor(primitives_[i].color);
440
      localMaterial.diffuseColor(primitives_[i].color);
Jan Möbius's avatar
Jan Möbius committed
441
      localMaterial.baseColor(primitives_[i].color * .5f);
442
    }
443

Jan Möbius's avatar
Jan Möbius committed
444
    ro.setMaterial(&localMaterial);
445
    ro.shaderDesc.shadeMode = SG_SHADE_PHONG;
446
447
448

    switch (primitives_[i].type) {
      case SPHERE:
449

450
        // Sphere
451
        ro.debugName = (QString("glutprimitive.sphere no %1: ").arg(i)+QString(name().c_str())).toLatin1();
452
453
454
455
456
457
458

        sphere_->addToRenderer(_renderer, &ro, primitives_[i].size);

        break;

      default:
        // TODO: The other glut primitives are not yet supported by the advanced renderers
459
        std::cerr << "Sorry, but the glut renderer objects are not available for this renderer yet!" << std::endl;
460
461
462
463
464
465
466
467
        break;
    }


  }

}

Jan Möbius's avatar
 
Jan Möbius committed
468
469
470
471
472

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