DrawModes.cc 29 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
63
64
65




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


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

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

#include <algorithm>
Matthias Möller's avatar
Matthias Möller committed
66
#include <sstream>
Jan Möbius's avatar
 
Jan Möbius committed
67
68
69
70
71
72
73
74
75


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

namespace ACG {
namespace SceneGraph {
namespace DrawModes {


76
// == Default Draw Mode initialization ======================================
Jan Möbius's avatar
Jan Möbius committed
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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;
111
112
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
113
  
Jan Möbius's avatar
Jan Möbius committed
114

Jan Möbius's avatar
 
Jan Möbius committed
115
116
117
//== IMPLEMENTATION ========================================================== 


118
119
120
121
122
123
124
125
/** \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
126
127
128
class DrawModeInternal {
  
  public:
129

Jan Möbius's avatar
 
Jan Möbius committed
130
    /** Initializing constructor. */
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    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
147
    
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    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
166
167
};

Jan Möbius's avatar
Jan Möbius committed
168
169

typedef std::vector< DrawModeInternal > VecDrawModes;
Jan Möbius's avatar
 
Jan Möbius committed
170
171
172

/** Vector of all currently defined DrawModes.
*/
173
static VecDrawModes registeredDrawModes_;
Jan Möbius's avatar
 
Jan Möbius committed
174
175

/** First free DrawMode ID for custom modes. */
Jan Möbius's avatar
Jan Möbius committed
176
static DrawMode firstFreeID_;
Jan Möbius's avatar
 
Jan Möbius committed
177
178


179
180
181
182
183
184
185
186
187
188
189
190
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)
191
{
192
193
194
}


Jan Möbius's avatar
Jan Möbius committed
195
DrawMode::DrawMode(size_t _index)
196
{
Jan Möbius's avatar
Jan Möbius committed
197
198
199
200
201
202
  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);
  }
203
  layers_.resize(1);
204
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
205
}
Jan Möbius's avatar
 
Jan Möbius committed
206

Jan Möbius's avatar
Jan Möbius committed
207
DrawMode::DrawMode() {
208
  layers_.resize(1);
209
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
210
211
}
    
Jan Möbius's avatar
Jan Möbius committed
212
213
214
DrawMode::DrawMode(ModeFlagSet _flags) :
        modeFlags_(_flags)
{
215
  layers_.resize(1);
216
  layers_[0] = DrawModeProperties();
Jan Möbius's avatar
Jan Möbius committed
217
}
Jan Möbius's avatar
 
Jan Möbius committed
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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;
}

239
DrawMode::operator bool() const {
Jan Möbius's avatar
Jan Möbius committed
240
241
  return( modeFlags_ != NONE.modeFlags_ );
}
242
/*
243
244
245
246
247
248
249
250
251
252
253
254
255
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;
  }
}
256
*/
257

258
void DrawMode::setDrawModeProperties(const DrawModeProperties* _props) {
259
260
261
262
263
264
265
266
267
  if (_props)
  {
    if (layers_.empty())
      layers_.push_back(*_props);
    else
      layers_[0] = *_props;
  }
  else
    layers_.erase(layers_.begin());
268
269
270
271
272
}

void DrawMode::setDrawModeProperties( const DrawModeProperties& _props )
{
  setDrawModeProperties(&_props);
273
274
}

Jan Möbius's avatar
Jan Möbius committed
275
bool DrawMode::operator==(const DrawMode& _mode) const {
276
277
    return modeFlags_ == _mode.modeFlags_;
  //return ((modeFlags_ & _mode.modeFlags_).any());
Jan Möbius's avatar
Jan Möbius committed
278
279
}

Jan Möbius's avatar
Jan Möbius committed
280
DrawMode & DrawMode::operator =(const DrawMode& _mode) {
281
  modeFlags_ = _mode.modeFlags_;
Jan Möbius's avatar
Jan Möbius committed
282
  layers_    = _mode.layers_;
283

Jan Möbius's avatar
Jan Möbius committed
284
285
  //  checkConsistency();
  return (*this);
286
287
}

