more scene work

This commit is contained in:
2026-05-09 23:40:52 -04:00
parent c54ca2adc5
commit 0b495e1e40
4 changed files with 90 additions and 94 deletions

View File

@@ -5,18 +5,18 @@
#ifndef B_ENGINE_COMPONENTS_H #ifndef B_ENGINE_COMPONENTS_H
#define B_ENGINE_COMPONENTS_H #define B_ENGINE_COMPONENTS_H
#include <vector>
#include "Model.h" #include "Model.h"
#include "entt/entt.hpp" #include "entt/entt.hpp"
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/ext/matrix_transform.hpp"
namespace Components { namespace Components {
struct Transform { struct Transform {
glm::vec3 position; glm::vec3 position{};
glm::vec3 rotation; glm::vec3 rotation{};
glm::vec3 scale; glm::vec3 scale{};
glm::mat4 model = glm::identity<glm::mat4>();
}; };
struct Relationship { struct Relationship {
@@ -27,7 +27,14 @@ namespace Components {
}; };
struct Drawable { struct Drawable {
std::shared_ptr<Model> model; std::shared_ptr<Model> model {nullptr};
};
struct DirectionalLight {
glm::vec3 direction{};
glm::vec3 ambient{};
glm::vec3 diffuse{};
glm::vec3 specular{};
}; };
} }

View File

@@ -26,19 +26,45 @@ entt::entity Scene::create_game_object(entt::entity parent) {
return entity; return entity;
} }
template<typename T> void Scene::update_transforms() {
T& Scene::attach_component(entt::entity e) {
return _registry.emplace<T>(e);
} }
template<typename T> void Scene::draw_scene(ShaderProgram *shader) {
T &Scene::attach_component(entt::entity e, T data) { auto view = _registry.view<Components::Transform, Components::Drawable>();
return _registry.emplace<T>(e, data); for (auto e : view) {
} const auto& transform = view.get<Components::Transform>(e);
shader->setMat4("model", transform.model);
template<typename T> const auto& drawable = view.get<Components::Drawable>(e);
T& Scene::fetch_component(entt::entity e) { const Model* model = drawable.model.get();
return _registry.get<T>(e); 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];
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) { void Scene::add_child(entt::entity parent, entt::entity child) {

View File

@@ -5,13 +5,9 @@
#ifndef B_ENGINE_SCENE_H #ifndef B_ENGINE_SCENE_H
#define B_ENGINE_SCENE_H #define B_ENGINE_SCENE_H
#include "ShaderProgram.h"
#include "entt/entt.hpp" #include "entt/entt.hpp"
struct SceneObject {
entt::entity e;
std::vector<SceneObject*> children;
};
class Scene { class Scene {
public: public:
Scene(); Scene();
@@ -21,17 +17,25 @@ public:
entt::entity create_game_object(entt::entity parent); entt::entity create_game_object(entt::entity parent);
template<typename T> template<typename T>
T& attach_component(entt::entity e); T& attach_component(entt::entity e) {
template<typename T> return _registry.emplace<T>(e);
T& attach_component(entt::entity e, T data); }
template<typename T> template<typename T>
T& fetch_component(entt::entity e); T& attach_component(entt::entity e, T data) {
return _registry.emplace<T>(e, data);
}
template<typename T>
T& fetch_component(entt::entity e) {
return _registry.get<T>(e);
}
void update_transforms();
void draw_scene(ShaderProgram* program);
private: private:
entt::registry _registry{}; entt::registry _registry{};
entt::entity root; entt::entity root{entt::null};
void add_child(entt::entity parent, entt::entity child); void add_child(entt::entity parent, entt::entity child);
}; };

View File

@@ -10,12 +10,14 @@
#include "entt/entt.hpp" #include "entt/entt.hpp"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "Components.h"
#include "FreeCamera.h" #include "FreeCamera.h"
#include "InputManager.h" #include "InputManager.h"
#include "stb_image.h" #include "stb_image.h"
#include "Model.h" #include "Model.h"
#include "ModelManager.h" #include "ModelManager.h"
#include "Scene.h"
#include "ShaderManager.h" #include "ShaderManager.h"
#include "ShaderProgram.h" #include "ShaderProgram.h"
#include "Texture.h" #include "Texture.h"
@@ -34,7 +36,7 @@ float gMouseSensitivity = 0.1f;
FreeCamera gCamera {}; FreeCamera gCamera {};
entt::registry gRegistry{}; Scene gScene{};
void glfw_error_callback(int error, const char* description); 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_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
@@ -50,14 +52,12 @@ void load_inputs();
void load_default_models(); void load_default_models();
void loop(); void loop();
std::shared_ptr<Model> load_model(std::string_view _path);
auto xAxis = glm::vec3{1.f, 0.f, 0.f}; auto xAxis = glm::vec3{1.f, 0.f, 0.f};
auto yAxis = glm::vec3{0.f, 1.f, 0.f}; auto yAxis = glm::vec3{0.f, 1.f, 0.f};
auto zAxis = glm::vec3{0.f, 0.f, 1.f}; auto zAxis = glm::vec3{0.f, 0.f, 1.f};
int main() { int main() {
spdlog::info("b_engine start"); spdlog::info("b_engine v0.0.3 start");
init_glfw(); init_glfw();
@@ -77,6 +77,13 @@ int main() {
load_default_models(); load_default_models();
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};
loop(); loop();
ShaderManager::shaders.clear(); ShaderManager::shaders.clear();
@@ -116,7 +123,7 @@ void init_glfw()
void create_main_window() 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) { if (!gWindow) {
spdlog::error("failed to create glfw window"); spdlog::error("failed to create glfw window");
std::exit(1); std::exit(1);
@@ -165,6 +172,7 @@ void init_camera() {
void load_default_textures() { void load_default_textures() {
spdlog::info("creating default textures"); spdlog::info("creating default textures");
unsigned char defaultDiffuseData[4] = {static_cast<unsigned char>(255), 255, 255, 255}; 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); TextureManager::textures["default_diffuse"] = TextureManager::load_from_data(reinterpret_cast<unsigned char*>(&defaultDiffuseData), 1, 1, 4);
@@ -200,16 +208,6 @@ void load_default_models() {
} }
void loop() { void loop() {
std::shared_ptr<Texture> defaultDiffuse = TextureManager::textures["default_diffuse"];
std::shared_ptr<Texture> defaultSpecular = TextureManager::textures["default_specular"];
std::shared_ptr<ShaderProgram> shader = ShaderManager::shaders["phong_shader"];
const auto vette = ModelManager::models["vette"];
auto model = glm::identity<glm::mat4>();
model = glm::scale(model, glm::vec3(.1f, .1f, .1f));
const auto& activeModel = vette;
while (!glfwWindowShouldClose(gWindow)) { while (!glfwWindowShouldClose(gWindow)) {
glfwPollEvents(); glfwPollEvents();
@@ -220,82 +218,43 @@ void loop() {
} }
if (InputManager::check_action_performed("move_forward")) { if (InputManager::check_action_performed("move_forward")) {
gCamera.move(CAMERA_MOVEMENT::FORWARD, .05); gCamera.move(FORWARD, .05);
} }
if (InputManager::check_action_performed("move_backward")) { if (InputManager::check_action_performed("move_backward")) {
gCamera.move(CAMERA_MOVEMENT::BACKWARD, .05); gCamera.move(BACKWARD, .05);
} }
if (InputManager::check_action_performed("move_left")) { if (InputManager::check_action_performed("move_left")) {
gCamera.move(CAMERA_MOVEMENT::LEFT, .05); gCamera.move(LEFT, .05);
} }
if (InputManager::check_action_performed("move_right")) { if (InputManager::check_action_performed("move_right")) {
gCamera.move(CAMERA_MOVEMENT::RIGHT, .05); gCamera.move(RIGHT, .05);
} }
if (InputManager::check_action_performed("move_up")) { if (InputManager::check_action_performed("move_up")) {
gCamera.move(CAMERA_MOVEMENT::UP, .05); gCamera.move(UP, .05);
} }
if (InputManager::check_action_performed("move_down")) { if (InputManager::check_action_performed("move_down")) {
gCamera.move(CAMERA_MOVEMENT::DOWN, .05); gCamera.move(DOWN, .05);
} }
// gl frame prep // gl frame prep
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);
// draw the stuff const auto shader = ShaderManager::shaders["phong_shader"];
if (shader && activeModel) { shader->bind();
shader->bind(); shader->setMat4("projection", gCamera.projection());
shader->setMat4("projection", gCamera.projection()); shader->setMat4("view", gCamera.view());
shader->setMat4("view", gCamera.view()); shader->setVec3("viewPosition", gCamera.position());
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));
/*for (const auto& mesh: activeModel->meshes) { gScene.draw_scene(shader.get());
unsigned int materialId = 0;
if (materialId >= activeModel->materials.size()) {
materialId = 0;
}
const std::shared_ptr<Material> mat = activeModel->materials[materialId]; glBindVertexArray(0);
ShaderProgram::unbind();
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();
}
// gl end frame stuff // gl end frame stuff
glfwSwapBuffers(gWindow); glfwSwapBuffers(gWindow);