Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glad/glad.h>
4#include <filesystem>
5#include <glm/glm.hpp>
6#include <string>
7#include <vector>
8
9#include <Math/AABB.hpp>
10
11namespace shkyera {
12
13class Mesh {
14 public:
15 struct Vertex {
16 glm::vec3 position;
17 glm::vec3 normal;
18 glm::vec3 tangent;
19 glm::vec2 texcoord;
20
21 Vertex(const glm::vec3& pos, const glm::vec3& norm, const glm::vec2& tex)
22 : position(pos), normal(norm), texcoord(tex) {
24 }
25 Vertex(const glm::vec3& pos) : Vertex(pos, glm::vec3{0, 1, 0}, glm::vec2{0}) {}
26
28 if (std::abs(normal.x) > std::abs(normal.z)) {
29 tangent = glm::normalize(glm::cross(normal, glm::vec3(0, 0, 1)));
30 } else {
31 tangent = glm::normalize(glm::cross(normal, glm::vec3(1, 0, 0)));
32 }
33 }
34 };
35
36 Mesh(const std::filesystem::path& path);
37 Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices);
38
39 Mesh(const Mesh& other) = delete;
40 Mesh& operator=(const Mesh& other) = delete;
41
42 Mesh(Mesh&& other) noexcept;
43 Mesh& operator=(Mesh&& other) noexcept;
44
45 ~Mesh();
46
47 void bind() const { glBindVertexArray(_vao); }
48 void unbind() const { glBindVertexArray(0); }
49 void draw() const;
50
51 AABB getBoundingBox() const;
52
53 GLuint getVAO() const { return _vao; }
54 GLuint getVBO() const { return _vbo; }
55 GLuint getEBO() const { return _ebo; }
56 GLsizei getMeshSize() const { return _meshSize; }
57
58 class Factory {
59 public:
60 enum class Type { UNDEFINED = 0, PLANE = 1, CUBE = 2, CUBEMAP = 3, CYLINDER = 4, CONE = 5, SPHERE = 6 };
61
62 static Mesh create(Type type);
63 static Mesh createTorus(float innerRadius, float outerRadius, int radialSegments, int tubularSegments);
64
65 private:
66 static Mesh createPlane();
67 static Mesh createCube();
68 static Mesh createCubeMap();
69 static Mesh createCylinder();
70 static Mesh createCone();
71 static Mesh createSphere();
72 };
73
74 private:
75 void loadFromFile(const std::filesystem::path& filepath);
76 void uploadToGPU();
77
78 std::vector<Vertex> _vertices;
79 std::vector<uint32_t> _indices;
80 GLuint _vao, _vbo, _ebo;
81 GLsizei _meshSize;
82};
83
84} // namespace shkyera
Definition Mesh.hpp:58
Type
Definition Mesh.hpp:60
static Mesh createCone()
Definition Mesh.cpp:405
static Mesh createSphere()
Definition Mesh.cpp:488
static Mesh createPlane()
Definition Mesh.cpp:255
static Mesh createCubeMap()
Definition Mesh.cpp:355
static Mesh createCylinder()
Definition Mesh.cpp:373
static Mesh create(Type type)
Definition Mesh.cpp:229
static Mesh createCube()
Definition Mesh.cpp:268
static Mesh createTorus(float innerRadius, float outerRadius, int radialSegments, int tubularSegments)
Definition Mesh.cpp:437
Definition Mesh.hpp:13
GLuint getEBO() const
Definition Mesh.hpp:55
std::vector< uint32_t > _indices
Definition Mesh.hpp:79
Mesh & operator=(const Mesh &other)=delete
Mesh(const Mesh &other)=delete
void loadFromFile(const std::filesystem::path &filepath)
Definition Mesh.cpp:138
AABB getBoundingBox() const
Definition Mesh.cpp:119
void uploadToGPU()
Definition Mesh.cpp:195
GLuint _vbo
Definition Mesh.hpp:80
GLuint getVAO() const
Definition Mesh.hpp:53
GLsizei getMeshSize() const
Definition Mesh.hpp:56
void unbind() const
Definition Mesh.hpp:48
~Mesh()
Definition Mesh.cpp:83
void draw() const
Definition Mesh.cpp:113
void bind() const
Definition Mesh.hpp:47
GLsizei _meshSize
Definition Mesh.hpp:81
std::vector< Vertex > _vertices
Definition Mesh.hpp:78
GLuint _ebo
Definition Mesh.hpp:80
GLuint getVBO() const
Definition Mesh.hpp:54
GLuint _vao
Definition Mesh.hpp:80
Definition Asset.hpp:6
Definition AABB.hpp:8
Definition Mesh.hpp:15
glm::vec3 tangent
Definition Mesh.hpp:18
glm::vec3 normal
Definition Mesh.hpp:17
glm::vec3 position
Definition Mesh.hpp:16
Vertex(const glm::vec3 &pos)
Definition Mesh.hpp:25
void calculateTangent()
Definition Mesh.hpp:27
Vertex(const glm::vec3 &pos, const glm::vec3 &norm, const glm::vec2 &tex)
Definition Mesh.hpp:21
glm::vec2 texcoord
Definition Mesh.hpp:19