106 lines
3.5 KiB
C++
106 lines
3.5 KiB
C++
#pragma once
|
|
|
|
// Must be the first include
|
|
#include <glad/glad.h>
|
|
|
|
// Other includes
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "Gedeng/Shader.h"
|
|
#include "Gedeng/Spatial.h"
|
|
#include "Material.h"
|
|
#include <vector>
|
|
|
|
namespace Gedeng {
|
|
|
|
struct Mesh : public Spatial {
|
|
Mesh() = default;
|
|
|
|
explicit Mesh(const std::vector<float> &_vertices, const std::vector<unsigned int> &_indices)
|
|
: vertex_count(_indices.size()), vertices(_vertices), indices(_indices) {
|
|
// Copy the vertices into a local classic float array. Nothing was displayed without this,
|
|
// maybe
|
|
// due to weird hidden type incompatibility or out of scope issues?
|
|
float vertices[_vertices.size()];
|
|
std::copy(_vertices.begin(), _vertices.end(), vertices);
|
|
|
|
unsigned int indices[_indices.size()];
|
|
std::copy(_indices.begin(), _indices.end(), indices);
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glGenBuffers(1, &EBO);
|
|
|
|
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then
|
|
// configure vertex attributes(s).
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
// position attribute
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
// Normal attribute
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)(3 * sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
|
|
// texture coord attribute
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)(6 * sizeof(float)));
|
|
glEnableVertexAttribArray(2);
|
|
|
|
// Tangent attribute
|
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)(8 * sizeof(float)));
|
|
glEnableVertexAttribArray(3);
|
|
|
|
// Bitangent attribute
|
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)(11 * sizeof(float)));
|
|
glEnableVertexAttribArray(4);
|
|
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
virtual void render(Shader &shader) const {
|
|
glm::mat4 model_matrix = get_matrix();
|
|
// model_matrix[3] = glm::vec4(get_origin(), 1.0);
|
|
|
|
shader.setMat4("model", model_matrix);
|
|
|
|
/* // 0 can't be a valid texture name, so we use it for meshes without textures here
|
|
if (texture_id != 0) {
|
|
glActiveTexture(GL_TEXTURE1);
|
|
glBindTexture(GL_TEXTURE_2D, texture_id);
|
|
}
|
|
if (normal_id != 0) {
|
|
glActiveTexture(GL_TEXTURE2);
|
|
glBindTexture(GL_TEXTURE_2D, normal_id);
|
|
} */
|
|
|
|
// TODO: Not always required (not when rendering shadows) - make functions separate?
|
|
// shader.setFloat("diffuseStrength", material.diffuse);
|
|
// shader.setFloat("specularStrength", material.specular);
|
|
// shader.setFloat("normalScale", material.normal_scale);
|
|
|
|
glBindVertexArray(VAO);
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0);
|
|
}
|
|
|
|
unsigned int vertex_count;
|
|
std::vector<float> vertices;
|
|
std::vector<unsigned int> indices;
|
|
|
|
private:
|
|
unsigned int EBO;
|
|
unsigned int VBO;
|
|
unsigned int VAO;
|
|
|
|
Material material;
|
|
};
|
|
|
|
} // namespace Gedeng
|