Debian Codegraph @

apps/tools/ivfix/IfFlattener.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 #include <Inventor/SoPrimitiveVertex.h>
38 #include <Inventor/actions/SoCallbackAction.h>
39 #include <Inventor/nodes/SoCoordinate3.h>
40 #include <Inventor/nodes/SoIndexedShape.h>
41 #include <Inventor/nodes/SoMaterialBinding.h>
42 #include <Inventor/nodes/SoNormal.h>
43 #include <Inventor/nodes/SoNormalBinding.h>
44 #include <Inventor/nodes/SoShape.h>
45 #include <Inventor/nodes/SoTextureCoordinate2.h>
46 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
47
48 #include "IfFlattener.h"
49 #include "IfHolder.h"
50
51 /////////////////////////////////////////////////////////////////////////////
52 //
53 // Constructor. Initializes data structures.
54 //
55 /////////////////////////////////////////////////////////////////////////////
56
57 IfFlattener::IfFlattener()
58 {
59 holder = NULL;
60
61 numVerts = 0;
62 numMtlIndices = 0;
63 }
64
65 /////////////////////////////////////////////////////////////////////////////
66 //
67 // Destructor.
68 //
69 /////////////////////////////////////////////////////////////////////////////
70
71 IfFlattener::~IfFlattener()
72 {
73 }
74
75 /////////////////////////////////////////////////////////////////////////////
76 //
77 // This function assumes that everything under the given root can be
78 // treated as a big bag o' triangles. It will store coordinates,
79 // normals, texture coordinates, and material indices for these
80 // triangles in the IfHolder's nodes.
81 //
82 /////////////////////////////////////////////////////////////////////////////
83
84 void
85 IfFlattener::flatten(IfHolder *_holder)
86 {
87 holder = _holder;
88
89 // Assume that we will find lots of triangles. We'll clean up
90 // later if there are fewer. Fields will start out with this many
91 // entries. We double this size whenever we run out of space. When
92 // we're done, we shrink the fields down to the amount used.
93 coordVals = NULL;
94 normalVals = NULL;
95 texCoordVals = NULL;
96 mtlIndexVals = NULL;
97 fieldSize = 20000;
98 expandFields();
99
100 // We'll apply a callback action to produce triangles from all shapes
101 SoCallbackAction cba;
102
103 // Set up a callback to call before and after shape is traversed
104 cba.addPreCallback(SoShape::getClassTypeId(), preShapeCB, this);
105 cba.addPostCallback(SoShape::getClassTypeId(), postShapeCB, this);
106
107 // Set up a callback to call when a shape is traversed
108 cba.addTriangleCallback(SoShape::getClassTypeId(), triangleCB, this);
109
110 // Traverse the graph, adding to the data structures
111 cba.apply(holder->origRoot);
112
113 // Finish editing
114 holder->coords->point.finishEditing();
115 holder->triSet->materialIndex.finishEditing();
116 if (holder->doNormals)
117 holder->normals->vector.finishEditing();
118 if (holder->doTexCoords)
119 holder->texCoords->point.finishEditing();
120
121 // Process the resulting structures to produce a graph
122 createGraph();
123 }
124
125 /////////////////////////////////////////////////////////////////////////////
126 //
127 // Expands the fields in the nodes to contain fieldSize entries.
128 //
129 /////////////////////////////////////////////////////////////////////////////
130
131 void
132 IfFlattener::expandFields()
133 {
134 // If already editing the fields, stop!
135 if (coordVals != NULL) {
136 holder->coords->point.finishEditing();
137 holder->triSet->materialIndex.finishEditing();
138 if (holder->doNormals)
139 holder->normals->vector.finishEditing();
140 if (holder->doTexCoords)
141 holder->texCoords->point.finishEditing();
142 }
143
144 // Increase the size
145 holder->coords->point.setNum(fieldSize);
146 if (holder->doNormals)
147 holder->normals->vector.setNum(fieldSize);
148 if (holder->doTexCoords)
149 holder->texCoords->point.setNum(fieldSize);
150 holder->triSet->coordIndex.setNum(fieldSize);
151 holder->triSet->materialIndex.setNum(fieldSize);
152
153 // Start editing (again, maybe)
154 coordVals = holder->coords->point.startEditing();
155 mtlIndexVals = holder->triSet->materialIndex.startEditing();
156 if (holder->doNormals)
157 normalVals = holder->normals->vector.startEditing();
158 if (holder->doTexCoords)
159 texCoordVals = holder->texCoords->point.startEditing();
160 }
161
162 /////////////////////////////////////////////////////////////////////////////
163 //
164 // Prepares to process the triangles making up a shape.
165 //
166 /////////////////////////////////////////////////////////////////////////////
167
168 void
169 IfFlattener::prepareForShape(SoCallbackAction *cba, const SoShape *)
170 {
171 // Get the current model matrix so we can transform the
172 // coordinates and normals
173 pointMatrix = cba->getModelMatrix();
174 if (holder->doNormals)
175 normalMatrix = pointMatrix.inverse().transpose();
176 if (holder->doTexCoords)
177 textureMatrix = cba->getTextureMatrix();
178 }
179
180 /////////////////////////////////////////////////////////////////////////////
181 //
182 // Adds a triangle generated by the current shape.
183 //
184 /////////////////////////////////////////////////////////////////////////////
185
186 void
187 IfFlattener::addTriangle(SoCallbackAction *, const SoPrimitiveVertex *verts[3])
188 {
189 // Make sure there's enough room in the fields
190 if (numMtlIndices + 4 >= fieldSize) {
191 fieldSize *= 2;
192 expandFields();
193 }
194
195 // For each of the three vertices
196 for (int i = 0; i < 3; i++) {
197
198 addCoordinate(verts[i]->getPoint());
199
200 if (holder->doNormals)
201 addNormal(verts[i]->getNormal());
202
203 if (holder->doTexCoords)
204 addTextureCoordinate(verts[i]->getTextureCoords());
205
206 addMaterialIndex(verts[i]->getMaterialIndex());
207
208 numVerts++;
209 numMtlIndices++;
210 }
211
212 // Add a material index to skip (for the end-of-triangle marker)
213 addMaterialIndex(-1);
214 numMtlIndices++;
215 }
216
217 /////////////////////////////////////////////////////////////////////////////
218 //
219 // Finishes processing of the current shape.
220 //
221 /////////////////////////////////////////////////////////////////////////////
222
223 void
224 IfFlattener::finishShape(SoCallbackAction *, const SoShape *)
225 {
226 }
227
228 /////////////////////////////////////////////////////////////////////////////
229 //
230 // Adds a point to the list of coordinates.
231 //
232 /////////////////////////////////////////////////////////////////////////////
233
234 void
235 IfFlattener::addCoordinate(const SbVec3f &point)
236 {
237 SbVec3f transformedPoint;
238 pointMatrix.multVecMatrix(point, transformedPoint);
239 coordVals[numVerts] = transformedPoint;
240 }
241
242 /////////////////////////////////////////////////////////////////////////////
243 //
244 // Adds a normal to the list of normals.
245 //
246 /////////////////////////////////////////////////////////////////////////////
247
248 void
249 IfFlattener::addNormal(const SbVec3f &normal)
250 {
251 SbVec3f transformedNormal;
252 normalMatrix.multDirMatrix(normal, transformedNormal);
253 transformedNormal.normalize();
254 normalVals[numVerts] = transformedNormal;
255 }
256
257 /////////////////////////////////////////////////////////////////////////////
258 //
259 // Adds a material index to the list of indices.
260 //
261 /////////////////////////////////////////////////////////////////////////////
262
263 void
264 IfFlattener::addMaterialIndex(int materialIndex)
265 {
266 mtlIndexVals[numMtlIndices] = materialIndex;
267 }
268
269 /////////////////////////////////////////////////////////////////////////////
270 //
271 // Adds a texture coordinate to the list.
272 //
273 /////////////////////////////////////////////////////////////////////////////
274
275 void
276 IfFlattener::addTextureCoordinate(const SbVec4f &texCoord)
277 {
278 // Ignore everything except S and T
279 SbVec3f stTexCoord(texCoord[0], texCoord[1], 0.0);
280
281 SbVec3f transformedTexCoord;
282 textureMatrix.multVecMatrix(stTexCoord, transformedTexCoord);
283
284 SbVec2f t(transformedTexCoord[0], transformedTexCoord[1]);
285 texCoordVals[numVerts] = t;
286 }
287
288 /////////////////////////////////////////////////////////////////////////////
289 //
290 // Processes the resulting structures to produce a graph.
291 //
292 /////////////////////////////////////////////////////////////////////////////
293
294 void
295 IfFlattener::createGraph()
296 {
297 // The coordinates, normals, and texture coordinates should
298 // already be set. All we need to do here is set up the
299 // indices in the SoIndexedShape.
300 int numTris = numVerts / 3;
301 holder->triSet->coordIndex.setNum(numTris * 4);
302 int32_t *cindex = holder->triSet->coordIndex.startEditing();
303 int ci = 0, v = 0;
304 for (int t = 0; t < numTris; t++) {
305 cindex[ci++] = v++;
306 cindex[ci++] = v++;
307 cindex[ci++] = v++;
308 cindex[ci++] = -1;
309 }
310 holder->triSet->coordIndex.finishEditing();
311
312 // The normal and texture coordinate indices start out the same as
313 // the coordinate indices
314 if (holder->doNormals)
315 holder->triSet->normalIndex = holder->triSet->coordIndex;
316 if (holder->doTexCoords)
317 holder->triSet->textureCoordIndex = holder->triSet->coordIndex;
318
319 // Decrease the sizes of the fields if there is any extra space
320 if (numVerts < fieldSize) {
321 holder->coords->point.setNum(numVerts);
322 if (holder->doNormals)
323 holder->normals->vector.setNum(numVerts);
324 if (holder->doTexCoords)
325 holder->texCoords->point.setNum(numVerts);
326 }
327 if (numMtlIndices < fieldSize)
328 holder->triSet->materialIndex.setNum(numMtlIndices);
329
330 // Assume that the normals, materials, and texture coordinates are
331 // bound per vertex, using the indices
332 if (holder->doNormals)
333 holder->normalBinding->value = SoNormalBinding::PER_VERTEX_INDEXED;
334 holder->materialBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
335 if (holder->doTexCoords)
336 holder->texCoordBinding->value =
337 SoTextureCoordinateBinding::PER_VERTEX_INDEXED;
338 }