started fixing stuff with model imports

This commit is contained in:
2026-05-03 00:08:13 -05:00
parent 782bbcbadc
commit 68020255d0
13 changed files with 199571 additions and 81 deletions

View File

@@ -7,8 +7,7 @@ Mesh::Mesh(const std::vector<float> &positions, const std::vector<float> &uvs, c
vao = 0;
ebo = 0;
numIndices = indices.size();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// positions
GLuint positionVbo = 0;
@@ -28,10 +27,7 @@ Mesh::Mesh(const std::vector<float> &positions, const std::vector<float> &uvs, c
glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
// build the vao
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
@@ -47,5 +43,9 @@ Mesh::Mesh(const std::vector<float> &positions, const std::vector<float> &uvs, c
glBindBuffer(GL_ARRAY_BUFFER, normalVbo);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
}

View File

@@ -12,11 +12,13 @@
#include <cstring>
#include "glm/ext/matrix_transform.hpp"
std::unordered_map<std::string, std::shared_ptr<Model>> ModelLoader::models;
Mesh process_ai_mesh(aiMesh* aiMesh);
void process_ai_node(aiNode* node, const aiScene* scene, Model* model);
Mesh process_ai_mesh(const aiMesh* aiMesh);
Material process_ai_material(aiMaterial* mat);
std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
@@ -25,9 +27,8 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
const aiScene* scene = importer.ReadFile(_path.data(),
aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_FlipUVs |
aiProcess_CalcTangentSpace);
aiProcess_GenNormals |
aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
@@ -37,64 +38,65 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
auto model = std::make_shared<Model>();
model->materials.resize(scene->mNumMaterials);
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
aiMaterial* mat = scene->mMaterials[i];
model->materials[i] = std::make_shared<Material>(process_ai_material(mat));
}
model->meshes.resize(scene->mNumMeshes);
for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[i];
model->meshes[i] = std::make_shared<Mesh>(process_ai_mesh(mesh));
}
process_ai_node(scene->mRootNode, scene, model.get());
return model;
}
void process_ai_node(aiNode* node, const aiScene* scene, Model* model)
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model)
{
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto t = node->mTransformation;
transform = transform * glm::mat4(nTransform)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
model->meshes.push_back(std::make_shared<Mesh>(process_ai_mesh(mesh)));
}
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
process_ai_node(node->mChildren[i], scene, model);
}
}
Mesh process_ai_mesh(const aiMesh* aiMesh)
Mesh process_ai_mesh(aiMesh* aiMesh)
{
auto positions = std::vector<float>(aiMesh->mNumVertices * 3);
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2);
auto normals = std::vector<float>(aiMesh->mNumVertices * 3);
auto indices = std::vector<unsigned int>(aiMesh->mNumFaces * 3);
auto indices = std::vector<unsigned int>();
for (unsigned int i = 0; i < aiMesh->mNumVertices; i++)
{
auto position = aiMesh->mVertices[i];
positions.push_back(position.x);
positions.push_back(position.y);
positions.push_back(position.z);
positions[3 * i] = position.x;
positions[3 * i + 1] = position.y;
positions[3 * i + 2] = position.z;
if (aiMesh->HasNormals())
{
normals.push_back(aiMesh->mNormals[i].x);
normals.push_back(aiMesh->mNormals[i].y);
normals.push_back(aiMesh->mNormals[i].z);
aiVector3D norm = aiMesh->mNormals[i];
normals[3 * i] = norm.x;
normals[3 * i + 1] = norm.y;
normals[3 * i + 2] = norm.z;
}
else
{
normals.push_back(0);
normals.push_back(0);
normals.push_back(0);
normals[3 * i] = 0;
normals[3 * i + 1] = 0;
normals[3 * i + 2] = 0;
}
if (aiMesh->HasTextureCoords(0))
{
uvs.push_back(aiMesh->mTextureCoords[0][i].x);
uvs.push_back(aiMesh->mTextureCoords[0][i].y);
uvs[2 * i] = aiMesh->mTextureCoords[0][i].x;
uvs[2 * i + 1] = aiMesh->mTextureCoords[0][i].y;
}
else
{
uvs.push_back(0);
uvs.push_back(0);
uvs[2 * i] = 0;
uvs[2 * i + 1] = 0;
}
}

