Added helper methods to Mesh.

This commit is contained in:
Relintai 2024-01-12 23:45:26 +01:00
parent 534f9a3d92
commit 083d5ac925
2 changed files with 75 additions and 0 deletions

View File

@ -46,6 +46,74 @@ void Mesh::add_triangle(uint32_t i1, uint32_t i2, uint32_t i3) {
indices.push_back(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() { void Mesh::clear() {
vertices.clear(); vertices.clear();
normals.clear(); normals.clear();

View File

@ -5,6 +5,7 @@
#include "core/vector.h" #include "core/vector.h"
#include "core/int_types.h" #include "core/int_types.h"
#include "core/color.h" #include "core/color.h"
#include "core/aabb.h"
#include "object/resource.h" #include "object/resource.h"
@ -27,6 +28,10 @@ public:
void add_index(uint32_t index); void add_index(uint32_t index);
void add_triangle(uint32_t i1, uint32_t i2, uint32_t i3); void add_triangle(uint32_t i1, uint32_t i2, uint32_t i3);
void flip_faces();
void update_aabb();
void clear(); void clear();
void upload(); void upload();
@ -48,6 +53,8 @@ public:
Vector<float> uvs; Vector<float> uvs;
Vector<uint32_t> indices; Vector<uint32_t> indices;
AABB aabb;
protected: protected:
uint32_t vertices_vbo_size; uint32_t vertices_vbo_size;
uint32_t normals_vbo_size; uint32_t normals_vbo_size;