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 SoTextureCoordinateSphere include/Inventor/nodes/SoTextureCoordinateSphere.h
35   \brief The SoTextureCoordinateSphere class autogenerates spheremapped texture coordinated for shapes.
36 
37   \ingroup nodes
38 
39   <b>FILE FORMAT/DEFAULTS:</b>
40   \code
41     TextureCoordinateSphere {
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/SoTextureCoordinateSphere.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_texcoordsphere_data;
84 
85 static void
so_texcoordsphere_construct_data(void * closure)86 so_texcoordsphere_construct_data(void * closure)
87 {
88   so_texcoordsphere_data * data = (so_texcoordsphere_data *) closure;
89   data->currentshape = NULL;
90   data->currentstate = NULL;
91   data->origo = SbVec3f(0,0,0);
92 }
93 
94 static void
so_texcoordsphere_destruct_data(void * COIN_UNUSED_ARG (closure))95 so_texcoordsphere_destruct_data(void * COIN_UNUSED_ARG(closure))
96 {
97 }
98 
99 SO_NODE_SOURCE(SoTextureCoordinateSphere);
100 
101 class SoTextureCoordinateSphereP {
102 
103 public:
SoTextureCoordinateSphereP(SoTextureCoordinateSphere * texturenode)104   SoTextureCoordinateSphereP(SoTextureCoordinateSphere * texturenode)
105     : master(texturenode) { }
106 
107   SbVec4f calculateTextureCoordinate(SbVec3f point, SbVec3f n);
108 
so_texcoord_get_data()109   so_texcoordsphere_data * so_texcoord_get_data() {
110     so_texcoordsphere_data * data = NULL;
111     data = (so_texcoordsphere_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   SoTextureCoordinateSphere * master;
120 };
121 
122 
123 static const SbVec4f & textureCoordinateSphereCallback(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   Constructor.
131 */
SoTextureCoordinateSphere(void)132 SoTextureCoordinateSphere::SoTextureCoordinateSphere(void)
133 {
134 
135   PRIVATE(this) = new SoTextureCoordinateSphereP(this);
136 
137   SO_NODE_INTERNAL_CONSTRUCTOR(SoTextureCoordinateSphere);
138 
139   pimpl->so_texcoord_storage = new SbStorage(sizeof(so_texcoordsphere_data),
140                                              so_texcoordsphere_construct_data,
141                                              so_texcoordsphere_destruct_data);
142 }
143 
144 /*!
145   Destructor.
146 */
~SoTextureCoordinateSphere()147 SoTextureCoordinateSphere::~SoTextureCoordinateSphere()
148 {
149   delete pimpl->so_texcoord_storage;
150   delete PRIVATE(this);
151 }
152 
153 // Documented in superclass.
154 void
initClass(void)155 SoTextureCoordinateSphere::initClass(void)
156 {
157   SO_NODE_INTERNAL_INIT_CLASS(SoTextureCoordinateSphere, SO_FROM_COIN_2_3);
158 
159   SO_ENABLE(SoGLRenderAction, SoGLMultiTextureCoordinateElement);
160   SO_ENABLE(SoCallbackAction, SoMultiTextureCoordinateElement);
161   SO_ENABLE(SoPickAction, SoMultiTextureCoordinateElement);
162 
163 }
164 
165 const SbVec4f &
textureCoordinateSphereCallback(void * userdata,const SbVec3f & point,const SbVec3f & normal)166 textureCoordinateSphereCallback(void * userdata,
167                           const SbVec3f & point,
168                           const SbVec3f & normal)
169 {
170 
171   SoTextureCoordinateSphereP * pimpl = (SoTextureCoordinateSphereP *) userdata;
172   so_texcoordsphere_data * data = pimpl->so_texcoord_get_data();
173 
174   SoState * state = data->currentstate;
175   SoFullPath * path = (SoFullPath *) state->getAction()->getCurPath();
176   SoNode * node = path->getTail();
177 
178 
179   if (!node->isOfType(SoShape::getClassTypeId())) {
180     // FIXME: A better way to handle this? (20040122 handegar)
181     assert(FALSE && "TextureCoordinateSphere callback called for a non-SoShape node.");
182   }
183 
184   // Cast the node into a shape
185   SoShape * shape = (SoShape *) node;
186 
187   if (shape != data->currentshape) {
188     data->boundingbox.makeEmpty();
189     const SoBoundingBoxCache * bboxcache = shape->getBoundingBoxCache();
190     if (bboxcache && bboxcache->isValid(state)) {
191       data->boundingbox = bboxcache->getProjectedBox();
192       data->origo = data->boundingbox.getCenter();
193     }
194     else {
195       shape->computeBBox(state->getAction(), data->boundingbox, data->origo);
196       data->origo = data->boundingbox.getCenter();
197     }
198     data->currentshape = shape;
199   }
200 
201   const SbVec4f & ret = pimpl->calculateTextureCoordinate(point, normal);
202 
203   data->texcoordreturn = ret;
204   return data->texcoordreturn;
205 
206 }
207 
208 SbVec4f
calculateTextureCoordinate(SbVec3f point,SbVec3f COIN_UNUSED_ARG (n))209 SoTextureCoordinateSphereP::calculateTextureCoordinate(SbVec3f point, SbVec3f COIN_UNUSED_ARG(n))
210 {
211 
212   // FIXME: This way of mapping will always lead to artifacts in the
213   // change between 360 and 0 degrees around the Y-axis. This is
214   // unavoidable as the callback cannot predict when the last vertex
215   // will be received, and therefore be able to patch up the
216   // transition. (20040127 handegar)
217 
218   SbVec4f tc((float) (atan2(point[0], point[2]) * (1.0/(2.0*M_PI)) + 0.5),
219              (float) (atan2(point[1], sqrt(point[0]*point[0] + point[2]*point[2])) * (1.0/M_PI) + 0.5),
220              0.0f, 1.0f);
221 
222   return tc;
223 
224 }
225 
226 
227 // Documented in superclass.
228 void
doAction(SoAction * action)229 SoTextureCoordinateSphere::doAction(SoAction * action)
230 {
231   so_texcoordsphere_data * data = PRIVATE(this)->so_texcoord_get_data();
232 
233   data->currentstate = action->getState();
234   data->currentshape = NULL;
235 
236   int unit = SoTextureUnitElement::get(data->currentstate);
237   SoMultiTextureCoordinateElement::setFunction(data->currentstate, this,
238                                                unit, textureCoordinateSphereCallback,
239                                                PRIVATE(this));
240 }
241 
242 // Documented in superclass.
243 void
GLRender(SoGLRenderAction * action)244 SoTextureCoordinateSphere::GLRender(SoGLRenderAction * action)
245 {
246   so_texcoordsphere_data * data = PRIVATE(this)->so_texcoord_get_data();
247 
248   data->currentstate = action->getState();
249   data->currentshape = NULL;
250 
251   int unit = SoTextureUnitElement::get(data->currentstate);
252   const cc_glglue * glue = cc_glglue_instance(SoGLCacheContextElement::get(action->getState()));
253   int maxunits = cc_glglue_max_texture_units(glue);
254   if (unit < maxunits) {
255     SoMultiTextureCoordinateElement::setFunction(data->currentstate, this,
256                                                  unit, textureCoordinateSphereCallback,
257                                                  PRIVATE(this));
258   }
259 }
260 
261 // Documented in superclass.
262 void
callback(SoCallbackAction * action)263 SoTextureCoordinateSphere::callback(SoCallbackAction * action)
264 {
265   SoTextureCoordinateSphere::doAction((SoAction *)action);
266 }
267 
268 // Documented in superclass.
269 void
pick(SoPickAction * action)270 SoTextureCoordinateSphere::pick(SoPickAction * action)
271 {
272   SoTextureCoordinateSphere::doAction((SoAction *)action);
273 }
274 
275 #undef PRIVATE
276 #undef PUBLIC
277