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 SoTexture2Transform SoTexture2Transform.h Inventor/nodes/SoTexture2Transform.h
35   \brief The SoTexture2Transform class is used to define 2D texture transformations.
36 
37   \ingroup nodes
38 
39   Textures applied to shapes in the scene can be transformed by
40   "prefixing" in the state with instances of this node
41   type. Translations, rotations and scaling in 2D can all be done.
42 
43   The default settings of this node's fields equals a "null
44   transform", ie no transformation.
45 
46   <b>FILE FORMAT/DEFAULTS:</b>
47   \code
48     Texture2Transform {
49         translation 0 0
50         rotation 0
51         scaleFactor 1 1
52         center 0 0
53     }
54   \endcode
55 
56   \sa SoTexture3Transform
57 */
58 
59 // *************************************************************************
60 
61 #include <Inventor/nodes/SoTexture2Transform.h>
62 
63 #include <Inventor/actions/SoGLRenderAction.h>
64 #include <Inventor/actions/SoPickAction.h>
65 #include <Inventor/actions/SoGetMatrixAction.h>
66 #include <Inventor/elements/SoGLMultiTextureMatrixElement.h>
67 #include <Inventor/elements/SoTextureUnitElement.h>
68 #include <Inventor/elements/SoGLCacheContextElement.h>
69 #include <Inventor/elements/SoShapeStyleElement.h>
70 #include <Inventor/actions/SoCallbackAction.h>
71 #include <Inventor/C/glue/gl.h>
72 
73 #include "nodes/SoSubNodeP.h"
74 
75 // *************************************************************************
76 
77 /*!
78   \var SoSFVec2f SoTexture2Transform::translation
79 
80   Texture coordinate translation. Default value is [0, 0].
81 */
82 /*!
83   \var SoSFFloat SoTexture2Transform::rotation
84 
85   Texture coordinate rotation in radians (around z-axis, s is x-axis and t is
86   y-axis).  Defaults to an identity rotation (ie zero rotation).
87 */
88 /*!
89   \var SoSFVec2f SoTexture2Transform::scaleFactor
90 
91   Texture coordinate scale factors. Default value is [1, 1].
92 */
93 /*!
94   \var SoSFVec2f SoTexture2Transform::center
95 
96   Center for scale and rotation. Default value is [0, 0].
97 */
98 
99 // *************************************************************************
100 
101 SO_NODE_SOURCE(SoTexture2Transform);
102 
103 /*!
104   Constructor.
105 */
SoTexture2Transform(void)106 SoTexture2Transform::SoTexture2Transform(void)
107 {
108   SO_NODE_INTERNAL_CONSTRUCTOR(SoTexture2Transform);
109 
110   SO_NODE_ADD_FIELD(translation, (0.0f, 0.0f));
111   SO_NODE_ADD_FIELD(rotation, (0.0f));
112   SO_NODE_ADD_FIELD(scaleFactor, (1.0f, 1.0f));
113   SO_NODE_ADD_FIELD(center, (0.0f, 0.0f));
114 }
115 
116 /*!
117   Destructor.
118 */
~SoTexture2Transform()119 SoTexture2Transform::~SoTexture2Transform()
120 {
121 }
122 
123 // Documented in superclass.
124 void
initClass(void)125 SoTexture2Transform::initClass(void)
126 {
127   SO_NODE_INTERNAL_INIT_CLASS(SoTexture2Transform, SO_FROM_INVENTOR_1|SoNode::VRML1);
128 
129   SO_ENABLE(SoGLRenderAction, SoGLMultiTextureMatrixElement);
130   SO_ENABLE(SoCallbackAction, SoMultiTextureMatrixElement);
131   SO_ENABLE(SoPickAction, SoMultiTextureMatrixElement);
132 }
133 
134 
135 // Documented in superclass.
136 void
GLRender(SoGLRenderAction * action)137 SoTexture2Transform::GLRender(SoGLRenderAction * action)
138 {
139   SoState * state = action->getState();
140 
141   // don't modify the texture matrix while rendering the shadow map
142   if (SoShapeStyleElement::get(state)->getFlags() & SoShapeStyleElement::SHADOWMAP) return;
143 
144   int unit = SoTextureUnitElement::get(state);
145 
146   const cc_glglue * glue =
147     cc_glglue_instance(SoGLCacheContextElement::get(state));
148   int maxunits = cc_glglue_max_texture_units(glue);
149 
150   if (unit < maxunits) {
151     SbMatrix mat;
152     this->makeMatrix(mat);
153     SoMultiTextureMatrixElement::mult(state, this, unit, mat);
154   }
155   else {
156     // we already warned in SoTextureUnit. I think it's best to just
157     // ignore the texture here so that all textures for non-supported
158     // units will be ignored. pederb, 2003-11-10
159   }
160 }
161 
162 // Documented in superclass.
163 void
doAction(SoAction * action)164 SoTexture2Transform::doAction(SoAction *action)
165 {
166   SbMatrix mat;
167   this->makeMatrix(mat);
168   SoState * state = action->getState();
169   int unit = SoTextureUnitElement::get(state);
170   SoMultiTextureMatrixElement::mult(state, this, unit, mat);
171 }
172 
173 // Documented in superclass.
174 void
callback(SoCallbackAction * action)175 SoTexture2Transform::callback(SoCallbackAction *action)
176 {
177   SoTexture2Transform::doAction(action);
178 }
179 
180 // Documented in superclass.
181 void
getMatrix(SoGetMatrixAction * action)182 SoTexture2Transform::getMatrix(SoGetMatrixAction * action)
183 {
184   int unit = SoTextureUnitElement::get(action->getState());
185   if (unit == 0) {
186     SbMatrix mat;
187     this->makeMatrix(mat);
188     action->getTextureMatrix().multLeft(mat);
189     action->getTextureInverse().multRight(mat.inverse());
190   }
191 }
192 
193 // Documented in superclass.
194 void
pick(SoPickAction * action)195 SoTexture2Transform::pick(SoPickAction * action)
196 {
197   SoTexture2Transform::doAction(action);
198 }
199 
200 //
201 // generate a matrix based on the fields
202 //
203 void
makeMatrix(SbMatrix & mat)204 SoTexture2Transform::makeMatrix(SbMatrix & mat)
205 {
206   SbMatrix tmp;
207   SbVec2f c = this->center.isIgnored() ?
208     SbVec2f(0.0f, 0.0f) :
209     center.getValue();
210 
211   mat.makeIdentity();
212   mat[3][0] = -c[0];
213   mat[3][1] = -c[1];
214 
215   SbVec2f scale = this->scaleFactor.getValue();
216   if (!this->scaleFactor.isIgnored() &&
217       scale != SbVec2f(1.0f, 1.0f)) {
218     tmp.makeIdentity();
219     tmp[0][0] = scale[0];
220     tmp[1][1] = scale[1];
221     mat.multRight(tmp);
222   }
223   if (!this->rotation.isIgnored() && (this->rotation.getValue() != 0.0f)) {
224     float cosa = (float)cos(this->rotation.getValue());
225     float sina = (float)sin(this->rotation.getValue());
226     tmp.makeIdentity();
227     tmp[0][0] = cosa;
228     tmp[1][0] = -sina;
229     tmp[0][1] = sina;
230     tmp[1][1] = cosa;
231     mat.multRight(tmp);
232   }
233   if (!translation.isIgnored()) c+= this->translation.getValue();
234   if (c != SbVec2f(0.0f, 0.0f)) {
235     tmp.makeIdentity();
236     tmp[3][0] = c[0];
237     tmp[3][1] = c[1];
238     mat.multRight(tmp);
239   }
240 }
241