First working animations, but lots of bugs and refactoring needed.
This commit is contained in:
parent
8b41591360
commit
a1c8208b47
158 changed files with 17271 additions and 0 deletions
73
45-animation/src/mesh.cpp
Normal file
73
45-animation/src/mesh.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include "mesh.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
void Mesh::draw(Shader &shader, bool with_textures) {
|
||||
if(with_textures) {
|
||||
apply_textures(shader);
|
||||
}
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::apply_textures(const Shader& shader) {
|
||||
int texture_i = 1;
|
||||
bool has_height = false;
|
||||
|
||||
for(auto& texture : textures) {
|
||||
texture.uniform_id = glGetUniformLocation(shader.ID, texture.target.c_str());
|
||||
if(texture.uniform_id == -1) continue;
|
||||
|
||||
if(texture.target.starts_with("texture_height")) {
|
||||
has_height = true;
|
||||
}
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + texture_i);
|
||||
glBindTexture(GL_TEXTURE_2D, texture.id);
|
||||
glUniform1i(texture.uniform_id, texture_i);
|
||||
|
||||
texture_i++;
|
||||
}
|
||||
|
||||
// quick hack to avoid applying height/normal to things without one
|
||||
shader.setBool("hasHeight", has_height);
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, tangent));
|
||||
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, bitangent));
|
||||
|
||||
// bone ids
|
||||
glEnableVertexAttribArray(5);
|
||||
glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, bone_ids));
|
||||
|
||||
// bone weights
|
||||
glEnableVertexAttribArray(6);
|
||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, weights));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue