#pragma once #include #include #include #include "texture.h" #include "shader.h" namespace glg { struct Vertex { glm::vec3 position; glm::vec3 normal; glm::vec2 texCoords; }; class Mesh { public: std::vector vertices; std::vector indices; std::vector textures; Mesh(std::vector& vertices, std::vector& indices, std::vector& textures) { this->vertices = vertices; this->indices = indices; this->textures = textures; setupMesh(); } void draw(Shader& shader) { unsigned int diffuseNr = 1; for (unsigned int i = 0; i < textures.size(); i++) { std::string number; std::string name = "diffuse"; if (name == "diffuse") { number = std::to_string(diffuseNr); diffuseNr++; } shader.setInt("texture_" + name + number, i); textures[i].activate(i); } glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, (int) indices.size(), GL_UNSIGNED_INT, 0); glBindVertexArray(0); glActiveTexture(GL_TEXTURE0); } private: unsigned int VAO, VBO, EBO; void setupMesh() { glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices), &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); // vertex positions glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); // vertex texture coords glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoords)); glBindVertexArray(0); } }; }