From 083d5ac92517c31096e79f9500fad0b5330fa6fe Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 12 Jan 2024 23:45:26 +0100 Subject: [PATCH] Added helper methods to Mesh. --- sfw/render_core/mesh.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ sfw/render_core/mesh.h | 7 +++++ 2 files changed, 75 insertions(+) diff --git a/sfw/render_core/mesh.cpp b/sfw/render_core/mesh.cpp index d57bd04..0f25ac2 100644 --- a/sfw/render_core/mesh.cpp +++ b/sfw/render_core/mesh.cpp @@ -46,6 +46,74 @@ void Mesh::add_triangle(uint32_t i1, uint32_t i2, uint32_t i3) { indices.push_back(i3); } +void Mesh::flip_faces() { + if (normals.size() > 0 && indices.size() > 0) { + { + int nc = normals.size(); + float *w = normals.ptrw(); + for (int i = 0; i < nc; i++) { + w[i] = -w[i]; + } + } + + { + int ic = indices.size(); + uint32_t *w = indices.ptrw(); + for (int i = 0; i < ic; i += 3) { + SWAP(w[i + 0], w[i + 1]); + } + } + } +} + +void Mesh::update_aabb() { + aabb = AABB(); + + if (vertex_dimesions == 2) { + int size = vertices.size(); + + if (size < 2) { + return; + } + + const float *v = vertices.ptr(); + + aabb.position.x = v[0]; + aabb.position.y = v[1]; + + Vector3 vert; + + for (int i = 0; i < size; i += 2) { + vert.x = v[i]; + vert.y = v[i + 1]; + + aabb.expand_to(vert); + } + } else if (vertex_dimesions == 3) { + int size = vertices.size(); + + if (size < 3) { + return; + } + + const float *v = vertices.ptr(); + + aabb.position.x = v[0]; + aabb.position.y = v[1]; + aabb.position.z = v[2]; + + Vector3 vert; + + for (int i = 0; i < size; i += 2) { + vert.x = v[i]; + vert.y = v[i + 1]; + vert.z = v[i + 2]; + + aabb.expand_to(vert); + } + } +} + void Mesh::clear() { vertices.clear(); normals.clear(); diff --git a/sfw/render_core/mesh.h b/sfw/render_core/mesh.h index 5c20f43..335c91c 100644 --- a/sfw/render_core/mesh.h +++ b/sfw/render_core/mesh.h @@ -5,6 +5,7 @@ #include "core/vector.h" #include "core/int_types.h" #include "core/color.h" +#include "core/aabb.h" #include "object/resource.h" @@ -27,6 +28,10 @@ public: void add_index(uint32_t index); void add_triangle(uint32_t i1, uint32_t i2, uint32_t i3); + void flip_faces(); + + void update_aabb(); + void clear(); void upload(); @@ -48,6 +53,8 @@ public: Vector uvs; Vector indices; + AABB aabb; + protected: uint32_t vertices_vbo_size; uint32_t normals_vbo_size;