continued with asset loader

This commit is contained in:
2026-05-18 18:10:06 -04:00
parent e02e92db10
commit 6a0e0d0685
28 changed files with 183 additions and 73575 deletions

View File

@@ -0,0 +1,5 @@
{
"name": "phong",
"vertex": "phong.vert",
"fragment": "phong.frag"
}

View File

Before

Width:  |  Height:  |  Size: 8.3 MiB

After

Width:  |  Height:  |  Size: 8.3 MiB

View File

@@ -1,12 +0,0 @@
# Blender 3.6.0 MTL File: 'None'
# www.blender.org
newmtl 01_-_Default.001
Ns 10.000005
Ka 1.000000 1.000000 1.000000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 1
map_Kd Corvette_C4_Base_Color.png

View File

@@ -1,46 +0,0 @@
# Blender v2.76 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 1.000000 0.333333
vt 1.000000 0.666667
vt 0.666667 0.666667
vt 0.666667 0.333333
vt 0.666667 0.000000
vt 0.000000 0.333333
vt 0.000000 0.000000
vt 0.333333 0.000000
vt 0.333333 1.000000
vt 0.000000 1.000000
vt 0.000000 0.666667
vt 0.333333 0.333333
vt 0.333333 0.666667
vt 1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 2/1/1 3/2/1 4/3/1
f 8/1/2 7/4/2 6/5/2
f 5/6/3 6/7/3 2/8/3
f 6/8/4 7/5/4 3/4/4
f 3/9/5 7/10/5 8/11/5
f 1/12/6 4/13/6 8/11/6
f 1/4/1 2/1/1 4/3/1
f 5/14/2 8/1/2 6/5/2
f 1/12/3 5/6/3 2/8/3
f 2/12/4 6/8/4 3/4/4
f 4/13/5 3/9/5 8/11/5
f 5/6/6 1/12/6 8/11/6

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,8 @@
#include <fstream> #include <fstream>
#include <unordered_set> #include <unordered_set>
#include "ModelManager.h" #include "MeshManager.h"
#include "ModelImporter.h"
#include "TextureManager.h" #include "TextureManager.h"
#include "uuid.h" #include "uuid.h"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
@@ -99,8 +100,7 @@ void AssetRegistry::read_directory(const Path& path) {
} }
std::optional<AssetRecord> record = read_meta_file(entry.path()); std::optional<AssetRecord> record = read_meta_file(entry.path());
if (!record) if (!record) {
{
continue; continue;
} }
@@ -183,17 +183,28 @@ bool AssetRegistry::generate_meta_file(const Path& path)
return true; return true;
} }
void AssetRegistry::load_asset(AssetRecord* asset) { void AssetRegistry::load_asset(const AssetRecord* asset) {
switch (asset->assetType) { switch (asset->assetType) {
case AssetType::SHADER: case AssetType::SHADER:
break; return;
case AssetType::TEXTURE: case AssetType::TEXTURE: {
TextureManager::textures[asset->uuid] = TextureManager::load_from_file(asset->sourcePath); ResourceHandle handle;
break; handle.assetUUID = asset->uuid;
case AssetType::MODEL: TextureManager::textures[handle] = TextureManager::load_from_file(asset->sourcePath);
ModelManager::models[asset->uuid] = ModelManager::load_from_file(asset->sourcePath); return;
break; }
default: case AssetType::MODEL: {
std::vector<Mesh> meshes = ModelImporter::load_from_file(asset->sourcePath);
for (int i = 0; i < meshes.size(); i++) {
ResourceHandle meshHandle;
meshHandle.assetUUID = asset->uuid;
meshHandle.localId = i + 1;
MeshManager::meshes[meshHandle] = std::make_unique<Mesh>(meshes[i]);
}
return;
}
case AssetType::DIRECTORY:
case AssetType::UNKNOWN:
break; break;
} }
} }

View File

@@ -31,7 +31,7 @@ private:
static bool does_meta_exist(const Path& path); static bool does_meta_exist(const Path& path);
static std::optional<AssetRecord> read_meta_file(const Path& path); static std::optional<AssetRecord> read_meta_file(const Path& path);
static bool generate_meta_file(const Path& path); static bool generate_meta_file(const Path& path);
static void load_asset(AssetRecord* asset); static void load_asset(const AssetRecord* asset);
}; };

