Skip to content

Commit

Permalink
Load models into the game level
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 11, 2024
1 parent eb3c554 commit fc44926
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/SWBF2/Level.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

#include <godot_cpp/classes/array_mesh.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/variant/color.hpp>

#include "Native/Chunks/ChunkProcessor.hpp"
#include "Native/Level.hpp"

#include "Level.hpp"

Expand All @@ -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)
Expand Down

0 comments on commit fc44926

Please sign in to comment.