#pragma once // Must be the first include #include // Other includes #include #include "Gedeng/Shader.h" #include "Gedeng/Spatial.h" #include "Material.h" #include namespace Gedeng { struct Mesh : public Spatial { Mesh() = default; explicit Mesh(const std::vector &_vertices, const std::vector &_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 vertices; std::vector indices; private: unsigned int EBO; unsigned int VBO; unsigned int VAO; Material material; }; } // namespace Gedeng