Developer Documentation

ColorStack.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // CLASS ColorStack - IMPLEMENTATION
46 //
47 //=============================================================================
48
49
50 //== INCLUDES =================================================================
51
52
53 #include "ColorStack.hh"
54 #include <ACG/GL/GLState.hh>
55 #include <iostream>
56
57
58 //== NAMESPACES ===============================================================
59
60
61 namespace ACG {
62
63
64 //== IMPLEMENTATION ==========================================================
65
66 ColorStack::ColorStack () :
67 initialized_(false),
68 root_ (0),
69 currentNode_ (0),
70 error_(false)
71 {
72 }
73
74 //----------------------------------------------------------------------------
75
76 ColorStack::~ColorStack ()
77 {
78 if (root_)
79 delete root_;
80 }
81
82 //----------------------------------------------------------------------------
83
84 void ColorStack::initialize (GLState* _state)
85 {
86 if (initialized_)
87 {
88 delete root_;
89 }
90 error_ = false;
91 translator_.initialize (_state);
92 root_ = currentNode_ = new ColorStack::Node (0, 0, &translator_);
93 initialized_ = true;
94 }
95
96 //----------------------------------------------------------------------------
97
98 bool ColorStack::setMaximumIndex (size_t _idx)
99 {
100 if (initialized_)
101 {
102 bool rv = currentNode_->setMaximumIndex (_idx);
103 if (!rv)
104 error_ = true;
105 return rv;
106 }
107 return false;
108 }
109
110 //----------------------------------------------------------------------------
111
112 void ColorStack::setIndex (size_t _idx)
113 {
114 if (initialized_)
115 {
116 if (!currentNode_->setIndex (_idx))
117 error_ = true;
118 }
119 }
120
121 //----------------------------------------------------------------------------
122
123 Vec4uc ColorStack::getIndexColor (size_t _idx)
124 {
125 if (initialized_)
126 {
127 Vec4uc rv;
128 if (!currentNode_->getIndexColor (_idx, rv))
129 error_ = true;
130 else
131 return rv;
132 }
133
134 return Vec4uc (0, 0, 0, 0);
135 }
136
137 //----------------------------------------------------------------------------
138
139 void ColorStack::pushIndex (size_t _idx)
140 {
141 if (initialized_)
142 currentNode_ = currentNode_->pushIndex (_idx);
143 }
144
145 //----------------------------------------------------------------------------
146
147 void ColorStack::popIndex ()
148 {
149 if (initialized_)
150 currentNode_ = currentNode_->popIndex ();
151 }
152
153 //----------------------------------------------------------------------------
154
155 std::vector<size_t> ColorStack::colorToStack (Vec4uc _rgba) const
156 {
157 std::vector<size_t> rv(0);
158 if (initialized_ && !error_)
159 {
160 const size_t idx = translator_.color2index (_rgba);
161 if (idx >= root_->startIndex () && idx < root_->endIndex ())
162 root_->colorToStack (rv, idx);
163 }
164 return rv;
165 }
166
167 //----------------------------------------------------------------------------
168
169 size_t ColorStack::freeIndicies() const
170 {
171 if (initialized_)
172 {
173 return translator_.max_index () - currentNode_->endIndex ();
174 }
175 else
176 return 0;
177 }
178
179 //----------------------------------------------------------------------------
180
181 size_t ColorStack::currentIndex () const
182 {
183 if (initialized_)
184 {
185 return currentNode_->colorIndex ();
186 }
187 return 0;
188 }
189
190
191 //----------------------------------------------------------------------------
192
193 ColorStack::Node::Node (size_t _idx, Node *_parent, ColorTranslator *_ct) :
194 parent_(_parent),
195 index_(_idx),
196 translator_(_ct),
197 colorStartIdx_(0),
198 colorEndIdx_(0)
199 {
200 if (parent_)
201 startIdx_ = endIdx_ = parent_->endIndex ();
202 else
203 startIdx_ = endIdx_ = 1;
204 }
205
206 //----------------------------------------------------------------------------
207
208 ColorStack::Node::~Node ()
209 {
210 for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
211 delete (*it);
212 }
213
214 //----------------------------------------------------------------------------
215
216 bool ColorStack::Node::setMaximumIndex (size_t _idx)
217 {
218 if (_idx == 0)
219 _idx = 1;
220
221 if (colorStartIdx_ == 0 && translator_->max_index () > endIdx_ + _idx)
222 {
223 colorStartIdx_ = endIdx_;
224 endIdx_ = colorEndIdx_ = colorStartIdx_ + _idx;
225 return true;
226 }
227 return false;
228 }
229
230 //----------------------------------------------------------------------------
231
232 bool ColorStack::Node::setIndex (size_t _idx) const
233 {
234 if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
235 {
236 if(ACG::compatibilityProfile()) //only change glColor in compat mode as shader pipeline
237 glColor(translator_->index2color(colorStartIdx_ + _idx)); //renderer on core profiles does not rely on glColor
238 return true;
239 }
240 return false;
241 }
242
243 //----------------------------------------------------------------------------
244
245 bool ColorStack::Node::getIndexColor (size_t _idx, Vec4uc &_rgba) const
246 {
247 if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
248 {
249 _rgba = translator_->index2color(colorStartIdx_ + _idx);
250 return true;
251 }
252 return false;
253 }
254
255 //----------------------------------------------------------------------------
256
257 ColorStack::Node * ColorStack::Node::pushIndex (size_t _idx)
258 {
259 ColorStack::Node *n = new ColorStack::Node (_idx, this, translator_);
260 nodes_.push_back (n);
261 return n;
262 }
263
264 //----------------------------------------------------------------------------
265
266 ColorStack::Node * ColorStack::Node::popIndex ()
267 {
268 parent_->endIdx_ = endIdx_;
269 return parent_;
270 }
271
272 //----------------------------------------------------------------------------
273
274 void ColorStack::Node::colorToStack (std::vector<size_t> &_stack, size_t _index)
275 {
276 if (_index >= colorStartIdx_ && _index < colorEndIdx_)
277 {
278 _stack.push_back (_index - colorStartIdx_);
279 }
280 else
281 {
282 for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
283 {
284 ColorStack::Node *n = (*it);
285 if (_index >= n->startIndex () && _index < n->endIndex ())
286 n->colorToStack (_stack, _index);
287 }
288 }
289 _stack.push_back (index_);
290 }
291
292 //=============================================================================
293 } // namespace ACG
294 //=============================================================================
295
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::ColorStack::colorToStack
std::vector< size_t > colorToStack(Vec4uc _rgba) const
converts the given color to index values on the stack
Definition: ColorStack.cc:155
ACG::Vec4uc
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:128
ACG::ColorTranslator::initialize
void initialize(ACG::GLState *)
Definition: ColorTranslator.cc:69
ACG::ColorStack::Node::pushIndex
Node * pushIndex(size_t _idx)
creates a new node the stack (like glPushName)
Definition: ColorStack.cc:257
ACG::ColorStack::currentIndex
size_t currentIndex() const
returns the current color index
Definition: ColorStack.cc:181
ACG::ColorStack::ColorStack
ColorStack()
Default constructor.
Definition: ColorStack.cc:66
ACG::ColorStack::freeIndicies
size_t freeIndicies() const
returns maximal available index count
Definition: ColorStack.cc:169
ACG::ColorTranslator::index2color
Vec4uc index2color(const size_t _idx) const
index -> color (one buffer)
Definition: ColorTranslator.cc:118
ACG::ColorTranslator::color2index
size_t color2index(const Vec4uc _rgba) const
color -> index (one buffer)
Definition: ColorTranslator.cc:156
ACG::ColorStack::popIndex
void popIndex()
pops the current node from the stack (like glPopName)
Definition: ColorStack.cc:147
ACG::ColorStack::pushIndex
void pushIndex(size_t _idx)
creates a new node the stack (like glPushName)
Definition: ColorStack.cc:139
ACG::compatibilityProfile
void compatibilityProfile(bool _enableCoreProfile)
Store opengl core profile setting.
Definition: gl.cc:166
ACG::ColorTranslator::max_index
size_t max_index() const
returns maximal convertible index
Definition: ColorTranslator.cc:183
ACG::ColorStack::Node::getIndexColor
bool getIndexColor(size_t _idx, Vec4uc &_rgba) const
gets the color instead of setting it directly
Definition: ColorStack.cc:245
ACG::GLState
Definition: GLState.hh:213
ACG::ColorStack::Node::setIndex
bool setIndex(size_t _idx) const
sets the current color the given index (like glLoadName)
Definition: ColorStack.cc:232
ACG::ColorStack::setIndex
void setIndex(size_t _idx)
sets the current color the given index (like glLoadName)
Definition: ColorStack.cc:112
ACG::ColorStack::~ColorStack
~ColorStack()
Destructor.
Definition: ColorStack.cc:76
ACG::ColorTranslator
Definition: ColorTranslator.hh:75
ACG::ColorStack::Node::popIndex
Node * popIndex()
pops the current node from the stack (like glPopName)
Definition: ColorStack.cc:266
OpenMesh::VectorT
Definition: Vector11T.hh:83
ACG::ColorStack::setMaximumIndex
bool setMaximumIndex(size_t _idx)
sets the maximum index number used in current node
Definition: ColorStack.cc:98
ACG::ColorStack::Node::setMaximumIndex
bool setMaximumIndex(size_t _idx)
sets the maximum index number used in current node
Definition: ColorStack.cc:216
ACG::ColorStack::getIndexColor
Vec4uc getIndexColor(size_t _idx)
gets the color instead of setting it directly
Definition: ColorStack.cc:123
ACG::glColor
void glColor(const Vec3f &_v)
Wrapper: glColor for Vec3f.
Definition: gl.hh:134
ACG::ColorStack::initialize
void initialize(ACG::GLState *)
init (takes current GL context/ like glInitNames (); glPushName (0))
Definition: ColorStack.cc:84
ACG::ColorStack::Node
Definition: ColorStack.hh:118