GlistEngine
Loading...
Searching...
No Matches
gMesh.h
Go to the documentation of this file.
1/*
2 * gMesh.h
3 *
4 * Created on: May 7, 2020
5 * Author: noyan
6 */
7
8#ifndef ENGINE_GRAPHICS_GMESH_H_
9#define ENGINE_GRAPHICS_GMESH_H_
10
11#include <vector>
12#include <unordered_map>
13
14#include "gNode.h"
15#include "gTexture.h"
16#include "gMaterial.h"
17#include "gVbo.h"
18#include "gBoundingBox.h"
19#include "gUbo.h"
20
21class gMesh : public gNode {
22public:
23 static const int DRAWMODE_POINTS = GL_POINTS, DRAWMODE_LINES = GL_LINES, DRAWMODE_LINELOOP = GL_LINE_LOOP, DRAWMODE_LINESTRIP = GL_LINE_STRIP,
24 DRAWMODE_TRIANGLES = GL_TRIANGLES, DRAWMODE_TRIANGLESTRIP = GL_TRIANGLE_STRIP, DRAWMODE_TRIANGLEFAN = GL_TRIANGLE_FAN;
25
27 gMesh(const std::vector<gVertex>& vertices, const std::vector<gIndex>& indices, const std::vector<gTexture*>& textures);
28 gMesh(std::shared_ptr<std::vector<gVertex>> vertices,
29 std::shared_ptr<std::vector<gIndex>> indices,
30 std::vector<gTexture*> textures);
31 gMesh(const gMesh&);
32 ~gMesh() override;
33
34 void setVertices(const std::vector<gVertex>& vertices, const std::vector<gIndex>& indices);
35 void setVertices(const std::vector<gVertex>& vertices);
36 void setVertices(std::shared_ptr<std::vector<gVertex>> vertices);
37 void setVertices(std::shared_ptr<std::vector<gVertex>> vertices, std::shared_ptr<std::vector<gIndex>> indices);
38
39 void setTextures(const std::vector<gTexture*>& textures);
40 void setTextures(const std::unordered_map<gTexture::TextureType, gTexture*>& textures);
41 void setTexture(gTexture* texture);
43
44 void setTextureTiling(float tilingX, float tilingY);
45 void setTextureTiling(float tiling);
46 const glm::vec2& getTextureTiling() const;
47
48 std::vector<gVertex>& getVertices();
49 std::vector<gIndex>& getIndices();
50 std::shared_ptr<std::vector<gVertex>> getVerticesPtr();
51 std::shared_ptr<std::vector<gIndex>> getIndicesPtr();
52 int getVerticesNum() const;
53 int getIndicesNum() const;
56 void clear();
57
58 void setName(const std::string& name);
59 const std::string& getName() const;
60
61 void setDrawMode(int drawMode);
62 int getDrawMode() const;
63
64 void setMaterial(gMaterial* material);
66
67 virtual void draw();
68 virtual void drawInstanced(const std::vector<glm::mat4>& instanceTransformations);
69
73
75
76 /*
77 * Sets the color of all vertices in this mesh to the given value.
78 * This operation overwrites any existing per-vertex colors and
79 * immediately updates the GPU vertex buffer.
80 *
81 * Typical use cases include debugging, object highlighting,
82 * or applying a uniform color override.
83 *
84 * @param color The color to apply to every vertex in the mesh
85 */
86 void setAllVertexColor(const glm::vec3& color);
87
88 /*
89 * Assigns a default color to vertices that do not have an explicit color set.
90 * Only vertices whose color is (0, 0, 0) are modified.
91 *
92 * If at least one vertex is updated, the GPU vertex buffer is refreshed.
93 * Existing non-zero vertex colors are preserved.
94 *
95 * @param defColor The default color to assign to uninitialized vertices
96 */
97 void fillMissingVertexColors(const glm::vec3& defColor = glm::vec3(1.0f));
98
99 /*
100 * Assigns each vertex a unique color based on its index in the mesh.
101 * This creates a deterministic, multi-color effect across the mesh.
102 * The colors do not depend on vertex positions but on their order.
103 * After updating, the GPU vertex buffer is refreshed immediately.
104 */
106
107protected:
109
110 void drawStart(bool isInstanced = false);
111 void drawVbo();
112 // Records this mesh through the Vulkan backend's 2D draw path instead of the
113 // VBO / shader path, which has no Vulkan equivalent. 3D meshes are skipped.
114 // Vulkan has no line loop topology at all, and triangle fan - while in the
115 // specification - is missing on MoltenVK, so a mesh drawn in either mode has its
116 // indices rewritten once into a form Vulkan does have. Done lazily on the first
117 // draw rather than at upload, because the draw mode is set after the vertices.
120
122 // instanceTransformations null for an ordinary draw; otherwise one model matrix
123 // per instance, uploaded here and drawn in a single call.
124 //
125 // sourceVbo overrides which buffer is drawn from, for the vertex-animated case
126 // where the mesh keeps one vbo per animation frame instead of a single one.
127 void drawVulkan3D(const std::vector<glm::mat4>* instanceTransformations = nullptr,
128 gVbo* sourceVbo = nullptr);
129 void drawVboInstanced(const std::vector<glm::mat4>& instanceTransformations);
131 void drawEnd();
134 std::unique_ptr<gVbo> vbo;
135 std::shared_ptr<std::vector<gVertex> >vertices;
137
138private:
139 std::shared_ptr<std::vector<gIndex>> indices;
140
141 std::string name;
142 std::unordered_map<gTexture::TextureType, gTexture*> textures;
143 std::unordered_map<gTexture::TextureType, std::string> texturenames;
144 std::unordered_map<gTexture::TextureType, int> texturecounters = {
154 };
155 int drawmode;
156 gMaterial material;
157 glm::vec2 texturetiling{1.0f, 1.0f};
158
159 gBoundingBox initialboundingbox;
160 gBoundingBox boundingbox;
161 bool needsboundingboxrecalculation;
162
163};
164
165#endif /* ENGINE_GRAPHICS_GMESH_H_ */
Definition gBoundingBox.h:36
Definition gMaterial.h:20
static const int DRAWMODE_LINELOOP
Definition gMesh.h:23
void clear()
std::shared_ptr< std::vector< gIndex > > getIndicesPtr()
void recalculateBoundingBox()
void setTexture(gTexture *texture)
void setTextureTiling(float tilingX, float tilingY)
const glm::vec2 & getTextureTiling() const
void drawExtraShaders()
static const int DRAWMODE_POINTS
Definition gMesh.h:23
float distanceTriangles(gRay *ray)
void drawEnd()
void bindMaterialUniforms(gShader &shader)
virtual void drawInstanced(const std::vector< glm::mat4 > &instanceTransformations)
~gMesh() override
gMaterial * getMaterial()
void drawVulkan2D()
void setName(const std::string &name)
bool vulkanindicesexpanded
Definition gMesh.h:118
void setTextures(const std::unordered_map< gTexture::TextureType, gTexture * > &textures)
static const int DRAWMODE_TRIANGLES
Definition gMesh.h:24
gVbo & getVbo()
void drawStart(bool isInstanced=false)
std::vector< gVertex > & getVertices()
void fillMissingVertexColors(const glm::vec3 &defColor=glm::vec3(1.0f))
bool isprojection2d
Definition gMesh.h:136
int getVerticesNum() const
int getIndicesNum() const
const std::string & getName() const
int getDrawMode() const
gTexture * getTexture(gTexture::TextureType type)
static const int DRAWMODE_LINESTRIP
Definition gMesh.h:23
void setTextureTiling(float tiling)
void setVertices(const std::vector< gVertex > &vertices)
void setDrawMode(int drawMode)
void setTextures(const std::vector< gTexture * > &textures)
void drawVbo()
gMesh(std::shared_ptr< std::vector< gVertex > > vertices, std::shared_ptr< std::vector< gIndex > > indices, std::vector< gTexture * > textures)
void setMaterial(gMaterial *material)
void setVertices(const std::vector< gVertex > &vertices, const std::vector< gIndex > &indices)
static const int DRAWMODE_TRIANGLESTRIP
Definition gMesh.h:24
std::vector< gIndex > & getIndices()
bool intersectsTriangles(gRay *ray)
void setVertices(std::shared_ptr< std::vector< gVertex > > vertices, std::shared_ptr< std::vector< gIndex > > indices)
void setVertices(std::shared_ptr< std::vector< gVertex > > vertices)
gMesh(const gMesh &)
void processTransformationMatrix() override
std::unique_ptr< gVbo > vbo
Definition gMesh.h:134
std::shared_ptr< std::vector< gVertex > > vertices
Definition gMesh.h:135
static const int DRAWMODE_LINES
Definition gMesh.h:23
const gBoundingBox & getInitialBoundingBox() const
const gBoundingBox & getBoundingBox()
void applyVertexGradient()
void bindMaterialTextures(gShader &shader)
static const int DRAWMODE_TRIANGLEFAN
Definition gMesh.h:24
void setAllVertexColor(const glm::vec3 &color)
gMesh(const std::vector< gVertex > &vertices, const std::vector< gIndex > &indices, const std::vector< gTexture * > &textures)
std::shared_ptr< std::vector< gVertex > > getVerticesPtr()
virtual void draw()
void drawVulkan3D(const std::vector< glm::mat4 > *instanceTransformations=nullptr, gVbo *sourceVbo=nullptr)
void drawVboInstanced(const std::vector< glm::mat4 > &instanceTransformations)
void expandIndicesForVulkan()
Definition gRay.h:32
Definition gShader.h:19
Definition gTexture.h:17
TextureType
Definition gTexture.h:19
@ TEXTURETYPE_PBR_AO
Definition gTexture.h:28
@ TEXTURETYPE_HEIGHT
Definition gTexture.h:23
@ TEXTURETYPE_NORMAL
Definition gTexture.h:22
@ TEXTURETYPE_PBR_ALBEDO
Definition gTexture.h:24
@ TEXTURETYPE_PBR_METALNESS
Definition gTexture.h:26
@ TEXTURETYPE_DIFFUSE
Definition gTexture.h:20
@ TEXTURETYPE_PBR_ROUGHNESS
Definition gTexture.h:25
@ TEXTURETYPE_PBR_NORMAL
Definition gTexture.h:27
@ TEXTURETYPE_SPECULAR
Definition gTexture.h:21
Definition gVbo.h:30