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 SoMultiTextureMatrixElement Inventor/elements/SoMultiTextureMatrixElement.h
35   \brief The SoMultiTextureMatrixElement class is used to manage the texture matrix stack for texture units > 0.
36 
37   \ingroup elements
38 
39   The texture matrix is used to transform texture coordinates before
40   being used to map textures onto polygons.
41 
42   \since Coin 2.2
43 */
44 
45 #include "SbBasicP.h"
46 
47 #include <Inventor/elements/SoMultiTextureMatrixElement.h>
48 #include <Inventor/lists/SbList.h>
49 
50 #define PRIVATE(obj) obj->pimpl
51 
52 class SoMultiTextureMatrixElementP {
53 public:
ensureCapacity(int unit) const54   void ensureCapacity(int unit) const {
55     while (unit >= this->unitdata.getLength()) {
56       this->unitdata.append(SoMultiTextureMatrixElement::UnitData());
57     }
58   }
59   mutable SbList<SoMultiTextureMatrixElement::UnitData> unitdata;
60 };
61 
62 SO_ELEMENT_CUSTOM_CONSTRUCTOR_SOURCE(SoMultiTextureMatrixElement);
63 
64 // doc from parent
65 void
initClass(void)66 SoMultiTextureMatrixElement::initClass(void)
67 {
68   SO_ELEMENT_INIT_CLASS(SoMultiTextureMatrixElement, inherited);
69 }
70 
71 /*!
72   The constructor.
73  */
SoMultiTextureMatrixElement(void)74 SoMultiTextureMatrixElement::SoMultiTextureMatrixElement(void)
75 {
76   PRIVATE(this) = new SoMultiTextureMatrixElementP;
77 
78   this->setTypeId(SoMultiTextureMatrixElement::classTypeId);
79   this->setStackIndex(SoMultiTextureMatrixElement::classStackIndex);
80 }
81 
82 /*!
83   The destructor.
84 */
~SoMultiTextureMatrixElement(void)85 SoMultiTextureMatrixElement::~SoMultiTextureMatrixElement(void)
86 {
87   delete PRIVATE(this);
88 }
89 
90 
91 void
set(SoState * const state,SoNode * const node,const int unit,const SbMatrix & matrix)92 SoMultiTextureMatrixElement::set(SoState * const state, SoNode * const node, const int unit, const SbMatrix & matrix)
93 {
94   SoMultiTextureMatrixElement * elem = coin_assert_cast<SoMultiTextureMatrixElement *>
95     (SoElement::getElement(state, classStackIndex));
96   elem->setElt(unit, matrix);
97   if (node) elem->addNodeId(node);
98 }
99 
100 
101 /*!
102   Multiplies \a matrix into the current texture matrix.
103 */
104 void
mult(SoState * const state,SoNode * const node,const int unit,const SbMatrix & matrix)105 SoMultiTextureMatrixElement::mult(SoState * const state,
106                                   SoNode * const node,
107                                   const int unit,
108                                   const SbMatrix & matrix)
109 {
110   SoMultiTextureMatrixElement * elem = coin_assert_cast<SoMultiTextureMatrixElement *>
111     (SoElement::getElement(state, classStackIndex));
112   elem->multElt(unit, matrix);
113   if (node) elem->addNodeId(node);
114 }
115 
116 /*!
117   Returns current texture matrix.
118 */
119 const SbMatrix &
get(SoState * const state,const int unit)120 SoMultiTextureMatrixElement::get(SoState * const state, const int unit)
121 {
122   const SoMultiTextureMatrixElement * elem =
123     coin_assert_cast<const SoMultiTextureMatrixElement *>
124     (SoElement::getConstElement(state, classStackIndex));
125   return elem->getElt(unit);
126 }
127 
128 SoMultiTextureMatrixElement::UnitData &
getUnitData(const int unit)129 SoMultiTextureMatrixElement::getUnitData(const int unit)
130 {
131   PRIVATE(this)->ensureCapacity(unit);
132   return PRIVATE(this)->unitdata[unit];
133 }
134 
135 const SoMultiTextureMatrixElement::UnitData &
getUnitData(const int unit) const136 SoMultiTextureMatrixElement::getUnitData(const int unit) const
137 {
138   PRIVATE(this)->ensureCapacity(unit);
139   return PRIVATE(this)->unitdata[unit];
140 }
141 
142 int
getNumUnits() const143 SoMultiTextureMatrixElement::getNumUnits() const
144 {
145   return PRIVATE(this)->unitdata.getLength();
146 }
147 
148 
149 /*!
150   virtual method which is called from mult(). Multiplies \a matrix
151   into element matrix.
152 */
153 void
multElt(const int unit,const SbMatrix & matrix)154 SoMultiTextureMatrixElement::multElt(const int unit, const SbMatrix & matrix)
155 {
156   PRIVATE(this)->ensureCapacity(unit);
157   PRIVATE(this)->unitdata[unit].textureMatrix.multLeft(matrix);
158 }
159 
160 /*!
161   virtual method which is called from set(). Sets \a matrix
162   intp element matrix.
163 */
164 void
setElt(const int unit,const SbMatrix & matrix)165 SoMultiTextureMatrixElement::setElt(const int unit, const SbMatrix & matrix)
166 {
167   PRIVATE(this)->ensureCapacity(unit);
168   PRIVATE(this)->unitdata[unit].textureMatrix = matrix;
169 }
170 
171 /*!
172   Returns element matrix. Called from get().
173 */
174 const SbMatrix &
getElt(const int unit) const175 SoMultiTextureMatrixElement::getElt(const int unit) const
176 {
177   PRIVATE(this)->ensureCapacity(unit);
178   return PRIVATE(this)->unitdata[unit].textureMatrix;
179 }
180 
181 // doc from parent
182 void
init(SoState * state)183 SoMultiTextureMatrixElement::init(SoState * state)
184 {
185   inherited::init(state);
186   this->clearNodeIds();
187 }
188 
189 // Documented in superclass. Overridden to copy current matrix and
190 // update accumulated node ids.
191 void
push(SoState * state)192 SoMultiTextureMatrixElement::push(SoState * state)
193 {
194   inherited::push(state);
195 
196   const SoMultiTextureMatrixElement * prev =
197     coin_assert_cast<const SoMultiTextureMatrixElement *>
198     (this->getNextInStack());
199 
200   PRIVATE(this)->unitdata = PRIVATE(prev)->unitdata;
201   // make sure node ids are accumulated properly
202   this->copyNodeIds(prev);
203 }
204 
205 #undef PRIVATE
206