more scene work
This commit is contained in:
@@ -5,18 +5,18 @@
|
||||
#ifndef B_ENGINE_COMPONENTS_H
|
||||
#define B_ENGINE_COMPONENTS_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#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<glm::mat4>();
|
||||
};
|
||||
|
||||
struct Relationship {
|
||||
@@ -27,7 +27,14 @@ namespace Components {
|
||||
};
|
||||
|
||||
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{};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -26,19 +26,45 @@ entt::entity Scene::create_game_object(entt::entity parent) {
|
||||
return entity;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& Scene::attach_component(entt::entity e) {
|
||||
return _registry.emplace<T>(e);
|
||||
void Scene::update_transforms() {
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T &Scene::attach_component(entt::entity e, T data) {
|
||||
return _registry.emplace<T>(e, data);
|
||||
void Scene::draw_scene(ShaderProgram *shader) {
|
||||
auto view = _registry.view<Components::Transform, Components::Drawable>();
|
||||
for (auto e : view) {
|
||||
const auto& transform = view.get<Components::Transform>(e);
|
||||
shader->setMat4("model", transform.model);
|
||||
|
||||
const auto& drawable = view.get<Components::Drawable>(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;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& Scene::fetch_component(entt::entity e) {
|
||||
return _registry.get<T>(e);
|
||||
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) {
|
||||
|
||||
24
src/Scene.h
24
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<SceneObject*> children;
|
||||
};
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene();
|
||||
@@ -21,17 +17,25 @@ public:
|
||||
entt::entity create_game_object(entt::entity parent);
|
||||
|
||||
template<typename T>
|
||||
T& attach_component(entt::entity e);
|
||||
template<typename T>
|
||||
T& attach_component(entt::entity e, T data);
|
||||
T& attach_component(entt::entity e) {
|
||||
return _registry.emplace<T>(e);
|
||||
}
|
||||
|
||||
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:
|
||||
entt::registry _registry{};
|
||||
entt::entity root;
|
||||
entt::entity root{entt::null};
|
||||
|
||||
void add_child(entt::entity parent, entt::entity child);
|
||||
};
|
||||
|
||||
83
src/main.cpp
83
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<Model> 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<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();
|
||||
|
||||
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<unsigned char>(255), 255, 255, 255};
|
||||
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() {
|
||||
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)) {
|
||||
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) {
|
||||
const auto shader = ShaderManager::shaders["phong_shader"];
|
||||
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));
|
||||
|
||||
/*for (const auto& mesh: activeModel->meshes) {
|
||||
unsigned int materialId = 0;
|
||||
if (materialId >= activeModel->materials.size()) {
|
||||
materialId = 0;
|
||||
}
|
||||
|
||||
const std::shared_ptr<Material> 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);
|
||||
}*/
|
||||
gScene.draw_scene(shader.get());
|
||||
|
||||
glBindVertexArray(0);
|
||||
ShaderProgram::unbind();
|
||||
}
|
||||
|
||||
// gl end frame stuff
|
||||
glfwSwapBuffers(gWindow);
|
||||
|
||||
Reference in New Issue
Block a user