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;
33class Texture;
34class Shader;
35using Stream = sys::Json; // replace with whatever stream we are using
36
38enum class uniforms : GLint
39{
40 world = 0,
42 view = 2,
45};
46
47
48class Mesh
49{
50public:
56 Mesh(unsigned tilesWide = 1, unsigned tilesHigh = 1);
58 Mesh(const Mesh& other) = default;
59 Mesh(Mesh&& other) noexcept = default;
60 auto operator=(const Mesh& other) -> Mesh& = default;
61 auto operator=(Mesh&& other) noexcept -> Mesh& = default;
62
63 auto activate() const -> void;
64 auto deactivate() const -> void;
65
66 auto draw(const Shader& shader, const Texture& texture) const -> void;
67 auto draw(const Shader& shader) const -> void;
68
69 auto load(Stream& stream) -> void;
70 auto save(Stream& stream) const -> void;
71
72 [[nodiscard]] auto name() const -> const std::string&;
73
74 auto setName(const std::string& name) -> void;
75
77 auto setFrame(unsigned newFrameNum) -> void;
78
80 auto getFrame() const -> unsigned;
81
82 auto incrementFrame() -> void;
83 auto decrementFrame() -> void;
84
86 auto getMaxFrames() const -> unsigned;
87
88
89 friend auto to_json(json& j, const Mesh& a) -> void;
90 friend auto from_json(const json& j, Mesh& a) -> void;
91
92 auto showMenu() -> void;
93
99 auto setObjectTransform(const AffineMatrix& transform) const -> void;
100 auto setObjectProjection(const AffineMatrix& projection) const -> void;
101 auto setObjectCamera(const AffineMatrix& camera) const -> void;
102 auto setObjectColor(const Vector4D& color) const -> void;
103
104private:
106 std::string _name;
108 std::shared_ptr<VAO> vao;
109 std::shared_ptr<IndexBuffer> triangleBuffer;
110 std::shared_ptr<VertexBuffer> vertexBuffer;
111
113 unsigned spriteTilesCountU = 1;
115 unsigned spriteTilesCountV = 1;
117 std::vector<gfx::Vertex> vertices;
118
120 unsigned frame = 0;
122 unsigned maxFrames;
123
124
126 auto uploadMeshToGPU() -> void;
127};
sys::Json Stream
Definition AudioObject.h:20
nlohmann::json json
Definition Json.cpp:19
uniforms
Definition Mesh.h:39
@ uvOffset
Definition Mesh.h:44
@ multColor
Definition Mesh.h:43
@ view
Definition Mesh.h:42
@ world
Definition Mesh.h:40
@ projection
Definition Mesh.h:41
int GLint
Definition VAO.h:20
Buffer that holds data for the vertices for OpenGL.
A struct for stroing vertexes and handing them to opengl.
Definition IndexBuffer.h:20
friend auto to_json(json &j, const Mesh &a) -> void
Definition Mesh.cpp:218
Mesh(unsigned tilesWide=1, unsigned tilesHigh=1)
constructs a mesh.
Definition Mesh.cpp:72
std::vector< gfx::Vertex > vertices
local copy of the Vertexes uploaded to the gpu
Definition Mesh.h:117
auto operator=(Mesh &&other) noexcept -> Mesh &=default
Mesh(const Mesh &other)=default
auto setObjectCamera(const AffineMatrix &camera) const -> void
Definition Mesh.cpp:203
unsigned maxFrames
max number of frames that makes sense for the mesh, assuming a tilesheet of spriteTilesCountV by spri...
Definition Mesh.h:122
std::shared_ptr< VAO > vao
vertex attribute object, the "mesh" in opengl
Definition Mesh.h:108
auto decrementFrame() -> void
Definition Mesh.cpp:173
auto setObjectColor(const Vector4D &color) const -> void
Definition Mesh.cpp:209
std::string _name
the name of the mesh, used for serialization
Definition Mesh.h:106
std::shared_ptr< IndexBuffer > triangleBuffer
Definition Mesh.h:109
auto operator=(const Mesh &other) -> Mesh &=default
unsigned spriteTilesCountU
number of sprite tiles wide the image would be for this mesh
Definition Mesh.h:113
Mesh(Mesh &&other) noexcept=default
unsigned spriteTilesCountV
number of sprite tiles high the image would be for this mesh
Definition Mesh.h:115
auto deactivate() const -> void
Definition Mesh.cpp:95
auto showMenu() -> void
Definition Mesh.cpp:184
auto name() const -> const std::string &
Definition Mesh.cpp:148
auto incrementFrame() -> void
Definition Mesh.cpp:168
friend auto from_json(const json &j, Mesh &a) -> void
Definition Mesh.cpp:228
auto setName(const std::string &name) -> void
Definition Mesh.cpp:153
auto activate() const -> void
Definition Mesh.cpp:90
auto draw(const Shader &shader, const Texture &texture) const -> void
Definition Mesh.cpp:100
auto getFrame() const -> unsigned
get the frame of the mesh. frames go right to left, then top top bottom
Definition Mesh.cpp:163
unsigned frame
frame of animation for calculating uv offsets, clamped to be valid for the sprite sheet size
Definition Mesh.h:120
auto setFrame(unsigned newFrameNum) -> void
Set the frame of the mesh, automatically calculates correct uv offset. Frames go right to left,...
Definition Mesh.cpp:158
auto save(Stream &stream) const -> void
Definition Mesh.cpp:143
auto uploadMeshToGPU() -> void
Upload mesh data to gpu, when creating mesh, or loading from a file.
Definition Mesh.cpp:38
auto setObjectTransform(const AffineMatrix &transform) const -> void
Sets the uniform for the object.
Definition Mesh.cpp:191
std::shared_ptr< VertexBuffer > vertexBuffer
Definition Mesh.h:110
auto load(Stream &stream) -> void
Definition Mesh.cpp:138
auto setObjectProjection(const AffineMatrix &projection) const -> void
Definition Mesh.cpp:198
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:178
Definition Shader.h:23
holds and manages textures for use with meshes in sprites
Definition Texture.h:31
Definition VAO.h:33
Buffer that holds data for the vertices for OpenGL.
Definition VertexBuffer.h:29
Definition Json.h:32
Definition VAO.h:23
Definition AffineMatrix.h:30
Definition Vector4D.h:23
Definition Vertex.h:27