more work on asset registry and importing process

This commit is contained in:
2026-05-17 19:12:27 -05:00
parent b3b7688f76
commit e02e92db10
13 changed files with 221 additions and 148 deletions

View File

@@ -7,6 +7,7 @@
#include "glm/ext/matrix_clip_space.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "AssetRegistry.h"
#include "stb_image.h"
#include "Camera.h"
@@ -28,6 +29,8 @@
#include "ui/UISceneGraph.h"
#include "ui/UISceneViewer.h"
#include <filesystem>
const char* B_ENGINE_VERSION = "v0.0.3";
GLFWwindow* gWindow = nullptr;
@@ -43,10 +46,11 @@ float gMouseSensitivity = 0.1f;
std::unique_ptr<ProjectManifest> gCurrentProject {nullptr};
Camera gCamera {};
Scene gScene{};
EditorContext gEditorCtx{};
FreeCameraController gCameraController{};
Camera gCamera {};
FreeCameraController gEditorCameraController{};
void glfw_error_callback(int error, const char* description);
void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
@@ -55,12 +59,8 @@ void glfw_framebuffer_size_callback(GLFWwindow* window, int width, int height);
void init_glfw();
void create_main_window();
void load_gl();
void init_camera();
void init_scene();
void load_default_textures();
void load_shaders();
void load_inputs();
void load_default_models();
void init_editor_camera();
void loop();
void create_entity(entt::entity parent);
@@ -81,14 +81,8 @@ int main() {
stbi_set_flip_vertically_on_load(true);
load_default_textures();
load_shaders();
load_inputs();
load_default_models();
UIManager::init(gWindow);
auto* menuBar = UIManager::add_ui_panel<UIMenuBar>("menu_bar");
menuBar->create_entity = create_entity;
@@ -97,14 +91,11 @@ int main() {
UIManager::add_ui_panel<UISceneGraph>("scene_graph");
UIManager::add_ui_panel<UIEntityInspector>("entity_inspector");
init_camera();
init_scene();
gCameraController.set_camera(gCamera);
gCameraController.look_sensitivity = 0.2;
gCameraController.movement_speed = 0.2;
gEditorCameraController.set_camera(gCamera);
gEditorCameraController.look_sensitivity = 0.2;
gEditorCameraController.movement_speed = 0.2;
InputManager::add_mouse_listener([](float xoff, float yoff) {
gCameraController.on_mouse_delta(xoff, yoff);
gEditorCameraController.on_mouse_delta(xoff, yoff);
});
gEditorCtx.scene = &gScene;
@@ -133,8 +124,7 @@ void glfw_framebuffer_size_callback(GLFWwindow *window, int width, int height) {
gCamera.update_aspect_ratio(gAspectRatio);
}
void init_glfw()
{
void init_glfw() {
if (!glfwInit()) {
spdlog::error("could not initialize glfw");
std::exit(1);
@@ -163,10 +153,6 @@ void create_main_window()
glfwSetCursorPosCallback(gWindow, InputManager::mouse_pos_callback);
glfwSetFramebufferSizeCallback(gWindow, glfw_framebuffer_size_callback);
#ifndef GLFW_CONTEXT_DEBUG
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
#endif
}
void load_gl()
@@ -187,45 +173,12 @@ void load_gl()
glEnable(GL_MULTISAMPLE);
}
void init_camera() {
void init_editor_camera() {
gCamera = {{10.f, 10.f, 10.f}, 0, 0, gAspectRatio};
gCamera.rotate_yaw(-135.f);
gCamera.rotate_pitch(-35.f);
}
void init_scene() {
gScene.set_active_camera(&gCamera);
const auto defaultLight = gScene.create_game_object();
auto& [name] = gScene.fetch_component<Components::Tag>(defaultLight);
name = "Directional Light";
auto& [direction, ambient, diffuse, specular] = gScene.attach_component<Components::DirectionalLight>(defaultLight);
direction = glm::vec3(-1, -1, -1);
ambient = glm::vec3(0.3);
diffuse = glm::vec3(0.7);
specular = glm::vec3(1.0);
}
void load_default_textures() {
spdlog::info("creating default textures");
unsigned char defaultDiffuseData[4] = {static_cast<unsigned char>(255), 255, 255, 255};
TextureManager::textures["default_diffuse"] = TextureManager::load_from_data(reinterpret_cast<unsigned char*>(&defaultDiffuseData), 1, 1, 4);
unsigned char defaultSpecularData[4] = {static_cast<unsigned char>(64), 64, 64, 255};
TextureManager::textures["default_specular"] = TextureManager::load_from_data(reinterpret_cast<unsigned char*>(&defaultSpecularData), 1, 1, 4);
}
void load_shaders() {
spdlog::info("compiling shaders");
ShaderManager::shaders["phong_shader"] = ShaderManager::load("./resources/standard.vert", "./resources/standard.frag");
if (ShaderManager::shaders["phong_shader"] == nullptr)
{
spdlog::error("failed to compile phong shader");
std::exit(1);
}
}
void load_inputs() {
InputManager::generate_input_action("move_forward", {{GLFW_KEY_W, KEY_DOWN}});
InputManager::generate_input_action("move_backward", {{GLFW_KEY_S, KEY_DOWN}});
@@ -235,13 +188,6 @@ void load_inputs() {
InputManager::generate_input_action("move_down", {{GLFW_KEY_LEFT_SHIFT, KEY_DOWN}});
}
void load_default_models() {
spdlog::info("loading default models");
ModelManager::models["cube"] = ModelManager::load_from_file("./resources/cube.obj");
ModelManager::models["vette"] = ModelManager::load_from_file("./resources/c4/C4Fixed.obj");
ModelManager::models["male"] = ModelManager::load_from_file("./resources/male.obj");
}
void loop() {
while (!glfwWindowShouldClose(gWindow)) {
// input
@@ -249,7 +195,7 @@ void loop() {
// update
UIManager::update(gEditorCtx);
gCameraController.update(gEditorCtx);
gEditorCameraController.update(gEditorCtx);
if (gCamera.aspect_ratio() != gEditorCtx.viewportAspectRatio) {
gCamera.update_aspect_ratio(gEditorCtx.viewportAspectRatio);
@@ -266,8 +212,8 @@ void loop() {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const auto shader = ShaderManager::shaders["phong_shader"];
gScene.draw_scene(shader.get());
// const auto shader = ShaderManager::shaders["phong_shader"];
// gScene.draw_scene(shader.get());
glBindVertexArray(0);
ShaderProgram::unbind();
@@ -285,7 +231,12 @@ void create_entity(entt::entity parent) {
}
void open_project(std::string_view _path) {
const std::filesystem::path projectFilePath {_path};
gCurrentProject = ProjectManifestFile::load(_path);
gCurrentProject->projectDirectory = projectFilePath.parent_path();
std::filesystem::path assetPath = gCurrentProject->projectDirectory.append(gCurrentProject->assetFolder);
AssetRegistry::read_directory(assetPath);
const std::string title = fmt::format("project \"{}\" | b_engine {}", gCurrentProject->name, B_ENGINE_VERSION);
glfwSetWindowTitle(gWindow, title.c_str());