Models all tested and refactored so that the draw() call doesn't do a bunch of irrelevant shit.

This commit is contained in:
Zed A. Shaw 2026-08-25 13:51:22 -04:00
parent 2431cf8c97
commit 47bd233722
6 changed files with 102 additions and 70 deletions

View file

@ -82,8 +82,8 @@ unsigned int texture_from_file(const std::string& directory, const std::string&
}
void Model::draw(Shader &shader) {
for(unsigned int i = 0; i < meshes.size(); i++) {
meshes[i].Draw(shader);
for(auto& mesh : meshes) {
mesh.draw(shader);
}
}
@ -122,36 +122,36 @@ Mesh Model::process_mesh(const aiScene *scene, aiMesh *mesh) {
std::vector<Texture> textures;
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex;
vertex.Position = glm::vec3(
glm::vec3 position{
mesh->mVertices[i].x,
mesh->mVertices[i].y,
mesh->mVertices[i].z);
mesh->mVertices[i].z};
glm::vec3 normal{};
glm::vec2 tex_coords{0.0f, 0.0f};
if(mesh->HasNormals()) {
vertex.Normal = glm::vec3(
normal = glm::vec3(
mesh->mNormals[i].x,
mesh->mNormals[i].y,
mesh->mNormals[i].z);
}
if(mesh->mTextureCoords[0]) {
vertex.TexCoords = glm::vec2(
tex_coords = glm::vec2(
mesh->mTextureCoords[0][i].x,
mesh->mTextureCoords[0][i].y
);
} else {
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
}
vertices.push_back(vertex);
vertices.emplace_back(position, normal, tex_coords);
}
for(unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
aiFace& face = mesh->mFaces[i];
for(unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
indices.emplace_back(face.mIndices[j]);
}
}
@ -179,32 +179,39 @@ Mesh Model::process_mesh(const aiScene *scene, aiMesh *mesh) {
return Mesh(vertices, indices, textures);
}
void Model::load_material_textures(const aiScene *scene, std::vector<Texture>& textures, aiMaterial *mat, aiTextureType type, std::string typeName)
void Model::load_material_textures(const aiScene *scene, std::vector<Texture>& textures, aiMaterial *mat, aiTextureType type, std::string type_name)
{
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
aiString str;
mat->GetTexture(type, i, &str);
std::string tx_str{str.C_Str()};
dbc::check(!tx_str.empty(), "Texture has empty path, should be impossible?");
std::string tx_path{str.C_Str()};
dbc::check(!tx_path.empty(), "Texture has empty path, should be impossible?");
if(textures_loaded.contains(tx_str)) {
textures.push_back(textures_loaded.at(tx_str));
if(textures_loaded.contains(tx_path)) {
textures.push_back(textures_loaded.at(tx_path));
} else {
unsigned int tx_id = 0;
// if it's a *# style path then it's internal
if(tx_str[0] == '*') {
if(tx_path[0] == '*') {
// get the texture from the internal version
tx_id = texture_from_internal(scene, std::stoi(tx_str.substr(1)));
tx_id = texture_from_internal(scene, std::stoi(tx_path.substr(1)));
} else {
// else get it from a file
tx_id = texture_from_file(directory, tx_str);
tx_id = texture_from_file(directory, tx_path);
}
// dubious code here, but seems to be right
auto& result = textures.emplace_back(tx_id, typeName, tx_str);
textures_loaded.try_emplace(tx_str, result);
auto& result = textures.emplace_back(tx_id, type_name, tx_path, texture_counts);
textures_loaded.try_emplace(tx_path, result);
}
}
}
void Model::connect_shader(const Shader& shader) {
for(auto& mesh : meshes) {
mesh.connect_shader(shader);
}
}