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 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 #include <Inventor/elements/SoGLMultiTextureEnabledElement.h>
43 #include <Inventor/misc/SoState.h>
44 #include <Inventor/actions/SoGLRenderAction.h>
45 #include "coindefs.h"
46 
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif // HAVE_CONFIG_H
50 
51 #include <Inventor/system/gl.h>
52 #include <Inventor/C/glue/gl.h>
53 #include <cassert>
54 
55 SO_ELEMENT_SOURCE(SoGLMultiTextureEnabledElement);
56 
57 // doc from parent
58 void
initClass(void)59 SoGLMultiTextureEnabledElement::initClass(void)
60 {
61   SO_ELEMENT_INIT_CLASS(SoGLMultiTextureEnabledElement, inherited);
62 }
63 
64 /*!
65   The destructor.
66 */
~SoGLMultiTextureEnabledElement(void)67 SoGLMultiTextureEnabledElement::~SoGLMultiTextureEnabledElement(void)
68 {
69 }
70 
71 // doc from parent
72 void
init(SoState * state)73 SoGLMultiTextureEnabledElement::init(SoState * state)
74 {
75   inherited::init(state);
76 
77   SoAction * action = state->getAction();
78   assert(action->isOfType(SoGLRenderAction::getClassTypeId()));
79   this->cachecontext = ((SoGLRenderAction*)action)->getCacheContext();
80 }
81 
82 // Documented in superclass. Overridden to track GL state.
83 void
push(SoState * state)84 SoGLMultiTextureEnabledElement::push(SoState * state)
85 {
86   SoGLMultiTextureEnabledElement * prev = (SoGLMultiTextureEnabledElement*) this->getNextInStack();
87 
88   this->cachecontext = prev->cachecontext;
89 
90   // copy state from previous element
91   inherited::push(state);
92   // capture previous element since we might or might not change the
93   // GL state in set/pop
94   prev->capture(state);
95 }
96 
97 // Documented in superclass. Overridden to track GL state.
98 void
pop(SoState * COIN_UNUSED_ARG (state),const SoElement * prevTopElement)99 SoGLMultiTextureEnabledElement::pop(SoState * COIN_UNUSED_ARG(state),
100                                     const SoElement * prevTopElement)
101 {
102   SoGLMultiTextureEnabledElement * prev = (SoGLMultiTextureEnabledElement*) prevTopElement;
103   const int maxunits = SbMax(this->getMaxUnits(), prev->getMaxUnits());
104 
105   for (int i = 0; i < maxunits; i++) {
106     Mode oldmode = prev->getMode(i);
107     Mode newmode =  this->getMode(i);
108     if (oldmode != newmode) {
109       this->updategl(i, newmode, oldmode);
110     }
111   }
112 }
113 
114 void
setElt(const int unit,const int value)115 SoGLMultiTextureEnabledElement::setElt(const int unit, const int value)
116 {
117   Mode oldmode = this->getMode(unit);
118   Mode newmode = (Mode) value;
119 
120   if (oldmode != newmode) {
121     inherited::setElt(unit, value);
122     this->updategl(unit, newmode, oldmode);
123   }
124 }
125 
126 //
127 // updates GL state
128 //
129 void
updategl(const int unit)130 SoGLMultiTextureEnabledElement::updategl(const int unit)
131 {
132   const cc_glglue * glue = cc_glglue_instance(this->cachecontext);
133   cc_glglue_glActiveTexture(glue, (GLenum) (int(GL_TEXTURE0) + unit));
134   if (this->isEnabled(unit)) glEnable(GL_TEXTURE_2D);
135   else glDisable(GL_TEXTURE_2D);
136   cc_glglue_glActiveTexture(glue, (GLenum) GL_TEXTURE0);
137 }
138 
139 void
updategl(const int unit,const Mode newvalue,const Mode oldvalue)140 SoGLMultiTextureEnabledElement::updategl(const int unit, const Mode newvalue, const Mode oldvalue)
141 {
142   const cc_glglue * glue = cc_glglue_instance(this->cachecontext);
143   cc_glglue_glActiveTexture(glue, (GLenum) (int(GL_TEXTURE0) + unit));
144 
145   switch (oldvalue) {
146   case DISABLED:
147     break;
148   case TEXTURE2D:
149     glDisable(GL_TEXTURE_2D);
150     break;
151   case RECTANGLE:
152     glDisable(GL_TEXTURE_RECTANGLE_EXT);
153     break;
154   case CUBEMAP:
155     glDisable(GL_TEXTURE_CUBE_MAP);
156     break;
157   case TEXTURE3D:
158     glDisable(GL_TEXTURE_3D);
159     break;
160   default:
161     assert(0 && "should not happen");
162     break;
163   }
164   switch (newvalue) {
165   case DISABLED:
166     break;
167   case TEXTURE2D:
168     glEnable(GL_TEXTURE_2D);
169     break;
170   case RECTANGLE:
171     glEnable(GL_TEXTURE_RECTANGLE_EXT);
172     break;
173   case CUBEMAP:
174     glEnable(GL_TEXTURE_CUBE_MAP);
175     break;
176   case TEXTURE3D:
177     glEnable(GL_TEXTURE_3D);
178     break;
179   default:
180     assert(0 && "should not happen");
181     break;
182   }
183   cc_glglue_glActiveTexture(glue, (GLenum) GL_TEXTURE0);
184 
185 }
186 
187