From 972df913c0bd7775fefc3d960f2832ce5ea718fb Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Wed, 18 Dec 2024 12:48:11 +0200 Subject: [PATCH] Fixed texture appearance, added scaling, removed unused code --- openVulkanoCpp/Scene/MeshWriter.cpp | 60 +++-------------------------- 1 file changed, 6 insertions(+), 54 deletions(-) diff --git a/openVulkanoCpp/Scene/MeshWriter.cpp b/openVulkanoCpp/Scene/MeshWriter.cpp index 16e4f85..64ad450 100644 --- a/openVulkanoCpp/Scene/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/MeshWriter.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include namespace OpenVulkano::Scene { @@ -101,11 +101,13 @@ namespace OpenVulkano::Scene mesh->mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; mesh->mMaterialIndex = 0; mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE; + mesh->mNumUVComponents[0] = 2; + float scaling = 100; // fbx units are centimeters... for (uint32_t i = 0; i < geometry->vertexCount; ++i) { const Vertex& vertex = geometry->vertices[i]; - mesh->mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z); + mesh->mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z) * scaling; mesh->mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); mesh->mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f); } @@ -136,56 +138,8 @@ namespace OpenVulkano::Scene scene.mMaterials[0] = material; scene.mNumMaterials = 1; - // aiColor3D diffuseColor(1.0f, 1.0f, 1.0f); - // material->AddProperty(&diffuseColor, 1, AI_MATKEY_COLOR_DIFFUSE); - - Array textureData = Utils::ReadFile(texturePath); - - aiTexture* texture = new aiTexture(); - texture->mWidth = static_cast(textureData.Size()); - texture->mHeight = 0; // indicates that this is compressed texture and the pcData has size mWidth - texture->pcData = reinterpret_cast(new uint8_t[textureData.Size()]); - std::memcpy(texture->pcData, textureData.Data(), textureData.Size()); - - scene.mTextures = new aiTexture*[1]; - scene.mTextures[0] = texture; - scene.mNumTextures = 1; - - std::string ext = texturePath.substr(texturePath.find_last_of('.') + 1); - if (ext == "png") - { - std::memcpy(texture->achFormatHint, "png", 3); - } - else if (ext == "jpg" || ext == "jpeg") - { - std::memcpy(texture->achFormatHint, "jpg", 3); - } - texture->achFormatHint[3] = '\0'; - - aiString texName = aiString("*0"); - material->AddProperty(&texName, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); - - // Validation - for (uint32_t i = 0; i < geometry->indexCount; ++i) { - if (geometry->GetIndex(i) >= geometry->vertexCount) { - std::cerr << "Invalid index at position " << i << ": " - << geometry->GetIndex(i) << " (vertex count: " - << geometry->vertexCount << ")" << std::endl; - assert(false); - } - } - - if (geometry->indexCount % 3 != 0) { - std::cerr << "Invalid index count: " << geometry->indexCount - << " (not divisible by 3)" << std::endl; - assert(false); - } - - // - //aiString externalPath(texturePath); - //scene.mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); - - // Export + aiString externalPath(texturePath); + scene.mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); Assimp::Exporter exporter; aiReturn result = exporter.Export(&scene, "fbx", fbxPath); @@ -218,8 +172,6 @@ namespace OpenVulkano::Scene SAFE_DELETE_ARRAY(scene.mMeshes); SAFE_DELETE_ARRAY(scene.mRootNode->mMeshes); SAFE_DELETE_ARRAY(scene.mMaterials); - SAFE_DELETE_ARRAY(scene.mTextures); - SAFE_DELETE_ARRAY(texture->pcData); delete scene.mRootNode; scene.mRootNode = nullptr; #undef SAFE_DELETE_ARRAY