195 struct aiMatrix4x4 m = node->mTransformation;
196 struct aiVector3D scaling;
197 struct aiQuaternion rotation;
198 struct aiVector3D position;
199 aiDecomposeMatrix(&m, &scaling, &rotation, &position);
201 (VuoPoint3d){position.x, position.y, position.z},
202 (VuoPoint4d){rotation.x, rotation.y, rotation.z, rotation.w},
203 (VuoPoint3d){scaling.x, scaling.y, scaling.z}));
208 if (node->mNumMeshes && node->mNumMeshes == 1 && node->mNumChildren == 0)
215 for (
unsigned int meshIndex = 0; meshIndex < node->mNumMeshes; ++meshIndex)
217 const struct aiMesh *meshObj = scene->mMeshes[node->mMeshes[meshIndex]];
219 if (!meshObj->mVertices)
221 VUserLog(
"Error: Mesh '%s' doesn't contain any positions. Skipping.", meshObj->mName.data);
225 if (!meshObj->mNormals || !meshObj->mTextureCoords[0])
226 VUserLog(
"Warning: Mesh '%s' is missing%s%s. These channels will be automatically generated, but lighting and 3D object filters may not work correctly.",
228 meshObj->mNormals ?
"" :
" [normals]",
229 meshObj->mTextureCoords[0] ?
"" :
" [texture coordinates]");
231 float *positions, *normals = NULL, *textureCoordinates = NULL, *colors = NULL;
232 unsigned int *elements;
235 meshObj->mNormals ? &normals : NULL,
236 meshObj->mTextureCoords[0] ? &textureCoordinates : NULL,
237 meshObj->mColors[0] ? &colors : NULL,
238 meshObj->mNumFaces * 3, &elements);
240 for (
unsigned int vertex = 0; vertex < meshObj->mNumVertices; ++vertex)
242 struct aiVector3D position = meshObj->mVertices[vertex];
243 positions[vertex * 3 ] = position.x;
244 positions[vertex * 3 + 1] = position.y;
245 positions[vertex * 3 + 2] = position.z;
247 if (meshObj->mNormals)
249 struct aiVector3D normal = meshObj->mNormals[vertex];
250 normals[vertex * 3 ] = normal.x;
251 normals[vertex * 3 + 1] = normal.y;
252 normals[vertex * 3 + 2] = normal.z;
255 if (meshObj->mTextureCoords[0])
257 struct aiVector3D textureCoordinate = meshObj->mTextureCoords[0][vertex];
258 textureCoordinates[vertex * 2 ] = textureCoordinate.x;
259 textureCoordinates[vertex * 2 + 1] = textureCoordinate.y;
262 if (meshObj->mColors[0])
264 struct aiColor4D color = meshObj->mColors[0][vertex];
265 colors[vertex * 4 ] = color.r;
266 colors[vertex * 4 + 1] = color.g;
267 colors[vertex * 4 + 2] = color.b;
268 colors[vertex * 4 + 3] = color.a;
272 unsigned int numValidElements = 0;
273 for (
unsigned int face = 0; face < meshObj->mNumFaces; ++face)
275 const struct aiFace *faceObj = &meshObj->mFaces[face];
276 if (faceObj->mNumIndices != 3)
278 VUserLog(
"Warning: Face %u isn't a triangle (it has %u indices); skipping.",face,faceObj->mNumIndices);
282 elements[numValidElements++] = faceObj->mIndices[0];
283 elements[numValidElements++] = faceObj->mIndices[1];
284 elements[numValidElements++] = faceObj->mIndices[2];
290 if (!meshObj->mTextureCoords[0])
293 if (node->mNumMeshes == 1 && node->mNumChildren == 0)
297 shadersUsed[meshObj->mMaterialIndex] =
true;
304 shadersUsed[meshObj->mMaterialIndex] =
true;
308 for (
unsigned int child = 0; child < node->mNumChildren; ++child)
329 struct aiPropertyStore *props = aiCreatePropertyStore();
332 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
333 aiSetImportPropertyInteger(props, AI_CONFIG_PP_SLM_TRIANGLE_LIMIT, maxIndices/3);
336 glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &maxVertices);
337 aiSetImportPropertyInteger(props, AI_CONFIG_PP_SLM_VERTEX_LIMIT, maxVertices);
339 static bool limitsLogged =
false;
343 VDebugLog(
"OpenGL driver reports maxIndices=%d, maxVertices=%d", maxIndices, maxVertices);
347 struct aiFileIO fileHandlers;
351 const struct aiScene *ais = aiImportFileExWithProperties(
353 aiProcess_Triangulate
356 | aiProcess_GenSmoothNormals
357 | aiProcess_SplitLargeMeshes
358 | aiProcess_GenUVCoords,
363 aiReleasePropertyStore(props);
366 VUserLog(
"Error: %s", aiGetErrorString());
370 if (ais->mFlags & AI_SCENE_FLAGS_INCOMPLETE)
371 VUserLog(
"Warning: Open Asset Import wasn't able to parse everything in this file.");
381 bool shadersUsed[ais->mNumMaterials];
382 for (
int i=0; i<ais->mNumMaterials; ++i)
384 struct aiMaterial *m = ais->mMaterials[i];
386 struct aiString name;
387 aiGetMaterialString(m, AI_MATKEY_NAME, &name);
398 struct aiColor4D diffuseColorAI = {1,1,1,1};
399 aiGetMaterialColor(m, AI_MATKEY_COLOR_DIFFUSE, &diffuseColorAI);
403 struct aiColor4D specularColorAI = {1,1,1,1};
404 aiGetMaterialColor(m, AI_MATKEY_COLOR_SPECULAR, &specularColorAI);
408 float shininess = 10;
409 aiGetMaterialFloatArray(m, AI_MATKEY_SHININESS, &shininess, NULL);
411 shininess =
MAX(1.0001 - 1./shininess, 0);
414 int diffuseTextures = aiGetMaterialTextureCount(m, aiTextureType_DIFFUSE);
419 struct aiString path;
420 aiGetMaterialTexture(m, aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
422 VuoText urlParts[2] = {sceneURLWithoutFilename, path.data};
432 int normalTextures = aiGetMaterialTextureCount(m, aiTextureType_NORMALS);
436 struct aiString path;
437 aiGetMaterialTexture(m, aiTextureType_NORMALS, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
439 VuoText urlParts[2] = {sceneURLWithoutFilename, path.data};
449 int specularTextures = aiGetMaterialTextureCount(m, aiTextureType_SPECULAR);
451 if (specularTextures)
453 struct aiString path;
454 aiGetMaterialTexture(m, aiTextureType_SPECULAR, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
456 VuoText urlParts[2] = {sceneURLWithoutFilename, path.data};
468 int ambientTextures = aiGetMaterialTextureCount(m, aiTextureType_AMBIENT);
471 struct aiString path;
472 aiGetMaterialTexture(m, aiTextureType_AMBIENT, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
473 VUserLog(
"\tambient: %s",path.data);
476 int emissiveTextures = aiGetMaterialTextureCount(m, aiTextureType_EMISSIVE);
477 if (emissiveTextures)
479 struct aiString path;
480 aiGetMaterialTexture(m, aiTextureType_EMISSIVE, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
481 VUserLog(
"\temissive: %s",path.data);
484 int opacityTextures = aiGetMaterialTextureCount(m, aiTextureType_OPACITY);
487 struct aiString path;
488 aiGetMaterialTexture(m, aiTextureType_OPACITY, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
489 VUserLog(
"\topacity: %s",path.data);
492 int lightmapTextures = aiGetMaterialTextureCount(m, aiTextureType_LIGHTMAP);
493 if (lightmapTextures)
495 struct aiString path;
496 aiGetMaterialTexture(m, aiTextureType_LIGHTMAP, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
497 VUserLog(
"\tlightmap: %s",path.data);
500 int reflectionTextures = aiGetMaterialTextureCount(m, aiTextureType_REFLECTION);
501 if (reflectionTextures)
503 struct aiString path;
504 aiGetMaterialTexture(m, aiTextureType_REFLECTION, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
505 VUserLog(
"\treflection: %s",path.data);
508 int displacementTextures = aiGetMaterialTextureCount(m, aiTextureType_DISPLACEMENT);
509 if (displacementTextures)
511 struct aiString path;
512 aiGetMaterialTexture(m, aiTextureType_DISPLACEMENT, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
513 VUserLog(
"\tdisplacement: %s",path.data);
516 int heightTextures = aiGetMaterialTextureCount(m, aiTextureType_HEIGHT);
519 struct aiString path;
520 aiGetMaterialTexture(m, aiTextureType_HEIGHT, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
524 int unknownTextures = aiGetMaterialTextureCount(m, aiTextureType_UNKNOWN);
527 struct aiString path;
528 aiGetMaterialTexture(m, aiTextureType_UNKNOWN, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL);
529 VUserLog(
"\tunknown: %s",path.data);
533 if (normalImage || specularImage)
549 else if (diffuseImage)
564 shadersUsed[i] =
false;
571 for (
unsigned int i = 0; i < ais->mNumMaterials; ++i)
584 if(hasLeftHandedCoordinates)
595 unsigned int vertexCount, elementCount, *elements;
599 for (
int n = 0; n < vertexCount; n++)
600 positions[n * 3] *= -1;
606 for(
int i = 0; i < elementCount; i+= 3)
608 unsigned int tmp = elements[i];
609 elements[i] = elements[i+2];
649 aiReleaseImport(ais);