/
mioUtils.cpp
445 lines (332 loc) · 12.9 KB
/
mioUtils.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// MeshIO Copyright © 2019 Andy Maloney <asmaloney@gmail.com>
// SPDX-License-Identifier: BSD-3-Clause
#include <QImageReader>
#include <QRegularExpression>
#include <QVector3D>
#include "ccHObjectCaster.h"
#include "ccMaterialSet.h"
#include "ccMesh.h"
#include "ccPointCloud.h"
#include "assimp/material.h"
#include "assimp/mesh.h"
#include "assimp/metadata.h"
#include "assimp/scene.h"
#include "mioUtils.h"
namespace mioUtils
{
static QImage _getEmbeddedTexture( unsigned int inTextureIndex, const aiScene *inScene )
{
QImage image;
if ( inScene->mNumTextures == 0 )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Scene requests embedded texture, but there are none" ) );
return image;
}
auto texture = inScene->mTextures[inTextureIndex];
// From assimp: "If mHeight is zero the texture is compressed"
bool isCompressed = (texture->mHeight == 0);
if ( !isCompressed )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Uncompressed embedded textures not yet implemented" ) );
return image;
}
// From assimp: "mWidth specifies the size of the memory area pcData is pointing to, in bytes"
auto dataSize = static_cast<const int32_t>(texture->mWidth);
const QByteArray imageDataByteArray( reinterpret_cast<const char *>(texture->pcData), dataSize );
return QImage::fromData( imageDataByteArray );
}
static QImage _getTextureFromFile( const QString &inPath, const QString &inTexturePath )
{
const QString cPath = QStringLiteral( "%1/%2" ).arg( inPath, inTexturePath );
if ( !QFile::exists( cPath ) )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Material not found: '%1'" ).arg( cPath ) );
return {};
}
QImageReader reader( cPath );
return reader.read();
}
inline ccColor::Rgbaf _convertColour( const aiColor4D &inColour )
{
return ccColor::Rgbaf{ inColour.r, inColour.g, inColour.b, inColour.a };
}
// Map all the material properties we know about from assimp
static void _assignMaterialProperties( aiMaterial *inAIMaterial, ccMaterial::Shared &inCCMaterial )
{
aiColor4D colour;
if ( inAIMaterial->Get( AI_MATKEY_COLOR_DIFFUSE, colour ) == AI_SUCCESS )
{
inCCMaterial->setDiffuse( _convertColour( colour ) );
}
if ( inAIMaterial->Get( AI_MATKEY_COLOR_AMBIENT, colour ) == AI_SUCCESS )
{
inCCMaterial->setAmbient( _convertColour( colour ) );
}
if ( inAIMaterial->Get( AI_MATKEY_COLOR_SPECULAR, colour ) == AI_SUCCESS )
{
inCCMaterial->setSpecular( _convertColour( colour ) );
}
if ( inAIMaterial->Get( AI_MATKEY_COLOR_EMISSIVE, colour ) == AI_SUCCESS )
{
inCCMaterial->setEmission( _convertColour( colour ) );
}
ai_real property;
if ( inAIMaterial->Get( AI_MATKEY_SHININESS, property ) == AI_SUCCESS )
{
inCCMaterial->setShininess( property );
}
if ( inAIMaterial->Get( AI_MATKEY_OPACITY, property ) == AI_SUCCESS )
{
inCCMaterial->setTransparency( property );
}
}
ccMaterialSet *createMaterialSetForMesh( const aiMesh *inMesh, const QString &inPath, const aiScene *inScene )
{
if ( inScene->mNumMaterials == 0 )
{
return nullptr;
}
unsigned int index = inMesh->mMaterialIndex;
const auto aiMaterial = inScene->mMaterials[index];
const aiString cName = aiMaterial->GetName();
auto newMaterial = ccMaterial::Shared( new ccMaterial( cName.C_Str() ) );
ccLog::PrintDebug( QStringLiteral( "[MeshIO] Creating material '%1'" ).arg( newMaterial->getName() ) );
// we only handle the diffuse texture for now
if ( aiMaterial->GetTextureCount( aiTextureType_DIFFUSE ) > 0 )
{
aiString texturePath;
if ( aiMaterial->GetTexture( aiTextureType_DIFFUSE, 0, &texturePath ) != AI_SUCCESS )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Could not get texture path" ) );
}
else
{
static QRegularExpression sRegExp( "^\\*(?<index>[0-9]+)$" );
auto match = sRegExp.match( texturePath.C_Str() );
QImage image;
QString path;
if ( match.hasMatch() )
{
const QString cIndex = match.captured( "index" );
image = _getEmbeddedTexture( cIndex.toUInt(), inScene );
}
else
{
image = _getTextureFromFile( inPath, texturePath.C_Str() );
path = QStringLiteral( "%1/%2" ).arg( inPath, texturePath.C_Str() );
}
if ( !image.isNull() )
{
newMaterial->setTexture( image, path );
}
}
}
_assignMaterialProperties( aiMaterial, newMaterial );
ccMaterialSet *materialSet = new ccMaterialSet( "Materials" );
materialSet->addMaterial( newMaterial );
return materialSet;
}
ccMesh *newCCMeshFromAIMesh( const aiMesh *inMesh )
{
auto newPC = new ccPointCloud( "Vertices" );
auto newMesh = new ccMesh( newPC );
QString name( inMesh->mName.C_Str() );
if ( name.isEmpty() )
{
name = QStringLiteral( "Mesh" );
}
ccLog::Print( QStringLiteral( "[MeshIO] Mesh '%1' has %2 verts & %3 faces" )
.arg( name,
QLocale::system().toString( inMesh->mNumVertices ),
QLocale::system().toString( inMesh->mNumFaces ) ) );
if ( !inMesh->HasPositions() || !inMesh->HasFaces() )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Mesh '%1' does not have vertices or faces" ).arg( name ) );
delete newPC;
delete newMesh;
return nullptr;
}
// reserve memory for points and mesh (because we need to do this before other memory allocations)
newPC->reserveThePointsTable( inMesh->mNumVertices );
if ( inMesh->HasFaces() )
{
newMesh->reserve( inMesh->mNumFaces );
}
// normals
if ( inMesh->HasNormals() )
{
bool allocated = newPC->reserveTheNormsTable();
if ( !allocated )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Cannot allocate normals for mesh '%1'" ).arg( name ) );
}
}
// texture coordinates
bool hasTextureCoordinates = inMesh->HasTextureCoords( 0 );
TextureCoordsContainer *texCoords = nullptr;
if ( hasTextureCoordinates )
{
texCoords = new TextureCoordsContainer;
texCoords->reserve( inMesh->mNumVertices );
bool allocated = texCoords->isAllocated();
allocated &= newMesh->reservePerTriangleTexCoordIndexes();
allocated &= newMesh->reservePerTriangleMtlIndexes();
if ( !allocated )
{
delete texCoords;
hasTextureCoordinates = false;
ccLog::Warning( QStringLiteral( "[MeshIO] Cannot allocate texture coordinates for mesh '%1'" ).arg( name ) );
}
else
{
newMesh->setTexCoordinatesTable( texCoords );
}
}
// vertices
for ( unsigned int i = 0; i < inMesh->mNumVertices; ++i )
{
const aiVector3D &point = inMesh->mVertices[i];
CCVector3 point2( static_cast<PointCoordinateType>( point.x ),
static_cast<PointCoordinateType>( point.y ),
static_cast<PointCoordinateType>( point.z ) );
newPC->addPoint( point2 );
// normals
if ( newPC->hasNormals() )
{
const aiVector3D &normal = inMesh->mNormals[i];
CCVector3 normal2( static_cast<PointCoordinateType>( normal.x ),
static_cast<PointCoordinateType>( normal.y ),
static_cast<PointCoordinateType>( normal.z ) );
newPC->addNorm( normal2 );
}
// texture coordinates
if ( hasTextureCoordinates )
{
const aiVector3D &texCoord = inMesh->mTextureCoords[0][i];
const TexCoords2D coord{ texCoord.x, texCoord.y };
texCoords->addElement( coord );
}
}
newPC->setEnabled( false );
// faces
if ( inMesh->HasFaces() )
{
newMesh->reserve( inMesh->mNumFaces );
for ( unsigned int i = 0; i < inMesh->mNumFaces; ++i )
{
const aiFace &face = inMesh->mFaces[i];
if ( face.mNumIndices != 3 )
{
continue;
}
newMesh->addTriangle( face.mIndices[0],
face.mIndices[1],
face.mIndices[2] );
// texture coordinates
if ( hasTextureCoordinates )
{
newMesh->addTriangleMtlIndex( 0 );
newMesh->addTriangleTexCoordIndexes( static_cast<int>(face.mIndices[0]),
static_cast<int>(face.mIndices[1]),
static_cast<int>(face.mIndices[2]) );
}
}
}
if ( newMesh->size() == 0 )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Mesh '%1' does not have any faces" ).arg( name ) );
delete newPC;
delete newMesh;
return nullptr;
}
newMesh->setName( name );
newMesh->setVisible( true );
if ( !newPC->hasNormals() )
{
ccLog::Warning( QStringLiteral( "[MeshIO] Mesh '%1' does not have normals - will compute them per vertex" ).arg( name ) );
newMesh->computeNormals( true );
}
newMesh->showNormals( true );
newMesh->addChild( newPC );
return newMesh;
}
ccGLMatrix convertMatrix( const aiMatrix4x4 &inAssimpMatrix )
{
const int cWidth = 4;
const int cHeight = 4;
PointCoordinateType data[OPENGL_MATRIX_SIZE];
for ( unsigned int i = 0; i < cWidth; ++i)
{
for ( unsigned int j = 0; j < cHeight; ++j)
{
data[j * cHeight + i] = static_cast<PointCoordinateType>(inAssimpMatrix[i][j]);
}
}
return ccGLMatrix( data );
}
QVariant convertMetaValueToVariant( aiMetadata *inData, unsigned int inValueIndex )
{
QVariant metaValue;
switch ( inData->mValues[inValueIndex].mType )
{
case AI_BOOL:
{
bool value = false;
inData->Get<bool>( inValueIndex, value );
metaValue = value;
break;
}
case AI_INT32:
{
int32_t value = 0;
inData->Get<int32_t>( inValueIndex, value );
metaValue = value;
break;
}
case AI_UINT64:
{
uint64_t value = 0;
inData->Get<uint64_t>( inValueIndex, value );
metaValue = value;
break;
}
case AI_FLOAT:
{
float value = 0;
inData->Get<float>( inValueIndex, value );
metaValue = value;
break;
}
case AI_DOUBLE:
{
double value = 0;
inData->Get<double>( inValueIndex, value );
metaValue = value;
break;
}
case AI_AISTRING:
{
aiString value;
inData->Get<aiString>( inValueIndex, value );
metaValue = value.C_Str();
break;
}
case AI_AIVECTOR3D:
{
aiVector3D value;
inData->Get<aiVector3D>( inValueIndex, value );
metaValue = QVector3D( value.x, value.y, value.z );
break;
}
case AI_META_MAX:
case FORCE_32BIT:
{
// This is necessary to avoid a warning.
// Assimp doesn't use enum type specifiers.
// It uses this odd trick w/FORCE_32BIT to force the type of the enum.
break;
}
}
return metaValue;
}
}