View File

@@ -6,6 +6,7 @@
#define B_ENGINE_COMPONENTS_H #define B_ENGINE_COMPONENTS_H
#include "Model.h" #include "Model.h"
#include "ResourceHandle.h"
#include "entt/entt.hpp" #include "entt/entt.hpp"
#include "glm/glm.hpp" #include "glm/glm.hpp"
@@ -27,8 +28,8 @@ namespace Components {
entt::entity next_sibling {entt::null}; entt::entity next_sibling {entt::null};
}; };
struct Drawable { struct Mesh {
std::shared_ptr<Model> model {nullptr}; ResourceHandle handle;
}; };
struct DirectionalLight { struct DirectionalLight {
@@ -41,6 +42,10 @@ namespace Components {
struct Tag { struct Tag {
std::string name; std::string name;
}; };
struct Material {
ResourceHandle handle;
};
} }

View File

@@ -5,11 +5,9 @@
#ifndef B_ENGINE_MATERIAL_H #ifndef B_ENGINE_MATERIAL_H
#define B_ENGINE_MATERIAL_H #define B_ENGINE_MATERIAL_H
#include <memory>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "Texture.h" #include "ResourceHandle.h"
typedef struct typedef struct
{ {
@@ -21,12 +19,9 @@ typedef struct
typedef struct typedef struct
{ {
std::shared_ptr<Texture> diffuse; ResourceHandle diffuse;
std::shared_ptr<Texture> specular; ResourceHandle specular;
std::shared_ptr<Texture> normal; ResourceHandle normal;
std::string name;
PhongProperties phong; PhongProperties phong;
} Material; } Material;

7
src/MaterialManager.cpp Normal file
View File

@@ -0,0 +1,7 @@
//
// Created by slinky on 5/17/26.
//
#include "MaterialManager.h"
std::unordered_map<ResourceHandle, std::unique_ptr<Material>> MaterialManager::materials = {};

17
src/MaterialManager.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by slinky on 5/17/26.
//
#ifndef B_ENGINE_MATERIALMANAGER_H
#define B_ENGINE_MATERIALMANAGER_Hs
#include <unordered_map>
#include "Material.h"
#include "ResourceHandle.h"
class MaterialManager {
public:
static std::unordered_map<ResourceHandle, std::unique_ptr<Material>> materials;
};
#endif //B_ENGINE_MATERIALMANAGER_H

View File

@@ -21,7 +21,6 @@ public:
GLuint vao = 0; GLuint vao = 0;
GLuint ebo = 0; GLuint ebo = 0;
unsigned int numIndices = 0; unsigned int numIndices = 0;
unsigned int materialId = 0;
}; };
#endif //B_ENGINE_MESH_H #endif //B_ENGINE_MESH_H

7
src/MeshManager.cpp Normal file
View File

@@ -0,0 +1,7 @@
//
// Created by slinky on 5/17/26.
//
#include "MeshManager.h"
std::unordered_map<ResourceHandle, std::unique_ptr<Mesh>> MeshManager::meshes;

18
src/MeshManager.h Normal file
View File

@@ -0,0 +1,18 @@
//
// Created by slinky on 5/17/26.
//
#ifndef B_ENGINE_MESHMANAGER_H
#define B_ENGINE_MESHMANAGER_H
#include <unordered_map>
#include "ResourceHandle.h"
#include "Mesh.h"
class MeshManager {
public:
static std::unordered_map<ResourceHandle, std::unique_ptr<Mesh>> meshes;
};
#endif //B_ENGINE_MESHMANAGER_H

View File

@@ -4,7 +4,7 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "ModelManager.h" #include "ModelImporter.h"
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
@@ -15,8 +15,6 @@
#include "TextureManager.h" #include "TextureManager.h"
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"
std::unordered_map<uuids::uuid, std::unique_ptr<Model>> ModelManager::models;
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, ModelNode& model); void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, ModelNode& model);
void process_ai_mesh(const aiMesh* aiMesh, Mesh& mesh); void process_ai_mesh(const aiMesh* aiMesh, Mesh& mesh);
@@ -31,7 +29,7 @@ auto zUpMatrix = glm::mat4(
0.0f, 0.0f, 0.0f, 1.0f // Column 3 0.0f, 0.0f, 0.0f, 1.0f // Column 3
); );
std::unique_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool zUp) std::vector<Mesh> ModelImporter::load_from_file(std::string_view _path, bool zUp)
{ {
Assimp::Importer importer; Assimp::Importer importer;
@@ -43,39 +41,19 @@ std::unique_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{ {
spdlog::error("failed to load model {}: {}", _path, importer.GetErrorString()); spdlog::error("failed to load model {}: {}", _path, importer.GetErrorString());
return nullptr; return {};
} }
auto model = std::make_unique<Model>(); auto meshes = std::vector<Mesh>{};
const std::filesystem::path modelPath = {_path};
/*for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
aiMaterial* aiMat = scene->mMaterials[i];
aiString matName;
if (aiMat->Get(AI_MATKEY_NAME, matName) && strcmp(matName.data, "DefaultMaterial") == 0) {
continue;
}
Material mat{};
process_ai_material(aiMat, mat, modelPath.parent_path());
model->materials.push_back(std::make_shared<Material>(mat));
}*/
for (unsigned int i = 0; i < scene->mNumMeshes; i++) { for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
aiMesh* aiMesh = scene->mMeshes[i]; aiMesh* aiMesh = scene->mMeshes[i];
Mesh mesh{}; Mesh mesh{};
mesh.materialId = aiMesh->mMaterialIndex;
process_ai_mesh(aiMesh, mesh); process_ai_mesh(aiMesh, mesh);
model->meshes.push_back(std::make_shared<Mesh>(mesh)); meshes.push_back(mesh);
} }
auto transform = glm::identity<glm::mat4>(); return meshes;
if (zUp) {
transform = zUpMatrix;
}
process_ai_node(scene->mRootNode, scene, transform, model->root);
return model;
} }
void process_ai_node(aiNode* aiNode, const aiScene* scene, glm::mat4 transform, ModelNode& node) void process_ai_node(aiNode* aiNode, const aiScene* scene, glm::mat4 transform, ModelNode& node)