Jan Möbius's avatar
Jan Möbius committed
288
289
290
291
292
293
294
295
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;
  }
296
297

  modeFlags_ <<= 1;
Jan Möbius's avatar
Jan Möbius committed
298
299
300
301
  
  return (*this);
}

302
DrawMode DrawMode::operator&(const DrawMode& _mode) const {
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  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
320
321
322
323
324
}

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

328
//  assert(checkConsistency());
329

Jan Möbius's avatar
Jan Möbius committed
330
331
  return (*this);
}
Jan Möbius's avatar
 
Jan Möbius committed
332

Jan Möbius's avatar
Jan Möbius committed
333
334
DrawMode& DrawMode::operator&=( const DrawMode& _mode2  ) {
  modeFlags_ &= _mode2.modeFlags_;
335
336
337
338
339
340
341
342
343

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

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

345
//  assert(checkConsistency());
346

Jan Möbius's avatar
Jan Möbius committed
347
348
349
350
  return (*this);
}

DrawMode DrawMode::operator|( const DrawMode& _mode2  ) const {
351
352
353
354
355
356
357
358
359
360
361
  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
362
363
364
}

DrawMode DrawMode::operator^( const DrawMode& _mode2  ) const {
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
410
411
412
413
414
415
416

  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())
417
418
  {
    xorMode.removeLayer(0u);
419
    return xorMode; // return default property set to not cause exceptions
420
  }
421
422
423
424
425

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

  xorMode.setDrawModeProperties(tmpLayers[0]);
426
  for (unsigned int i = 1; i < tmpLayers.size(); ++i)
427
428
    xorMode.addLayer(tmpLayers[i]);

429
430
431

//  assert(xorMode.checkConsistency());

432
  return xorMode;
Jan Möbius's avatar
Jan Möbius committed
433
434
435
436
437
438
439
440
}

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



Jan Möbius's avatar
Jan Möbius committed
441
size_t DrawMode::getIndex() const {
Jan Möbius's avatar
Jan Möbius committed
442
443
444
445
446
447
448
449
450
  if ( modeFlags_.count() == 1 ) {
    for ( size_t i = 0 ; i < modeFlags_.size() ; ++i ) 
     if ( modeFlags_[i] ) 
      return i; 
  } 
    
  return 0;
}

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

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

470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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));

495
//  checkConsistency();
496
497
498
499
500
//  assert(checkConsistency());
}

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

Jan Möbius's avatar
Jan Möbius committed
501
std::vector< DrawMode >
502
DrawMode::getAtomicDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
503
{
Jan Möbius's avatar
Jan Möbius committed
504
505
  std::vector< DrawMode > draw_mode_ids;
  
506
507
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
  for( modeIter = registeredDrawModes_.begin();
Jan Möbius's avatar
Jan Möbius committed
508
509
  modeIter != modeEnd;
  ++modeIter )
510
511
  if( (*this) & modeIter->id() )
    draw_mode_ids.push_back( modeIter->id() );
Jan Möbius's avatar
Jan Möbius committed
512
  
Jan Möbius's avatar
 
Jan Möbius committed
513
514
515
516
517
518
  return draw_mode_ids;
}


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

Jan Möbius's avatar
Jan Möbius committed
519
bool DrawMode::isAtomic() const {
Jan Möbius's avatar
Jan Möbius committed
520
521
522
523
524
  return(modeFlags_.count() == 1 );
}
 

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
525
526
 
bool
527
DrawMode::containsAtomicDrawMode( DrawMode _atomicDrawMode) const
Jan Möbius's avatar
 
Jan Möbius committed
528
{
Jan Möbius's avatar
Jan Möbius committed
529
530
531
532
533
  return (*this) & _atomicDrawMode;
}
 
//---------------------------------------------------------------------------- 
 
