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 SoBaseColor SoBaseColor.h Inventor/nodes/SoBaseColor.h
35 \brief The SoBaseColor class provides a node type for convenient setting of the base material color.
36
37 \ingroup nodes
38
39 If you want to just set the diffuse color of the following geometry,
40 you can use this node for simplicity.
41
42 <b>FILE FORMAT/DEFAULTS:</b>
43 \code
44 BaseColor {
45 rgb 0.8 0.8 0.8
46 }
47 \endcode
48
49 \sa SoMaterial
50 */
51
52 #include <Inventor/nodes/SoBaseColor.h>
53
54 #ifdef HAVE_CONFIG_H
55 #include <config.h>
56 #endif // HAVE_CONFIG_H
57
58 #include <Inventor/actions/SoCallbackAction.h>
59 #include <Inventor/actions/SoGLRenderAction.h>
60 #include <Inventor/actions/SoPickAction.h>
61 #include <Inventor/elements/SoGLLazyElement.h>
62 #include <Inventor/elements/SoDiffuseColorElement.h>
63 #include <Inventor/elements/SoOverrideElement.h>
64 #include <Inventor/elements/SoGLVBOElement.h>
65 #ifdef COIN_THREADSAFE
66 #include <Inventor/threads/SbStorage.h>
67 #endif // COIN_THREADSAFE
68
69 #include "rendering/SoVBO.h"
70 #include "nodes/SoSubNodeP.h"
71
72 /*!
73 \var SoMFColor SoBaseColor::rgb
74
75 Color values. Default value of field is to have a single grey color.
76 */
77
78 #ifndef DOXYGEN_SKIP_THIS
79
80 class SoBaseColorP {
81 public:
SoBaseColorP()82 SoBaseColorP() :
83 #ifdef COIN_THREADSAFE
84 colorpacker_storage(sizeof(void*), alloc_colorpacker, free_colorpacker),
85 #endif // COIN_THREADSAFE
86 vbo(NULL) { }
~SoBaseColorP()87 ~SoBaseColorP() { delete this->vbo; }
88
89 #ifdef COIN_THREADSAFE
90 SbStorage colorpacker_storage;
91 #else // COIN_THREADSAFE
92 SoColorPacker single_colorpacker;
93 #endif // COIN_THREADSAFE
94
getColorPacker(void)95 SoColorPacker * getColorPacker(void) {
96 #ifdef COIN_THREADSAFE
97 SoColorPacker ** cptr = (SoColorPacker**) this->colorpacker_storage.get();
98 return * cptr;
99 #else // COIN_THREADSAFE
100 return &this->single_colorpacker;
101 #endif // COIN_THREADSAFE
102 }
103 SoVBO * vbo;
104 #ifdef COIN_THREADSAFE
105 private:
alloc_colorpacker(void * data)106 static void alloc_colorpacker(void * data) {
107 SoColorPacker ** cptr = (SoColorPacker**) data;
108 *cptr = new SoColorPacker;
109 }
free_colorpacker(void * data)110 static void free_colorpacker(void * data) {
111 SoColorPacker ** cptr = (SoColorPacker**) data;
112 delete *cptr;
113 }
114 #endif // COIN_THREADSAFE
115 };
116
117 #endif // DOXYGEN_SKIP_THIS
118
119 #define PRIVATE(obj) ((obj)->pimpl)
120
121 // *************************************************************************
122
123 SO_NODE_SOURCE(SoBaseColor);
124
125 /*!
126 Constructor.
127 */
SoBaseColor()128 SoBaseColor::SoBaseColor()
129 {
130 PRIVATE(this) = new SoBaseColorP;
131 SO_NODE_INTERNAL_CONSTRUCTOR(SoBaseColor);
132
133 SO_NODE_ADD_FIELD(rgb, (SbColor(0.8f, 0.8f, 0.8f)));
134 }
135
136 /*!
137 Destructor.
138 */
~SoBaseColor()139 SoBaseColor::~SoBaseColor()
140 {
141 delete PRIVATE(this);
142 }
143
144 // Doc from superclass.
145 void
initClass(void)146 SoBaseColor::initClass(void)
147 {
148 SO_NODE_INTERNAL_INIT_CLASS(SoBaseColor, SO_FROM_INVENTOR_1);
149
150 SO_ENABLE(SoGLRenderAction, SoGLLazyElement);
151 SO_ENABLE(SoCallbackAction, SoLazyElement);
152
153 SO_ENABLE(SoCallbackAction, SoDiffuseColorElement);
154 SO_ENABLE(SoGLRenderAction, SoDiffuseColorElement);
155 }
156
157 // Doc from superclass.
158 void
GLRender(SoGLRenderAction * action)159 SoBaseColor::GLRender(SoGLRenderAction * action)
160 {
161 SoBaseColor::doAction(action);
162 }
163
164 // Doc from superclass.
165 void
doAction(SoAction * action)166 SoBaseColor::doAction(SoAction * action)
167 {
168 SoState * state = action->getState();
169
170 if (!this->rgb.isIgnored() && this->rgb.getNum() &&
171 !SoOverrideElement::getDiffuseColorOverride(state)) {
172 const int num = this->rgb.getNum();
173 SoLazyElement::setDiffuse(state, this, num,
174 this->rgb.getValues(0), PRIVATE(this)->getColorPacker());
175
176 if (state->isElementEnabled(SoGLVBOElement::getClassStackIndex())) {
177 SbBool setvbo = FALSE;
178 SoBase::staticDataLock();
179 if (SoGLVBOElement::shouldCreateVBO(state, num)) {
180 setvbo = TRUE;
181 if (PRIVATE(this)->vbo == NULL) {
182 PRIVATE(this)->vbo = new SoVBO(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
183 }
184 }
185 else if (PRIVATE(this)->vbo) {
186 PRIVATE(this)->vbo->setBufferData(NULL, 0, 0);
187 }
188 // don't fill in any data in the VBO. Data will be filled in
189 // using the ColorPacker right before the VBO is used
190 SoBase::staticDataUnlock();
191 if (setvbo) {
192 SoGLVBOElement::setColorVBO(state, PRIVATE(this)->vbo);
193 }
194 }
195 if (this->isOverride()) {
196 SoOverrideElement::setDiffuseColorOverride(state, this, TRUE);
197 }
198 }
199 }
200
201 // Doc from superclass.
202 void
callback(SoCallbackAction * action)203 SoBaseColor::callback(SoCallbackAction * action)
204 {
205 SoBaseColor::doAction(action);
206 }
207
208 #undef PRIVATE
209