ColorStack.hh 6.95 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
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
41

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



//=============================================================================
//
//  CLASS ColorStack
//
//=============================================================================

#ifndef ACG_COLORSTACK_HH
#define ACG_COLORSTACK_HH


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

#include <vector>

Jan Möbius's avatar
evert    
Jan Möbius committed
66
#include <ACG/GL/gl.hh>
Jan Möbius's avatar
Jan Möbius committed
67
#include <ACG/Math/VectorT.hh>
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "ColorTranslator.hh"


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


namespace ACG {


//== CLASS DEFINITION =========================================================


/** This class can be used to implement a gl picking stack based on colors
*/
class ACGDLLEXPORT ColorStack
{
public:
   
  /// Default constructor.
  ColorStack();
   /// Destructor.
  ~ColorStack();

  
  /// init (takes current GL context/ like glInitNames (); glPushName (0))
  void initialize();
  /// has it been initialized?
  bool initialized() const { return initialized_; }

  /// sets the maximum index number used in current node 
98
  bool setMaximumIndex (size_t _idx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
99
100

  /// sets the current color the given index (like glLoadName)
101
  void setIndex (size_t _idx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
102

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
103
  /// gets the color instead of setting it directly
104
  Vec4uc getIndexColor (size_t _idx);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
105

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
106
  /// creates a new node the stack (like glPushName)
107
  void pushIndex (size_t _idx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
108
109
110
111
112

  /// pops the current node from the stack (like glPopName)
  void popIndex ();

  /// converts the given color to index values on the stack
113
  std::vector<size_t> colorToStack (Vec4uc _rgba) const;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
114
115

  /// returns maximal available index count
116
  size_t freeIndicies () const;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
117
118
119
120
121

  /// Did an error occur during picking
  bool error () const { return error_ && initialized_; };

  /// returns the current color index
122
  size_t currentIndex () const;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
123
124
125
126
127
128
129

private:

  // Internal class used realize the color stack

  class Node {
    public:
130
      Node (size_t _idx, Node *_parent, ColorTranslator *_ct);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
131
132
133
      ~Node ();

      /// sets the maximum index number used in current node 
134
      bool setMaximumIndex (size_t _idx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
135
136

      /// sets the current color the given index (like glLoadName)
137
      bool setIndex (size_t _idx) const;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
138

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
139
      /// gets the color instead of setting it directly
140
      bool getIndexColor (size_t _idx, Vec4uc &_rgba) const;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
141

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
142
      /// creates a new node the stack (like glPushName)
143
      Node * pushIndex (size_t _idx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
144
145
146
147

      /// pops the current node from the stack (like glPopName)
      Node * popIndex ();

148
      void colorToStack (std::vector<size_t> &_stack, size_t size_t);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
149

150
151
152
      size_t startIndex () const { return startIdx_; };
      size_t endIndex ()   const { return endIdx_; };
      size_t colorIndex () const { return colorStartIdx_; };
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
153
154
155

    private:
      Node                *parent_;
156
      size_t              index_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
157
158
159
      ColorTranslator     *translator_;
      std::vector<Node *> nodes_;

160
161
162
163
      size_t startIdx_;
      size_t endIdx_;
      size_t colorStartIdx_;
      size_t colorEndIdx_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

  };

  bool            initialized_;
  ColorTranslator translator_;
  Node            *root_;
  Node            *currentNode_;
  bool            error_;
};


//=============================================================================
} // namespace ACG
//=============================================================================
#endif // ACG_COLORSTACK_HH defined
//=============================================================================