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 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36
37 #ifdef HAVE_VRML97
38
39 /*!
40 \class SoVRMLTextureTransform SoVRMLTextureTransform.h Inventor/VRMLnodes/SoVRMLTextureTransform.h
41 \brief The SoVRMLTextureTransform class defines a transformation applied to texture coordinates.
42
43 \ingroup VRMLnodes
44
45 \WEB3DCOPYRIGHT
46
47 \verbatim
48 TextureTransform {
49 exposedField SFVec2f center 0 0 # (-inf, inf)
50 exposedField SFFloat rotation 0 # (-inf, inf)
51 exposedField SFVec2f scale 1 1 # (-inf, inf)
52 exposedField SFVec2f translation 0 0 # (-inf, inf)
53 }
54 \endverbatim
55
56 The TextureTransform node defines a 2D transformation that is
57 applied to texture coordinates (see SoVRMLTextureCoordinate). This
58 node affects the way textures coordinates are applied to the
59 geometric surface. The transformation consists of (in order):
60
61 - a translation;
62 - a rotation about the centre point;
63 - a non-uniform scale about the centre point.
64
65 These parameters support changes to the size, orientation, and
66 position of textures on shapes. Note that these operations appear
67 reversed when viewed on the surface of geometry. For example, a
68 scale value of (2 2) will scale the texture coordinates and have the
69 net effect of shrinking the texture size by a factor of 2 (texture
70 coordinates are twice as large and thus cause the texture to
71 repeat). A translation of (0.5 0.0) translates the texture
72 coordinates +.5 units along the S-axis and has the net effect of
73 translating the texture -0.5 along the S-axis on the geometry's
74 surface. A rotation of pi/2 of the texture coordinates results in a
75 -pi/2 rotation of the texture on the geometry.
76
77 The \e center field specifies a translation offset in texture
78 coordinate space about which the rotation and scale fields are
79 applied. The \e scale field specifies a scaling factor in S and T
80 of the texture coordinates about the center point. scale values
81 shall be in the range (-pi, pi). The \e rotation field specifies a
82 rotation in radians of the texture coordinates about the center
83 point after the scale has been applied. A positive rotation value
84 makes the texture coordinates rotate counterclockwise about the
85 centre, thereby rotating the appearance of the texture itself
86 clockwise. The \e translation field specifies a translation of the
87 texture coordinates.
88
89 In matrix transformation notation, where Tc is the untransformed
90 texture coordinate, Tc' is the transformed texture coordinate, C
91 (center), T (translation), R (rotation), and S (scale) are the
92 intermediate transformation matrices,
93
94 \verbatim
95 Tc' = -C � S � R � C � T � Tc
96 \endverbatim
97
98 Note that this transformation order is the reverse of the Transform
99 node transformation order since the texture coordinates, not the
100 texture, are being transformed (i.e., the texture coordinate
101 system).
102
103 */
104
105 /*!
106 \var SoSFVec2f SoVRMLTextureTransform::translation
107 Translation value. Default values is (0, 0, 0).
108 */
109
110 /*!
111 \var SoSFFloat SoVRMLTextureTransform::rotation
112 Rotation, in radians, around the centre points. Default value is 0.
113 */
114
115 /*!
116 \var SoSFVec2f SoVRMLTextureTransform::scale
117 Scale vector about the centre point. Default value is (1, 1).
118 */
119
120 /*!
121 \var SoSFVec2f SoVRMLTextureTransform::center
122 Texture centre. Default value is (0.0, 0.0).
123 */
124
125 #include <Inventor/VRMLnodes/SoVRMLTextureTransform.h>
126
127 #include <cmath>
128
129 #include <Inventor/VRMLnodes/SoVRMLMacros.h>
130 #include <Inventor/elements/SoMultiTextureMatrixElement.h>
131 #include <Inventor/actions/SoAction.h>
132 #include <Inventor/actions/SoGetMatrixAction.h>
133 #include <Inventor/SbMatrix.h>
134 #include <Inventor/SbRotation.h>
135
136 #include "nodes/SoSubNodeP.h"
137
138 SO_NODE_SOURCE(SoVRMLTextureTransform);
139
140 // Doc in parent
141 void
initClass(void)142 SoVRMLTextureTransform::initClass(void)
143 {
144 SO_NODE_INTERNAL_INIT_CLASS(SoVRMLTextureTransform, SO_VRML97_NODE_TYPE);
145 }
146
147 /*!
148 Constructor.
149 */
SoVRMLTextureTransform(void)150 SoVRMLTextureTransform::SoVRMLTextureTransform(void)
151 {
152 SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLTextureTransform);
153
154 SO_VRMLNODE_ADD_EXPOSED_FIELD(translation, (0.0f, 0.0f));
155 SO_VRMLNODE_ADD_EXPOSED_FIELD(rotation, (0.0f));
156 SO_VRMLNODE_ADD_EXPOSED_FIELD(scale, (1.0f, 1.0f));
157 SO_VRMLNODE_ADD_EXPOSED_FIELD(center, (0.0f, 0.0f));
158 }
159
160 /*!
161 Destructor.
162 */
~SoVRMLTextureTransform()163 SoVRMLTextureTransform::~SoVRMLTextureTransform()
164 {
165 }
166
167 // Doc in parent
168 void
doAction(SoAction * action)169 SoVRMLTextureTransform::doAction(SoAction * action)
170 {
171 SbMatrix mat;
172 this->makeMatrix(mat);
173 SoMultiTextureMatrixElement::mult(action->getState(), this, 0,
174 mat);
175 }
176
177 // Doc in parent
178 void
callback(SoCallbackAction * action)179 SoVRMLTextureTransform::callback(SoCallbackAction * action)
180 {
181 SoVRMLTextureTransform::doAction((SoAction*)action);
182 }
183
184 // Doc in parent
185 void
GLRender(SoGLRenderAction * action)186 SoVRMLTextureTransform::GLRender(SoGLRenderAction * action)
187 {
188 SoVRMLTextureTransform::doAction((SoAction*)action);
189 }
190
191 // Doc in parent
192 void
getMatrix(SoGetMatrixAction * action)193 SoVRMLTextureTransform::getMatrix(SoGetMatrixAction * action)
194 {
195 SbMatrix mat;
196 this->makeMatrix(mat);
197 action->getTextureMatrix().multLeft(mat);
198 action->getTextureInverse().multRight(mat.inverse());
199 }
200
201 // Doc in parent
202 void
pick(SoPickAction * action)203 SoVRMLTextureTransform::pick(SoPickAction * action)
204 {
205 SoVRMLTextureTransform::doAction((SoAction*)action);
206 }
207
208 //
209 // generate a matrix based on the fields
210 //
211 void
makeMatrix(SbMatrix & mat) const212 SoVRMLTextureTransform::makeMatrix(SbMatrix & mat) const
213 {
214 SbMatrix tmp;
215 SbVec2f c = this->center.isIgnored() ?
216 SbVec2f(0.0f, 0.0f) :
217 center.getValue();
218
219 mat.makeIdentity();
220 mat[3][0] = -c[0];
221 mat[3][1] = -c[1];
222
223 SbVec2f thescale = this->scale.getValue();
224 if (!this->scale.isIgnored() &&
225 thescale != SbVec2f(1.0f, 1.0f)) {
226 tmp.makeIdentity();
227 tmp[0][0] = thescale[0];
228 tmp[1][1] = thescale[1];
229 mat.multRight(tmp);
230 }
231 if (!this->rotation.isIgnored() && (this->rotation.getValue() != 0.0f)) {
232 float cosa = (float)cos(this->rotation.getValue());
233 float sina = (float)sin(this->rotation.getValue());
234 tmp.makeIdentity();
235 tmp[0][0] = cosa;
236 tmp[1][0] = -sina;
237 tmp[0][1] = sina;
238 tmp[1][1] = cosa;
239 mat.multRight(tmp);
240 }
241 if (!this->translation.isIgnored()) c+= this->translation.getValue();
242 if (c != SbVec2f(0.0f, 0.0f)) {
243 tmp.makeIdentity();
244 tmp[3][0] = c[0];
245 tmp[3][1] = c[1];
246 mat.multRight(tmp);
247 }
248 }
249
250 #endif // HAVE_VRML97
251