diff --git a/src/Components.h b/src/Components.h index 7e7858a..a83cc63 100644 --- a/src/Components.h +++ b/src/Components.h @@ -5,18 +5,18 @@ #ifndef B_ENGINE_COMPONENTS_H #define B_ENGINE_COMPONENTS_H -#include - #include "Model.h" #include "entt/entt.hpp" #include "glm/glm.hpp" +#include "glm/ext/matrix_transform.hpp" namespace Components { struct Transform { - glm::vec3 position; - glm::vec3 rotation; - glm::vec3 scale; + glm::vec3 position{}; + glm::vec3 rotation{}; + glm::vec3 scale{}; + glm::mat4 model = glm::identity(); }; struct Relationship { @@ -27,7 +27,14 @@ namespace Components { }; struct Drawable { - std::shared_ptr model; + std::shared_ptr model {nullptr}; + }; + + struct DirectionalLight { + glm::vec3 direction{}; + glm::vec3 ambient{}; + glm::vec3 diffuse{}; + glm::vec3 specular{}; }; } diff --git a/src/Scene.cpp b/src/Scene.cpp index 13fd1d2..6c8b7e3 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -26,19 +26,45 @@ entt::entity Scene::create_game_object(entt::entity parent) { return entity; } -template -T& Scene::attach_component(entt::entity e) { - return _registry.emplace(e); +void Scene::update_transforms() { + } -template -T &Scene::attach_component(entt::entity e, T data) { - return _registry.emplace(e, data); -} +void Scene::draw_scene(ShaderProgram *shader) { + auto view = _registry.view(); + for (auto e : view) { + const auto& transform = view.get(e); + shader->setMat4("model", transform.model); -template -T& Scene::fetch_component(entt::entity e) { - return _registry.get(e); + const auto& drawable = view.get(e); + const Model* model = drawable.model.get(); + for (const auto& mesh: drawable.model->meshes) { + unsigned int materialId = mesh->materialId; + if (materialId >= model->materials.size()) { + materialId = 0; + } + + const std::shared_ptr mat = model->materials[materialId]; + + shader->setVec3("phongAmbient", mat->phong.ambient); + shader->setVec3("phongDiffuse", mat->phong.diffuse); + shader->setVec3("phongSpecular", mat->phong.specular); + shader->setFloat("phongShininess", mat->phong.shininess); + + glActiveTexture(GL_TEXTURE0); + const auto diffuse = mat->diffuse; + diffuse->bind(); + shader->setInt("diffuseMap", 0); + + glActiveTexture(GL_TEXTURE1); + const auto specular = mat->specular; + specular->bind(); + shader->setInt("specularMap", 1); + + glBindVertexArray(mesh.get()->vao); + glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0); + } + } } void Scene::add_child(entt::entity parent, entt::entity child) { diff --git a/src/Scene.h b/src/Scene.h index edf7f25..309d239 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -5,13 +5,9 @@ #ifndef B_ENGINE_SCENE_H #define B_ENGINE_SCENE_H +#include "ShaderProgram.h" #include "entt/entt.hpp" -struct SceneObject { - entt::entity e; - std::vector children; -}; - class Scene { public: Scene(); @@ -21,17 +17,25 @@ public: entt::entity create_game_object(entt::entity parent); template - T& attach_component(entt::entity e); - template - T& attach_component(entt::entity e, T data); + T& attach_component(entt::entity e) { + return _registry.emplace(e); + } template - T& fetch_component(entt::entity e); + T& attach_component(entt::entity e, T data) { + return _registry.emplace(e, data); + } + template + T& fetch_component(entt::entity e) { + return _registry.get(e); + } + void update_transforms(); + void draw_scene(ShaderProgram* program); private: entt::registry _registry{}; - entt::entity root; + entt::entity root{entt::null}; void add_child(entt::entity parent, entt::entity child); }; diff --git a/src/main.cpp b/src/main.cpp index de09f2b..94b0f00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,12 +10,14 @@ #include "entt/entt.hpp" #define STB_IMAGE_IMPLEMENTATION +#include "Components.h" #include "FreeCamera.h" #include "InputManager.h" #include "stb_image.h" #include "Model.h" #include "ModelManager.h" +#include "Scene.h" #include "ShaderManager.h" #include "ShaderProgram.h" #include "Texture.h" @@ -34,7 +36,7 @@ float gMouseSensitivity = 0.1f; FreeCamera gCamera {}; -entt::registry gRegistry{}; +Scene gScene{}; void glfw_error_callback(int error, const char* description); void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); @@ -50,14 +52,12 @@ void load_inputs(); void load_default_models(); void loop(); -std::shared_ptr load_model(std::string_view _path); - 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}; int main() { - spdlog::info("b_engine start"); + spdlog::info("b_engine v0.0.3 start"); init_glfw(); @@ -77,6 +77,13 @@ int main() { load_default_models(); + entt::entity dirLight = gScene.create_game_object(); + auto& [direction, ambient, diffuse, specular] = gScene.attach_component(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}; + loop(); ShaderManager::shaders.clear(); @@ -116,7 +123,7 @@ void init_glfw() void create_main_window() { - gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, "b_engine v0.0.2", nullptr, nullptr); + gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, "b_engine v0.0.3", nullptr, nullptr); if (!gWindow) { spdlog::error("failed to create glfw window"); std::exit(1); @@ -165,6 +172,7 @@ void init_camera() { void load_default_textures() { spdlog::info("creating default textures"); + unsigned char defaultDiffuseData[4] = {static_cast(255), 255, 255, 255}; TextureManager::textures["default_diffuse"] = TextureManager::load_from_data(reinterpret_cast(&defaultDiffuseData), 1, 1, 4); @@ -200,16 +208,6 @@ void load_default_models() { } void loop() { - std::shared_ptr defaultDiffuse = TextureManager::textures["default_diffuse"]; - std::shared_ptr defaultSpecular = TextureManager::textures["default_specular"]; - std::shared_ptr shader = ShaderManager::shaders["phong_shader"]; - - const auto vette = ModelManager::models["vette"]; - auto model = glm::identity(); - model = glm::scale(model, glm::vec3(.1f, .1f, .1f)); - - const auto& activeModel = vette; - while (!glfwWindowShouldClose(gWindow)) { glfwPollEvents(); @@ -220,82 +218,43 @@ void loop() { } if (InputManager::check_action_performed("move_forward")) { - gCamera.move(CAMERA_MOVEMENT::FORWARD, .05); + gCamera.move(FORWARD, .05); } if (InputManager::check_action_performed("move_backward")) { - gCamera.move(CAMERA_MOVEMENT::BACKWARD, .05); + gCamera.move(BACKWARD, .05); } if (InputManager::check_action_performed("move_left")) { - gCamera.move(CAMERA_MOVEMENT::LEFT, .05); + gCamera.move(LEFT, .05); } if (InputManager::check_action_performed("move_right")) { - gCamera.move(CAMERA_MOVEMENT::RIGHT, .05); + gCamera.move(RIGHT, .05); } if (InputManager::check_action_performed("move_up")) { - gCamera.move(CAMERA_MOVEMENT::UP, .05); + gCamera.move(UP, .05); } if (InputManager::check_action_performed("move_down")) { - gCamera.move(CAMERA_MOVEMENT::DOWN, .05); + gCamera.move(DOWN, .05); } // gl frame prep glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // draw the stuff - if (shader && activeModel) { - shader->bind(); - shader->setMat4("projection", gCamera.projection()); - shader->setMat4("view", gCamera.view()); - shader->setMat4("model", model); - shader->setVec3("viewPosition", gCamera.position()); - shader->setVec3("lightPosition", glm::vec3{-2.f, 0, 2.0f}); - shader->setVec3("lightDirection", glm::vec3(1, -1, 1)); - shader->setVec3("lightAmbient", glm::vec3(0.3f, 0.3f, 0.3f)); - shader->setVec3("lightDiffuse", glm::vec3(0.5f, 0.5f, 0.5f)); - shader->setVec3("lightSpecular", glm::vec3(1.0f, 1.0f, 1.0f)); + const auto shader = ShaderManager::shaders["phong_shader"]; + shader->bind(); + shader->setMat4("projection", gCamera.projection()); + shader->setMat4("view", gCamera.view()); + shader->setVec3("viewPosition", gCamera.position()); - /*for (const auto& mesh: activeModel->meshes) { - unsigned int materialId = 0; - if (materialId >= activeModel->materials.size()) { - materialId = 0; - } + gScene.draw_scene(shader.get()); - const std::shared_ptr mat = activeModel->materials[materialId]; - - shader->setVec3("phongAmbient", mat->phong.ambient); - shader->setVec3("phongDiffuse", mat->phong.diffuse); - shader->setVec3("phongSpecular", mat->phong.specular); - shader->setFloat("phongShininess", mat->phong.shininess); - - glActiveTexture(GL_TEXTURE0); - auto diffuse = mat->diffuse; - if (!diffuse) { - diffuse = defaultDiffuse; - } - diffuse->bind(); - shader->setInt("diffuseMap", 0); - - glActiveTexture(GL_TEXTURE1); - auto specular = mat->specular; - if (!specular) { - specular = defaultSpecular; - } - specular->bind(); - shader->setInt("specularMap", 1); - - glBindVertexArray(mesh.get()->vao); - glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0); - }*/ - - glBindVertexArray(0); - ShaderProgram::unbind(); - } + glBindVertexArray(0); + ShaderProgram::unbind(); // gl end frame stuff glfwSwapBuffers(gWindow);