Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic geometry importing (Assimp) #26

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions BRenderer/Core/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Scene/Entity.h>
#include <Scene/Components/Mesh3DComponent.h>

#include "Importer/Importer.h"


namespace brr
{
Expand Down Expand Up @@ -48,17 +50,16 @@ namespace brr
glm::ivec2 extent = m_window_manager->GetMainWindow()->GetWindowExtent();

m_scene.reset(new Scene(new PerspectiveCamera (
glm::vec3{ 0.f, 0.f, 5.f },
glm::vec3{ 8.f, 2.f, 0.f },
glm::vec3{ 0.f },
glm::radians(45.f),
extent.x / (float)extent.y,
0.1f, 100.f)));
m_scene->InitSceneRenderer();

m_window_manager->GetMainWindow()->SetScene(m_scene.get());
Entity entity = m_scene->Add3DEntity({});
Mesh3DComponent& mesh = entity.AddComponent<Mesh3DComponent>();
mesh.AddSurface({ vertices_uv, indices });

brr::SceneImporter::LoadFileIntoScene("Monkey.obj", m_scene.get());
}

void App::MainLoop()
Expand Down
158 changes: 86 additions & 72 deletions BRenderer/Importer/Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,92 @@

void ConvertAssimpScene(const aiScene* assimp_scene, brr::Scene& scene, brr::Entity parent = {})
{
//struct NodeInfo
//{
// aiNode* assimp_node;
// brr::Transform3DComponent* parent_transform;
//};
//std::list<NodeInfo> nodes;

//// Insert root node (as a child of the passed parent Entity, if that's the case)
//{
// brr::Transform3DComponent* parent_transf{ nullptr };
// if (parent)
// {
// parent_transf = &parent.GetComponent<brr::Transform3DComponent>();
// }
// nodes.push_back({assimp_scene->mRootNode, parent_transf });
//}

//while (!nodes.empty())
//{
// NodeInfo node_info = nodes.front();
// nodes.pop_front();

// brr::Entity entity = scene.Add3DEntity({});

// brr::Transform3DComponent& node_transform = entity.GetComponent<brr::Transform3DComponent>();

// if (node_info.parent_transform)
// {
// node_transform.SetParent(node_info.parent_transform);
// }

// aiNode* node = node_info.assimp_node;
// {
// if (node->mNumMeshes > 0)
// {
// //brr::Entity mesh_entity = scene.Add3DEntity();
// brr::Mesh3DComponent& mesh_component = entity.AddComponent<brr::Mesh3DComponent>();

// mesh_component.m_surfaces.resize(node->mNumMeshes);
//
// for (size_t i = 0; i < node->mNumMeshes; i++)
// {
// brr::Transform3DComponent& mesh_transform = entity.GetComponent<brr::Transform3DComponent>();
// mesh_transform.SetParent(&node_transform);

// uint32_t mesh_index = node->mMeshes[i];
// aiMesh* mesh = assimp_scene->mMeshes[mesh_index];

//
// mesh_component.m_surfaces[i].m_vertices.resize(mesh->mNumVertices);
// memcpy(mesh_component.m_surfaces[i].m_vertices.data(), &mesh->mVertices, mesh->mNumVertices * sizeof(glm::vec3));

// mesh_component.m_surfaces[i].m_indices.resize(mesh->mNumFaces * 3);

// for (uint32_t i = 0, j = 0; i < mesh->mNumFaces; i++, j += 3)
// {
// aiFace face = mesh->mFaces[i];

// mesh_component.m_surfaces[i].m_indices[ j ] = face.mIndices[0];
// mesh_component.m_surfaces[i].m_indices[j+1] = face.mIndices[1];
// mesh_component.m_surfaces[i].m_indices[j+2] = face.mIndices[2];
// }
// }
// }
// }

// for (size_t i = 0; i < node->mNumChildren; i++)
// {
// nodes.push_back({node->mChildren[i], &node_transform });
// }

//}
struct NodeInfo
{
aiNode* assimp_node;
brr::Transform3DComponent* parent_transform;
};
std::list<NodeInfo> nodes;

// Insert root node (as a child of the passed parent Entity, if that's the case)
{
brr::Transform3DComponent* parent_transf{ nullptr };
if (parent)
{
parent_transf = &parent.GetComponent<brr::Transform3DComponent>();
}
nodes.push_back({assimp_scene->mRootNode, parent_transf });
}

while (!nodes.empty())
{
NodeInfo node_info = nodes.front();
nodes.pop_front();

brr::Entity entity = scene.Add3DEntity({});

brr::Transform3DComponent& node_transform = entity.GetComponent<brr::Transform3DComponent>();

if (node_info.parent_transform)
{
node_transform.SetParent(node_info.parent_transform);
}

aiNode* node = node_info.assimp_node;
{
if (node->mNumMeshes > 0)
{
brr::Mesh3DComponent& mesh_component = entity.AddComponent<brr::Mesh3DComponent>();

for (size_t i = 0; i < node->mNumMeshes; i++)
{
uint32_t mesh_index = node->mMeshes[i];
aiMesh* mesh = assimp_scene->mMeshes[mesh_index];

std::vector<brr::Vertex3> vertices(mesh->mNumVertices);
for (uint32_t vertex_idx = 0; vertex_idx < mesh->mNumVertices; vertex_idx++)
{
const aiVector3D& aiVertex = mesh->mVertices[vertex_idx];
brr::Vertex3& vertex = vertices[vertex_idx];
vertex.pos = {aiVertex.x, aiVertex.y, aiVertex.z};
if (mesh->HasNormals())
{
}

if (mesh->HasTextureCoords(0))
{
const aiVector3D& aiUV = mesh->mTextureCoords[0][vertex_idx];
vertex.uv = {aiUV.x, aiUV.y};
}

if (mesh->HasVertexColors(0))
{
const aiColor4D& aiColor = mesh->mColors[0][vertex_idx];
vertex.color = {aiColor.r, aiColor.g, aiColor.b};
}
}

std::vector<uint32_t> indices (mesh->mNumFaces * 3);
for (uint32_t face_idx = 0, index_idx = 0; face_idx < mesh->mNumFaces; face_idx++, index_idx += 3)
{
aiFace face = mesh->mFaces[face_idx];

indices[index_idx] = face.mIndices[0];
indices[index_idx+1] = face.mIndices[1];
indices[index_idx+2] = face.mIndices[2];
}

mesh_component.AddSurface(vertices, indices);
}
}
}

for (size_t i = 0; i < node->mNumChildren; i++)
{
nodes.push_back({node->mChildren[i], &node_transform });
}

}
}

namespace brr
Expand Down