AireEngine
A 3D Open-World Game Engine
Loading...
Searching...
No Matches
mesh.h
1#pragma once
2#include <glm/glm.hpp>
3#include <vector>
4#include <string>
5#include <iostream>
6#include <fstream>
7#include <sstream>
8#include "../rendering/vao.h"
9#include "../rendering/window.h"
10#include "../rendering/camera.h"
11#include"material.h"
12#include"../rendering/shaderProgram.h"
13#include <glm/gtc/matrix_transform.hpp>
14#include <glm/gtc/type_ptr.hpp>
15using namespace std;
16#define MAX_BONE_INFLUENCE 4
17namespace Aire {
18
19 using MeshHandle = int;
20
23 struct Vertex {
25 glm::vec3 Position;
27 glm::vec3 Normal;
29 glm::vec2 TexCoords;
31 glm::vec3 Tangent;
33 glm::vec3 Bitangent;
34
36 int m_BoneIDs[MAX_BONE_INFLUENCE];
38 float m_Weights[MAX_BONE_INFLUENCE];
39 };
40
43 glm::vec3 DiffuseColors = glm::vec3(0.5f, 0.5f, 0.5f);
44 glm::vec3 Specular = glm::vec3(0.5f, 0.5f, 0.5f);
45 glm::vec3 Ambient = glm::vec3(0.5f, 0.5f, 0.5f);
46 float Shininess = 0.0f;
47 };
48
50 struct meshTexture {
51 unsigned int id;
52 std::string type;
53 std::string path;
54 };
55
57 class Mesh : public Resource{
58
59 private:
60 // render data
61 unsigned int VBO, EBO;
62 MaterialPtr material;
63 MeshHandle meshHandle;
65 void setupMesh()
66 {
67 // create buffers/arrays
68 glGenVertexArrays(1, &VAO);
69 glGenBuffers(1, &VBO);
70 glGenBuffers(1, &EBO);
71
72 glBindVertexArray(VAO);
73 // load data into vertex buffers
74 glBindBuffer(GL_ARRAY_BUFFER, VBO);
75
76 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
77
78 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
79 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
80
81 // set the vertex attribute pointers
82 // vertex Positions
83 glEnableVertexAttribArray(0);
84 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
85 // vertex normals
86 glEnableVertexAttribArray(1);
87 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
88 // vertex texture coords
89 glEnableVertexAttribArray(2);
90 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
91 // vertex tangent
92 glEnableVertexAttribArray(3);
93 glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
94 // vertex bitangent
95 glEnableVertexAttribArray(4);
96 glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
97 // ids
98 glEnableVertexAttribArray(5);
99 glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
100
101 // weights
102 glEnableVertexAttribArray(6);
103 glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weights));
104 glBindVertexArray(0);
105 }
106
107 public:
109 std::vector<Vertex> vertices;
111 std::vector<unsigned int> indices;
112 //std::vector<meshTexture> textures;
113 // MaterialColor materialColor;
114
115 unsigned int VAO;
116
121 Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices,MaterialPtr& handle)
122 {
123 this->vertices = vertices;
124 this->indices = indices;
125 this->material = handle;
126 this->meshHandle = 0;
127 this->EBO = NULL;
128 this->VBO = NULL;
129 this->VAO = NULL;
130 }
131
137 Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, MaterialPtr& handle,MeshHandle& meshHandle)
138 {
139 this->vertices = vertices;
140 this->indices = indices;
141 this->material = handle;
142 this->meshHandle = meshHandle;
143 this->EBO = NULL;
144 this->VBO = NULL;
145 this->VAO = NULL;
146 }
147
148 ~Mesh();
149
152 void render(ShaderProgram& shader)
153 //void Draw(ShaderProgram& shader, Aire::Camera& camera, glm::mat4 modelMatrix)
154 {
155 glm::vec3* diffuseCol = material->getColor(DIFFUSE_COLOR);
156 glm::vec3* specularCol = material->getColor(SPECULAR_COLOR);
157 glm::vec3* ambientCol = material->getColor(AMBIENT_COLOR);
158 float* shininessValue = material->getShininess();
159
160 TexturePtr diffuseMap = material->getTexture(DIFFUSE_MAP);
161 TexturePtr specularMap = material->getTexture(SPECULAR_MAP);
162 TexturePtr normalMap = material->getTexture(HEIGHT_MAP);
163 TexturePtr heightMap = material->getTexture(NORMAL_MAP);
164
165 glUniform1f(glGetUniformLocation(shader.getProgramId(), "shininessValue"), *shininessValue);
166
167 int countTexturesAssigned = 0;
168 if (diffuseMap != nullptr) {
169 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasDiffuseMap"), 1);
170 shader.set2DTextureUniform("diffuseMap", diffuseMap->getHandle());
171 countTexturesAssigned++;
172 }
173 else {
174 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasDiffuseMap"), 0);
175 glUniform3f(glGetUniformLocation(shader.getProgramId(), "diffuseColor"), diffuseCol->x, diffuseCol->y, diffuseCol->z);
176 }
177
178 if (specularMap != nullptr) {
179 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasSpecularMap"), 1);
180 shader.set2DTextureUniform("specularMap", specularMap->getHandle());
181 countTexturesAssigned++;
182 }
183 else {
184 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasSpecularMap"), 0);
185 glUniform3f(glGetUniformLocation(shader.getProgramId(), "specularColor"), specularCol->x, specularCol->y, specularCol->z);
186 }
187
188 if (normalMap != nullptr) {
189 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasNormalMap"), 1);
190 shader.set2DTextureUniform("normalMap", normalMap->getHandle());
191 countTexturesAssigned++;
192 }
193 else {
194 glUniform1i(glGetUniformLocation(shader.getProgramId(), "hasNormalMap"), 0);
195 }
196
197 // draw mesh
198 glBindVertexArray(VAO);
199 glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
200
201 shader.unbind2DTextures(countTexturesAssigned);
202 }
203
205 unsigned int* getVBO();
206
208 unsigned int* getEB0();
209
211 std::vector<Vertex> getVertice();
212
214 std::vector<unsigned int> getIndices();
215
217 MaterialPtr getMaterial();
218
220 uint32_t getSize();
221
223 MeshHandle getMeshHandle();
224
228 void translate(glm::mat4& modelMatrix, glm::vec3 direction);
229
233 void scale(glm::mat4& modelMatrix, float val);
234
239 void rotate(glm::mat4& modelMatrix, float degree,glm::vec3 direction);
240
241
242
243
244 };
245 using MeshPtr = shared_ptr<Mesh>;
246
247}
A class for a 3D indexed triangular mesh.
Definition mesh.h:57
std::vector< unsigned int > indices
Mesh indices.
Definition mesh.h:111
std::vector< Vertex > getVertice()
Definition mesh.cpp:26
uint32_t getSize()
Definition mesh.cpp:41
void rotate(glm::mat4 &modelMatrix, float degree, glm::vec3 direction)
Definition mesh.cpp:61
MaterialPtr getMaterial()
Definition mesh.cpp:36
std::vector< unsigned int > getIndices()
Definition mesh.cpp:31
unsigned int * getEB0()
Definition mesh.cpp:21
MeshHandle getMeshHandle()
Definition mesh.cpp:46
void scale(glm::mat4 &modelMatrix, float val)
Definition mesh.cpp:56
unsigned int * getVBO()
Definition mesh.cpp:16
Mesh(std::vector< Vertex > vertices, std::vector< unsigned int > indices, MaterialPtr &handle)
Definition mesh.h:121
void translate(glm::mat4 &modelMatrix, glm::vec3 direction)
Definition mesh.cpp:51
Mesh(std::vector< Vertex > vertices, std::vector< unsigned int > indices, MaterialPtr &handle, MeshHandle &meshHandle)
Definition mesh.h:137
std::vector< Vertex > vertices
Mesh vertices.
Definition mesh.h:109
void render(ShaderProgram &shader)
Definition mesh.h:152
A base resource class.
Definition resource.h:26
A linked OpenGL program.
Definition shaderProgram.h:16
void set2DTextureUniform(const char *uniformName, const GLuint textureHandle)
Definition shaderProgram.h:82
GLuint getProgramId() const
Definition shaderProgram.h:71
void unbind2DTextures(int numTextures)
Definition shaderProgram.h:109
Material colours for Blinn-Phong rendering.
Definition mesh.h:42
float Shininess
Shininess value.
Definition mesh.h:46
glm::vec3 Ambient
Ambient colour.
Definition mesh.h:45
glm::vec3 Specular
Specular colour.
Definition mesh.h:44
glm::vec3 DiffuseColors
Diffuse colour.
Definition mesh.h:43
Definition mesh.h:23
glm::vec3 Normal
Normal vector.
Definition mesh.h:27
int m_BoneIDs[MAX_BONE_INFLUENCE]
Bone indices which will influence this vertex.
Definition mesh.h:36
glm::vec2 TexCoords
Texture coordinates.
Definition mesh.h:29
float m_Weights[MAX_BONE_INFLUENCE]
Weights from each bone.
Definition mesh.h:38
glm::vec3 Bitangent
Bitangent vector.
Definition mesh.h:33
glm::vec3 Position
Position in world space.
Definition mesh.h:25
glm::vec3 Tangent
Tangent vector.
Definition mesh.h:31
Texture for the mesh.
Definition mesh.h:50
std::string path
Textuer path.
Definition mesh.h:53
unsigned int id
Texture ID.
Definition mesh.h:51
std::string type
Texture type.
Definition mesh.h:52