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

@@ -35,8 +35,8 @@ add_executable(${PROJECT_NAME}
src/Texture.h src/Texture.h
src/TextureLoader.cpp src/TextureLoader.cpp
src/TextureLoader.h src/TextureLoader.h
src/ShaderLoader.cpp src/ShaderManager.cpp
src/ShaderLoader.h) src/ShaderManager.h)
target_link_libraries(${PROJECT_NAME} glfw spdlog assimp glm) target_link_libraries(${PROJECT_NAME} glfw spdlog assimp glm)
target_include_directories(${PROJECT_NAME} PRIVATE ${ASSIMP_INCLUDE_INSTALL_DIR}) target_include_directories(${PROJECT_NAME} PRIVATE ${ASSIMP_INCLUDE_INSTALL_DIR})

View File

@@ -10,7 +10,7 @@ Ke 0.0 0.0 0.0
Ni 1.450000 Ni 1.450000
d 1.000000 d 1.000000
illum 2 illum 2
map_Kd diffuse.jpg map_Kd backpack/diffuse.jpg
map_Bump normal.png map_Bump backpack/normal.png
map_Ks specular.jpg map_Ks backpack/specular.jpg

73398
resources/male.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@ in vec3 Position;
in vec2 TexCoord; in vec2 TexCoord;
in vec3 Normal; in vec3 Normal;
uniform sampler2D diffuse;
uniform vec3 viewPosition; uniform vec3 viewPosition;
uniform vec3 phongAmbient; uniform vec3 phongAmbient;
@@ -19,13 +21,15 @@ uniform vec3 lightDiffuse;
uniform vec3 lightSpecular; uniform vec3 lightSpecular;
void main() { void main() {
vec4 diffuseColor = texture(diffuse, TexCoord);
vec3 norm = normalize(Normal); vec3 norm = normalize(Normal);
vec3 lightDir = normalize(-lightDirection); vec3 lightDir = normalize(-lightDirection);
vec3 ambient = (phongAmbient); vec3 ambient = phongAmbient * vec3(diffuseColor);
float diffImpact = max(dot(norm, lightDir), 0.0); float diffImpact = max(dot(norm, lightDir), 0.0);
vec3 diffuse = (phongDiffuse * diffImpact); vec3 diffuse = (phongDiffuse * vec3(diffuseColor)) * diffImpact;
vec3 result = ambient + diffuse; vec3 result = ambient + diffuse;
FragColor = vec4(result, 1.0); FragColor = vec4(result, 1.0);

View File

@@ -12,22 +12,30 @@
#include <cstring> #include <cstring>
#include "TextureLoader.h"
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"
std::unordered_map<std::string, std::shared_ptr<Model>> ModelLoader::models; std::unordered_map<std::string, std::shared_ptr<Model>> ModelLoader::models;
Mesh process_ai_mesh(aiMesh* aiMesh); void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform, Mesh& mesh);
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);
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; Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(_path.data(), const aiScene* scene = importer.ReadFile(_path.data(),
aiProcess_Triangulate | aiProcess_Triangulate |
aiProcess_GenNormals | aiProcess_GenSmoothNormals |
aiProcess_FlipUVs); aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) 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>(); 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; return model;
} }
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model) void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model)
{ {
auto t = node->mTransformation; auto m = node->mTransformation;
transform = transform * glm::mat4(nTransform) 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++) for (unsigned int i = 0; i < node->mNumMeshes; i++)
{ {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; aiMesh* aiMesh = scene->mMeshes[node->mMeshes[i]];
model->meshes.push_back(std::make_shared<Mesh>(process_ai_mesh(mesh))); 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++) 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 positions = std::vector<float>(aiMesh->mNumVertices * 3);
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2); 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++) 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] = position.x;
positions[3 * i + 1] = position.y; positions[3 * i + 1] = position.y;
positions[3 * i + 2] = position.z; positions[3 * i + 2] = position.z;
@@ -107,50 +127,58 @@ Mesh process_ai_mesh(aiMesh* aiMesh)
indices.push_back(face.mIndices[j]); 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; aiString matName;
aiReturn ret = mat->Get(AI_MATKEY_NAME, matName); aiReturn ret = aiMat->Get(AI_MATKEY_NAME, matName);
if (ret != AI_SUCCESS) { if (ret != AI_SUCCESS) {
spdlog::error("Could not find material name"); spdlog::error("Could not find material name");
} }
// grab the phong stuff
aiColor3D ambientColor {0.f, 0.f, 0.f}; 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) { if (ret != AI_SUCCESS) {
spdlog::error("Could not find ambient color for material: {}", matName.C_Str()); spdlog::error("Could not find ambient color for material: {}", matName.C_Str());
ambientColor = {.3f, .3f, .3f}; ambientColor = {.3f, .3f, .3f};
} }
aiColor3D diffuseColor {1.f, 1.f, 1.f}; 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) { if (ret != AI_SUCCESS) {
spdlog::error("Could not find diffuse color for material: {}", matName.C_Str()); spdlog::error("Could not find diffuse color for material: {}", matName.C_Str());
diffuseColor = {1.f, 1.f, 1.f}; diffuseColor = {1.f, 1.f, 1.f};
} }
aiColor3D specularColor {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) { if (ret != AI_SUCCESS) {
spdlog::error("Could not find specular color for material: {}", matName.C_Str()); spdlog::error("Could not find specular color for material: {}", matName.C_Str());
specularColor = {1.f, 1.f, 1.f}; specularColor = {1.f, 1.f, 1.f};
} }
float shininess = 32.f; float shininess = 32.f;
ret = mat->Get(AI_MATKEY_SHININESS, shininess); ret = aiMat->Get(AI_MATKEY_SHININESS, shininess);
if (ret != AI_SUCCESS) { if (ret != AI_SUCCESS) {
spdlog::error("Could not find shininess for material: {}", matName.C_Str()); spdlog::error("Could not find shininess for material: {}", matName.C_Str());
shininess = 32.f; shininess = 32.f;
} }
Material material; // now grab the textures, first diffuse
material.name = matName.C_Str(); for (unsigned int i = 0; i < aiMat->GetTextureCount(aiTextureType_DIFFUSE); i++) {
material.phong.ambient = glm::vec3{ambientColor.r, ambientColor.g, ambientColor.b}; aiString textureName;
material.phong.diffuse = glm::vec3{diffuseColor.r, diffuseColor.g, diffuseColor.b}; aiMat->GetTexture(aiTextureType_DIFFUSE, i, &textureName);
material.phong.specular = glm::vec3{specularColor.r, specularColor.g, specularColor.b};
material.phong.shininess = shininess;
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: public:
static std::unordered_map<std::string, std::shared_ptr<Model>> models; 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. // Created by slinky on 5/1/26.
// //
#include "ShaderLoader.h" #include "ShaderManager.h"
#include "glad/gl.h" #include "glad/gl.h"
@@ -14,9 +14,9 @@
void log_shader_compile_errors(GLuint shader, std::string_view shaderType); void log_shader_compile_errors(GLuint shader, std::string_view shaderType);
void log_program_compile_errors(GLuint program); 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 vertexSource;
std::string fragmentSource; std::string fragmentSource;

View File

@@ -2,8 +2,8 @@
// Created by slinky on 5/1/26. // Created by slinky on 5/1/26.
// //
#ifndef B_ENGINE_SHADERLOADER_H #ifndef B_ENGINE_SHADERMANAGER_H
#define B_ENGINE_SHADERLOADER_H #define B_ENGINE_SHADERMANAGER_H
#include <string> #include <string>
#include <string_view> #include <string_view>
@@ -11,11 +11,11 @@
#include "ShaderProgram.h" #include "ShaderProgram.h"
class ShaderLoader { class ShaderManager {
public: public:
static std::unordered_map<std::string, std::shared_ptr<ShaderProgram>> shaders; 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); 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 "Model.h"
#include "ModelLoader.h" #include "ModelLoader.h"
#include "ShaderLoader.h" #include "ShaderManager.h"
#include "ShaderProgram.h" #include "ShaderProgram.h"
#include "Texture.h" #include "Texture.h"
#include "TextureLoader.h" #include "TextureLoader.h"
@@ -61,14 +61,15 @@ int main() {
unsigned char defaultSpecularData[4] = {static_cast<unsigned char>(128), 128, 128, 255}; 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); 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"); ShaderManager::shaders["phong_shader"] = ShaderManager::load("./resources/standard.vert", "./resources/standard.frag");
if (ShaderLoader::shaders["phong_shader"] == nullptr) if (ShaderManager::shaders["phong_shader"] == nullptr)
{ {
std::exit(1); 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["backpack"] = ModelLoader::load_from_file("./resources/backpack/backpack.obj");
/*ModelLoader::models["male"] = ModelLoader::load_from_file("./resources/male.obj");*/
loop(); loop();
@@ -139,14 +140,11 @@ void load_gl()
void init_camera() void init_camera()
{ {
cameraPosition = glm::vec3{5, 0, 5}; cameraPosition = glm::vec3{10, 0, 10};
auto cameraTarget = glm::vec3{0, 0, 0}; 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); float aspectRatio = static_cast<float>(gWindowWidth) / static_cast<float>(gWindowHeight);
projection = glm::perspective(glm::radians(75.f), aspectRatio, 0.1f, 100.0f); projection = glm::perspective(glm::radians(75.f), aspectRatio, 0.1f, 100.0f);
} }
@@ -157,16 +155,21 @@ void loop() {
while (!glfwWindowShouldClose(gWindow)) { while (!glfwWindowShouldClose(gWindow)) {
auto model = glm::identity<glm::mat4>(); 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(); glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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* cube = ModelLoader::models["cube"].get();
const Model* backpack = ModelLoader::models["backpack"].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->bind();
shader->setMat4("projection", projection); shader->setMat4("projection", projection);
@@ -180,7 +183,7 @@ void loop() {
shader->setVec3("phongSpecular", glm::vec3(1.0f, 1.0f, 1.0f)); shader->setVec3("phongSpecular", glm::vec3(1.0f, 1.0f, 1.0f));
shader->setFloat("phongShininess", 32.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("lightAmbient", glm::vec3(0.3f, 0.3f, 0.3f));
shader->setVec3("lightDiffuse", glm::vec3(1.0f, 1.0f, 1.0f)); shader->setVec3("lightDiffuse", glm::vec3(1.0f, 1.0f, 1.0f));
shader->setVec3("lightSpecular", glm::vec3(1.0f, 1.0f, 1.0f)); shader->setVec3("lightSpecular", glm::vec3(1.0f, 1.0f, 1.0f));