/
COLLADAFWMesh.cpp
236 lines (200 loc) · 7.2 KB
/
COLLADAFWMesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright (c) 2008-2009 NetAllied Systems GmbH
This file is part of COLLADAFramework.
Licensed under the MIT Open Source License,
for details please see LICENSE file or the website
http://www.opensource.org/licenses/mit-license.php
*/
#include "COLLADAFWStableHeaders.h"
#include "COLLADAFWMesh.h"
#include "COLLADAFWTristrips.h"
#include "COLLADAFWLinestrips.h"
namespace COLLADAFW
{
//----------------------------------
Mesh::Mesh( const UniqueId& uniqueId )
: Geometry ( uniqueId, Geometry::GEO_TYPE_MESH )
, mMeshPrimitives(MeshPrimitiveArray::OWNER)
{
}
//----------------------------------
Mesh::~Mesh()
{
for ( size_t i = 0, count = mMeshPrimitives.getCount(); i < count; ++i)
delete mMeshPrimitives[i];
}
//----------------------------------
size_t Mesh::getTrianglesTriangleCount()
{
return getPrimitiveCount(MeshPrimitive::TRIANGLES);
}
//---------------------------------------------------------------
size_t Mesh::getTristripsTriangleCount()
{
return getFaceCount(MeshPrimitive::TRIANGLE_STRIPS);
}
//---------------------------------------------------------------
size_t Mesh::getTrifansTriangleCount()
{
return getFaceCount(MeshPrimitive::TRIANGLE_FANS);
}
//---------------------------------------------------------------
size_t Mesh::getPolygonsPolygonCount()
{
return getPrimitiveCount(MeshPrimitive::POLYGONS);
}
//---------------------------------------------------------------
size_t Mesh::getPolylistPolygonCount()
{
return getPrimitiveCount(MeshPrimitive::POLYLIST);
}
//---------------------------------------------------------------
size_t Mesh::getPrimitiveCount( MeshPrimitive::PrimitiveType primitiveType )
{
size_t primitiveCount = 0;
for ( size_t i = 0, count = mMeshPrimitives.getCount(); i < count; ++i)
{
MeshPrimitive* primitive = mMeshPrimitives[i];
if ( primitive && ( primitive->getPrimitiveType() == primitiveType ) )
{
switch ( primitiveType )
{
case MeshPrimitive::TRIANGLES:
case MeshPrimitive::LINES:
case MeshPrimitive::POLYGONS:
case MeshPrimitive::POLYLIST:
primitiveCount += primitive->getFaceCount();
break;
case MeshPrimitive::TRIANGLE_STRIPS:
{
Tristrips* tristrips = (Tristrips*)primitive;
primitiveCount += tristrips->getTristripCount();
}
break;
case MeshPrimitive::TRIANGLE_FANS:
{
// TODO not implemented
//Tristrips* tristrips = (Tristrips*)primitive;
//primitiveCount += tristrips->getTristripCount();
}
break;
case MeshPrimitive::LINE_STRIPS:
{
Linestrips* linestrips = (Linestrips*)primitive;
primitiveCount += linestrips->getLinestripCount ();
}
break;
case MeshPrimitive::UNDEFINED_PRIMITIVE_TYPE:
default:
break;
}
}
}
return primitiveCount;
}
//----------------------------------
size_t Mesh::getFaceCount( MeshPrimitive::PrimitiveType primitiveType )
{
size_t faceCount = 0;
for ( size_t i = 0, count = mMeshPrimitives.getCount(); i < count; ++i)
{
MeshPrimitive* primitive = mMeshPrimitives[i];
if ( primitive && ( primitive->getPrimitiveType() == primitiveType ) )
faceCount += primitive->getFaceCount();
}
return faceCount;
}
//---------------------------------------------------------------
size_t Mesh::getTrianglesCount()
{
return getPrimitiveCount(MeshPrimitive::TRIANGLES);
}
//---------------------------------------------------------------
size_t Mesh::getTristripsCount()
{
return getPrimitiveCount(MeshPrimitive::TRIANGLE_STRIPS);
}
//---------------------------------------------------------------
size_t Mesh::getTrifansCount()
{
return getPrimitiveCount(MeshPrimitive::TRIANGLE_FANS);
}
//---------------------------------------------------------------
size_t Mesh::getPolygonsCount()
{
return getPrimitiveCount(MeshPrimitive::POLYGONS);
}
//---------------------------------------------------------------
size_t Mesh::getMeshPrimitiveCount( MeshPrimitive::PrimitiveType primitiveType )
{
size_t primitiveCount = 0;
for ( size_t i = 0, count = mMeshPrimitives.getCount(); i < count; ++i)
{
MeshPrimitive* primitive = mMeshPrimitives[i];
if ( primitive && ( primitive->getPrimitiveType() == primitiveType ) )
primitiveCount++;
}
return primitiveCount;
}
//----------------------------------
bool Mesh::hasNormals( )const
{
return getNormals().getValuesCount() != 0;
}
//----------------------------------
const size_t Mesh::getNormalsCount() const
{
// The number of normals in the current mesh.
size_t numNormals = 0;
// We have to go through every mesh primitive.
const MeshPrimitiveArray& meshPrimitives = this->getMeshPrimitives ();
size_t count = meshPrimitives.getCount ();
for ( size_t i=0; i<count; ++i )
{
// Get the current primitive element.
const MeshPrimitive* meshPrimitive = meshPrimitives [ i ];
// Get the normal indices of the current primitive.
const UIntValuesArray& normalIndices = meshPrimitive->getNormalIndices ();
switch ( meshPrimitive->getPrimitiveType () )
{
case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
case COLLADAFW::MeshPrimitive::TRIANGLE_STRIPS:
{
COLLADAFW::MeshPrimitiveWithFaceVertexCount<unsigned int>* trifans = (COLLADAFW::MeshPrimitiveWithFaceVertexCount<unsigned int>*) meshPrimitive;
COLLADAFW::MeshPrimitiveWithFaceVertexCount<unsigned int>::VertexCountArray& vertexCountArray = trifans->getGroupedVerticesVertexCountArray ();
size_t groupedVtxCount = vertexCountArray.getCount ();
for ( size_t groupedVtxIndex=0; groupedVtxIndex<groupedVtxCount; ++groupedVtxIndex )
{
// Iterate over the indices and write their normal values into the maya file.
size_t indexCount = vertexCountArray[groupedVtxIndex]; //normalIndices.getCount();
numNormals += (indexCount - 2) * 3;
}
}
break;
default:
{
// Add the normals to the sum of normals
numNormals += normalIndices.getCount ();
}
}
}
return numNormals;
}
//----------------------------------
const size_t Mesh::getFacesCount() const
{
// The number of faces in the current mesh.
size_t numFaces = 0;
// We have to go through every mesh primitive.
const MeshPrimitiveArray& meshPrimitives = this->getMeshPrimitives ();
size_t count = meshPrimitives.getCount ();
for ( size_t i=0; i<count; ++i )
{
// Get the current primitive element.
const MeshPrimitive* meshPrimitive = meshPrimitives [ i ];
// Add the face count to the sum of faces
numFaces += meshPrimitive->getFaceCount ();
}
return numFaces;
}
} // namespace COLLADAFW