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 SoGLMultiTextureMatrixElement Inventor/elements/SoGLMultiTextureMatrixElement.h
35   \brief The SoGLMultiTextureMatrixElement class is used to update the OpenGL texture matrix.
36 
37   \ingroup elements
38 
39   Since (for some weird reason) most OpenGL implementations have a very
40   small texture matrix stack, and since the matrix stack also is broken
41   on many OpenGL implementations, the texture matrix is always loaded
42   into OpenGL. We do not push() and pop() matrices.
43 */
44 
45 #include <Inventor/elements/SoGLMultiTextureMatrixElement.h>
46 #include <Inventor/actions/SoGLRenderAction.h>
47 #include <Inventor/C/glue/gl.h>
48 #include <cassert>
49 
50 #ifdef HAVE_CONFIG_H
51 #include <config.h>
52 #endif // HAVE_CONFIG_H
53 
54 #include <Inventor/system/gl.h>
55 
56 SO_ELEMENT_SOURCE(SoGLMultiTextureMatrixElement);
57 
58 
59 // doc from parent
60 void
initClass(void)61 SoGLMultiTextureMatrixElement::initClass(void)
62 {
63   SO_ELEMENT_INIT_CLASS(SoGLMultiTextureMatrixElement, inherited);
64 }
65 
66 /*!
67   The destructor.
68 */
69 
~SoGLMultiTextureMatrixElement(void)70 SoGLMultiTextureMatrixElement::~SoGLMultiTextureMatrixElement(void)
71 {
72 }
73 
74 // doc from parent
75 void
init(SoState * state)76 SoGLMultiTextureMatrixElement::init(SoState * state)
77 {
78   inherited::init(state);
79 
80   SoAction * action = state->getAction();
81   assert(action->isOfType(SoGLRenderAction::getClassTypeId()));
82   // fetch cache context from action since SoGLCacheContextElement
83   // might not be initialized yet.
84   SoGLRenderAction * glaction = (SoGLRenderAction*) action;
85   this->cachecontext = glaction->getCacheContext();
86 }
87 
88 void
push(SoState * state)89 SoGLMultiTextureMatrixElement::push(SoState * state)
90 {
91   inherited::push(state);
92   SoGLMultiTextureMatrixElement * prev = (SoGLMultiTextureMatrixElement*)
93     this->getNextInStack();
94 
95   this->cachecontext = prev->cachecontext;
96 
97   // capture previous element since we might or might not change the
98   // GL state in set/pop
99   prev->capture(state);
100 }
101 
102 // doc from parent
103 void
pop(SoState * state,const SoElement * prevTopElement)104 SoGLMultiTextureMatrixElement::pop(SoState * state,
105                                    const SoElement * prevTopElement)
106 {
107   inherited::pop(state, prevTopElement);
108 
109   const SoGLMultiTextureMatrixElement * prev =
110     static_cast<const SoGLMultiTextureMatrixElement *> (prevTopElement);
111 
112   SbMatrix identity = SbMatrix::identity();
113   const int numunits = SbMax(this->getNumUnits(),
114                              prev->getNumUnits());
115   for (int i = 0; i < numunits; i++) {
116     const SbMatrix & thism =
117       (i < this->getNumUnits()) ?
118       this->getUnitData(i).textureMatrix : identity;
119 
120     const SbMatrix & prevm =
121       (i < prev->getNumUnits()) ?
122       prev->getUnitData(i).textureMatrix : identity;
123 
124     if (thism != prevm) {
125       this->updategl(i);
126     }
127   }
128 }
129 
130 // doc from parent
131 void
multElt(const int unit,const SbMatrix & matrix)132 SoGLMultiTextureMatrixElement::multElt(const int unit, const SbMatrix & matrix)
133 {
134   inherited::multElt(unit, matrix);
135   this->updategl(unit);
136 }
137 
138 void
setElt(const int unit,const SbMatrix & matrix)139 SoGLMultiTextureMatrixElement::setElt(const int unit, const SbMatrix & matrix)
140 {
141   inherited::setElt(unit, matrix);
142   this->updategl(unit);
143 }
144 
145 
146 // updates GL state
147 void
updategl(const int unit) const148 SoGLMultiTextureMatrixElement::updategl(const int unit) const
149 {
150   const cc_glglue * glue = cc_glglue_instance(this->cachecontext);
151   if (unit != 0) {
152     cc_glglue_glActiveTexture(glue, (GLenum) (int(GL_TEXTURE0) + unit));
153   }
154   glMatrixMode(GL_TEXTURE);
155   if (unit < this->getNumUnits()) {
156     glLoadMatrixf(this->getUnitData(unit).textureMatrix[0]);
157   }
158   else {
159     glLoadIdentity();
160   }
161   glMatrixMode(GL_MODELVIEW);
162   if (unit != 0) {
163     cc_glglue_glActiveTexture(glue, (GLenum) GL_TEXTURE0);
164   }
165 }
166 
167