Can now load a glb model with textures embedded in the model. Further cleanup coming.

This commit is contained in:
Zed A. Shaw 2026-08-24 16:29:06 -04:00
parent 5b5e8a9fb0
commit c6a7c42a4e
12 changed files with 323 additions and 194 deletions

View file

@ -1,4 +1,5 @@
#include "mesh.hpp"
#include "dbc.hpp"
void Mesh::Draw(Shader &shader) {
unsigned int diffuseNr = 1;
@ -8,21 +9,25 @@ void Mesh::Draw(Shader &shader) {
for(unsigned int i = 0; i < textures.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
std::string number;
std::string name = textures[i].type;
unsigned int number = 0;
std::string& name = textures[i].type;
if(name == "texture_diffuse") {
number = std::to_string(diffuseNr++);
number = diffuseNr++;
} else if(name == "texture_specular") {
number = std::to_string(specularNr++);
number = specularNr++;
} else if(name == "texture_normal") {
number = std::to_string(normalNr++);
number = normalNr++;
} else if(name == "texture_height") {
number = std::to_string(heightNr++);
number = heightNr++;
} else {
dbc::sentinel($F("Invalid texture type name {}", name));
}
std::string target = std::format("{}{}", name, number);
// TODO: Get this uniform ID once and cache it?
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
glUniform1i(glGetUniformLocation(shader.ID, target.c_str()), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
@ -46,6 +51,7 @@ void Mesh::setupMesh() {
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));