19
src/ModelImporter.h Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by lbmas on 4/29/2026.
//
#ifndef B_ENGINE_MODELIMPORTER_H
#define B_ENGINE_MODELIMPORTER_H
#include <string_view>
#include "Model.h"
class ModelImporter
{
public:
static std::vector<Mesh> load_from_file(std::string_view _path, bool zUp = false);
};
#endif //B_ENGINE_MODELIMPORTER_H

View File

@@ -1,23 +0,0 @@
//
// Created by lbmas on 4/29/2026.
//
#ifndef B_ENGINE_MODELLOADER_H
#define B_ENGINE_MODELLOADER_H
#include <memory>
#include <string_view>
#include "Model.h"
#include "uuid.h"
class ModelManager
{
public:
static std::unordered_map<uuids::uuid, std::unique_ptr<Model>> models;
static std::unique_ptr<Model> load_from_file(std::string_view _path, bool zUp = false);
};
#endif //B_ENGINE_MODELLOADER_H

32
src/ResourceHandle.h Normal file
View File

@@ -0,0 +1,32 @@
//
// Created by slinky on 5/17/26.
//
#ifndef B_ENGINE_RESOURCEHANDLE_H
#define B_ENGINE_RESOURCEHANDLE_H
#include "uuid.h"
struct ResourceHandle {
uuids::uuid assetUUID;
uint32_t localId = 0;
bool operator==(const ResourceHandle& other) const {
return assetUUID == other.assetUUID && localId == other.localId;
}
};
namespace std {
template<>
struct hash<ResourceHandle> {
size_t operator()(const ResourceHandle& handle) const noexcept {
// Use the UUID library's native hash function
size_t h1 = std::hash<uuids::uuid>{}(handle.assetUUID);
size_t h2 = std::hash<uint32_t>{}(handle.localId);
return h1 ^ h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
}
};
}
#endif //B_ENGINE_RESOURCEHANDLE_H

View File