Jan Möbius's avatar
Jan Möbius committed
534
size_t DrawMode::maxModes() const {
Jan Möbius's avatar
Jan Möbius committed
535
  return (modeFlags_.size() );
Jan Möbius's avatar
 
Jan Möbius committed
536
}
537

Jan Möbius's avatar
Jan Möbius committed
538
size_t DrawMode::getNumLayers() const {
539
540
541
542
543
544
545
546
547
548
  return layers_.size();
}

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


void DrawMode::addLayer( const DrawModeProperties* _props )
{
549
  if (getLayerIndex(_props) < 0 && _props)
550
    layers_.push_back(*_props);
551
552
553
554
}

bool DrawMode::removeLayer( unsigned int _i )
{
Jan Möbius's avatar
Jan Möbius committed
555
  if (_i < layers_.size() )
556
557
558
559
560
561
  {
    layers_.erase(layers_.begin() + _i);
    return true;
  }

  return false;
562
563
}

564
565
566
567
568
569
570
571
572
573
bool DrawMode::removeLayer( const DrawModeProperties* _prop )
{
  int layerId = getLayerIndex(_prop);

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

  return false;
}

574
575
576
577
578
const DrawModeProperties* DrawMode::getDrawModeProperties() const
{
  return getLayer(0);
}

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

580
581
bool DrawMode::checkConsistency() const
{
582
583

  // allow at most one layer per primitive
584
585
586
587
588
589
590
591
  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;
    }
  }
592

593
594
595
596
597
598


  // bitflag -> layer parallelism

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

Hans-Christian Ebke's avatar
Hans-Christian Ebke committed
599
600
601
  if ((*this & DrawModes::POINTS) ||
      (*this & DrawModes::POINTS_COLORED) ||
      (*this & DrawModes::POINTS_SHADED))
602
603
604
605
606
607
608
609
610
611
612
613
614
  {
    int pointsLayer = 0;
    for (unsigned int k = 0; k < layers_.size(); ++k)
    {
      if (layers_[k].primitive() == PRIMITIVE_POINT)
        pointsLayer++;
    }

    if (!pointsLayer)
      return false;
  } 


615
616
617
618
619
  return true;
}

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

622
623
  for (unsigned int i = 0; i < layers_.size(); ++i)
  {
624
    if ( layers_[i] == *_prop )
625
      return (int)i;
626
627
628

    //    if (!memcmp(&layers_[i], &bla, sizeof(DrawModeProperties)))
    //      return (int)i;
629
630
631
632
  }
  return -1;
}

633
634
635
636
637
638
639
640
641
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;
}
642

Jan Möbius's avatar
 
Jan Möbius committed
643
644
645
646
647
648
649
650
651
652
//----------------------------------------------------------------------------


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

    if( initialized_ )
	return;

653
    registeredDrawModes_.clear();
Jan Möbius's avatar
 
Jan Möbius committed
654

655
656
657
658
659
660
661
662
663
664
665
    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));
666
    EDGES_COLORED.              setDrawModeProperties(DrawModeProperties(PRIMITIVE_EDGE, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_EDGE));
667
668
669
670
671
672
673
674
675
676
677
678

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

679
    SOLID_FACES_COLORED.        setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_UNLIT, NORMAL_NONE, COLOR_PER_FACE));
680
    
681
682
    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));
683
684
685
686
687
688
689
690
691
692
693
694
695
696

    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));
697
    SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED.setDrawModeProperties(DrawModeProperties(PRIMITIVE_POLYGON, LIGHTSTAGE_SMOOTH, NORMAL_PER_VERTEX, COLOR_PER_FACE, TEXCOORD_PER_HALFEDGE));
698

699
700
    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));
701
702
703
704
705
706
707
708
709
710

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


