DrawModes.cc 28.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58




//=============================================================================
//
//  CLASS DrawModes - IMPLEMENTATION
//
//=============================================================================


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

#include "DrawModes.hh"
#include "BaseNode.hh"

#include <algorithm>
Matthias Möller's avatar
Matthias Möller committed
59
#include <sstream>
Jan Möbius's avatar
 
Jan Möbius committed
60
61
62
63
64
65
66
67
68


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

namespace ACG {
namespace SceneGraph {
namespace DrawModes {


69
// == Default Draw Mode initialization ======================================
Jan Möbius's avatar
Jan Möbius committed
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
DrawMode NONE                              = ModeFlagSet(0);
DrawMode DEFAULT                           = ModeFlagSet(1);
DrawMode POINTS                            = ModeFlagSet(1) << 1;
DrawMode POINTS_COLORED                    = ModeFlagSet(1) << 2;
DrawMode POINTS_SHADED                     = ModeFlagSet(1) << 3;
DrawMode EDGES                             = ModeFlagSet(1) << 4;
DrawMode EDGES_COLORED                     = ModeFlagSet(1) << 5;
DrawMode WIREFRAME                         = ModeFlagSet(1) << 6;
DrawMode FACES                             = ModeFlagSet(1) << 7;
DrawMode HIDDENLINE                        = ModeFlagSet(1) << 8;
DrawMode SOLID_FLAT_SHADED                 = ModeFlagSet(1) << 9;
DrawMode SOLID_SMOOTH_SHADED               = ModeFlagSet(1) << 10;
DrawMode SOLID_PHONG_SHADED                = ModeFlagSet(1) << 11;
DrawMode SOLID_FACES_COLORED               = ModeFlagSet(1) << 12;
DrawMode SOLID_POINTS_COLORED              = ModeFlagSet(1) << 13;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
DrawMode SOLID_POINTS_COLORED_SHADED       = ModeFlagSet(1) << 14;
DrawMode SOLID_ENV_MAPPED                  = ModeFlagSet(1) << 15;
DrawMode SOLID_TEXTURED                    = ModeFlagSet(1) << 16;
DrawMode SOLID_TEXTURED_SHADED             = ModeFlagSet(1) << 17;
DrawMode SOLID_1DTEXTURED                  = ModeFlagSet(1) << 18;
DrawMode SOLID_1DTEXTURED_SHADED           = ModeFlagSet(1) << 19;
DrawMode SOLID_3DTEXTURED                  = ModeFlagSet(1) << 20;
DrawMode SOLID_3DTEXTURED_SHADED           = ModeFlagSet(1) << 21;
DrawMode SOLID_FACES_COLORED_FLAT_SHADED   = ModeFlagSet(1) << 22;
DrawMode SOLID_FACES_COLORED_SMOOTH_SHADED = ModeFlagSet(1) << 23;
DrawMode SOLID_2DTEXTURED_FACE             = ModeFlagSet(1) << 24;
DrawMode SOLID_2DTEXTURED_FACE_SHADED      = ModeFlagSet(1) << 25;
DrawMode SOLID_SHADER                      = ModeFlagSet(1) << 26;
DrawMode SOLID_SMOOTH_SHADED_FEATURES      = ModeFlagSet(1) << 27;
DrawMode CELLS                             = ModeFlagSet(1) << 28;
DrawMode CELLS_COLORED                     = ModeFlagSet(1) << 29;
DrawMode HALFEDGES                         = ModeFlagSet(1) << 30;
DrawMode HALFEDGES_COLORED                 = ModeFlagSet(1) << 31;
104
105
DrawMode SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED = ModeFlagSet(1) << 32;
DrawMode UNUSED                            = ModeFlagSet(1) << 33;
Jan Möbius's avatar
Jan Möbius committed
106
  
Jan Möbius's avatar
Jan Möbius committed
107

Jan Möbius's avatar
 
Jan Möbius committed
108
109
110
//== IMPLEMENTATION ========================================================== 


111
112
113
114
115
116
117
118
/** \brief Definition of a draw mode
 *
 * This class is used to collect Information about a DrawMode in one central class.
 * It stores the name and the id of a DrawMode.
 *
 * If the DrawMode gets switched to a property based one, this class will also
 * store the properties.
 */
Jan Möbius's avatar
Jan Möbius committed
119
120
121
class DrawModeInternal {
  
