fixed a lot of stuff with camera and importing

This commit is contained in:
2026-05-03 12:43:39 -04:00
parent 68020255d0
commit 9a8e618bbb
9 changed files with 73488 additions and 55 deletions

View File

@@ -12,22 +12,30 @@
#include <cstring>
#include "TextureLoader.h"
#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);
void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform, Mesh& mesh);
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model);
Material process_ai_material(aiMaterial* mat);
void process_ai_material(aiMaterial* aiMat, const aiScene* scene, Material& mat);
std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
auto zUpMatrix = glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f, // Column 0
0.0f, 0.0f, 1.0f, 0.0f, // Column 1
0.0f, -1.0f, 0.0f, 0.0f, // Column 2
0.0f, 0.0f, 0.0f, 1.0f // Column 3
);
std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path, bool zUp)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(_path.data(),
aiProcess_Triangulate |
aiProcess_GenNormals |
aiProcess_GenSmoothNormals |
aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
@@ -38,29 +46,41 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
auto model = std::make_shared<Model>();
process_ai_node(scene->mRootNode, scene, model.get());
auto transform = glm::identity<glm::mat4>();
if (zUp) {
transform = zUpMatrix;
}
process_ai_node(scene->mRootNode, scene, transform, model.get());
return model;
}
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model)
{
auto t = node->mTransformation;
transform = transform * glm::mat4(nTransform)
auto m = node->mTransformation;
transform = transform * glm::mat4(m.a1, m.a2, m.a3, m.a4, m.b1, m.b2, m.b3, m.b4, m.c1, m.c2, m.c3, m.c4, m.d1, m.d2, m.d3, m.d4);
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)));
aiMesh* aiMesh = scene->mMeshes[node->mMeshes[i]];
Mesh mesh{};
process_ai_mesh(aiMesh, scene, transform, mesh);
model->meshes.push_back(std::make_shared<Mesh>(mesh));
aiMaterial* aiMat = scene->mMaterials[aiMesh->mMaterialIndex];
Material mat{};
process_ai_material(aiMat, scene, mat);
model->materials.push_back(std::make_shared<Material>(mat));
}
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
process_ai_node(node->mChildren[i], scene, model);
process_ai_node(node->mChildren[i], scene, transform, model);
}
}
Mesh process_ai_mesh(aiMesh* aiMesh)
void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform, Mesh& mesh)
{
auto positions = std::vector<float>(aiMesh->mNumVertices * 3);
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2);
@@ -69,7 +89,7 @@ Mesh process_ai_mesh(aiMesh* aiMesh)
for (unsigned int i = 0; i < aiMesh->mNumVertices; i++)
{
auto position = aiMesh->mVertices[i];
auto position = transform * glm::vec4(aiMesh->mVertices[i].x, aiMesh->mVertices[i].y, aiMesh->mVertices[i].z, 1.0f);
positions[3 * i] = position.x;
positions[3 * i + 1] = position.y;
positions[3 * i + 2] = position.z;
@@ -107,50 +127,58 @@ Mesh process_ai_mesh(aiMesh* aiMesh)
indices.push_back(face.mIndices[j]);
}
return {positions, uvs, normals, indices};
mesh = {positions, uvs, normals, indices};
}
Material process_ai_material(aiMaterial *mat) {
void process_ai_material(aiMaterial* aiMat, const aiScene* scene, Material& mat) {
aiString matName;
aiReturn ret = mat->Get(AI_MATKEY_NAME, matName);
aiReturn ret = aiMat->Get(AI_MATKEY_NAME, matName);
if (ret != AI_SUCCESS) {
spdlog::error("Could not find material name");
}
// grab the phong stuff
aiColor3D ambientColor {0.f, 0.f, 0.f};
ret = mat->Get(AI_MATKEY_COLOR_DIFFUSE, ambientColor);
ret = aiMat->Get(AI_MATKEY_COLOR_DIFFUSE, ambientColor);
if (ret != AI_SUCCESS) {
spdlog::error("Could not find ambient color for material: {}", matName.C_Str());
ambientColor = {.3f, .3f, .3f};
}
aiColor3D diffuseColor {1.f, 1.f, 1.f};
ret = mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor);
ret = aiMat->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor);
if (ret != AI_SUCCESS) {
spdlog::error("Could not find diffuse color for material: {}", matName.C_Str());
diffuseColor = {1.f, 1.f, 1.f};
}
aiColor3D specularColor {1.f, 1.f, 1.f};
ret = mat->Get(AI_MATKEY_COLOR_SPECULAR, specularColor);
ret = aiMat->Get(AI_MATKEY_COLOR_SPECULAR, specularColor);
if (ret != AI_SUCCESS) {
spdlog::error("Could not find specular color for material: {}", matName.C_Str());
specularColor = {1.f, 1.f, 1.f};
}
float shininess = 32.f;
ret = mat->Get(AI_MATKEY_SHININESS, shininess);
ret = aiMat->Get(AI_MATKEY_SHININESS, shininess);
if (ret != AI_SUCCESS) {
spdlog::error("Could not find shininess for material: {}", matName.C_Str());
shininess = 32.f;
}
Material material;
material.name = matName.C_Str();
material.phong.ambient = glm::vec3{ambientColor.r, ambientColor.g, ambientColor.b};
material.phong.diffuse = glm::vec3{diffuseColor.r, diffuseColor.g, diffuseColor.b};
material.phong.specular = glm::vec3{specularColor.r, specularColor.g, specularColor.b};
material.phong.shininess = shininess;
// now grab the textures, first diffuse
for (unsigned int i = 0; i < aiMat->GetTextureCount(aiTextureType_DIFFUSE); i++) {
aiString textureName;
aiMat->GetTexture(aiTextureType_DIFFUSE, i, &textureName);
return material;
if (!TextureLoader::textures.contains(textureName.C_Str())) {
TextureLoader::textures[textureName.C_Str()] = TextureLoader::load_from_file(textureName.C_Str());
}
}
mat.name = matName.C_Str();
mat.phong.ambient = glm::vec3{ambientColor.r, ambientColor.g, ambientColor.b};
mat.phong.diffuse = glm::vec3{diffuseColor.r, diffuseColor.g, diffuseColor.b};
mat.phong.specular = glm::vec3{specularColor.r, specularColor.g, specularColor.b};
mat.phong.shininess = shininess;
}

