Removed SetupAssimpScene from header, using indices array per mesh

This commit is contained in:
Vladyslav Baranovskyi
2025-02-07 16:50:08 +02:00
parent dc460cb106
commit 480b667e5c
2 changed files with 92 additions and 81 deletions

View File

@@ -24,6 +24,82 @@
#include <assimp/material.h>
#endif
namespace
{
#if __has_include("assimp/Exporter.hpp")
void SetupAssimpScene(OpenVulkano::Scene::Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, unsigned int*& indices,
bool withTexCoords, float scaling)
{
mesh.mVertices = nullptr;
mesh.mNormals = nullptr;
mesh.mTangents = nullptr;
mesh.mBitangents = nullptr;
mesh.mFaces = nullptr;
for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
mesh.mTextureCoords[i] = nullptr;
mesh.mNumUVComponents[i] = 0;
}
for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
mesh.mColors[i] = nullptr;
}
rootNode.mName = aiString("RootNode");
rootNode.mChildren = nullptr;
rootNode.mNumChildren = 0;
scene.mRootNode = &rootNode;
mesh.mName = aiString("Mesh");
mesh.mNumVertices = geometry->vertexCount;
mesh.mMaterialIndex = 0;
mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
mesh.mVertices = new aiVector3D[geometry->vertexCount];
mesh.mNormals = new aiVector3D[geometry->vertexCount];
if (withTexCoords)
{
mesh.mNumUVComponents[0] = 2;
mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount];
}
for (int i = 0; i < geometry->vertexCount; ++i)
{
const OpenVulkano::Vertex& vertex = geometry->vertices[i];
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);
if (withTexCoords)
{
mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f);
}
}
mesh.mNumFaces = geometry->indexCount / 3;
mesh.mFaces = new aiFace[mesh.mNumFaces];
indices = new unsigned int[geometry->indexCount];
for (unsigned int i = 0; i < geometry->indexCount; ++i)
{
indices[i] = geometry->GetIndex(i);
}
for (unsigned int i = 0; i < mesh.mNumFaces; ++i)
{
mesh.mFaces[i].mNumIndices = 3;
mesh.mFaces[i].mIndices = &indices[i * 3];
}
scene.mNumMeshes = 1;
scene.mMeshes = new aiMesh*[1] { &mesh };
scene.mNumMaterials = 1;
scene.mMaterials = new aiMaterial*[1] { new aiMaterial() };
rootNode.mNumMeshes = 1;
rootNode.mMeshes = new unsigned int[1] { 0 };
}
#endif
}
namespace OpenVulkano::Scene
{
void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath)
@@ -90,83 +166,15 @@ namespace OpenVulkano::Scene
}
}
#if __has_include("assimp/Exporter.hpp")
void MeshWriter::SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh,
bool withTexCoords, float scaling)
{
mesh.mVertices = nullptr;
mesh.mNormals = nullptr;
mesh.mTangents = nullptr;
mesh.mBitangents = nullptr;
mesh.mFaces = nullptr;
for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
mesh.mTextureCoords[i] = nullptr;
mesh.mNumUVComponents[i] = 0;
}
for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
mesh.mColors[i] = nullptr;
}
rootNode.mName = aiString("RootNode");
rootNode.mChildren = nullptr;
rootNode.mNumChildren = 0;
scene.mRootNode = &rootNode;
mesh.mName = aiString("Mesh");
mesh.mNumVertices = geometry->vertexCount;
mesh.mMaterialIndex = 0;
mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
mesh.mVertices = new aiVector3D[geometry->vertexCount];
mesh.mNormals = new aiVector3D[geometry->vertexCount];
if (withTexCoords)
{
mesh.mNumUVComponents[0] = 2;
mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount];
}
for (int 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) * scaling;
mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z);
if (withTexCoords)
{
mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f);
}
}
mesh.mNumFaces = geometry->indexCount / 3;
mesh.mFaces = new aiFace[mesh.mNumFaces];
for (unsigned int i = 0; i < mesh.mNumFaces; ++i)
{
mesh.mFaces[i].mNumIndices = 3;
mesh.mFaces[i].mIndices = new unsigned int[3];
mesh.mFaces[i].mIndices[0] = geometry->GetIndex(i * 3 + 0);
mesh.mFaces[i].mIndices[1] = geometry->GetIndex(i * 3 + 1);
mesh.mFaces[i].mIndices[2] = geometry->GetIndex(i * 3 + 2);
}
scene.mNumMeshes = 1;
scene.mMeshes = new aiMesh*[1] { &mesh };
scene.mNumMaterials = 1;
scene.mMaterials = new aiMaterial*[1] { new aiMaterial() };
rootNode.mNumMeshes = 1;
rootNode.mMeshes = new unsigned int[1] { 0 };
}
void MeshWriter::WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary)
{
#if __has_include("assimp/Exporter.hpp")
std::unique_ptr<aiScene> scene(new aiScene());
std::unique_ptr<aiNode> rootNode(new aiNode());
std::unique_ptr<aiMesh> mesh(new aiMesh());
unsigned int* indices = nullptr;
SetupAssimpScene(geometry, *scene, *rootNode, *mesh, false, 1.0f);
SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, false, 1.0f);
Assimp::Exporter exporter;
const char* formatId = binary ? "stlb" : "stl";
@@ -174,6 +182,12 @@ namespace OpenVulkano::Scene
scene->mRootNode = nullptr;
scene->mMeshes = nullptr;
for (uint32_t i = 0; i < mesh->mNumFaces; ++i)
{
aiFace& face = mesh->mFaces[i];
face.mIndices = nullptr;
}
delete[] indices;
if (result != aiReturn_SUCCESS)
{
@@ -190,8 +204,9 @@ namespace OpenVulkano::Scene
std::unique_ptr<aiScene> scene(new aiScene());
std::unique_ptr<aiNode> rootNode(new aiNode());
std::unique_ptr<aiMesh> mesh(new aiMesh());
unsigned int* indices = nullptr;
SetupAssimpScene(geometry, *scene, *rootNode, *mesh, true, 100.0f);
SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, true, 100.0f);
if (!texturePath.empty())
{
@@ -204,6 +219,12 @@ namespace OpenVulkano::Scene
scene->mRootNode = nullptr;
scene->mMeshes = nullptr;
for (uint32_t i = 0; i < mesh->mNumFaces; ++i)
{
aiFace& face = mesh->mFaces[i];
face.mIndices = nullptr;
}
delete[] indices;
if (result != aiReturn_SUCCESS)
{
@@ -213,5 +234,4 @@ namespace OpenVulkano::Scene
throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!");
#endif
}
#endif
}

View File

@@ -8,10 +8,6 @@
#include <string>
#if __has_include("assimp/Exporter.hpp")
#include <assimp/scene.h>
#endif
namespace OpenVulkano::Scene
{
class Geometry;
@@ -24,10 +20,5 @@ namespace OpenVulkano::Scene
static void WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath);
static void WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath);
static void WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary);
private:
#if __has_include("assimp/Exporter.hpp")
static void SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh,
bool withTexCoords, float scaling);
#endif
};
}