  public:
122

Jan Möbius's avatar
 
Jan Möbius committed
123
    /** Initializing constructor. */
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    DrawModeInternal(const std::string & _name, const DrawMode & _id, const bool _propertyBased = false) :
      name_(_name),
      id_(_id),
      propertyBased_(_propertyBased)
    {
    }

    /// Set the name of the DrawMode
    void name(const std::string& _name) {
      name_ = _name;
    }

    /// Get the name of the DrawMode
    const std::string& name() const {
      return name_;
    }
Jan Möbius's avatar
 
Jan Möbius committed
140
    
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    const DrawMode& id() const {
      return id_;
    }

    bool propertyBased() const {
      return propertyBased_;
    }


    DrawModeProperties& properties() {
      return properties_;
    }

  private:
    std::string        name_;          ///< Human Readable Name
    DrawMode           id_;            ///< The id of the DrawMode
    bool               propertyBased_; ///< Flag if the DrawMode is property based
    DrawModeProperties properties_;    ///< The properties associated with this DrawMode
Jan Möbius's avatar
 
Jan Möbius committed
159
160
};

Jan Möbius's avatar
Jan Möbius committed
161
162

typedef std::vector< DrawModeInternal > VecDrawModes;
Jan Möbius's avatar
 
Jan Möbius committed
163
164
165

/** Vector of all currently defined DrawModes.
*/
166
static VecDrawModes registeredDrawModes_;
Jan Möbius's avatar
 
Jan Möbius committed
167
168

/** First free DrawMode ID for custom modes. */
Jan Möbius's avatar
Jan Möbius committed
169
static DrawMode firstFreeID_;
Jan Möbius's avatar
 
Jan Möbius committed
170
171


172
173
174
175
176
177
178
179
180
181
182
183
DrawModeProperties::DrawModeProperties(DrawModePrimitive _primitive, 
                                       DrawModeLightStage _lightStage, 
                                       DrawModeNormalSource _normalSource,
                                       DrawModeColorSource _colorSource,
                                       DrawModeTexCoordSource _texcoordSource,
                                       bool _envMapping):
envMapped_(_envMapping),
primitive_(_primitive),
lightStage_(_lightStage),
colorSource_(_colorSource),
texcoordSource_(_texcoordSource),
normalSource_(_normalSource)
184
{
185
186
187
}


Jan Möbius's avatar
Jan Möbius committed
188
DrawMode::DrawMode(size_t _index)
189
{
Jan Möbius's avatar
Jan Möbius committed
190
191
192
193
194
195
  modeFlags_.reset();
  if ( _index >= modeFlags_.size() ) {
    std::cerr << "Illegal drawMode specification from unsigned int. This should not be a bitset!!!" << std::endl;
  } else {
    modeFlags_.set(_index);
  }
196
  layers_.resize(1);
197
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
198
}
Jan Möbius's avatar
 
Jan Möbius committed
199

Jan Möbius's avatar
Jan Möbius committed
200
DrawMode::DrawMode() {
201
  layers_.resize(1);
202
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
203
204
}
    
Jan Möbius's avatar
Jan Möbius committed
205
206
207
DrawMode::DrawMode(ModeFlagSet _flags) :
        modeFlags_(_flags)
{
208
  layers_.resize(1);
209
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
210
}
Jan Möbius's avatar
 
Jan Möbius committed
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
DrawMode DrawMode::getFromDescription(std::string _description)
{
    DrawMode val;
    bool found = false;
    std::istringstream f(_description);
    std::string s;
    while (std::getline(f, s, '+')) {
        VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
            for( modeIter = registeredDrawModes_.begin();  modeIter != modeEnd;  ++modeIter ) {
                if(modeIter->name() == s) {
                    val |= modeIter->id();
                    found = true;
                }
            }
    }
    if(!found)
        return DEFAULT;
    else return val;
}

232
DrawMode::operator bool() const {
Jan Möbius's avatar
Jan Möbius committed
233
234
  return( modeFlags_ != NONE.modeFlags_ );
}
235
/*
236
237
238
239
240
241
242
243
244
245
246
247
248
bool DrawMode::propertyBased() const {
  if ( isAtomic() ) {
    return registeredDrawModes_[getIndex()].propertyBased();
  } else {
    // Combined drawmode, iterate over all contained modes and return
    VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
    for( modeIter = registeredDrawModes_.begin();  modeIter != modeEnd;  ++modeIter )
      if( ((*this) & modeIter->id()) && modeIter->propertyBased() )
        return true;

    return false;
  }
}
249
*/
250

251
void DrawMode::setDrawModeProperties(const DrawModeProperties* _props) {
252
253
254
255
256
257
258
259
260
  if (_props)
  {
    if (layers_.empty())
      layers_.push_back(*_props);
    else
      layers_[0] = *_props;
  }
  else
    layers_.erase(layers_.begin());
261
262
263
264
265
}

void DrawMode::setDrawModeProperties( const DrawModeProperties& _props )
{
  setDrawModeProperties(&_props);
266
267
}

Jan Möbius's avatar
Jan Möbius committed
268
bool DrawMode::operator==(const DrawMode& _mode) const {
269
270
    return modeFlags_ == _mode.modeFlags_;
  //return ((modeFlags_ & _mode.modeFlags_).any());
Jan Möbius's avatar
Jan Möbius committed
271
272
}

Jan Möbius's avatar
Jan Möbius committed
273
DrawMode & DrawMode::operator =(const DrawMode& _mode) {
274
  modeFlags_ = _mode.modeFlags_;
Jan Möbius's avatar
Jan Möbius committed
275
  layers_    = _mode.layers_;
276

Jan Möbius's avatar
Jan Möbius committed
277
278
  //  checkConsistency();
  return (*this);
279
280
}

Jan Möbius's avatar
Jan Möbius committed
281
282
283
284
285
286
287
288
bool DrawMode::operator!=( const DrawMode& _mode2  ) const {
  return (modeFlags_ != _mode2.modeFlags_);
}

DrawMode& DrawMode::operator++() {
  if ( modeFlags_.count() != 1 ) {
    std::cerr << "Operator ++ for drawMode which is not atomic!!" << std::endl;
  }
289
290

  modeFlags_ <<= 1;
Jan Möbius's avatar
Jan Möbius committed
291
292
293
294
  
  return (*this);
}

295
DrawMode DrawMode::operator&(const DrawMode& _mode) const {
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  DrawMode andMode = (modeFlags_ & _mode.modeFlags_);

  andMode.setDrawModeProperties(getDrawModeProperties());

  for (unsigned int i = 1; i < getNumLayers(); ++i)
    andMode.addLayer(getLayer(i));

  // remove all distinct layers
  for (int i = (int)andMode.getNumLayers() - 1; i >= 0; --i)
  {
    int layerIndex = _mode.getLayerIndex(andMode.getLayer(i));

    if (layerIndex < 0)
      andMode.removeLayer(i);
  }

  return andMode;
Jan Möbius's avatar
Jan Möbius committed
313
314
315
316
317
}

DrawMode& DrawMode::operator|=( const DrawMode& _mode2  ) {
  modeFlags_ |= _mode2.modeFlags_;
  
318
319
320
  for (unsigned int i = 0; i < _mode2.getNumLayers(); ++i)
    addLayer(_mode2.getLayer(i));

321
//  assert(checkConsistency());
322

Jan Möbius's avatar
Jan Möbius committed
323
324
  return (*this);
}
Jan Möbius's avatar
 
Jan Möbius committed
325

Jan Möbius's avatar
Jan Möbius committed
326
327
DrawMode& DrawMode::operator&=( const DrawMode& _mode2  ) {
  modeFlags_ &= _mode2.modeFlags_;
328
329
330
331
332
333
334
335
336

  // remove all distinct layers
  for (int i = (int)getNumLayers() - 1; i >= 0; --i)
  {
    int layerIndex2 = _mode2.getLayerIndex(getLayer(i));

    if (layerIndex2 < 0)
      removeLayer(i);
  }
337

338
//  assert(checkConsistency());
339

Jan Möbius's avatar
Jan Möbius committed
340
341
342
343
  return (*this);
}

DrawMode DrawMode::operator|( const DrawMode& _mode2  ) const {
344
345
346
347
348
349
350
351
352
353
354
  DrawMode combined = ( modeFlags_ | _mode2.modeFlags_ );

  combined.setDrawModeProperties(getDrawModeProperties());

  for (unsigned int i = 1; i < getNumLayers(); ++i)
    combined.addLayer(getLayer(i));

  for (unsigned int i = 0; i < _mode2.getNumLayers(); ++i)
    combined.addLayer(_mode2.getLayer(i));

  return combined;
Jan Möbius's avatar
Jan Möbius committed
355
356
357
}

DrawMode DrawMode::operator^( const DrawMode& _mode2  ) const {
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

  DrawMode xorMode = ( modeFlags_ ^ _mode2.modeFlags_ );


  // xor on properties
  const DrawModeProperties* curProps = 0;

  // do xor on new temporary DrawMode
  //  internal layers of this and _mode2 must stay the same
  std::vector<const DrawModeProperties*> tmpLayers;


  // initialize tmpLayers with my own layers
  for (unsigned int i = 0; i < getNumLayers(); ++i)
  {
    curProps = getLayer(i);

    if (curProps)
      tmpLayers.push_back(curProps);
  }


  // xor on tmpLayers
  for (unsigned int i = 0; i < _mode2.getNumLayers(); ++i)
  {
    curProps = _mode2.getLayer(i);

    if (!curProps) continue;
   


    int addToVec = 1;

    // is the other layer already contained in my own list?
    for (unsigned int k = 0; addToVec && k < tmpLayers.size(); ++k)
    {
      if (!memcmp(tmpLayers[k], curProps, sizeof(DrawModeProperties)))
      {
        // yes, remove it  (layer exists in both drawmodes)
        tmpLayers.erase(tmpLayers.begin() + k);
        addToVec = 0;
      }
    }

    if (addToVec) // no, add it
      tmpLayers.push_back(curProps);
  }



  // DrawModes equal?
  if (tmpLayers.empty())
410
411
  {
    xorMode.removeLayer(0u);
412
    return xorMode; // return default property set to not cause exceptions
413
  }
414
415
416
417
418

  // layers not empty,
  //  copy to temporary drawmode and return

  xorMode.setDrawModeProperties(tmpLayers[0]);
419
  for (unsigned int i = 1; i < tmpLayers.size(); ++i)
420
421
    xorMode.addLayer(tmpLayers[i]);

422
423
424

//  assert(xorMode.checkConsistency());

425
  return xorMode;
Jan Möbius's avatar
Jan Möbius committed
426
427
428
429
430
431
432
433
}

DrawMode DrawMode::operator~( ) const {
  return( ~modeFlags_ );
}    



Jan Möbius's avatar
Jan Möbius committed
434
size_t DrawMode::getIndex() const {
Jan Möbius's avatar
Jan Möbius committed
435
436
437
438
439
440
441
442
443
  if ( modeFlags_.count() == 1 ) {
    for ( size_t i = 0 ; i < modeFlags_.size() ; ++i ) 
     if ( modeFlags_[i] ) 
      return i; 
  } 
    
  return 0;
}

444
std::string DrawMode::description() const
Jan Möbius's avatar
Jan Möbius committed
445
{
Jan Möbius's avatar
 
Jan Möbius committed
446
447
  std::string text("");
  
448
449
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
  for( modeIter = registeredDrawModes_.begin();  modeIter != modeEnd;  ++modeIter )
Jan Möbius's avatar
 
Jan Möbius committed
450
  {
451
    if( (*this) & modeIter->id() )
Jan Möbius's avatar
 
Jan Möbius committed
452
453
    {
      if (!text.empty()) text += "+";
454
      text += modeIter->name();
Jan Möbius's avatar
 
Jan Möbius committed
455
456
    }
  }
Jan Möbius's avatar
Jan Möbius committed
457
  
Jan Möbius's avatar
 
Jan Möbius committed
458
459
460
461
462
  return text;
}

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

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
void DrawMode::filter( DrawMode _filter )
{
  modeFlags_ = (modeFlags_ | _filter.modeFlags_) ^ _filter.modeFlags_;

  for (unsigned int i = 0; i < _filter.getNumLayers(); ++i)
  {
    int idx = getLayerIndex(_filter.getLayer(i));

    removeLayer((unsigned int)idx);
  }
}



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

void DrawMode::combine( DrawMode _mode )
{
  // XOR on bitflag
  modeFlags_ = (modeFlags_ ^ _mode.modeFlags_);

  // addLayer does redundancy check here
  for (unsigned int i = 0; i < _mode.getNumLayers(); ++i)
    addLayer(_mode.getLayer(i));

488
//  checkConsistency();
489
490
491
492
493
//  assert(checkConsistency());
}

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

Jan Möbius's avatar
Jan Möbius committed
494
std::vector< DrawMode >
495
DrawMode::getAtomicDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
496
{
Jan Möbius's avatar
Jan Möbius committed
497
498
  std::vector< DrawMode > draw_mode_ids;
  
499
500
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
  for( modeIter = registeredDrawModes_.begin();
Jan Möbius's avatar
Jan Möbius committed
501
502
  modeIter != modeEnd;
  ++modeIter )
503
504
  if( (*this) & modeIter->id() )
    draw_mode_ids.push_back( modeIter->id() );
Jan Möbius's avatar
Jan Möbius committed
505
  
Jan Möbius's avatar
 
Jan Möbius committed
506
507
508
509
510
511
  return draw_mode_ids;
}


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

Jan Möbius's avatar
Jan Möbius committed
512
bool DrawMode::isAtomic() const {
Jan Möbius's avatar
Jan Möbius committed
513
514
515
516
517
  return(modeFlags_.count() == 1 );
}
 

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
518
519
 
bool
520
DrawMode::containsAtomicDrawMode( DrawMode _atomicDrawMode) const
Jan Möbius's avatar
 
Jan Möbius committed
521
{
Jan Möbius's avatar
Jan Möbius committed
522
523
524
525
526
  return (*this) & _atomicDrawMode;
}
 
//---------------------------------------------------------------------------- 
 
Jan Möbius's avatar
Jan Möbius committed
527
size_t DrawMode::maxModes() const {
Jan Möbius's avatar
Jan Möbius committed
528
  return (modeFlags_.size() );
Jan Möbius's avatar
 
Jan Möbius committed
529
}
530

Jan Möbius's avatar
Jan Möbius committed
531
size_t DrawMode::getNumLayers() const {
532
533
534
535
536
537
538
539
540
541
  return layers_.size();
}

const DrawModeProperties* DrawMode::getLayer( unsigned int i ) const {
  return (i >= layers_.size() ? 0 : &layers_[i]);
}


void DrawMode::addLayer( const DrawModeProperties* _props )
{
542
  if (getLayerIndex(_props) < 0 && _props)
543
    layers_.push_back(*_props);
544
545
546
547
}

bool DrawMode::removeLayer( unsigned int _i )
{
Jan Möbius's avatar
Jan Möbius committed
548
  if (_i < layers_.size() )
549
550
551
552
553
554
  {
    layers_.erase(layers_.begin() + _i);
    return true;
  }

  return false;
555
556
}

557
558
559
560
561
562
563
564
565
566
bool DrawMode::removeLayer( const DrawModeProperties* _prop )
{
  int layerId = getLayerIndex(_prop);

  if (layerId >= 0)
    return removeLayer((unsigned int)layerId);

  return false;
}

567
568
569
570
571
const DrawModeProperties* DrawMode::getDrawModeProperties() const
{
  return getLayer(0);
}

Jan Möbius's avatar
 
Jan Möbius committed
572

573
574
bool DrawMode::checkConsistency() const
{
575
576

  // allow at most one layer per primitive
577
578
579
580
581
582
583
584
  for (unsigned int i = 0; i < layers_.size(); ++i)
  {
    for (unsigned int k = i+1; k < layers_.size(); ++k)
    {
      if (layers_[i].primitive() == layers_[k].primitive())
        return false;
    }
  }
585

586
587
588
589
590
591


  // bitflag -> layer parallelism

  // points-mode in bitflag => point layer expected

Hans-Christian Ebke's avatar
Hans-Christian Ebke committed
592
593
594
  if ((*this & DrawModes::POINTS) ||
      (*this & DrawModes::POINTS_COLORED) ||
      (*this & DrawModes::POINTS_SHADED))
595
596
597
598
599
600
601
602
603
604
605
606
607
  {
    int pointsLayer = 0;
    for (unsigned int k = 0; k < layers_.size(); ++k)
    {
      if (layers_[k].primitive() == PRIMITIVE_POINT)
        pointsLayer++;
    }

    if (!pointsLayer)
      return false;
  } 


608
609
610
611
612
  return true;
}

int DrawMode::getLayerIndex( const DrawModeProperties* _prop ) const
{
613
614
  if (!_prop) return -1;

615
616
  for (unsigned int i = 0; i < layers_.size(); ++i)
  {
617
    if ( layers_[i] == *_prop )
618
      return (int)i;
619
620
621

    //    if (!memcmp(&layers_[i], &bla, sizeof(DrawModeProperties)))
    //      return (int)i;
622
623
624
625
  }
  return -1;
}

626
627
628
629
630
631
632
633
634
int DrawMode::getLayerIndexByPrimitive( DrawModePrimitive _type ) const
{
  for (unsigned int i = 0; i < layers_.size(); ++i)
  {
    if ( layers_[i].primitive() == _type )
      return (int)i;
  }
  return -1;
}
635

Jan Möbius's avatar
 
Jan Möbius committed
636
637
638
639
640
641
642
643
644
645
//----------------------------------------------------------------------------


void initializeDefaultDrawModes( void )
{
    static bool initialized_ = false;

    if( initialized_ )
	return;

646
    registeredDrawModes_.clear();
Jan Möbius's avatar
 
Jan Möbius committed
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
    NONE.removeLayer(0u);
    DEFAULT.removeLayer(0u);

    POINTS.                     setDrawModeProperties(DrawModeProperties(PRIMITIVE_POINT));
    POINTS_COLORED.             setDrawModeProperties(DrawModeProperties(PRIMITIVE_POINT, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_VERTEX));
    POINTS_SHADED.              setDrawModeProperties(DrawModeProperties(PRIMITIVE_POINT, LIGHTSTAGE_SMOOTH, NORMAL_PER_VERTEX));

    EDGES.                      setDrawModeProperties(DrawModeProperties(PRIMITIVE_EDGE));
    EDGES_COLORED.              setDrawModeProperties(DrawModeProperties(PRIMITIVE_EDGE, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_VERTEX));

    WIREFRAME.                  setDrawModeProperties(DrawModeProperties(PRIMITIVE_EDGE));
    EDGES_COLORED.              setDrawModeProperties(DrawModeProperties(PRIMITIVE_EDGE, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_VERTEX));

    WIREFRAME.                  setDrawModeProperties(DrawModeProperties(PRIMITIVE_WIREFRAME));

    FACES.                      setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON));