@@ -5,6 +5,10 @@
#include "Scene.h" #include "Scene.h"
#include "Components.h" #include "Components.h"
#include "MeshManager.h"
#include "MaterialManager.h"
#include "fmt/format.h" #include "fmt/format.h"
Scene::Scene() { Scene::Scene() {
@@ -84,7 +88,7 @@ void Scene::update_transforms(entt::entity entity) {
} }
} }
void Scene::draw_scene(ShaderProgram *program) { void Scene::draw_scene() {
program->bind(); program->bind();
program->setMat4("projection", activeCamera->projection()); program->setMat4("projection", activeCamera->projection());
program->setMat4("view", activeCamera->view()); program->setMat4("view", activeCamera->view());
@@ -100,39 +104,34 @@ void Scene::draw_scene(ShaderProgram *program) {
break; break;
} }
auto view = _registry.view<Components::Transform, Components::Drawable>(); auto view = _registry.view<Components::Transform, Components::Mesh, Components::Material>();
for (const auto e : view) { for (const auto e : view) {
const auto& transform = view.get<Components::Transform>(e); const auto& transform = view.get<Components::Transform>(e);
program->setMat4("model", transform.model); program->setMat4("model", transform.model);
const auto& drawable = view.get<Components::Drawable>(e); const auto&[meshHandle] = view.get<Components::Mesh>(e);
const Model* model = drawable.model.get(); const auto* mesh = MeshManager::meshes[meshHandle].get();
for (const auto& mesh: drawable.model->meshes) {
unsigned int materialId = mesh->materialId;
if (materialId >= model->materials.size()) {
materialId = 0;
}
const std::shared_ptr<Material> mat = model->materials[materialId]; const auto&[matHandle] = view.get<Components::Material>(e);
const auto* mat = MaterialManager::materials[matHandle].get();
program->setVec3("phongAmbient", mat->phong.ambient); program->setVec3("phongAmbient", mat->phong.ambient);
program->setVec3("phongDiffuse", mat->phong.diffuse); program->setVec3("phongDiffuse", mat->phong.diffuse);
program->setVec3("phongSpecular", mat->phong.specular); program->setVec3("phongSpecular", mat->phong.specular);
program->setFloat("phongShininess", mat->phong.shininess); program->setFloat("phongShininess", mat->phong.shininess);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
const auto diffuse = mat->diffuse; const auto diffuse = mat->diffuse;
diffuse->bind(); diffuse->bind();
program->setInt("diffuseMap", 0); program->setInt("diffuseMap", 0);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
const auto specular = mat->specular; const auto specular = mat->specular;
specular->bind(); specular->bind();
program->setInt("specularMap", 1); program->setInt("specularMap", 1);
glBindVertexArray(mesh.get()->vao); glBindVertexArray(mesh->vao);
glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0);
}
} }
} }

View File

@@ -38,7 +38,7 @@ public:
} }
void update_transforms(); void update_transforms();
void draw_scene(ShaderProgram* program); void draw_scene();
[[nodiscard]] entt::entity get_root() const; [[nodiscard]] entt::entity get_root() const;
[[nodiscard]] std::vector<entt::entity> get_children(entt::entity parent); [[nodiscard]] std::vector<entt::entity> get_children(entt::entity parent);

View File