711
712
    registeredDrawModes_.push_back( DrawModeInternal( "<invalid>", NONE ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Default", DEFAULT ) );
Jan Möbius's avatar
 
Jan Möbius committed
713

714
715
716
    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
717
    
718
719
    registeredDrawModes_.push_back( DrawModeInternal( "Edges", EDGES ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Edges Colored", EDGES_COLORED ) );
Jan Möbius's avatar
Jan Möbius committed
720
    
721
    registeredDrawModes_.push_back( DrawModeInternal( "Wireframe", WIREFRAME ) );
Jan Möbius's avatar
Jan Möbius committed
722
    
723
    registeredDrawModes_.push_back( DrawModeInternal( "Faces", FACES ) );
Jan Möbius's avatar
 
Jan Möbius committed
724
    
725
    registeredDrawModes_.push_back( DrawModeInternal( "Hiddenline", HIDDENLINE ) );
Jan Möbius's avatar
 
Jan Möbius committed
726
    
727
728
729
    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
730
    
731
732
733
    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
734
    
735
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (environment mapped)", SOLID_ENV_MAPPED ) );
Jan Möbius's avatar
 
Jan Möbius committed
736
    
737
738
    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
739
    
740
741
    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
742
    
743
744
    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
745

746
747
    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
748

749
750
751
    registeredDrawModes_.push_back(DrawModeInternal("Solid (colored per-face, face textured, smooth shaded)", SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED));
    

752
753
754
    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 ) );
755

756
    registeredDrawModes_.push_back( DrawModeInternal( "Solid (smooth shaded, features)", SOLID_SMOOTH_SHADED_FEATURES ) );
757

758
759
    registeredDrawModes_.push_back( DrawModeInternal( "Cells", CELLS ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Cells Colored", CELLS_COLORED ) );
Jan Möbius's avatar
 
Jan Möbius committed
760
    
761
762
    registeredDrawModes_.push_back( DrawModeInternal( "Halfedges", HALFEDGES ) );
    registeredDrawModes_.push_back( DrawModeInternal( "Halfedges Colored", HALFEDGES_COLORED ) );
763

Jan Möbius's avatar
 
Jan Möbius committed
764
765
766
767
768
769
770
771
    firstFreeID_ = UNUSED;
    initialized_ = true;
}


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


772
const DrawMode& addDrawMode( const std::string & _name , bool _propertyBased)
Jan Möbius's avatar
 
Jan Möbius committed
773
{
774
  // check if mode exists already
775
  VecDrawModes::iterator modeIter, modeEnd( registeredDrawModes_.end() );
Jan Möbius's avatar
 
Jan Möbius committed
776

777
778
779
  for( modeIter = registeredDrawModes_.begin(); modeIter != modeEnd; ++modeIter ) {
    if( _name == modeIter->name() ) {
      return modeIter->id();
780
    }
781
  }
Jan Möbius's avatar
 
Jan Möbius committed
782
783


784
  // add new mode
785
  registeredDrawModes_.push_back( DrawModeInternal( _name, firstFreeID_ , _propertyBased) );
786
  ++firstFreeID_;
787

788
  return registeredDrawModes_[ registeredDrawModes_.size() - 1 ].id();
Jan Möbius's avatar
 
Jan Möbius committed
789
790
}

791
792
//----------------------------------------------------------------------------

793
ACGDLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
794
const DrawMode& addDrawMode( const std::string & _name, const DrawModeProperties& _properties)
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
{
  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;
}

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


814
const DrawMode& getDrawMode( const std::string & _name )
815
{
816
  // check if mode exists
817
  VecDrawModes::const_iterator modeIter, modeEnd( registeredDrawModes_.end() );
818

819
  for( modeIter = registeredDrawModes_.begin(); modeIter != modeEnd;  ++modeIter )
820
  {
821
    if( _name == modeIter->name() )
822
    {
823
      return modeIter->id();
824
    }
825
  }
826

827
828
  // the DrawMode does not exists
  return DrawModes::NONE;
829
830
}

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

846

Jan Möbius's avatar
Jan Möbius committed
847
848
849
DrawMode getDrawModeFromIndex( unsigned int _index ) {
  return DrawMode(_index);
}
Jan Möbius's avatar
 
Jan Möbius committed
850
851
852
853
854
855

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