View File

@@ -15,7 +15,7 @@ class ModelLoader
public:
static std::unordered_map<std::string, std::shared_ptr<Model>> models;
static std::shared_ptr<Model> load_from_file(std::string_view _path);
static std::shared_ptr<Model> load_from_file(std::string_view _path, bool zUp = false);
};

View File

@@ -2,7 +2,7 @@
// Created by slinky on 5/1/26.
//
#include "ShaderLoader.h"
#include "ShaderManager.h"
#include "glad/gl.h"
@@ -14,9 +14,9 @@
void log_shader_compile_errors(GLuint shader, std::string_view shaderType);
void log_program_compile_errors(GLuint program);
std::unordered_map<std::string, std::shared_ptr<ShaderProgram>> ShaderLoader::shaders;
std::unordered_map<std::string, std::shared_ptr<ShaderProgram>> ShaderManager::shaders;
std::shared_ptr<ShaderProgram> ShaderLoader::load(std::string_view vertexPath, std::string_view fragmentPath)
std::shared_ptr<ShaderProgram> ShaderManager::load(std::string_view vertexPath, std::string_view fragmentPath)
{
std::string vertexSource;
std::string fragmentSource;

View File

@@ -2,8 +2,8 @@
// Created by slinky on 5/1/26.
//
#ifndef B_ENGINE_SHADERLOADER_H
#define B_ENGINE_SHADERLOADER_H
#ifndef B_ENGINE_SHADERMANAGER_H
#define B_ENGINE_SHADERMANAGER_H
#include <string>
#include <string_view>
@@ -11,11 +11,11 @@
#include "ShaderProgram.h"
class ShaderLoader {
class ShaderManager {
public:
static std::unordered_map<std::string, std::shared_ptr<ShaderProgram>> shaders;
static std::shared_ptr<ShaderProgram> load(std::string_view vertexPath, std::string_view fragmentPath);
};
#endif //B_ENGINE_SHADERLOADER_H
#endif //B_ENGINE_SHADERMANAGER_H

View File

@@ -12,7 +12,7 @@
#include "Model.h"
#include "ModelLoader.h"
#include "ShaderLoader.h"
#include "ShaderManager.h"
#include "ShaderProgram.h"
#include "Texture.h"
#include "TextureLoader.h"
@@ -61,14 +61,15 @@ int main() {
unsigned char defaultSpecularData[4] = {static_cast<unsigned char>(128), 128, 128, 255};
TextureLoader::textures["default_specular"] = TextureLoader::load_from_data(reinterpret_cast<unsigned char*>(&defaultSpecularData), 1, 1, 4);
ShaderLoader::shaders["phong_shader"] = ShaderLoader::load("./resources/standard.vert", "./resources/standard.frag");
if (ShaderLoader::shaders["phong_shader"] == nullptr)
ShaderManager::shaders["phong_shader"] = ShaderManager::load("./resources/standard.vert", "./resources/standard.frag");
if (ShaderManager::shaders["phong_shader"] == nullptr)
{
std::exit(1);
}
ModelLoader::models["cube"] = ModelLoader::load_from_file("./resources/cube.obj");
/*ModelLoader::models["cube"] = ModelLoader::load_from_file("./resources/cube.obj", true);*/
ModelLoader::models["backpack"] = ModelLoader::load_from_file("./resources/backpack/backpack.obj");
/*ModelLoader::models["male"] = ModelLoader::load_from_file("./resources/male.obj");*/
loop();
@@ -139,14 +140,11 @@ void load_gl()
void init_camera()
{
cameraPosition = glm::vec3{5, 0, 5};
cameraPosition = glm::vec3{10, 0, 10};
auto cameraTarget = glm::vec3{0, 0, 0};
auto behind = glm::normalize(cameraPosition - cameraTarget);
auto right = glm::normalize(glm::cross(glm::vec3{0, 1, 0}, behind));
auto up = glm::normalize(glm::cross(right, behind));
view = glm::lookAt(cameraPosition, cameraTarget, up);
view = glm::lookAt(cameraPosition, cameraTarget, glm::vec3(0.0f, 1.0f, 0.0f));
float aspectRatio = static_cast<float>(gWindowWidth) / static_cast<float>(gWindowHeight);
projection = glm::perspective(glm::radians(75.f), aspectRatio, 0.1f, 100.0f);
}
@@ -157,16 +155,21 @@ void loop() {
while (!glfwWindowShouldClose(gWindow)) {
auto model = glm::identity<glm::mat4>();
// model = glm::translate(model, glm::vec3(0, -5.f, 0));
// model = glm::scale(model, glm::vec3(.5f, .5f, .5f));
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram* shader = ShaderLoader::shaders["phong_shader"].get();
ShaderProgram* shader = ShaderManager::shaders["phong_shader"].get();
const Model* cube = ModelLoader::models["cube"].get();
const Model* backpack = ModelLoader::models["backpack"].get();
const Model* male = ModelLoader::models["male"].get();
if (shader && backpack) {
if (shader) {
Material* mat = backpack->materials[0].get();
shader->bind();
shader->setMat4("projection", projection);
@@ -180,7 +183,7 @@ void loop() {
shader->setVec3("phongSpecular", glm::vec3(1.0f, 1.0f, 1.0f));
shader->setFloat("phongShininess", 32.0f);
shader->setVec3("lightDirection", glm::vec3(0, 1, 0));
shader->setVec3("lightDirection", glm::vec3(-.5f, -.5f, -.5f));
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));