    HIDDENLINE.                 setDrawModeProperties(DrawModeProperties(PRIMITIVE_HIDDENLINE));

    SOLID_FLAT_SHADED.          setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_FACE));
    SOLID_SMOOTH_SHADED.        setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_VERTEX));

    SOLID_PHONG_SHADED.         setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_PHONG, NORMAL_PER_VERTEX));

672
    SOLID_FACES_COLORED.        setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_FACE));
673
    
674
675
    SOLID_POINTS_COLORED.       setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT,   NORMAL_NONE,      COLOR_PER_VERTEX));
    SOLID_POINTS_COLORED_SHADED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH,  NORMAL_PER_FACE,  COLOR_PER_VERTEX));
676
677
678
679
680
681
682
683
684
685
686
687
688
689

    SOLID_ENV_MAPPED.           setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT, NORMAL_NONE,      COLOR_NONE, TEXCOORD_PER_VERTEX, true));

    SOLID_TEXTURED.             setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT,   NORMAL_NONE,      COLOR_NONE, TEXCOORD_PER_VERTEX));
    SOLID_TEXTURED_SHADED.      setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH,  NORMAL_PER_VERTEX,COLOR_NONE, TEXCOORD_PER_VERTEX));

    SOLID_1DTEXTURED.           setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT,   NORMAL_NONE,  COLOR_NONE, TEXCOORD_PER_VERTEX));
    SOLID_1DTEXTURED_SHADED.    setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH,  NORMAL_PER_VERTEX,COLOR_NONE, TEXCOORD_PER_VERTEX));

    SOLID_3DTEXTURED.           setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT,   NORMAL_NONE,  COLOR_NONE, TEXCOORD_PER_VERTEX));
    SOLID_3DTEXTURED_SHADED.    setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH,  NORMAL_PER_VERTEX,COLOR_NONE, TEXCOORD_PER_VERTEX));

    SOLID_FACES_COLORED_FLAT_SHADED.  setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_FACE,   COLOR_PER_FACE));
    SOLID_FACES_COLORED_SMOOTH_SHADED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_VERTEX, COLOR_PER_FACE));
