learn-opengl/17-refactor-bug-fix/src/mesh.cpp
2026-08-27 11:59:34 -04:00

45 lines
1.5 KiB
C++

#include "mesh.hpp"
#include "dbc.hpp"
void Mesh::draw(Shader &shader) {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
}
void Mesh::connect_shader(const Shader& shader) {
for(unsigned int i = 0; i < textures.size(); i++) {
Texture& texture = textures[i];
glActiveTexture(GL_TEXTURE0 + i);
// move this to setup_mesh
glUniform1i(texture.uniform_id, i);
glBindTexture(GL_TEXTURE_2D, texture.id);
texture.uniform_id = glGetUniformLocation(shader.ID, texture.target.c_str());
}
}
void Mesh::setup_mesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
// this matches the locations in the vertex shader
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, tex_coords));
}