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 SoTextureCoordinateCache SoTextureCoordinateCache.h Inventor/caches/SoTextureCoordinateCache.h
35   The SoTextureCoordinateClass is used to cache generated texture coordinates.
36 */
37 
38 #include <Inventor/caches/SoTextureCoordinateCache.h>
39 #include <Inventor/lists/SbList.h>
40 #include <Inventor/SbBox3f.h>
41 #include <Inventor/SbVec2f.h>
42 
43 #ifndef DOXYGEN_SKIP_THIS
44 
45 class SoTextureCoordinateCacheP {
46 public:
47   SbList <SbVec2f> texCoords;
48 };
49 
50 #endif // DOXYGEN_SKIP_THIS
51 
52 #define PRIVATE(obj) ((obj)->pimpl)
53 
54 /*!
55   Constructor.
56 */
SoTextureCoordinateCache(SoState * const state)57 SoTextureCoordinateCache::SoTextureCoordinateCache(SoState * const state)
58   : SoCache(state)
59 {
60   PRIVATE(this) = new SoTextureCoordinateCacheP;
61 }
62 
63 /*!
64   Destructor.
65 */
~SoTextureCoordinateCache()66 SoTextureCoordinateCache::~SoTextureCoordinateCache()
67 {
68   delete PRIVATE(this);
69 }
70 
71 /*!
72   Generates texture coordinates based on the bounding box of the
73   geometry. This is usually called default texture coordinates
74   in OIV.
75 */
76 void
generate(const SbBox3f & bbox,const SbVec3f * vertices,const int numvertices)77 SoTextureCoordinateCache::generate(const SbBox3f & bbox,
78                                    const SbVec3f * vertices,
79                                    const int numvertices)
80 {
81   // FIXME: Support 3D texture coordinates. This functionality
82   // is more or less a duplicate of that in SoTextureCoordinateBundle
83   // (kintel 20020203)
84   float sizes[3];
85   float minvalues[3];
86   int   offsets[3];
87 
88   bbox.getSize(sizes[0], sizes[1], sizes[2]);
89   minvalues[0] = bbox.getMin()[0];
90   minvalues[1] = bbox.getMin()[1];
91   minvalues[2] = bbox.getMin()[2];
92   offsets[0] = 0;
93   offsets[1] = 1;
94   offsets[2] = 2;
95 
96   // bubblesort :-)
97   if (sizes[0] < sizes[1]) {
98     SbSwap(sizes[0], sizes[1]);
99     SbSwap(minvalues[0], minvalues[1]);
100     SbSwap(offsets[0], offsets[1]);
101   }
102   if (sizes[1] < sizes[2]) {
103     SbSwap(sizes[1], sizes[2]);
104     SbSwap(minvalues[1], minvalues[2]);
105     SbSwap(offsets[1], offsets[2]);
106   }
107   if (sizes[0] < sizes[1]) {
108     SbSwap(sizes[0], sizes[1]);
109     SbSwap(minvalues[0], minvalues[1]);
110     SbSwap(offsets[0], offsets[1]);
111   }
112 
113   float s, t;
114   for (int i = 0; i < numvertices; i++) {
115     s = vertices[i][offsets[0]];
116     t = vertices[i][offsets[1]];
117     s -= minvalues[0];
118     t -= minvalues[1];
119     s /= sizes[0];
120     t /= sizes[1];
121     // expand list array as needed
122     if (i >= PRIVATE(this)->texCoords.getLength()) PRIVATE(this)->texCoords.append(SbVec2f());
123     PRIVATE(this)->texCoords[i].setValue(s, t);
124   }
125 
126   // fit list array in case we used to have more items than now
127   PRIVATE(this)->texCoords.truncate(numvertices);
128 }
129 
130 /*!
131   Returns the generated texture coordinates.
132 */
133 const SbVec2f *
get(void) const134 SoTextureCoordinateCache::get(void) const
135 {
136   return PRIVATE(this)->texCoords.getArrayPtr();
137 }
138 
139 /*!
140   Returns the number of generated texture coordinates.
141 */
142 int
getNum(void) const143 SoTextureCoordinateCache::getNum(void) const
144 {
145   return PRIVATE(this)->texCoords.getLength();
146 }
147 
148 #undef PRIVATE
149