everything
This commit is contained in:
parent
79fa59c3aa
commit
8998bc3936
19 changed files with 855 additions and 7504 deletions
178
engine/mesh.h
178
engine/mesh.h
|
|
@ -1,63 +1,147 @@
|
|||
|
||||
#include <exception>
|
||||
#ifndef ENGINE2026_MESH_H
|
||||
#define ENGINE2026_MESH_H
|
||||
|
||||
#include <glad/glad.h> // holds all OpenGL type declarations
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include <engine/shader.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <glad/glad.h>
|
||||
using namespace std;
|
||||
|
||||
#define MAX_BONE_INFLUENCE 4
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z; // position
|
||||
float nX, nY, zY; // normal
|
||||
float u, v; // texcoord
|
||||
// position
|
||||
glm::vec3 Position;
|
||||
// normal
|
||||
glm::vec3 Normal;
|
||||
// texCoords
|
||||
glm::vec2 TexCoords;
|
||||
// tangent
|
||||
glm::vec3 Tangent;
|
||||
// bitangent
|
||||
glm::vec3 Bitangent;
|
||||
//bone indexes which will influence this vertex
|
||||
int m_BoneIDs[MAX_BONE_INFLUENCE];
|
||||
//weights from each bone
|
||||
float m_Weights[MAX_BONE_INFLUENCE];
|
||||
};
|
||||
|
||||
class Mesh
|
||||
{
|
||||
struct Texture {
|
||||
unsigned int id;
|
||||
string type;
|
||||
string path;
|
||||
};
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
unsigned int VBO, VAO, EBO;
|
||||
std::size_t indexCount = 0;
|
||||
// mesh Data
|
||||
vector<Vertex> vertices;
|
||||
vector<unsigned int> indices;
|
||||
vector<Texture> textures;
|
||||
unsigned int VAO;
|
||||
|
||||
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices) : indexCount(indices.size())
|
||||
{
|
||||
if (vertices.empty() || indices.empty()) {
|
||||
throw std::runtime_error("Mesh created with empty vertices or indices");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
// constructor
|
||||
Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures)
|
||||
{
|
||||
this->vertices = vertices;
|
||||
this->indices = indices;
|
||||
this->textures = textures;
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
// now that we have all the required data, set the vertex buffers and its attribute pointers.
|
||||
setupMesh();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
||||
// render the mesh
|
||||
void Draw(Shader &shader)
|
||||
{
|
||||
// bind appropriate textures
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
unsigned int normalNr = 1;
|
||||
unsigned int heightNr = 1;
|
||||
for(unsigned int i = 0; i < textures.size(); i++)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
|
||||
// retrieve texture number (the N in diffuse_textureN)
|
||||
string number;
|
||||
string name = textures[i].type;
|
||||
if(name == "texture_diffuse")
|
||||
number = std::to_string(diffuseNr++);
|
||||
else if(name == "texture_specular")
|
||||
number = std::to_string(specularNr++); // transfer unsigned int to string
|
||||
else if(name == "texture_normal")
|
||||
number = std::to_string(normalNr++); // transfer unsigned int to string
|
||||
else if(name == "texture_height")
|
||||
number = std::to_string(heightNr++); // transfer unsigned int to string
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
// now set the sampler to the correct texture unit
|
||||
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
|
||||
// and finally bind the texture
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i].id);
|
||||
}
|
||||
|
||||
// Position
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x));
|
||||
glEnableVertexAttribArray(0);
|
||||
// draw mesh
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Normals
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, nX));
|
||||
glEnableVertexAttribArray(1);
|
||||
// always good practice to set everything back to defaults once configured.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
||||
// Texcoords
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cout << "Failed to load mesh" << std::endl;
|
||||
}
|
||||
}
|
||||
private:
|
||||
// render data
|
||||
unsigned int VBO, EBO;
|
||||
|
||||
void Draw() const
|
||||
{
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount),
|
||||
GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
// initializes all the buffer objects/arrays
|
||||
void setupMesh()
|
||||
{
|
||||
// create buffers/arrays
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
// load data into vertex buffers
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
// A great thing about structs is that their memory layout is sequential for all its items.
|
||||
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
|
||||
// again translates to 3/2 floats which translates to a byte array.
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
|
||||
|
||||
// set the vertex attribute pointers
|
||||
// vertex Positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
// vertex normals
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
|
||||
// vertex texture coords
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
|
||||
// vertex tangent
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
|
||||
// vertex bitangent
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
|
||||
// ids
|
||||
glEnableVertexAttribArray(5);
|
||||
glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
|
||||
|
||||
// weights
|
||||
glEnableVertexAttribArray(6);
|
||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weights));
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
};
|
||||
#endif //ENGINE2026_MESH_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue