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 SoBumpMapMatrixElement Inventor/elements/SoBumpMapMatrixElement.h
35   \brief The SoBumpMapMatrixElement class is used to manage the bump map matrix stack.
36 
37   \ingroup elements
38 
39   The bump map matrix is used to transform bump map coordinates before
40   being used to map bump maps onto polygons.
41 */
42 
43 #include <Inventor/elements/SoBumpMapMatrixElement.h>
44 #include "coindefs.h" // COIN_OBSOLETED()
45 #include "SbBasicP.h"
46 
47 /*!
48   \fn SoBumpMapMatrixElement::bumpMapMatrix
49 
50   The matrix.
51 */
52 
53 SO_ELEMENT_SOURCE(SoBumpMapMatrixElement);
54 
55 // doc from parent
56 void
initClass(void)57 SoBumpMapMatrixElement::initClass(void)
58 {
59   SO_ELEMENT_INIT_CLASS(SoBumpMapMatrixElement, inherited);
60 }
61 
62 /*!
63   The destructor.
64 */
~SoBumpMapMatrixElement(void)65 SoBumpMapMatrixElement::~SoBumpMapMatrixElement(void)
66 {
67 }
68 
69 /*!
70   Sets current texture matrix to identity.
71 */
72 void
makeIdentity(SoState * const state,SoNode * const node)73 SoBumpMapMatrixElement::makeIdentity(SoState * const state,
74                                      SoNode * const node)
75 {
76   SoBumpMapMatrixElement * elem = coin_assert_cast<SoBumpMapMatrixElement *>
77     (
78      SoElement::getElement(state, classStackIndex)
79      );
80   if (elem) {
81     elem->makeEltIdentity();
82     if (node) elem->setNodeId(node);
83   }
84 }
85 
86 /*!
87   Sets the current texture matrix to \a matrix.
88 
89   This method is an extension versus the Open Inventor API.
90 */
91 void
set(SoState * const state,SoNode * const node,const SbMatrix & matrix)92 SoBumpMapMatrixElement::set(SoState * const state,
93                             SoNode * const node,
94                             const SbMatrix & matrix)
95 {
96   SoBumpMapMatrixElement * elem =
97        coin_assert_cast<SoBumpMapMatrixElement *>
98     (
99      SoElement::getElement(state, classStackIndex)
100      );
101   if (elem) {
102     elem->setElt(matrix);
103     if (node) elem->setNodeId(node);
104   }
105 }
106 
107 /*!
108   Multiplies \a matrix into the current texture matrix.
109 */
110 void
mult(SoState * const state,SoNode * const node,const SbMatrix & matrix)111 SoBumpMapMatrixElement::mult(SoState * const state,
112                              SoNode * const node,
113                              const SbMatrix & matrix)
114 {
115   SoBumpMapMatrixElement * elem = coin_assert_cast< SoBumpMapMatrixElement *>
116     (
117      SoElement::getElement(state, classStackIndex)
118      );
119   if (elem) {
120     elem->multElt(matrix);
121     if (node) elem->addNodeId(node);
122   }
123 }
124 
125 
126 /*!
127   Appends \a translation to the current texture matrix.
128 */
129 void
translateBy(SoState * const state,SoNode * const node,const SbVec3f & translation)130 SoBumpMapMatrixElement::translateBy(SoState * const state,
131                                     SoNode * const node,
132                                     const SbVec3f & translation)
133 {
134   SoBumpMapMatrixElement * elem = coin_assert_cast< SoBumpMapMatrixElement *>
135     (
136      SoElement::getElement(state, classStackIndex)
137      );
138   if (elem) {
139     elem->translateEltBy(translation);
140     if (node) elem->addNodeId(node);
141   }
142 }
143 
144 /*!
145   Appends \a rotation to the current texture matrix.
146 */
147 void
rotateBy(SoState * const state,SoNode * const node,const SbRotation & rotation)148 SoBumpMapMatrixElement::rotateBy(SoState * const state,
149                                  SoNode * const node,
150                                  const SbRotation & rotation)
151 {
152   SoBumpMapMatrixElement * elem = coin_assert_cast<SoBumpMapMatrixElement*>
153     (
154      SoElement::getElement(state, classStackIndex)
155      );
156   if (elem) {
157     elem->rotateEltBy(rotation);
158     if (node) elem->addNodeId(node);
159   }
160 }
161 
162 /*!
163   Appends \a scaleFactor to the current texture matrix.
164 */
165 void
scaleBy(SoState * const state,SoNode * const node,const SbVec3f & scaleFactor)166 SoBumpMapMatrixElement::scaleBy(SoState * const state,
167                                 SoNode * const node,
168                                 const SbVec3f & scaleFactor)
169 {
170   SoBumpMapMatrixElement * elem = coin_assert_cast<SoBumpMapMatrixElement *>
171     (
172      SoElement::getElement(state, classStackIndex)
173      );
174   if (elem) {
175     elem->scaleEltBy(scaleFactor);
176     if (node) elem->addNodeId(node);
177   }
178 }
179 
180 
181 /*!
182   Returns current texture matrix.
183 */
184 const SbMatrix &
get(SoState * const state)185 SoBumpMapMatrixElement::get(SoState * const state)
186 {
187   const SoBumpMapMatrixElement * elem = coin_assert_cast<const SoBumpMapMatrixElement *>
188     (
189      SoElement::getConstElement(state, classStackIndex)
190      );
191   return elem->getElt();
192 }
193 
194 /*!
195   virtual method which is called from makeIdentity().
196   Sets element matrix to identity.
197 */
198 void
makeEltIdentity(void)199 SoBumpMapMatrixElement::makeEltIdentity(void)
200 {
201   this->bumpMapMatrix.makeIdentity();
202 }
203 
204 /*!
205   virtual method which is called from set(). Sets the texture matrix
206   to \a matrix.
207 
208   This method is an extension versus the Open Inventor API.
209 */
210 void
setElt(const SbMatrix & matrix)211 SoBumpMapMatrixElement::setElt(const SbMatrix & matrix)
212 {
213   this->bumpMapMatrix = matrix;
214 }
215 
216 /*!
217   virtual method which is called from mult(). Multiplies \a matrix
218   into element matrix.
219 */
220 void
multElt(const SbMatrix & matrix)221 SoBumpMapMatrixElement::multElt(const SbMatrix & matrix)
222 {
223   this->bumpMapMatrix.multLeft(matrix);
224 }
225 
226 /*!
227   virtual method which is called from translateBy().
228   Appends \a translation to the element matrix.
229 */
230 void
translateEltBy(const SbVec3f & translation)231 SoBumpMapMatrixElement::translateEltBy(const SbVec3f & translation)
232 {
233   SbMatrix matrix = SbMatrix::identity();
234   matrix.setTranslate(translation);
235   this->bumpMapMatrix.multLeft(matrix);
236 }
237 
238 /*!
239   virtual method which is called from rotateBy().
240   Appends \a rotation to the element matrix.
241 */
242 void
rotateEltBy(const SbRotation & rotation)243 SoBumpMapMatrixElement::rotateEltBy(const SbRotation & rotation)
244 {
245   SbMatrix matrix = SbMatrix::identity();
246   matrix.setRotate(rotation);
247   this->bumpMapMatrix.multLeft(matrix);
248 }
249 
250 /*!
251   virtual method which is called from scaleBy().
252   Append \a scaleFactor to the element matrix.
253 */
254 void
scaleEltBy(const SbVec3f & scaleFactor)255 SoBumpMapMatrixElement::scaleEltBy(const SbVec3f & scaleFactor)
256 {
257   SbMatrix matrix = SbMatrix::identity();
258   matrix.setScale(scaleFactor);
259   this->bumpMapMatrix.multLeft(matrix);
260 }
261 
262 /*!
263   Returns element matrix. Called from get().
264 */
265 const SbMatrix &
getElt(void) const266 SoBumpMapMatrixElement::getElt(void) const
267 {
268   return this->bumpMapMatrix;
269 }
270 
271 // doc from parent
272 void
init(SoState * state)273 SoBumpMapMatrixElement::init(SoState * state)
274 {
275   inherited::init(state);
276   this->bumpMapMatrix.makeIdentity();
277   this->clearNodeIds();
278 }
279 
280 // Documented in superclass. Overridden to copy current matrix and
281 // update accumulated node ids.
282 void
push(SoState * state)283 SoBumpMapMatrixElement::push(SoState * state)
284 {
285   inherited::push(state);
286 
287   const SoBumpMapMatrixElement * prev =
288     coin_assert_cast<const SoBumpMapMatrixElement *>(this->getNextInStack());
289   this->bumpMapMatrix = prev->bumpMapMatrix;
290 
291   // make sure node ids are accumulated properly
292   this->copyNodeIds(prev);
293 }
294