2026-04-29 00:17:00 -04:00
|
|
|
#include "glad/gl.h"
|
|
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
|
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
#include "glm/glm.hpp"
|
|
|
|
|
#include "glm/ext/matrix_clip_space.hpp"
|
|
|
|
|
#include "glm/ext/matrix_transform.hpp"
|
|
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
#include "entt/entt.hpp"
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
2026-05-09 23:40:52 -04:00
|
|
|
#include "Components.h"
|
2026-05-04 00:41:34 -04:00
|
|
|
#include "FreeCamera.h"
|
2026-05-11 00:36:17 -04:00
|
|
|
#include "imgui.h"
|
|
|
|
|
#include "imgui_impl_glfw.h"
|
|
|
|
|
#include "imgui_impl_opengl3.h"
|
2026-05-04 00:41:34 -04:00
|
|
|
#include "InputManager.h"
|
2026-05-01 20:06:54 -05:00
|
|
|
#include "stb_image.h"
|
|
|
|
|
|
|
|
|
|
#include "Model.h"
|
2026-05-04 00:41:34 -04:00
|
|
|
#include "ModelManager.h"
|
2026-05-09 23:40:52 -04:00
|
|
|
#include "Scene.h"
|
2026-05-03 12:43:39 -04:00
|
|
|
#include "ShaderManager.h"
|
2026-05-01 20:06:54 -05:00
|
|
|
#include "ShaderProgram.h"
|
|
|
|
|
#include "Texture.h"
|
2026-05-04 00:41:34 -04:00
|
|
|
#include "TextureManager.h"
|
2026-05-01 20:06:54 -05:00
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
#include "fmt/base.h"
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
GLFWwindow* gWindow = nullptr;
|
2026-05-11 00:36:17 -04:00
|
|
|
int gWindowWidth = 1920;
|
|
|
|
|
int gWindowHeight = 1080;
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
int glVersionMajor = 0;
|
|
|
|
|
int glVersionMinor = 0;
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
float gAspectRatio = 0.f;
|
|
|
|
|
|
|
|
|
|
float gMouseSensitivity = 0.1f;
|
|
|
|
|
|
|
|
|
|
FreeCamera gCamera {};
|
2026-05-01 20:06:54 -05:00
|
|
|
|
2026-05-09 23:40:52 -04:00
|
|
|
Scene gScene{};
|
2026-05-08 23:44:20 -04:00
|
|
|
|
2026-04-29 00:17:00 -04:00
|
|
|
void glfw_error_callback(int error, const char* description);
|
|
|
|
|
void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
|
|
|
|
void glfw_framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
void init_glfw();
|
|
|
|
|
void create_main_window();
|
|
|
|
|
void load_gl();
|
2026-05-11 00:36:17 -04:00
|
|
|
void load_imgui();
|
2026-05-01 20:06:54 -05:00
|
|
|
void init_camera();
|
2026-05-04 00:41:34 -04:00
|
|
|
void load_default_textures();
|
|
|
|
|
void load_shaders();
|
2026-05-08 23:44:20 -04:00
|
|
|
void load_inputs();
|
|
|
|
|
void load_default_models();
|
2026-05-02 21:24:21 -04:00
|
|
|
void loop();
|
2026-05-11 00:36:17 -04:00
|
|
|
void draw_ui();
|
2026-05-01 20:06:54 -05:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
auto xAxis = glm::vec3{1.f, 0.f, 0.f};
|
|
|
|
|
auto yAxis = glm::vec3{0.f, 1.f, 0.f};
|
|
|
|
|
auto zAxis = glm::vec3{0.f, 0.f, 1.f};
|
|
|
|
|
|
2026-04-29 00:17:00 -04:00
|
|
|
int main() {
|
2026-05-09 23:40:52 -04:00
|
|
|
spdlog::info("b_engine v0.0.3 start");
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
init_glfw();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
create_main_window();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
load_gl();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
load_imgui();
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
init_camera();
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
stbi_set_flip_vertically_on_load(true);
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
load_default_textures();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
load_shaders();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
load_inputs();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
load_default_models();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-09 23:40:52 -04:00
|
|
|
entt::entity dirLight = gScene.create_game_object();
|
|
|
|
|
auto& [direction, ambient, diffuse, specular] = gScene.attach_component<Components::DirectionalLight>(dirLight);
|
|
|
|
|
direction = {1, -1, 1};
|
|
|
|
|
ambient = {0.3f, 0.3f, 0.3f};
|
|
|
|
|
diffuse = {0.5f, 0.5f, 0.5f};
|
|
|
|
|
specular = {1.0f, 1.0f, 1.0f};
|
|
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
auto& tag = gScene.fetch_component<Components::Tag>(dirLight);
|
|
|
|
|
tag.name = "directional light";
|
|
|
|
|
|
2026-05-02 21:24:21 -04:00
|
|
|
loop();
|
2026-04-29 00:17:00 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
ShaderManager::shaders.clear();
|
|
|
|
|
TextureManager::textures.clear();
|
|
|
|
|
ModelManager::models.clear();
|
|
|
|
|
|
2026-04-29 00:17:00 -04:00
|
|
|
glfwDestroyWindow(gWindow);
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glfw_error_callback(int error, const char* description) {
|
|
|
|
|
spdlog::error("glfw error: {}", description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glfw_framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
|
|
|
glViewport(0, 0, width, height);
|
2026-05-04 00:41:34 -04:00
|
|
|
gAspectRatio = (float)width / (float)height;
|
|
|
|
|
gCamera.update_aspect_ratio(gAspectRatio);
|
2026-04-29 00:17:00 -04:00
|
|
|
}
|
2026-05-01 20:06:54 -05:00
|
|
|
|
|
|
|
|
void init_glfw()
|
|
|
|
|
{
|
|
|
|
|
if (!glfwInit()) {
|
|
|
|
|
spdlog::error("could not initialize glfw");
|
|
|
|
|
std::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
2026-05-04 00:41:34 -04:00
|
|
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
2026-05-01 20:06:54 -05:00
|
|
|
|
|
|
|
|
glfwSetErrorCallback(glfw_error_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void create_main_window()
|
|
|
|
|
{
|
2026-05-09 23:40:52 -04:00
|
|
|
gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, "b_engine v0.0.3", nullptr, nullptr);
|
2026-05-01 20:06:54 -05:00
|
|
|
if (!gWindow) {
|
|
|
|
|
spdlog::error("failed to create glfw window");
|
|
|
|
|
std::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(gWindow);
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
glfwSetKeyCallback(gWindow, InputManager::key_callback);
|
|
|
|
|
glfwSetCursorPosCallback(gWindow, InputManager::mouse_pos_callback);
|
|
|
|
|
|
2026-05-01 20:06:54 -05:00
|
|
|
glfwSetFramebufferSizeCallback(gWindow, glfw_framebuffer_size_callback);
|
2026-05-04 00:41:34 -04:00
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
#ifndef GLFW_CONTEXT_DEBUG
|
2026-05-04 00:41:34 -04:00
|
|
|
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2026-05-08 23:44:20 -04:00
|
|
|
#endif
|
2026-05-01 20:06:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void load_gl()
|
|
|
|
|
{
|
|
|
|
|
int version = gladLoadGL(glfwGetProcAddress);
|
|
|
|
|
glVersionMajor = GLAD_VERSION_MAJOR(version);
|
|
|
|
|
glVersionMinor = GLAD_VERSION_MINOR(version);
|
|
|
|
|
spdlog::info("gl version {}.{}", glVersionMajor, glVersionMinor);
|
|
|
|
|
|
|
|
|
|
if (gWindow)
|
|
|
|
|
{
|
|
|
|
|
int fbWidth, fbHeight;
|
|
|
|
|
glfwGetFramebufferSize(gWindow, &fbWidth, &fbHeight);
|
2026-05-04 00:41:34 -04:00
|
|
|
gAspectRatio = (float)fbWidth / (float)fbHeight;
|
2026-05-01 20:06:54 -05:00
|
|
|
}
|
2026-05-03 00:08:13 -05:00
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2026-05-04 00:41:34 -04:00
|
|
|
glEnable(GL_MULTISAMPLE);
|
2026-05-01 20:06:54 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
void load_imgui() {
|
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
|
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(gWindow, true);
|
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 330");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
void init_camera() {
|
|
|
|
|
gCamera = {{5.f, 5.f, 5.f}, 0, 0, gAspectRatio};
|
|
|
|
|
gCamera.rotate_yaw(-135.f);
|
|
|
|
|
gCamera.rotate_pitch(-35.f);
|
|
|
|
|
|
|
|
|
|
InputManager::add_mouse_listener([](float xoff, float yoff) {
|
|
|
|
|
gCamera.rotate_yaw(xoff * gMouseSensitivity);
|
|
|
|
|
gCamera.rotate_pitch(yoff * gMouseSensitivity);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-01 20:06:54 -05:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
void load_default_textures() {
|
|
|
|
|
spdlog::info("creating default textures");
|
2026-05-09 23:40:52 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
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);
|
2026-05-03 12:43:39 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
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);
|
|
|
|
|
}
|
2026-05-01 20:06:54 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
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}});
|
|
|
|
|
InputManager::generate_input_action("move_right", {{GLFW_KEY_D, KEY_DOWN}});
|
|
|
|
|
InputManager::generate_input_action("move_left", {{GLFW_KEY_A, KEY_DOWN}});
|
|
|
|
|
InputManager::generate_input_action("move_up", {{GLFW_KEY_SPACE, KEY_DOWN}});
|
|
|
|
|
InputManager::generate_input_action("move_down", {{GLFW_KEY_LEFT_SHIFT, KEY_DOWN}});
|
|
|
|
|
InputManager::generate_input_action("exit_application", {{GLFW_KEY_ESCAPE, 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");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 21:24:21 -04:00
|
|
|
void loop() {
|
|
|
|
|
while (!glfwWindowShouldClose(gWindow)) {
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
// check inputs
|
|
|
|
|
if (InputManager::check_action_performed("exit_application")) {
|
|
|
|
|
glfwSetWindowShouldClose(gWindow, true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-05-02 21:24:21 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
if (InputManager::check_action_performed("move_forward")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(FORWARD, .05);
|
2026-05-04 00:41:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InputManager::check_action_performed("move_backward")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(BACKWARD, .05);
|
2026-05-04 00:41:34 -04:00
|
|
|
}
|
2026-05-03 12:43:39 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
if (InputManager::check_action_performed("move_left")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(LEFT, .05);
|
2026-05-04 00:41:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InputManager::check_action_performed("move_right")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(RIGHT, .05);
|
2026-05-04 00:41:34 -04:00
|
|
|
}
|
2026-05-02 21:24:21 -04:00
|
|
|
|
2026-05-08 23:44:20 -04:00
|
|
|
if (InputManager::check_action_performed("move_up")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(UP, .05);
|
2026-05-08 23:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InputManager::check_action_performed("move_down")) {
|
2026-05-09 23:40:52 -04:00
|
|
|
gCamera.move(DOWN, .05);
|
2026-05-08 23:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
draw_ui();
|
|
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
// gl frame prep
|
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
2026-05-11 00:36:17 -04:00
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
|
2026-05-09 23:40:52 -04:00
|
|
|
const auto shader = ShaderManager::shaders["phong_shader"];
|
|
|
|
|
shader->bind();
|
|
|
|
|
shader->setMat4("projection", gCamera.projection());
|
|
|
|
|
shader->setMat4("view", gCamera.view());
|
|
|
|
|
shader->setVec3("viewPosition", gCamera.position());
|
|
|
|
|
|
|
|
|
|
gScene.draw_scene(shader.get());
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
ShaderProgram::unbind();
|
2026-05-02 21:24:21 -04:00
|
|
|
|
2026-05-04 00:41:34 -04:00
|
|
|
// gl end frame stuff
|
2026-05-02 21:24:21 -04:00
|
|
|
glfwSwapBuffers(gWindow);
|
|
|
|
|
}
|
2026-05-11 00:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void draw_ui() {
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
{
|
|
|
|
|
if (ImGui::Begin("Scene")) {
|
|
|
|
|
ImGui::Text("Scene");
|
|
|
|
|
const auto root = gScene.get_root();
|
|
|
|
|
auto entity = gScene.fetch_component<Components::Relationship>(root).first_child;
|
|
|
|
|
while (entity != entt::null) {
|
|
|
|
|
const auto&[name] = gScene.fetch_component<Components::Tag>(entity);
|
|
|
|
|
std::string text = fmt::format(" {0}", name);
|
|
|
|
|
ImGui::Text((char*)text.data());
|
|
|
|
|
entity = gScene.fetch_component<Components::Relationship>(entity).next_sibling;
|
|
|
|
|
}
|
|
|
|
|
ImGui::End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImGui::BeginMainMenuBar()) {
|
|
|
|
|
if (ImGui::BeginMenu("File")) {
|
|
|
|
|
if (ImGui::MenuItem("New Scene", "Ctrl+N")) {
|
|
|
|
|
/* Handle New File logic */
|
|
|
|
|
}
|
|
|
|
|
if (ImGui::MenuItem("Open Scene", "Ctrl+O")) {
|
|
|
|
|
/* Handle Open logic */
|
|
|
|
|
}
|
|
|
|
|
ImGui::Separator(); // Adds a visual line between sections
|
|
|
|
|
if (ImGui::MenuItem("Save", "Ctrl+S")) {
|
|
|
|
|
/* Handle Save logic */
|
|
|
|
|
}
|
|
|
|
|
if (ImGui::MenuItem("Exit", "Alt+F4")) {
|
|
|
|
|
/* Handle Exit logic */
|
|
|
|
|
}
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImGui::BeginMenu("Scene")) {
|
|
|
|
|
if (ImGui::MenuItem("New Scene Object")) {
|
|
|
|
|
gScene.create_game_object();
|
|
|
|
|
// Do other stuff here, open this entity in the entity editor, etc...
|
|
|
|
|
}
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
|
}
|
|
|
|
|
ImGui::EndMainMenuBar();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImGui::Render();
|
2026-05-01 20:06:54 -05:00
|
|
|
}
|