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 SoTextureCoordinateCylinder include/Inventor/nodes/SoTextureCoordinateCylinder.h
35   \brief The SoTextureCoordinateCylinder class autogenerates cylinder mapped texture coordinated for shapes.
36 
37   \ingroup nodes
38 
39   <b>FILE FORMAT/DEFAULTS:</b>
40   \code
41     TextureCoordinateCylinder {
42     }
43   \endcode
44 
45   \since Coin 2.3
46 */
47 // FIXME: Add a better class description (20040123 handegar)
48 
49 // *************************************************************************
50 
51 #include <Inventor/nodes/SoTextureCoordinateCylinder.h>
52 #include "coindefs.h"
53 
54 #ifdef HAVE_CONFIG_H
55 #include <config.h>
56 #endif // HAVE_CONFIG
57 
58 #include <Inventor/C/glue/gl.h>
59 #include <Inventor/SbBox3f.h>
60 #include <Inventor/SoFullPath.h>
61 #include <Inventor/actions/SoCallbackAction.h>
62 #include <Inventor/actions/SoGLRenderAction.h>
63 #include <Inventor/actions/SoPickAction.h>
64 #include <Inventor/caches/SoBoundingBoxCache.h>
65 #include <Inventor/elements/SoGLCacheContextElement.h>
66 #include <Inventor/elements/SoGLMultiTextureCoordinateElement.h>
67 #include <Inventor/elements/SoTextureUnitElement.h>
68 #include <Inventor/misc/SoState.h>
69 #include <Inventor/nodes/SoNode.h>
70 #include <Inventor/nodes/SoShape.h>
71 #include <Inventor/threads/SbStorage.h>
72 
73 #include "nodes/SoSubNodeP.h"
74 
75 // *************************************************************************
76 
77 typedef struct {
78   SbVec3f origo;
79   SbBox3f boundingbox;
80   SoNode * currentshape;
81   SoState * currentstate;
82   SbVec4f texcoordreturn;
83 } so_texcoordcylinder_data;
84 
85 static void
so_texcoordcylinder_construct_data(void * closure)86 so_texcoordcylinder_construct_data(void * closure)
87 {
88   so_texcoordcylinder_data * data = (so_texcoordcylinder_data *) closure;
89   data->currentshape = NULL;
90   data->currentstate = NULL;
91   data->origo = SbVec3f(0,0,0);
92 }
93 
94 static void
so_texcoordcylinder_destruct_data(void * COIN_UNUSED_ARG (closure))95 so_texcoordcylinder_destruct_data(void * COIN_UNUSED_ARG(closure))
96 {
97 }
98 
99 // *************************************************************************
100 
101 class SoTextureCoordinateCylinderP {
102 
103 public:
SoTextureCoordinateCylinderP(SoTextureCoordinateCylinder * texturenode)104   SoTextureCoordinateCylinderP(SoTextureCoordinateCylinder * texturenode)
105     : master(texturenode) { }
106 
107   SbVec4f calculateTextureCoordinate(SbVec3f point, SbVec3f n);
108 
so_texcoord_get_data()109   so_texcoordcylinder_data * so_texcoord_get_data() {
110     so_texcoordcylinder_data * data = NULL;
111     data = (so_texcoordcylinder_data *) this->so_texcoord_storage->get();
112     assert(data && "Error retrieving thread data.");
113     return data;
114   }
115 
116   SbStorage * so_texcoord_storage;
117 
118 private:
119   SoTextureCoordinateCylinder * master;
120 };
121 
122 
123 static const SbVec4f & textureCoordinateCylinderCallback(void * userdata, const SbVec3f & point, const SbVec3f & normal);
124 
125 #define PRIVATE(p) (p->pimpl)
126 #define PUBLIC(p) (p->master)
127 
128 // *************************************************************************
129 
130 SO_NODE_SOURCE(SoTextureCoordinateCylinder);
131 
132 // *************************************************************************
133 
134 /*!
135   Constructor.
136 */
SoTextureCoordinateCylinder(void)137 SoTextureCoordinateCylinder::SoTextureCoordinateCylinder(void)
138 {
139 
140   PRIVATE(this) = new SoTextureCoordinateCylinderP(this);
141   SO_NODE_INTERNAL_CONSTRUCTOR(SoTextureCoordinateCylinder);
142 
143   PRIVATE(this)->so_texcoord_storage = new SbStorage(sizeof(so_texcoordcylinder_data),
144                                                      so_texcoordcylinder_construct_data,
145                                                      so_texcoordcylinder_destruct_data);
146 }
147 
148 /*!
149   Destructor.
150 */
~SoTextureCoordinateCylinder()151 SoTextureCoordinateCylinder::~SoTextureCoordinateCylinder()
152 {
153   delete pimpl->so_texcoord_storage;
154   delete PRIVATE(this);
155 }
156 
157 // Documented in superclass.
158 void
initClass(void)159 SoTextureCoordinateCylinder::initClass(void)
160 {
161   SO_NODE_INTERNAL_INIT_CLASS(SoTextureCoordinateCylinder, SO_FROM_COIN_2_3);
162 
163   SO_ENABLE(SoGLRenderAction, SoGLMultiTextureCoordinateElement);
164   SO_ENABLE(SoCallbackAction, SoMultiTextureCoordinateElement);
165   SO_ENABLE(SoPickAction, SoMultiTextureCoordinateElement);
166 
167 }
168 
169 const SbVec4f &
textureCoordinateCylinderCallback(void * userdata,const SbVec3f & point,const SbVec3f & normal)170 textureCoordinateCylinderCallback(void * userdata,
171                           const SbVec3f & point,
172                           const SbVec3f & normal)
173 {
174 
175   SoTextureCoordinateCylinderP * pimpl = (SoTextureCoordinateCylinderP *) userdata;
176   so_texcoordcylinder_data * data = pimpl->so_texcoord_get_data();
177 
178   SoState * state = data->currentstate;
179   SoFullPath * path = (SoFullPath *) state->getAction()->getCurPath();
180   SoNode * node = path->getTail();
181 
182   if (!node->isOfType(SoShape::getClassTypeId())) {
183     // FIXME: A better way to handle this? (20040122 handegar)
184     assert(FALSE && "TextureCoordinateCylinder callback called for a non-SoShape node.");
185   }
186 
187   // Cast the node into a shape
188   SoShape * shape = (SoShape *) node;
189 
190   if (shape != data->currentshape) {
191     data->boundingbox.makeEmpty();
192     const SoBoundingBoxCache * bboxcache = shape->getBoundingBoxCache();
193     if (bboxcache && bboxcache->isValid(state)) {
194       data->boundingbox = bboxcache->getProjectedBox();
195       data->origo = data->boundingbox.getCenter();
196     }
197     else {
198       shape->computeBBox(state->getAction(), data->boundingbox, data->origo);
199       data->origo = data->boundingbox.getCenter();
200     }
201     data->currentshape = shape;
202   }
203 
204   const SbVec4f & ret = pimpl->calculateTextureCoordinate(point, normal);
205 
206   data->texcoordreturn = ret;
207   return data->texcoordreturn;
208 
209 }
210 
211 SbVec4f
calculateTextureCoordinate(SbVec3f point,SbVec3f n)212 SoTextureCoordinateCylinderP::calculateTextureCoordinate(SbVec3f point, SbVec3f n)
213 {
214 
215   // FIXME: This way of mapping will always lead to artifacts in the
216   // change between 360 and 0 degrees around the Y-axis. This is
217   // unavoidable as the callback cannot predict when the last vertex
218   // will be received, and therefore be able to patch up the
219   // transition. (20040127 handegar)
220 
221   SbVec4f tc;
222   so_texcoordcylinder_data * data = this->so_texcoord_get_data();
223 
224   const SbVec3f bmax = data->boundingbox.getMax();
225   const SbVec3f bmin = data->boundingbox.getMin();
226 
227   double maxv = fabs(n[0]);
228   int maxi = 0;
229 
230   if (fabs(n[1]) > maxv) { maxi = 1; maxv = fabs(n[1]); }
231   if (fabs(n[2]) > maxv) { maxi = 2; }
232 
233   if (maxi == 1) { // Cylinder top or bottom?
234 
235     // FIXME: A nicer solution might be to calculate the angle between
236     // the origo<->point and the origo<->bboxcorner before deciding whether this
237     // is the cylinder top/bottom or not. (20040127 handegar)
238     float d0 = bmax[2] - bmin[2];
239     float d1 = bmax[0] - bmin[0];
240     if (d0 == 0.0f) d0 = 1.0f;
241     if (d1 == 0.0f) d1 = 1.0f;
242     tc = SbVec4f((point[0] - bmin[0]) / d1,
243                  (point[2] - bmin[2]) / d0,
244                  0.0f, 1.0f);
245     if (n[maxi] > 0.0f) tc[1] = 1.0f - tc[1];
246   }
247   else {
248     float d = bmax[1] - bmin[1];
249     if (d == 0.0f) d = 1.0f;
250     tc = SbVec4f((float) (atan2(point[0], point[2]) * (1.0/(2.0*M_PI)) + 0.5),
251                  (point[1] - bmin[1]) / d,
252                  0.0f, 1.0f);
253   }
254 
255   return tc;
256 
257 }
258 
259 
260 // Documented in superclass.
261 void
doAction(SoAction * action)262 SoTextureCoordinateCylinder::doAction(SoAction * action)
263 {
264   so_texcoordcylinder_data * data = pimpl->so_texcoord_get_data();
265 
266   data->currentstate = action->getState();
267   data->currentshape = NULL;
268 
269   int unit = SoTextureUnitElement::get(data->currentstate);
270   SoMultiTextureCoordinateElement::setFunction(data->currentstate, this,
271                                                unit, textureCoordinateCylinderCallback,
272                                                PRIVATE(this));
273 }
274 
275 // Documented in superclass.
276 void
GLRender(SoGLRenderAction * action)277 SoTextureCoordinateCylinder::GLRender(SoGLRenderAction * action)
278 {
279   so_texcoordcylinder_data * data = pimpl->so_texcoord_get_data();
280 
281   data->currentstate = action->getState();
282   data->currentshape = NULL;
283 
284   int unit = SoTextureUnitElement::get(data->currentstate);
285   const cc_glglue * glue = cc_glglue_instance(SoGLCacheContextElement::get(action->getState()));
286   int maxunits = cc_glglue_max_texture_units(glue);
287   if (unit < maxunits) {
288     SoMultiTextureCoordinateElement::setFunction(data->currentstate, this,
289                                                  unit, textureCoordinateCylinderCallback,
290                                                  PRIVATE(this));
291   }
292 }
293 
294 // Documented in superclass.
295 void
callback(SoCallbackAction * action)296 SoTextureCoordinateCylinder::callback(SoCallbackAction * action)
297 {
298   SoTextureCoordinateCylinder::doAction((SoAction *)action);
299 }
300 
301 // Documented in superclass.
302 void
pick(SoPickAction * action)303 SoTextureCoordinateCylinder::pick(SoPickAction * action)
304 {
305   SoTextureCoordinateCylinder::doAction((SoAction *)action);
306 }
307 
308 #undef PRIVATE
309 #undef PUBLIC
310