1 /**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32
33 /*!
34 \class SoDiffuseColorElement Inventor/elements/SoDiffuseColorElement.h
35 \brief The SoDiffuseColorElement class is yet to be documented.
36
37 \ingroup elements
38
39 FIXME: write doc.
40 */
41
42 #include <Inventor/elements/SoDiffuseColorElement.h>
43 #include <Inventor/elements/SoLazyElement.h>
44 #include <Inventor/SbColor.h>
45 #include <Inventor/errors/SoDebugError.h>
46 #include <cassert>
47 #include <cstdlib>
48
49 #include "SbBasicP.h"
50
51 SO_ELEMENT_SOURCE(SoDiffuseColorElement);
52
53 /*!
54 This static method initializes static data for the
55 SoDiffuseColorElement class.
56 */
57
58 void
initClass(void)59 SoDiffuseColorElement::initClass(void)
60 {
61 SO_ELEMENT_INIT_CLASS(SoDiffuseColorElement, inherited);
62 }
63
64 //! FIXME: write doc.
65
66 void
init(SoState * stateptr)67 SoDiffuseColorElement::init(SoState * stateptr)
68 {
69 inherited::init(stateptr);
70 this->state = stateptr;
71 }
72
73 /*!
74 The destructor.
75 */
76
~SoDiffuseColorElement()77 SoDiffuseColorElement::~SoDiffuseColorElement()
78 {
79 }
80
81 //! FIXME: write doc.
82
83 void
set(SoState * const state,SoNode * const node,const int32_t numcolors,const SbColor * const colors)84 SoDiffuseColorElement::set(SoState * const state, SoNode * const node,
85 const int32_t numcolors,
86 const SbColor * const colors)
87 {
88 SoDiffuseColorElement * elem = const_cast<SoDiffuseColorElement *>
89 (
90 SoDiffuseColorElement::getInstance(state)
91 );
92
93 SoLazyElement::setDiffuse(state, node, numcolors, colors, &elem->colorpacker);
94 }
95
96 //! FIXME: write doc.
97
98 void
set(SoState * const state,SoNode * const node,const int32_t numcolors,const uint32_t * const colors,const SbBool packedtransparency)99 SoDiffuseColorElement::set(SoState * const state, SoNode * const node,
100 const int32_t numcolors,
101 const uint32_t * const colors,
102 const SbBool packedtransparency)
103 {
104 SoLazyElement::setPacked(state, node, numcolors, colors, packedtransparency);
105 }
106
107 SbBool
isPacked() const108 SoDiffuseColorElement::isPacked() const
109 {
110 SoLazyElement * lazy = SoLazyElement::getInstance(this->state);
111 return lazy->isPacked();
112 }
113
114 //! FIXME: write doc.
115
116 int32_t
getNum(void) const117 SoDiffuseColorElement::getNum(void) const
118 {
119 SoLazyElement * lazy = SoLazyElement::getInstance(this->state);
120 return lazy->getNumDiffuse();
121 }
122
123 /*!
124 Returns the color array. Don't use this unless
125 SoDiffuseColorElement::isPacked() returns \c FALSE.
126
127 This method is not part of the original SGI Open Inventor v2.1 API.
128
129 \since Coin 1.0
130 */
131 const SbColor *
getColorArrayPtr(void) const132 SoDiffuseColorElement::getColorArrayPtr(void) const
133 {
134 SoLazyElement * lazy = SoLazyElement::getInstance(this->state);
135
136 #if COIN_DEBUG
137 if (lazy->isPacked()) {
138 SoDebugError::postWarning("SoDiffuseColorElement::getColorArrayPtr",
139 "colors are packed -- use getPackedArrayPtr() "
140 "instead");
141 }
142 #endif // COIN_DEBUG
143 return lazy->getDiffusePointer();
144 }
145
146 /*!
147 Returns the packed color array. Don't use this unless
148 SoDiffuseColorElement::isPacked() returns \c TRUE.
149
150 This method is not part of the original SGI Open Inventor v2.1 API.
151
152 \since Coin 1.0
153 */
154 const uint32_t *
getPackedArrayPtr(void) const155 SoDiffuseColorElement::getPackedArrayPtr(void) const
156 {
157 SoLazyElement * lazy = SoLazyElement::getInstance(this->state);
158
159 #if COIN_DEBUG
160 if (!lazy->isPacked()) {
161 SoDebugError::postWarning("SoDiffuseColorElement::getPackedArrayPtr",
162 "colors are *not* packed -- use "
163 "getColorArrayPtr() instead");
164 }
165 #endif // COIN_DEBUG
166 return lazy->getPackedPointer();
167 }
168
169 //! FIXME: write doc.
170
171 const SbColor &
get(const int index) const172 SoDiffuseColorElement::get(const int index) const
173 {
174 return SoLazyElement::getDiffuse(this->state, index);
175 }
176
177 SbBool
hasPackedTransparency(void) const178 SoDiffuseColorElement::hasPackedTransparency(void) const
179 {
180 return SoLazyElement::getInstance(this->state)->isTransparent();
181 }
182
183 //! FIXME: write doc.
184
185 const SoDiffuseColorElement *
getInstance(SoState * state)186 SoDiffuseColorElement::getInstance(SoState *state)
187 {
188 return coin_assert_cast<const SoDiffuseColorElement *>
189 (
190 state->getElementNoPush(classStackIndex)
191 );
192 }
193