From fc449268bb052b6840ba5d2d320d7d8db4b170aa Mon Sep 17 00:00:00 2001 From: Lyuu17 Date: Tue, 11 Jun 2024 13:07:30 +0200 Subject: [PATCH] Load models into the game level --- src/SWBF2/Level.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/SWBF2/Level.cpp b/src/SWBF2/Level.cpp index 35e476b..44f7fa7 100644 --- a/src/SWBF2/Level.cpp +++ b/src/SWBF2/Level.cpp @@ -1,5 +1,10 @@ +#include +#include +#include + #include "Native/Chunks/ChunkProcessor.hpp" +#include "Native/Level.hpp" #include "Level.hpp" @@ -8,6 +13,69 @@ namespace SWBF2 void Level::_ready() { SWBF2::Native::UcfbChunk::ReadUcfbFile("data/_lvl_pc/cor/cor1.lvl"); + + for (auto const &[id, model] : Native::Level::m_models) + { + godot::MeshInstance3D *meshInstance = memnew(godot::MeshInstance3D); + meshInstance->set_name("_lvl_terrain"); + + add_child(meshInstance); + + godot::PackedVector3Array vertices; + godot::PackedVector3Array normals; + //godot::PackedVector3Array tangents; + godot::PackedColorArray colors; + godot::PackedVector2Array uvs; + godot::PackedInt32Array indices; + for (auto const &segment : model.m_segments) + { + for (uint32_t i = 0; i < segment.m_verticesBuf.m_positions.size(); i++) + { + vertices.push_back({ segment.m_verticesBuf.m_positions[i].x, segment.m_verticesBuf.m_positions[i].y, segment.m_verticesBuf.m_positions[i].z }); + } + + for (uint32_t i = 0; i < segment.m_verticesBuf.m_normals.size(); i++) + { + normals.push_back({ segment.m_verticesBuf.m_normals[i].x, segment.m_verticesBuf.m_normals[i].y, segment.m_verticesBuf.m_normals[i].z }); + } + + /*for (uint32_t i = 0; i < segment.m_verticesBuf.m_tangents.size(); i++) + { + tangents.push_back({ segment.m_verticesBuf.m_tangents[i].x, segment.m_verticesBuf.m_tangents[i].y, segment.m_verticesBuf.m_tangents[i].z }); + }*/ + + for (uint32_t i = 0; i < segment.m_verticesBuf.m_colors.size(); i++) + { + colors.push_back({ (float)segment.m_verticesBuf.m_colors[i].color.r, (float)segment.m_verticesBuf.m_colors[i].color.g, (float)segment.m_verticesBuf.m_colors[i].color.b, (float)segment.m_verticesBuf.m_colors[i].color.a }); + } + + for (uint32_t i = 0; i < segment.m_verticesBuf.m_texCoords.size(); i++) + { + uvs.push_back({ segment.m_verticesBuf.m_texCoords[i].x, segment.m_verticesBuf.m_texCoords[i].y }); + } + + for (uint32_t i = 0; i < segment.m_indicesBuf.m_indices.size(); i++) + { + indices.push_back({ segment.m_indicesBuf.m_indices[i] }); + } + } + + + + godot::Array arrays; + arrays.resize(godot::ArrayMesh::ARRAY_MAX); + arrays[godot::ArrayMesh::ARRAY_VERTEX] = vertices; + arrays[godot::ArrayMesh::ARRAY_NORMAL] = normals; + //arrays[godot::ArrayMesh::ARRAY_TANGENT] = tangents; + arrays[godot::ArrayMesh::ARRAY_COLOR] = colors; + arrays[godot::ArrayMesh::ARRAY_TEX_UV] = uvs; + arrays[godot::ArrayMesh::ARRAY_INDEX] = indices; + + godot::ArrayMesh *arrMesh = memnew(godot::ArrayMesh); + arrMesh->add_surface_from_arrays(godot::Mesh::PRIMITIVE_TRIANGLE_STRIP, arrays); + + meshInstance->set_mesh(arrMesh); + } } void Level::_process(double delta_time)