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 SoGLTextureEnabledElement SoGLTextureEnabledElement.h Inventor/elements/SoGLTextureEnabledElement.h
35 \brief The SoGLTextureEnabledElement class is an element which controls whether texturing is enabled or not.
36
37 \ingroup elements
38
39 \sa SoGLTexture3EnabledElement
40 */
41
42 /*!
43 \enum SoMultiTextureEnabledElement::Mode
44 */
45
46 /*!
47 \var SoGLTextureEnabledElement::Mode SoGLTextureEnabledElement::DISABLED
48 */
49 /*!
50 \var SoGLTextureEnabledElement::Mode SoGLTextureEnabledElement::RECTANGLE
51 */
52 /*!
53 \var SoGLTextureEnabledElement::Mode SoGLTextureEnabledElement::CUBEMAP
54 */
55
56
57 #include <Inventor/elements/SoGLTextureEnabledElement.h>
58 #include <Inventor/elements/SoShapeStyleElement.h>
59
60 #ifdef HAVE_CONFIG_H
61 #include <config.h>
62 #endif // HAVE_CONFIG_H
63
64 #include <Inventor/system/gl.h>
65 #include <cassert>
66
67 SO_ELEMENT_SOURCE(SoGLTextureEnabledElement);
68
69 // doc from parent
70 void
initClass(void)71 SoGLTextureEnabledElement::initClass(void)
72 {
73 SO_ELEMENT_INIT_CLASS(SoGLTextureEnabledElement, inherited);
74 }
75
76 /*!
77 The destructor.
78 */
~SoGLTextureEnabledElement(void)79 SoGLTextureEnabledElement::~SoGLTextureEnabledElement(void)
80 {
81 }
82
83 /*
84 Sets the state of this element. Used for enabling GL_TEXTURE_2D and
85 disabling GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_EXT or
86 GL_TEXTURE_CUBE_MAP.
87 */
88 void
set(SoState * const state,SoNode * const node,const SbBool enabled)89 SoGLTextureEnabledElement::set(SoState * const state,
90 SoNode * const node,
91 const SbBool enabled)
92 {
93 inherited::set(state, node, enabled);
94 SoShapeStyleElement::setTextureEnabled(state, enabled);
95 }
96
97 /*!
98 Enables GL_TEXTURE_RECTANGLE_EXT/NV. GL_TEXTURE_2D/GL_TEXTURE_CUBE_MAP will be
99 disabled if it's enabled earlier.
100
101 \since Coin 2.2
102 */
103 void
enableRectangle(SoState * state,SoNode * node)104 SoGLTextureEnabledElement::enableRectangle(SoState * state, SoNode * node)
105 {
106 SoInt32Element::set(classStackIndex, state, node, (int32_t) RECTANGLE);
107 SoShapeStyleElement::setTextureEnabled(state, TRUE);
108 }
109
110 /*!
111 Enables GL_TEXTURE_CUBE+MAP. GL_TEXTURE_2D and/or
112 GL_TEXTURE_RECTANGLE will be disabled if it's enabled earlier.
113 */
114 void
enableCubeMap(SoState * state,SoNode * node)115 SoGLTextureEnabledElement::enableCubeMap(SoState * state, SoNode * node)
116 {
117 SoInt32Element::set(classStackIndex, state, node, (int32_t) CUBEMAP);
118 SoShapeStyleElement::setTextureEnabled(state, TRUE);
119 }
120
121 /*!
122
123 Returns the current texture mode.
124
125 */
126 SoGLTextureEnabledElement::Mode
getMode(SoState * state)127 SoGLTextureEnabledElement::getMode(SoState * state)
128 {
129 return (Mode) SoInt32Element::get(classStackIndex, state);
130 }
131
132 // doc from parent
133 void
init(SoState * state)134 SoGLTextureEnabledElement::init(SoState * state)
135 {
136 inherited::init(state);
137 }
138
139 // Documented in superclass. Overridden to track GL state.
140 void
push(SoState * state)141 SoGLTextureEnabledElement::push(SoState * state)
142 {
143 SoGLTextureEnabledElement * prev = (SoGLTextureEnabledElement*) this->getNextInStack();
144
145 this->data = prev->data;
146 // capture previous element since we might or might not change the
147 // GL state in set/pop
148 prev->capture(state);
149 }
150
151 // Documented in superclass. Overridden to track GL state.
152 void
pop(SoState * state,const SoElement * prevTopElement)153 SoGLTextureEnabledElement::pop(SoState * state,
154 const SoElement * prevTopElement)
155 {
156 SoGLTextureEnabledElement * prev = (SoGLTextureEnabledElement*) prevTopElement;
157 if (this->data != prev->data) {
158 this->updategl((Mode) this->data, (Mode) prev->data);
159 }
160 }
161
162 /*!
163 Sets the state of the element.
164 */
165 void
set(SoState * const state,const SbBool enabled)166 SoGLTextureEnabledElement::set(SoState * const state, const SbBool enabled)
167 {
168 SoGLTextureEnabledElement::set(state, NULL, enabled);
169 }
170
171
172 /*!
173 Return current state of this element.
174 */
175 SbBool
get(SoState * const state)176 SoGLTextureEnabledElement::get(SoState * const state)
177 {
178 return inherited::get(state);
179 }
180
181
182 /*!
183 Returns default state of this element (FALSE).
184 */
185 SbBool
getDefault(void)186 SoGLTextureEnabledElement::getDefault(void)
187 {
188 return inherited::getDefault();
189 }
190
191 void
setElt(int32_t value)192 SoGLTextureEnabledElement::setElt(int32_t value)
193 {
194 if (this->data != value) {
195 this->updategl((Mode) value, (Mode)this->data);
196 this->data = value;
197 }
198 }
199
200 //
201 // updates GL state (obsoleted)
202 //
203 void
updategl(void)204 SoGLTextureEnabledElement::updategl(void)
205 {
206 assert(0 && "obsoleted");
207 if (this->data) glEnable(GL_TEXTURE_2D);
208 else glDisable(GL_TEXTURE_2D);
209 }
210
211 void
updategl(const Mode newvalue,const Mode oldvalue)212 SoGLTextureEnabledElement::updategl(const Mode newvalue, const Mode oldvalue)
213 {
214 // FIXME: the code below looks fairly non-optimal. Should at least
215 // avoid doing glDisable() then glEnable(). 20040802 mortene.
216 //
217 // We check for this before calling this method. 2005-01-27 pederb.
218
219 switch (oldvalue) {
220 case DISABLED:
221 break;
222 case TEXTURE2D:
223 glDisable(GL_TEXTURE_2D);
224 break;
225 case RECTANGLE:
226 glDisable(GL_TEXTURE_RECTANGLE_EXT);
227 break;
228 case CUBEMAP:
229 glDisable(GL_TEXTURE_CUBE_MAP);
230 break;
231 default:
232 assert(0 && "should not happen");
233 break;
234 }
235 switch (newvalue) {
236 case DISABLED:
237 break;
238 case TEXTURE2D:
239 glEnable(GL_TEXTURE_2D);
240 break;
241 case RECTANGLE:
242 glEnable(GL_TEXTURE_RECTANGLE_EXT);
243 break;
244 case CUBEMAP:
245 glEnable(GL_TEXTURE_CUBE_MAP);
246 break;
247 default:
248 assert(0 && "should not happen");
249 break;
250 }
251 }
252