From 7e932fd83d6f93ff154a93061dbacdb4283085cd Mon Sep 17 00:00:00 2001 From: outfrost Date: Fri, 12 Jun 2020 00:48:35 +0200 Subject: [PATCH] Some debug prints for assimp structures --- src/engine/asset.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ src/engine/render.c | 5 ++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/engine/asset.c b/src/engine/asset.c index 8bb19e5..a26a7e4 100644 --- a/src/engine/asset.c +++ b/src/engine/asset.c @@ -2,18 +2,88 @@ #include #include +//#include #include #include #include "logger.h" #include "tga.h" +#define IMPORT_DEBUG_ 1 + static const struct aiScene* importScene(const char* path); static Vector3D convertAiVector3D(struct aiVector3D vect); static const char* replaceFileExtension(const struct aiString path, const char* ext); +#if IMPORT_DEBUG_ +static void printMetadata(const struct aiMetadata* meta) { + if (meta) { + for (size_t i = 0; i < meta->mNumProperties; ++i) { + char* key = memcpy(malloc((meta->mKeys[i].length + 1) * sizeof(char)), + meta->mKeys[i].data, + meta->mKeys[i].length * sizeof(char)); + key[meta->mKeys[i].length] = '\0'; + const struct aiMetadataEntry value = meta->mValues[i]; + switch (value.mType) { + case AI_BOOL: + logDebug("\"%s\": (bool) %d", key, *((int*) value.mData)); + break; + case AI_INT32: + logDebug("\"%s\": (int32) %d", key, *((int32_t*) value.mData)); + break; + case AI_UINT64: + logDebug("\"%s\": (uint64) %llu", key, *((uint64_t*) value.mData)); + break; + case AI_FLOAT: + logDebug("\"%s\": (float) %f", key, *((float*) value.mData)); + break; + case AI_DOUBLE: + logDebug("\"%s\": (double) %f", key, *((double*) value.mData)); + break; + case AI_AISTRING: { + struct aiString aistr = *((struct aiString*) value.mData); + char* str = memcpy(malloc((aistr.length + 1) * sizeof(char)), + aistr.data, + aistr.length * sizeof(char)); + str[aistr.length] = '\0'; + logDebug("\"%s\": (string) %s", key, str); + free(str); + break; } + case AI_AIVECTOR3D: { + struct aiVector3D vec = *((struct aiVector3D*) value.mData); + logDebug("\"%s\": (vector3d) { %f, %f, %f }", key, vec.x, vec.y, vec.z); + break; } + case AI_META_MAX: + default: + logDebug("\"%s\": (unrecognized type)", key); + break; + } + free(key); + } + } +} + +void printAiNodeMetadata(const struct aiNode* node) { + if (!node) { + return; + } + + struct aiString aistr = node->mName; + char* name = memcpy(malloc((aistr.length + 1) * sizeof(char)), + aistr.data, + aistr.length * sizeof(char)); + name[aistr.length] = '\0'; + logDebug("Metadata from node \"%s\": %p", name, node->mMetaData); + printMetadata(node->mMetaData); + + for (size_t i = 0; i < node->mNumChildren; ++i) { + printAiNodeMetadata(node->mChildren[i]); + } +} +#endif // IMPORT_DEBUG_ + const Solid* importSolid(const char* path) { const struct aiScene* scene = importScene(path); if (scene == NULL) { @@ -21,6 +91,13 @@ const Solid* importSolid(const char* path) { return NULL; } +#if IMPORT_DEBUG_ + const struct aiMetadata* meta = scene->mMetaData; + logDebug("Metadata from %s: %p", path, meta); + printMetadata(meta); + printAiNodeMetadata(scene->mRootNode); +#endif // IMPORT_DEBUG_ + const unsigned int numMeshes = scene->mNumMeshes; const unsigned int numMaterials = scene->mNumMaterials; @@ -86,6 +163,24 @@ const Solid* importSolid(const char* path) { for (unsigned int matIndex = 0; matIndex < numMaterials; ++matIndex) { Material material = { .textureId = textureIds[matIndex] }; + +#if IMPORT_DEBUG_ + const struct aiMaterialProperty* prop; + aiGetMaterialProperty(scene->mMaterials[matIndex], + AI_MATKEY_SHADING_MODEL, + &prop); + // print mKey + struct aiString aistr = prop->mKey; + char* key = memcpy(malloc((aistr.length + 1) * sizeof(char)), + aistr.data, + aistr.length * sizeof(char)); + key[aistr.length] = '\0'; + + logDebug("Material property \"%s\": Shading model: (length %u) %d", + key, + prop->mDataLength, + *((int32_t*) prop->mData)); +#endif // IMPORT_DEBUG_ glBindTexture(GL_TEXTURE_2D, material.textureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/src/engine/render.c b/src/engine/render.c index b09f25b..c75dc71 100644 --- a/src/engine/render.c +++ b/src/engine/render.c @@ -7,7 +7,7 @@ #include "performance.h" #define SCENE_DEBUG_ 0 -#define RENDER_DEBUG_ 1 +#define RENDER_DEBUG_ 0 float viewportAspectRatio = 1.0f; const Scene* cameraAnchor; @@ -185,12 +185,13 @@ static void drawSolid(const Solid* solid) { for (size_t i = 0; i < face.numIndices; ++i) { size_t vertIndex = face.indices[i]; + size_t normalIndex = face.indices[2]; if (hasNormals) { if (hasTextureCoords) { Vector3D coords = mesh.textureCoords[vertIndex]; glTexCoord2f(coords.x, coords.y); } - Vector3D normal = mesh.normals[vertIndex]; + Vector3D normal = mesh.normals[normalIndex]; glNormal3f(normal.x, normal.y, normal.z); } Vector3D vertex = mesh.vertices[vertIndex]; -- 2.44.0