Debian Codegraph @

lib/database/src/so/upgraders/SoV1IndexedTriangleMesh.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 /*
38 * Copyright (C) 1990,91,92 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoV1IndexedTriangleMesh
47 |
48 | Author(s) : Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include "SoV1IndexedTriangleMesh.h"
55 #include <Inventor/nodes/SoIndexedTriangleStripSet.h>
56
57 SO_NODE_SOURCE(SoV1IndexedTriangleMesh);
58
59 ////////////////////////////////////////////////////////////////////////
60 //
61 // Description:
62 // Default constructor
63 //
64 // Use: public
65
66 SoV1IndexedTriangleMesh::SoV1IndexedTriangleMesh()
67 //
68 ////////////////////////////////////////////////////////////////////////
69 {
70 SO_NODE_CONSTRUCTOR(SoV1IndexedTriangleMesh);
71
72 SO_NODE_ADD_FIELD(coordIndex, (-1));
73 SO_NODE_ADD_FIELD(materialIndex, (-1));
74 SO_NODE_ADD_FIELD(normalIndex, (-1));
75 SO_NODE_ADD_FIELD(textureCoordIndex, (-1));
76 }
77
78 ////////////////////////////////////////////////////////////////////////
79 //
80 // Description:
81 // Destructor.
82 //
83 // Use: private
84
85 SoV1IndexedTriangleMesh::~SoV1IndexedTriangleMesh()
86 //
87 ////////////////////////////////////////////////////////////////////////
88 {
89 }
90
91 ////////////////////////////////////////////////////////////////////////
92 //
93 // Description:
94 // Convert the triangle mesh to an IndexedFaceSet.
95 //
96 // Use: private
97
98 SoNode *
99 SoV1IndexedTriangleMesh::createNewNode()
100 //
101 ////////////////////////////////////////////////////////////////////////
102 {
103 SoIndexedTriangleStripSet *result = SO_UPGRADER_CREATE_NEW(SoIndexedTriangleStripSet);
104
105 if (coordIndex.getNum() < 3) return result;
106
107 result->coordIndex.deleteValues(0);
108
109 // First, guess whether or not the other index fields are
110 // PER_VERTEX_INDEXED; we'll assume they are if they have the same
111 // (or greater) number of indices as the coordIndex field. That
112 // is a fair assumption, since the other indexed binding
113 // (PER_FACE_INDEXED) requires fewer indices.
114 inorm = FALSE;
115 imat = FALSE;
116 itex = FALSE;
117
118 if (normalIndex.getNum() >= coordIndex.getNum()) inorm = TRUE;
119 else { // Copy over field...
120 result->normalIndex.setValues(0, normalIndex.getNum(),
121 normalIndex.getValues(0));
122 }
123 if (materialIndex.getNum() >= coordIndex.getNum()) imat = TRUE;
124 else { // Copy over field...
125 result->materialIndex.setValues(0, materialIndex.getNum(),
126 materialIndex.getValues(0));
127 }
128 if (textureCoordIndex.getNum() >= coordIndex.getNum()) itex = TRUE;
129 else { // Copy over field...
130 result->textureCoordIndex.setValues(0, textureCoordIndex.getNum(),
131 textureCoordIndex.getValues(0));
132 }
133
134 // Converting is only tricky because of swaps. We can get the
135 // same effect as a swap by re-sending the previous vertex, to
136 // form one degenerate triangle. This is almost always more
137 // efficient than ending the strip and beginning a new one.
138 //
139 // If there are swaps, PER_VERTEX bindings will come out wrong,
140 // since vertices are repeated.
141
142 // These keep track of the last two indices in the last triangle
143 // formed:
144 int triVerts[2];
145 triVerts[0] = triVerts[1] = -1;
146
147 for (int i = 0; i < coordIndex.getNum(); i++) {
148
149 // Repeat last vertex:
150 if (coordIndex[i] == SO_SWAP_MESH_INDEX) {
151 if (triVerts[0] != -1) {
152 addVertex(result, triVerts[0]);
153
154 // Emulate swap:
155 int temp = triVerts[0];
156 triVerts[0] = triVerts[1];
157 triVerts[1] = temp;
158 }
159 else {
160 // Do nothing
161 #ifdef DEBUG
162 SoDebugError::post("SoV1IndexedTriangleMesh",
163 "Got an SO_SWAP_MESH_INDEX before any vertices");
164 #endif
165 }
166 }
167 // At the end of the mesh, reset triVerts and add END_INDEX
168 // to indexedTriStrip:
169 else if (coordIndex[i] == SO_END_MESH_INDEX) {
170 addVertex(result, SO_END_MESH_INDEX);
171
172 triVerts[0] = triVerts[1] = -1;
173 }
174 // Normal case, got a new vertex:
175 else {
176 if (coordIndex[i] < 0) {
177 #ifdef DEBUG
178 SoDebugError::post("SoV1IndexedTriangleMesh",
179 "coordIndex[%d]=%d out of range",
180 i, coordIndex[i]);
181 #endif
182 }
183 else {
184 addVertex(result, i);
185
186 triVerts[0] = triVerts[1];
187 triVerts[1] = i;
188 }
189 }
190 }
191 return result;
192 }
193
194 // Helper routine:
195 void
196 SoV1IndexedTriangleMesh::addVertex(SoIndexedTriangleStripSet *result,
197 int v)
198 {
199 int index = result->coordIndex.getNum();
200
201 if (v < 0) { // Have an END_MESH
202 result->coordIndex.set1Value(index, v);
203 if (inorm)
204 result->normalIndex.set1Value(index, v);
205 if (imat)
206 result->materialIndex.set1Value(index, v);
207 if (itex)
208 result->textureCoordIndex.set1Value(index, v);
209 } else {
210 result->coordIndex.set1Value(index, coordIndex[v]);
211 if (inorm)
212 result->normalIndex.set1Value(index, normalIndex[v]);
213 if (imat)
214 result->materialIndex.set1Value(index, materialIndex[v]);
215 if (itex)
216 result->textureCoordIndex.set1Value(index,
217 textureCoordIndex[v]);
218 }
219 }