GlutPrimitiveNode.cc 14.8 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * 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.              *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/




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


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

#include "GlutPrimitiveNode.hh"

#include <ACG/GL/IRenderer.hh>


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

namespace ACG {
namespace SceneGraph {


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

75
76
GlutPrimitiveNode::GlutPrimitiveNode( BaseNode*                 _parent,
                                      const std::string &       _name )
Jan Möbius's avatar
Jan Möbius committed
77
78
79
80
81
82
83
84
85
86
87
  : BaseNode(_parent, _name),
    setColor_(true)
{
  const int slices = 20;
  const int stacks = 20;

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

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

88
89
90
GlutPrimitiveNode::GlutPrimitiveNode(GlutPrimitiveType    _type,
                                     BaseNode*            _parent,
                                     const std::string &  _name) :
Jan Möbius's avatar
Jan Möbius committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
        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);
}

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);
}

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

void
GlutPrimitiveNode::
set_size(double _s, int _idx) 
{
  if (_idx > -1 && _idx < (int)primitives_.size())
    primitives_[_idx].size = _s; 
}

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

double
GlutPrimitiveNode::
get_size(int _idx) const
{
  if (_idx > -1 && _idx < (int)primitives_.size())
    return primitives_[_idx].size; 
  return -1;
}

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

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);
    _bbMax.maximize(primitives_[i].position + sizeVec);
    _bbMin.minimize(primitives_[i].position - sizeVec);
  }
}

//----------------------------------------------------------------------------
  
DrawModes::DrawMode
GlutPrimitiveNode::
availableDrawModes() const
{
  return ( DrawModes::POINTS              |
	   DrawModes::WIREFRAME           |
	   DrawModes::HIDDENLINE          |
	   DrawModes::SOLID_FLAT_SHADED   |
	   DrawModes::SOLID_SMOOTH_SHADED |
	   DrawModes::SOLID_FACES_COLORED );
}

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

void
GlutPrimitiveNode::
draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
{  
179
180
  Vec4f backupColorDiffuse;
  Vec4f backupColorAmbient;
Jan Möbius's avatar
Jan Möbius committed
181
  if ( setColor_ ) {
182
183
    backupColorDiffuse = _state.diffuse_color();
    backupColorAmbient = _state.ambient_color();
Jan Möbius's avatar
Jan Möbius committed
184
185
186
  }
  for (size_t i = 0; i < primitives_.size(); ++i)
  {
187
188
    _state.push_modelview_matrix();
    _state.translate(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
Jan Möbius's avatar
Jan Möbius committed
189
190
191
192


    if (_drawMode & DrawModes::POINTS)
    {
193
194
      if(setColor_)
        _state.set_color(primitives_[i].color);
Jan Möbius's avatar
Jan Möbius committed
195
196
197
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
      glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
198
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
199
200
201
202
203
204
205
206
207
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }


    if (_drawMode & DrawModes::WIREFRAME)
    {
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
208
209
      _state.set_color(primitives_[i].color);
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
210
211
212
213
214
215
216
217
218
219
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

    if (_drawMode & DrawModes::SOLID_FACES_COLORED)
    {
      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      
      if ( setColor_ ) {
220
221
222
        _state.set_diffuse_color(primitives_[i].color);
        _state.set_ambient_color(primitives_[i].color);
        _state.set_color(primitives_[i].color);
Jan Möbius's avatar
Jan Möbius committed
223
224
      }

225
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
226
227
228
229
230
231
232
233
234
235
236
      
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

    if (_drawMode & DrawModes::HIDDENLINE)
    {
      Vec4f base_color_backup = _state.base_color();

      ACG::GLState::disable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);

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

      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
243
244
245
246
      if(setColor_)
        _state.set_color(primitives_[i].color);
      else
        _state.set_color(base_color_backup);
Jan Möbius's avatar
Jan Möbius committed
247
      ACG::GLState::depthRange(0.0, 1.0);
248
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
249
250
251
252
253
254
255
256
257
258
259
260

      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }


    if (_drawMode & DrawModes::SOLID_FLAT_SHADED)
    {
      ACG::GLState::enable( GL_COLOR_MATERIAL );
      ACG::GLState::enable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_FLAT);
      
      if ( setColor_ ) {
261
262
263
        _state.set_diffuse_color(primitives_[i].color);
        _state.set_ambient_color(primitives_[i].color);
        _state.set_color(primitives_[i].color);
Jan Möbius's avatar
Jan Möbius committed
264
265
      }
      
266
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
267
268
269
270
271
272
273
274
275
276
    }


    if (_drawMode & DrawModes::SOLID_SMOOTH_SHADED)
    {
      ACG::GLState::enable( GL_COLOR_MATERIAL );
      ACG::GLState::enable(GL_LIGHTING);
      ACG::GLState::shadeModel(GL_SMOOTH);
      
      if ( setColor_ ) {
277
278
279
280
281
282
283
        _state.set_diffuse_color(primitives_[i].color);
        _state.set_ambient_color(primitives_[i].color);
        _state.set_color(primitives_[i].color);
        //not sure if ambient and diffuse color have to be set here.
        //the original call was:
        //glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        //glColor(primitives_[i].color);
Jan Möbius's avatar
Jan Möbius committed
284
285
      }

286
      draw_obj(_state, i);
Jan Möbius's avatar
Jan Möbius committed
287
288
    }

289
    _state.pop_modelview_matrix();
Jan Möbius's avatar
Jan Möbius committed
290
    if ( setColor_ ) {
291
292
      _state.set_diffuse_color(backupColorDiffuse);
      _state.set_ambient_color(backupColorAmbient);
Jan Möbius's avatar
Jan Möbius committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    }
  } // end of primitives iter
}

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

size_t
GlutPrimitiveNode::
add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec4f _color)
{
  Primitive p(_type, _pos, _axis, _color);
  primitives_.push_back(p);
  return primitives_.size() - 1;
}

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

