Implement LearnOpenGL::Mesh

This commit is contained in:
2024-02-22 18:39:50 -05:00
parent 868f52464e
commit 5b7829b1f8

149
include/LearnOpenGL/Mesh.h Normal file
View File

@@ -0,0 +1,149 @@
/// @file Mesh.h
/// @description
/// @author LearnOpenGL - Updated Josh O'Leary
/// @lastedit 2024-02-22
// Original Source:
// https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/mesh.h
#pragma once
#include <glad/glad.h>
#include <GL/glew.h>
#include <GL/glext.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <string>
#include "Shader.h"
using J3ML::LinearAlgebra::Vector2;
using J3ML::LinearAlgebra::Vector3;
using J3ML::u32;
constexpr int MAX_BONE_INFLUENCE = 4;
namespace LearnOpenGL
{
struct Vertex
{
Vector3 Position;
Vector3 Normal;
Vector2 TexCoords;
Vector3 Tangent;
Vector3 BiTangent;
int BoneIDs[MAX_BONE_INFLUENCE];
float BoneWeights[MAX_BONE_INFLUENCE];
};
// Different from Texture2D in purpose
struct Texture {
unsigned int id;
std::string type;
std::string path;
};
class Mesh {
public:
std::vector<Vertex> vertices;
std::vector<uint> indices;
std::vector<Texture> textures;
uint VAO;
Mesh(std::vector<Vertex> verts, std::vector<uint> indices, std::vector<Texture> textures)
{
this->vertices = verts;
this->indices = indices;
this->textures = textures;
setupMesh();
}
void Draw(Shader &shader)
{
uint diffuse = 1;
uint specular = 1;
uint normal = 1;
uint height = 1;
for (uint i = 0; i < textures.size(); i++)
{
// activate proper texture unit before binding
glActiveTexture(GL_TEXTURE0 + i);
// retrieve texture number (the N in diffuse_textureN)
std::string number;
std::string name = textures[i].type;
if (name == "texture_diffuse")
number = std::to_string(diffuse++);
else if (name == "texture_specular")
number = std::to_string(specular++);
else if (name == "texture_normal")
number = std::to_string(normal++);
else if (name == "texture_height")
number = std::to_string(height++);
// 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);
}
// draw mesh
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, static_cast<uint>(indices.size()), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// always good practice to set everything back to defaults once configured.
glActiveTexture(GL_TEXTURE0);
}
private:
// render data
unsigned int VBO, EBO;
// initializes all the buffer objects/arrays
void setupMesh()
{
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 member items
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/vec2 array
// which again translates to 3/2 floats which translates to a byte array
// Except we use J3ML instead of glm, but the concept applies nonetheless
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(uint), &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, BoneIDs));
// weights
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, BoneWeights));
glBindVertexArray(0);
}
};
}