@@ -14,7 +14,7 @@
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<uuids::uuid, std::unique_ptr<ShaderProgram>> ShaderManager::shaders; std::unordered_map<ResourceHandle, std::unique_ptr<ShaderProgram>> ShaderManager::shaders;
std::unique_ptr<ShaderProgram> ShaderManager::load(std::string_view vertexPath, std::string_view fragmentPath) std::unique_ptr<ShaderProgram> ShaderManager::load(std::string_view vertexPath, std::string_view fragmentPath)
{ {
@@ -68,7 +68,7 @@ std::unique_ptr<ShaderProgram> ShaderManager::load(std::string_view vertexPath,
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
return std::make_shared<ShaderProgram>(ShaderProgram(shaderId)); return std::make_unique<ShaderProgram>(shaderId);
} }
void log_shader_compile_errors(GLuint shader, std::string_view shaderType) void log_shader_compile_errors(GLuint shader, std::string_view shaderType)

View File

@@ -5,16 +5,15 @@
#ifndef B_ENGINE_SHADERMANAGER_H #ifndef B_ENGINE_SHADERMANAGER_H
#define B_ENGINE_SHADERMANAGER_H #define B_ENGINE_SHADERMANAGER_H
#include <string>
#include <string_view> #include <string_view>
#include <memory> #include <memory>
#include "ResourceHandle.h"
#include "ShaderProgram.h" #include "ShaderProgram.h"
#include "uuid.h"
class ShaderManager { class ShaderManager {
public: public:
static std::unordered_map<uuids::uuid, std::unique_ptr<ShaderProgram>> shaders; static std::unordered_map<ResourceHandle, std::unique_ptr<ShaderProgram>> shaders;
static std::unique_ptr<ShaderProgram> load(std::string_view vertexPath, std::string_view fragmentPath); static std::unique_ptr<ShaderProgram> load(std::string_view vertexPath, std::string_view fragmentPath);
}; };

View File

@@ -5,16 +5,14 @@
#include "TextureManager.h" #include "TextureManager.h"
#include "stb_image.h" #include "stb_image.h"
#include "uuid.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "glad/gl.h" #include "glad/gl.h"
std::unordered_map<uuids::uuid, std::unique_ptr<Texture>> TextureManager::textures; std::unordered_map<ResourceHandle, std::unique_ptr<Texture>> TextureManager::textures;
std::unique_ptr<Texture> TextureManager::load_from_file(const std::string_view filePath) std::unique_ptr<Texture> TextureManager::load_from_file(const std::string_view filePath) {
{
int width, height, channels; int width, height, channels;
unsigned char *data = stbi_load(filePath.data(), &width, &height, &channels, 0); unsigned char *data = stbi_load(filePath.data(), &width, &height, &channels, 0);

View File

@@ -5,16 +5,15 @@
#ifndef B_ENGINE_TEXTUREMANAGER_H #ifndef B_ENGINE_TEXTUREMANAGER_H
#define B_ENGINE_TEXTUREMANAGER_H #define B_ENGINE_TEXTUREMANAGER_H
#include <filesystem>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include "ResourceHandle.h"
#include "Texture.h" #include "Texture.h"
#include "uuid.h"
class TextureManager { class TextureManager {
public: public:
static std::unordered_map<uuids::uuid, std::unique_ptr<Texture>> textures; static std::unordered_map<ResourceHandle, std::unique_ptr<Texture>> textures;
static std::unique_ptr<Texture> load_from_file(std::string_view filePath); static std::unique_ptr<Texture> load_from_file(std::string_view filePath);
static std::unique_ptr<Texture> load_from_data(unsigned char* data, int width, int height, int channels); static std::unique_ptr<Texture> load_from_data(unsigned char* data, int width, int height, int channels);

View File

@@ -11,11 +11,9 @@
#include "stb_image.h" #include "stb_image.h"
#include "Camera.h" #include "Camera.h"
#include "Components.h"
#include "EditorContext.h" #include "EditorContext.h"
#include "FreeCameraController.h" #include "FreeCameraController.h"
#include "InputManager.h" #include "InputManager.h"
#include "ModelManager.h"
#include "ProjectManifest.h" #include "ProjectManifest.h"
#include "ProjectManifestFile.h" #include "ProjectManifestFile.h"
#include "Scene.h" #include "Scene.h"
@@ -31,6 +29,8 @@
#include <filesystem> #include <filesystem>
#include "MeshManager.h"
const char* B_ENGINE_VERSION = "v0.0.3"; const char* B_ENGINE_VERSION = "v0.0.3";
GLFWwindow* gWindow = nullptr; GLFWwindow* gWindow = nullptr;
@@ -106,7 +106,7 @@ int main() {
ShaderManager::shaders.clear(); ShaderManager::shaders.clear();
TextureManager::textures.clear(); TextureManager::textures.clear();
ModelManager::models.clear(); MeshManager::meshes.clear();
glfwDestroyWindow(gWindow); glfwDestroyWindow(gWindow);
glfwTerminate(); glfwTerminate();
@@ -212,8 +212,7 @@ void loop() {
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);
// const auto shader = ShaderManager::shaders["phong_shader"]; gScene.draw_scene(shader.get());
// gScene.draw_scene(shader.get());
glBindVertexArray(0); glBindVertexArray(0);
ShaderProgram::unbind(); ShaderProgram::unbind();