Brunot
Loading...
Searching...
No Matches
Mesh.h
Go to the documentation of this file.
1
12// ____ __ __ __
13// /\__ _\/\ \ /\ \/\ \
14// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
15// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
16// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
17// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
18// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
19
20
21#pragma once
22
23#include <memory>
24
25#include "VAO.h"
26#include "IndexBuffer.h"
27#include "VertexBuffer.h"
28#include "Graphics/Vertex.h"
29#include "System/json.h"
31
32struct AffineMatrix;
33
34namespace gfx
35{
36class Texture;
37class Shader;
38}
39using Stream = sys::Json; // replace with whatever stream we are using
40
42enum class uniforms : GLint
43{
44 world = 0,
46 view = 2,
49};
50
51namespace gfx
52{
53
54
55class Mesh
56{
57public:
63 Mesh(unsigned tilesWide = 1, unsigned tilesHigh = 1);
65 Mesh(const Mesh& other) = default;
66 Mesh(Mesh&& other) noexcept = default;
67 auto operator=(const Mesh& other) -> Mesh& = default;
68 auto operator=(Mesh&& other) noexcept -> Mesh& = default;
69
70 auto activate() const -> void;
71 auto deactivate() const -> void;
72
73 auto draw(const Shader& shader, const Texture& texture) const -> void;
74 auto draw(const Shader& shader) const -> void;
75
76 auto load(const Stream& stream) -> void;
77 auto save(Stream& stream) const -> void;
78
79 [[nodiscard]] auto name() const -> const std::string&;
80
81 auto setName(const std::string& name) -> void;
82
84 auto setFrame(unsigned newFrameNum) -> void;
85
87 auto getFrame() const -> unsigned;
88
89 auto incrementFrame() -> void;
90 auto decrementFrame() -> void;
91
93 auto getMaxFrames() const -> unsigned;
94
95
96 friend auto to_json(json& j, const Mesh& a) -> void;
97 friend auto from_json(const json& j, Mesh& a) -> void;
98
99 auto showMenu() -> void;
100
106 auto setObjectTransform(const AffineMatrix& transform) const -> void;
107 auto setObjectProjection(const AffineMatrix& projection) const -> void;
108 auto setObjectCamera(const AffineMatrix& camera) const -> void;
109 auto setObjectColor(const Vector4D& color) const -> void;
110
111private:
113 std::string _name;
115 std::shared_ptr<VAO> vao;
116 std::shared_ptr<IndexBuffer> triangleBuffer;
117 std::shared_ptr<VertexBuffer> vertexBuffer;
118
120 unsigned spriteTilesCountU = 1;
122 unsigned spriteTilesCountV = 1;
124 std::vector<Vertex> vertices;
125
127 unsigned frame = 0;
129 unsigned maxFrames;
130
131
133 auto uploadMeshToGPU() -> void;
134};
135
136}
sys::Json Stream
Definition AudioObject.h:20
nlohmann::json json
Definition Json.cpp:19
uniforms
Definition Mesh.h:43
@ uvOffset
Definition Mesh.h:48
@ multColor
Definition Mesh.h:47
@ view
Definition Mesh.h:46
@ world
Definition Mesh.h:44
@ projection
Definition Mesh.h:45
int GLint
Definition VAO.h:23
Buffer that holds data for the vertices for OpenGL.
A struct for stroing vertexes and handing them to opengl.
Definition IndexBuffer.h:23
friend auto to_json(json &j, const Mesh &a) -> void
Definition Mesh.cpp:221
auto uploadMeshToGPU() -> void
Upload mesh data to gpu, when creating mesh, or loading from a file.
Definition Mesh.cpp:41
auto getMaxFrames() const -> unsigned
get the max number of frames of the mesh, based on the tiles wide and high of the mesh
Definition Mesh.cpp:181
std::vector< Vertex > vertices
local copy of the Vertexes uploaded to the gpu
Definition Mesh.h:124
auto operator=(const Mesh &other) -> Mesh &=default
auto incrementFrame() -> void
Definition Mesh.cpp:171
auto getFrame() const -> unsigned
get the frame of the mesh. frames go right to left, then top top bottom
Definition Mesh.cpp:166
unsigned spriteTilesCountU
number of sprite tiles wide the image would be for this mesh
Definition Mesh.h:120
std::shared_ptr< IndexBuffer > triangleBuffer
Definition Mesh.h:116
auto setObjectColor(const Vector4D &color) const -> void
Definition Mesh.cpp:212
unsigned maxFrames
max number of frames that makes sense for the mesh, assuming a tilesheet of spriteTilesCountV by spri...
Definition Mesh.h:129
auto setObjectTransform(const AffineMatrix &transform) const -> void
Sets the uniform for the object.
Definition Mesh.cpp:194
Mesh(unsigned tilesWide=1, unsigned tilesHigh=1)
constructs a mesh.
Definition Mesh.cpp:75
unsigned spriteTilesCountV
number of sprite tiles high the image would be for this mesh
Definition Mesh.h:122
auto name() const -> const std::string &
Definition Mesh.cpp:151
auto setFrame(unsigned newFrameNum) -> void
Set the frame of the mesh, automatically calculates correct uv offset. Frames go right to left,...
Definition Mesh.cpp:161
auto decrementFrame() -> void
Definition Mesh.cpp:176
auto save(Stream &stream) const -> void
Definition Mesh.cpp:146
auto operator=(Mesh &&other) noexcept -> Mesh &=default
auto setObjectProjection(const AffineMatrix &projection) const -> void
Definition Mesh.cpp:201
auto activate() const -> void
Definition Mesh.cpp:93
auto deactivate() const -> void
Definition Mesh.cpp:98
std::string _name
the name of the mesh, used for serialization
Definition Mesh.h:113
friend auto from_json(const json &j, Mesh &a) -> void
Definition Mesh.cpp:231
std::shared_ptr< VertexBuffer > vertexBuffer
Definition Mesh.h:117
std::shared_ptr< VAO > vao
vertex attribute object, the "mesh" in opengl
Definition Mesh.h:115
auto setObjectCamera(const AffineMatrix &camera) const -> void
Definition Mesh.cpp:206
Mesh(Mesh &&other) noexcept=default
unsigned frame
frame of animation for calculating uv offsets, clamped to be valid for the sprite sheet size
Definition Mesh.h:127
auto load(const Stream &stream) -> void
Definition Mesh.cpp:141
auto setName(const std::string &name) -> void
Definition Mesh.cpp:156
auto draw(const Shader &shader, const Texture &texture) const -> void
Definition Mesh.cpp:103
auto showMenu() -> void
Definition Mesh.cpp:187
Mesh(const Mesh &other)=default
Definition Shader.h:25
holds and manages textures for use with meshes in sprites
Definition Texture.h:33
Definition VAO.h:40
Buffer that holds data for the vertices for OpenGL.
Definition VertexBuffer.h:32
Definition Json.h:32
Definition Sprite.h:25
Definition AffineMatrix.h:30
Definition Vector4D.h:23
Definition Vertex.h:27