View File

@@ -5,6 +5,8 @@
#ifndef B_ENGINE_SHADERLOADER_H
#define B_ENGINE_SHADERLOADER_H
#include <string>
#include <string_view>
#include <memory>
#include "ShaderProgram.h"

View File

@@ -68,6 +68,7 @@ int main() {
}
ModelLoader::models["cube"] = ModelLoader::load_from_file("./resources/cube.obj");
ModelLoader::models["backpack"] = ModelLoader::load_from_file("./resources/backpack/backpack.obj");
loop();
@@ -132,11 +133,13 @@ void load_gl()
glfwGetFramebufferSize(gWindow, &fbWidth, &fbHeight);
glViewport(0, 0, fbWidth, fbHeight);
}
glEnable(GL_DEPTH_TEST);
}
void init_camera()
{
cameraPosition = glm::vec3{0, 5, 5};
cameraPosition = glm::vec3{5, 0, 5};
auto cameraTarget = glm::vec3{0, 0, 0};
auto behind = glm::normalize(cameraPosition - cameraTarget);
@@ -153,49 +156,42 @@ void loop() {
Texture* defaultSpecular = TextureLoader::textures["default_specular"].get();
while (!glfwWindowShouldClose(gWindow)) {
auto modelMatrix = glm::identity<glm::mat4>();
auto model = glm::identity<glm::mat4>();
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram* shader = ShaderLoader::shaders["phong_shader"].get();
Model* model = ModelLoader::models["cube"].get();
const Model* cube = ModelLoader::models["cube"].get();
const Model* backpack = ModelLoader::models["backpack"].get();
if (shader && model) {
Material* mat = model->materials[0].get();
if (shader && backpack) {
shader->bind();
shader->setMat4("projection", projection);
shader->setMat4("view", view);
shader->setMat4("model", modelMatrix);
shader->setMat4("model", model);
shader->setVec3("viewPosition", cameraPosition);
if (mat) {
shader->setVec3("phongAmbient", mat->phong.ambient);
shader->setVec3("phongDiffuse", mat->phong.diffuse);
shader->setVec3("phongSpecular", mat->phong.specular);
shader->setFloat("phongShininess", mat->phong.shininess);
}
shader->setVec3("phongAmbient", glm::vec3(0.3f, 0.3f, 0.3f));
shader->setVec3("phongDiffuse", glm::vec3(0.8f, 0.8f, 0.8f));
shader->setVec3("phongSpecular", glm::vec3(1.0f, 1.0f, 1.0f));
shader->setFloat("phongShininess", 32.0f);
shader->setVec3("lightPosition", glm::vec3(5.f, 5.f, 5.f));
shader->setVec3("lightAmbient", glm::vec3{.1f, .1f, .1f});
shader->setVec3("lightDiffuse", glm::vec3{.8f, .8f, .8f});
shader->setVec3("lightSpecular", glm::vec3{1.f, 1.f, 1.f});
shader->setVec3("lightDirection", glm::vec3(0, 1, 0));
shader->setVec3("lightAmbient", glm::vec3(0.3f, 0.3f, 0.3f));
shader->setVec3("lightDiffuse", glm::vec3(1.0f, 1.0f, 1.0f));
shader->setVec3("lightSpecular", glm::vec3(1.0f, 1.0f, 1.0f));
glActiveTexture(GL_TEXTURE0);
defaultDiffuse->bind();
shader->setInt("diffuse1", 0);
glActiveTexture(GL_TEXTURE1);
defaultSpecular->bind();
shader->setInt("specular1", 1);
for (auto mesh: model->meshes) {
for (const auto& mesh: backpack->meshes) {
glBindVertexArray(mesh.get()->vao);
glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
ShaderProgram::unbind();
}
glfwSwapBuffers(gWindow);