Added more helper methods to Mesh.

This commit is contained in:
Relintai 2024-01-13 14:38:30 +01:00
parent e3b62728bb
commit 1782ec9348
2 changed files with 84 additions and 4 deletions

View File

@ -8,17 +8,34 @@ void Mesh::add_vertex2(float x, float y) {
vertices.push_back(x);
vertices.push_back(y);
}
void Mesh::add_vertex2(const Vector2 &v) {
vertices.push_back(v.x);
vertices.push_back(v.y);
}
void Mesh::add_vertex3(float x, float y, float z) {
vertices.push_back(x);
vertices.push_back(y);
vertices.push_back(z);
}
void Mesh::add_vertex3(const Vector3 &v) {
vertices.push_back(v.x);
vertices.push_back(v.y);
vertices.push_back(v.z);
}
void Mesh::add_normal(float x, float y, float z) {
normals.push_back(x);
normals.push_back(y);
normals.push_back(z);
}
void Mesh::add_normal(const Vector3 &n) {
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
}
void Mesh::add_color(float r, float g, float b, float a) {
colors.push_back(r);
colors.push_back(g);
@ -36,6 +53,10 @@ void Mesh::add_uv(float u, float v) {
uvs.push_back(u);
uvs.push_back(v);
}
void Mesh::add_uv(const Vector2 &uv) {
uvs.push_back(uv.x);
uvs.push_back(uv.y);
}
void Mesh::add_index(uint32_t index) {
indices.push_back(index);
@ -66,6 +87,49 @@ void Mesh::flip_faces() {
}
}
void Mesh::fill_colors(const Color &p_color) {
while (colors.size() % 4 != 0) {
colors.push_back(0);
}
int needed_color_count = get_vertex_count() * 4;
if (colors.size() > needed_color_count) {
colors.resize(needed_color_count);
return;
}
while (colors.size() < needed_color_count) {
colors.push_back(p_color.r);
colors.push_back(p_color.g);
colors.push_back(p_color.b);
colors.push_back(p_color.a);
}
}
void Mesh::fill_colors_random() {
while (colors.size() % 4 != 0) {
colors.push_back(0);
}
int needed_color_count = get_vertex_count() * 4;
if (colors.size() > needed_color_count) {
colors.resize(needed_color_count);
return;
}
RandomPCG r;
r.randomize();
while (colors.size() < needed_color_count) {
colors.push_back(r.randf());
colors.push_back(r.randf());
colors.push_back(r.randf());
colors.push_back(1);
}
}
void Mesh::update_aabb() {
aabb = AABB();
@ -233,6 +297,10 @@ void Mesh::render() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
int Mesh::get_vertex_count() const {
return vertices.size() / vertex_dimesions;
}
Mesh::Mesh() {
VBO = 0;
IBO = 0;

View File

@ -2,10 +2,10 @@
#define MESH_H
//--STRIP
#include "core/vector.h"
#include "core/int_types.h"
#include "core/color.h"
#include "core/aabb.h"
#include "core/color.h"
#include "core/int_types.h"
#include "core/vector.h"
#include "object/resource.h"
@ -16,19 +16,29 @@ class Mesh : public Resource {
SFW_OBJECT(Mesh, Resource);
public:
//TODO remove the float based api
void add_vertex2(float x, float y);
void add_vertex2(const Vector2 &v);
void add_vertex3(float x, float y, float z);
void add_vertex3(const Vector3 &v);
void add_normal(float x, float y, float z);
void add_normal(const Vector3 &n);
void add_color(float r = 1, float g = 1, float b = 1, float a = 1);
void add_color(const Color &p_color);
void add_color(const Color &p_color);
void add_uv(float u, float v);
void add_uv(const Vector2 &uv);
void add_index(uint32_t index);
void add_triangle(uint32_t i1, uint32_t i2, uint32_t i3);
void flip_faces();
void fill_colors(const Color &p_color);
void fill_colors_random();
void update_aabb();
@ -38,6 +48,8 @@ public:
void destroy();
void render();
int get_vertex_count() const;
Mesh();
Mesh(int vert_dim);
virtual ~Mesh();