void
311
GlutPrimitiveNode::draw_obj(GLState& _state, size_t _idx) const
Jan Möbius's avatar
Jan Möbius committed
312
313
314
315
316
317
318
319
320
{
  if ( _idx >= primitives_.size()) // range check
    return;

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

  if (size > 1e-10)
  {
321
    _state.push_modelview_matrix();
Jan Möbius's avatar
Jan Möbius committed
322
323
324
325
326
327
328
329
330
331
332
    Vec3d direction = axis;
    Vec3d z_axis(0,0,1);
    Vec3d 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)
333
334
335
    {
      _state.rotate(rot_angle,rot_normal[0], rot_normal[1], rot_normal[2]);
    }
Jan Möbius's avatar
Jan Möbius committed
336
    else
337
338
339
    {
      _state.rotate(rot_angle,1,0,0);
    }
Jan Möbius's avatar
Jan Möbius committed
340
341
342
343

    switch (primitives_[_idx].type)
    {
      case CONE: 
344
        ACG::GLCone(primitives_[_idx].slices, primitives_[_idx].stacks, primitives_[_idx].size, 0.0f, true, true).draw(_state, primitives_[_idx].innersize);
Jan Möbius's avatar
Jan Möbius committed
345
346
        break;

347
348
349
      case CUBE:
        _state.scale(primitives_[_idx].size);
        ACG::GLBox().draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
350
351
352
        break;
      
      case DODECAHEDRON: 
353
        ACG::GLDodecahedron().draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
354
355
356
        break;
      
      case ICOSAHEDRON: 
357
        ACG::GLIcosahedron().draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
358
359
360
        break;

      case OCTAHEDRON:
361
        ACG::GLOctahedron().draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
362
363
364
        break;

      case  SPHERE: 
365
        ACG::GLSphere(primitives_[_idx].slices, primitives_[_idx].stacks).draw(_state,primitives_[_idx].size);
Jan Möbius's avatar
Jan Möbius committed
366
367
        break;

368
369
      case TETRAHEDRON:       
        ACG::GLTetrahedron().draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
370
371
372
        break;

      case TORUS: 
Martin Schultz's avatar
Martin Schultz committed
373
        ACG::GLTorus(primitives_[_idx].innersize, primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks).draw_primitive();
Jan Möbius's avatar
Jan Möbius committed
374
375
376
        break;
    }

377
    _state.pop_modelview_matrix();
Jan Möbius's avatar
Jan Möbius committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  }
}

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

void
GlutPrimitiveNode::
pick(GLState& _state , PickTarget _target)
{
  // initialize picking stack
  if (!_state.pick_set_maximum (primitives_.size()))
  {
    std::cerr << "Strange pickSetMaximum failed for index " << primitives_.size() << " in GlutPrimitiveNode\n";
    return;
  }

  switch (_target)
  {
    case PICK_ANYTHING:
    case PICK_FACE: 
    { 
      for (size_t i = 0; i < primitives_.size(); ++i)
      {
        _state.pick_set_name(i);
402
403
        _state.push_modelview_matrix();
        _state.translate(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
404
        draw_obj(_state, i);
405
        _state.pop_modelview_matrix();
Jan Möbius's avatar
Jan Möbius committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
      }
      break; 
    }

    default:
      break;
  }      
}

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

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

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

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

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

    // Set the right position
    _state.push_modelview_matrix();
    _state.translate(primitives_[i].position);
    ro.modelview = _state.modelview();
    _state.pop_modelview_matrix();

    Material localMaterial = *_mat;
    if (setColor_)
    {
      //localMaterial.color(primitives_[i].color);
      //localMaterial.ambientColor(primitives_[i].color);
      localMaterial.diffuseColor(primitives_[i].color);
      localMaterial.baseColor(primitives_[i].color * .5f);
    }

    ro.setMaterial(&localMaterial);
    ro.shaderDesc.shadeMode = SG_SHADE_PHONG;

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

        // Sphere
457
        ro.debugName = std::string("glutprimitive.sphere no ") + std::to_string(i) + ": " + name();
Jan Möbius's avatar
Jan Möbius committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

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

        break;

      default:
        // TODO: The other glut primitives are not yet supported by the advanced renderers
        std::cerr << "Sorry, but the glut renderer objects are not available for this renderer yet!" << std::endl;
        break;
    }


  }

}


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