690
    SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_VERTEX, COLOR_PER_FACE, TEXCOORD_PER_HALFEDGE));
691

692
693
    SOLID_2DTEXTURED_FACE.       setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_NONE, TEXCOORD_PER_HALFEDGE));
    SOLID_2DTEXTURED_FACE_SHADED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_FACE, COLOR_NONE, TEXCOORD_PER_HALFEDGE));
694
695
696
697
698
699
700
701
702
703

    SOLID_SMOOTH_SHADED_FEATURES.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_HALFEDGE));

    CELLS.setDrawModeProperties(DrawModeProperties(PRIMITIVE_CELL));
    CELLS_COLORED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_CELL, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_VERTEX));

    HALFEDGES.setDrawModeProperties(DrawModeProperties(PRIMITIVE_HALFEDGE));
    HALFEDGES_COLORED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_HALFEDGE, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_HALFEDGE));


704
705
    registeredDrawModes_.push_back( DrawModeInternal( "<invalid>", NONE ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Default", DEFAULT ) );
Jan Möbius's avatar
 
Jan Möbius committed
706

707
708
709
    registeredDrawModes_.push_back( DrawModeInternal( "Points", POINTS ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Points (colored)", POINTS_COLORED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Points (shaded)", POINTS_SHADED  ) );
Jan Möbius's avatar
Jan Möbius committed
710
    
711
712
    registeredDrawModes_.push_back( DrawModeInternal( "Edges", EDGES ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Edges Colored", EDGES_COLORED ) );
Jan Möbius's avatar
Jan Möbius committed
713
    
714
    registeredDrawModes_.push_back( DrawModeInternal( "Wireframe", WIREFRAME ) );
Jan Möbius's avatar
Jan Möbius committed
715
    
716
    registeredDrawModes_.push_back( DrawModeInternal( "Faces", FACES ) );
Jan Möbius's avatar
 
Jan Möbius committed
717
    
718
    registeredDrawModes_.push_back( DrawModeInternal( "Hiddenline", HIDDENLINE ) );
Jan Möbius's avatar
 
Jan Möbius committed
719
    
720
721
722
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (flat shaded)", SOLID_FLAT_SHADED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (smooth shaded)", SOLID_SMOOTH_SHADED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (Phong shaded)", SOLID_PHONG_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
723
    
724
725
726
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (colored per-face)", SOLID_FACES_COLORED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (colored per-vertex)", SOLID_POINTS_COLORED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (colored per-vertex, shaded)", SOLID_POINTS_COLORED_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
727
    
728
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (environment mapped)", SOLID_ENV_MAPPED ) );
Jan Möbius's avatar
 
Jan Möbius committed
729
    
730
731
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (textured)", SOLID_TEXTURED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (textured, shaded)", SOLID_TEXTURED_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
732
    
733
734
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (scalar field)", SOLID_1DTEXTURED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (scalar field, shaded)", SOLID_1DTEXTURED_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
735
    
736
737
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (3D textured)", SOLID_3DTEXTURED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (3D textured, shaded)", SOLID_3DTEXTURED_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
738

739
740
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (colored per-face, flat shaded)", SOLID_FACES_COLORED_FLAT_SHADED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (colored per-face, smooth shaded)", SOLID_FACES_COLORED_SMOOTH_SHADED ) );
Jan Möbius's avatar
 
Jan Möbius committed
741

742
743
744
    registeredDrawModes_.push_back(DrawModeInternal("Solid (colored per-face, face textured, smooth shaded)", SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED));
    

745
746
747
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (face textured)", SOLID_2DTEXTURED_FACE ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (face textured, shaded)", SOLID_2DTEXTURED_FACE_SHADED ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Shader controlled", SOLID_SHADER ) );
748

749
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (smooth shaded, features)", SOLID_SMOOTH_SHADED_FEATURES ) );
750

751
752
    registeredDrawModes_.push_back( DrawModeInternal( "Cells", CELLS ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Cells Colored", CELLS_COLORED ) );
Jan Möbius's avatar
 
Jan Möbius committed
753
    
754
755
    registeredDrawModes_.push_back( DrawModeInternal( "Halfedges", HALFEDGES ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Halfedges Colored", HALFEDGES_COLORED ) );
756

Jan Möbius's avatar
 
Jan Möbius committed
757
758
759
760
761
762
763
764
    firstFreeID_ = UNUSED;
    initialized_ = true;
}


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


765
const DrawMode& addDrawMode( const std::string & _name , bool _propertyBased)
Jan Möbius's avatar
 
Jan Möbius committed
766
{
767
  // check if mode exists already
768
  VecDrawModes::iterator modeIter, modeEnd( registeredDrawModes_.end() );
Jan Möbius's avatar
 
Jan Möbius committed
769

770
771
772
  for( modeIter = registeredDrawModes_.begin(); modeIter != modeEnd; ++modeIter ) {
    if( _name == modeIter->name() ) {
      return modeIter->id();
773
    }
774
  }
Jan Möbius's avatar
 
Jan Möbius committed
775
776


777
  // add new mode
778
  registeredDrawModes_.push_back( DrawModeInternal( _name, firstFreeID_ , _propertyBased) );
779
  ++firstFreeID_;
780

781
  return registeredDrawModes_[ registeredDrawModes_.size() - 1 ].id();
Jan Möbius's avatar
 
Jan Möbius committed
782
783
}

784
785
//----------------------------------------------------------------------------

786
ACGDLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
787
const DrawMode& addDrawMode( const std::string & _name, const DrawModeProperties& _properties)
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
{
  const DrawMode& drawmode = addDrawMode( _name , true );

  // Get the internal DrawMode
  VecDrawModes::iterator modeIter, modeEnd( registeredDrawModes_.end() );

  for( modeIter = registeredDrawModes_.begin(); modeIter != modeEnd; ++modeIter ) {
    if( _name == modeIter->name() ) {
      modeIter->properties() = _properties;
      return drawmode;
    }
  }

  return drawmode;
}

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


807
const DrawMode& getDrawMode( const std::string & _name )
808
{
809
  // check if mode exists
810
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
811

812
  for( modeIter = registeredDrawModes_.begin(); modeIter != modeEnd;  ++modeIter )
813
  {
814
    if( _name == modeIter->name() )
815
    {
816
      return modeIter->id();
817
    }
818
  }
819

820
821
  // the DrawMode does not exists
  return DrawModes::NONE;
822
823
}

Jan Möbius's avatar
Jan Möbius committed
824
825
826
bool drawModeExists(const std::string & _name) {
  
  // check if mode exists
827
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
Jan Möbius's avatar
Jan Möbius committed
828
  
829
  for( modeIter = registeredDrawModes_.begin();  modeIter != modeEnd;  ++modeIter )
Jan Möbius's avatar
Jan Möbius committed
830
  {
831
    if( _name == modeIter->name() )
Jan Möbius's avatar
Jan Möbius committed
832
833
834
835
836
837
838
      return true;
  }
  
  // the DrawMode does not exists
  return false;  
}

839

Jan Möbius's avatar
Jan Möbius committed
840
841
842
DrawMode getDrawModeFromIndex( unsigned int _index ) {
  return DrawMode(_index);
}
Jan Möbius's avatar
 
Jan Möbius committed
843
844
845
846
847
848

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