Basic Entity Components implementation

This commit is contained in:
Nigel Barink 2022-10-23 00:14:47 +02:00
parent b359a940ba
commit 7458254b2d
40 changed files with 160 additions and 344 deletions

View File

@ -10,7 +10,7 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "../Scene/SceneNodeTypes.h"
#include "../Scene/TransformTree/SceneNodeTypes.h"
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_vertices);

View File

@ -6,7 +6,6 @@ int main(int argc, char* argv[]) {
// Setup performance sampler
PerfomanceSamplerInit();
// Startup services
BarinkWindow MainWindow = BarinkWindow(800, 600);
@ -14,27 +13,19 @@ int main(int argc, char* argv[]) {
InputSystem = BarinkEngine::InputManager();
ES = EngineStatistics();
InputSystem.attach(&MainWindow);
GUIManager GUISystem = GUIManager(&MainWindow);
glEnable(GL_DEPTH_TEST);
// First call to setup game
Start();
// Runtime loop
while (!MainWindow.WindowShouldClose()) {
SamplePerformance();
// Execute main logic
InputSystem.PollEvents();
@ -47,11 +38,9 @@ int main(int argc, char* argv[]) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
// Shutdown game
Stop();
// Shutdown Services
return 0;

View File

@ -1,8 +1,8 @@
#pragma once
#include "glm/glm.hpp"
#include "graphics/Shader.h"
#include "graphics/Primitives/Shader.h"
#include "Platform/Window.h"
#include "graphics/Texture.h"
#include "graphics/Primitives/Texture.h"
#include "graphics/Primitives/Camera.h"
#include "graphics/Renderable.h"
#include "Graphics/Renderer.h"
@ -13,7 +13,7 @@
#include "Input/InputManager.h"
#include "Graphics/Renderer.h"
#include "GUI/GUIManager.h"
#include "Scene.h"
#include "Scene/Scene.h"
#include "PerfCounter.h"

View File

@ -1,6 +1,6 @@
#pragma once
#include "VertexArray.h"
#include "Buffer.h"
#include "Memory/VertexArray.h"
#include "Memory/Buffer.h"
#include "Primitives/Mesh.h"
#include "Primitives/Material.h"
#include <glm/glm.hpp>

View File

@ -1,7 +1,7 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include "../Shader.h"
#include "Shader.h"
class Material {
public:

View File

@ -2,7 +2,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "../stb_image.h"
#include <iostream>
Texture::Texture(const std::string texturePath) {

View File

@ -1,8 +1,8 @@
#pragma once
#include "Primitives/Mesh.h"
#include "Primitives/Material.h"
#include "Texture.h"
#include "../Scene.h"
#include "Primitives/Texture.h"
#include "../Scene/Scene.h"
namespace BarinkEngine {

View File

@ -6,125 +6,18 @@ glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f
BarinkEngine::Renderer::Renderer()
{
models = std::vector<GPU_Bucket*>();
}
BarinkEngine::Renderer::~Renderer()
{
// CleanUp!
// For each model submitted
for ( auto packet : models )
{
delete packet;
}
// glDeleteBuffers(1, &UV_id);
}
void BarinkEngine::Renderer::Render()
{
Angle += 0.0001f;
for (auto model : models) {
// Push matrices etc ....
model->vertexarray.Bind();
model->elementBuffer.Bind(true);
if (model->material == nullptr) {
std::cout << "No material attached!" << std::endl;
// continue; //NOTE: Or use some default Material
}
else {
model->material->shader.Use();
model->material->Apply();
model->material->shader.setUniformVec3("Color", model->material->Color);
void BarinkEngine::Renderer::Render(Scene& scene)
{}
model->material->shader.setUniformMat4("M", glm::rotate(glm::mat4(), Angle, glm::vec3(0.5f, 0.5f, 0.0f)));
model->material->shader.setUniformMat4("V", cam.GetViewMatrix());
model->material->shader.setUniformMat4("P", projection);
// Update perf counters
ES.verts = model->mesh->vertices.size();
ES.DC++;
glDrawElements(GL_TRIANGLES,
static_cast<unsigned int>(model->mesh->elements.size()),
GL_UNSIGNED_INT,
NULL
);
}
model->vertexarray.Unbind();
}
}
void BarinkEngine::Renderer::Render(Framebuffer& framebuffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.GetId());
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Angle += 0.0001f;
for (auto model : models) {
// Push matrices etc ....
model->vertexarray.Bind();
model->elementBuffer.Bind(true);
if (model->material == nullptr) {
std::cout << "No material attached!" << std::endl;
// continue; //NOTE: Or use some default Material
}
else {
model->material->shader.Use();
model->material->Apply();
model->material->shader.setUniformVec3("Color", model->material->Color);
model->material->shader.setUniformMat4("M", glm::rotate(glm::mat4(), Angle, glm::vec3(0.5f, 0.5f, 0.0f)));
model->material->shader.setUniformMat4("V", cam.GetViewMatrix());
model->material->shader.setUniformMat4("P", projection);
// Update perf counters
ES.verts = model->mesh->vertices.size();
ES.DC++;
glDrawElements(GL_TRIANGLES,
static_cast<unsigned int>(model->mesh->elements.size()),
GL_UNSIGNED_INT,
NULL
);
}
model->vertexarray.Unbind();
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
// Upload data to cpu and add object to render list
void BarinkEngine::Renderer::Submit(Renderable* model)
{
// Upload mesh data to gpu for render
GPU_Bucket* bucket = new GPU_Bucket();
bucket->material = model->material;
bucket->Upload(*model->mesh);
models.push_back(bucket);
void Render(Framebuffer& framebuffer) {
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <vector>
#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
@ -10,7 +10,8 @@
#include "Primitives/Camera.h"
#include "Renderable.h"
#include "GPUBucket.h"
#include "Framebuffer.h"
#include "Memory/Framebuffer.h"
#include "../Scene/Components.h"
namespace BarinkEngine {
@ -19,14 +20,7 @@ namespace BarinkEngine {
Renderer();
~Renderer();
void Render();
void Render(Scene& scene );
void Render(Framebuffer& framebuffer);
void Submit(Renderable* model);
private:
std::vector<GPU_Bucket*> models;
};
}

View File

@ -11,7 +11,8 @@ bool BarinkWindow::InitGLFW(){
}
BarinkWindow::BarinkWindow(const int width, const int height) :
Width(width), Height(height), FullScreen(false){
Width(width), Height(height), FullScreen(false)
{
if (InitGLFW()==false) {
exit(-1);
}
@ -22,7 +23,7 @@ Width(width), Height(height), FullScreen(false){
// glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
// glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
// Disable resizing the window
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL);

View File

@ -1,66 +0,0 @@
#include "Scene.h"
#include "Scene.h"
#include "Scene/Node.h"
void DeleteSubGraph(Node* tree);
Scene::Scene(const std::string& sceneName)
{
// Create a root node
root = new Group(sceneName);
}
Scene::~Scene()
{
// Delete all nodes in the graph.
DeleteSubGraph(root);
}
Node* SearchInChildren(Node* root, const std::string name ) {
if (root->name == name)
return root;
Node* found = nullptr;
for (auto child : root->children) {
found = SearchInChildren(child, name);
}
return found;
}
Node& Scene::GetSceneNode(std::string name)
{
return *SearchInChildren(root, name);
}
Node& Scene::GetRoot()
{
return *root;
}
void Node::addChild(Node& node)
{
children.push_back(&node);
}
void DeleteSubGraph(Node* tree)
{
if (tree->children.size() == 0) {
delete tree;
return;
}
for (auto child : tree->children) {
if (child->children.size() > 0) {
DeleteSubGraph(child);
}
}
}

View File

@ -1,23 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include "Graphics/Transform.h"
#include "Scene/Node.h"
/*
* Scene should be a description of a game world
*/
class Scene {
public:
Node& GetSceneNode(std::string);
Node& GetRoot();
Scene(const std::string& sceneName = "Default Scene");
~Scene();
private:
Node* root;
};

View File

@ -0,0 +1,27 @@
#pragma once
#include <glm/glm.hpp>
namespace BarinkEngine {
struct TransformComponent {
glm::mat4& transform = glm::mat4(1.0f);
};
struct CameraComponent {
glm::mat4& view;
};
struct Render3DComponent {
unsigned int VAO;
unsigned int IBO;
};
struct Render2DComponent {
glm::vec3 Colour;
};
};

View File

@ -0,0 +1,7 @@
#include "Entity.h"
Entity::Entity(entt::entity e, Scene* scene)
: m_entity(e), m_scene(scene)
{
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <entt/entt.hpp>
class Scene;
class Entity {
public:
Entity() = default;
Entity(entt::entity e, Scene* scene);
Entity(const Entity& other) = default;
template<class T >
T AddComponent() {
return m_scene->m_registry.emplace<T>(m_entity);
}
private:
entt::entity m_entity;
Scene* m_scene;
};

View File

@ -0,0 +1,14 @@
#include "Scene.h"
#include "Entity.h"
Scene::Scene()
{
}
Scene::~Scene()
{}
Entity Scene::AddEntity(std::string& name)
{
return { m_registry.create(), this };
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <string>
#include <entt/entt.hpp>
class Entity;
class Scene
{
public:
Scene();
~Scene();
Entity AddEntity(std::string& name);
entt::registry& getReg() { return m_registry; }
private:
entt::registry m_registry;
friend class Entity;
};

View File

@ -1,15 +0,0 @@
#pragma once
#include "Graphics/Renderable.h"
#include "Scene/SceneNodeTypes.h"
#include <glm/glm.hpp>
#include "../../src/Scene/SceneNodeTypes.cpp"
/*
* Define a helper class to more easily build a proper scene
*/
static class SceneBuilder {
static Group* AddGroup(std::string name);
static BarinkEngine::SceneObject* AddVisual(std::string name, BarinkEngine::Renderable& object, glm::vec3 position );
};

View File

@ -1,22 +0,0 @@
#include "SceneManager.h"
Scene* SceneManager::CreateScene(const std::string& name)
{
/*
Scenes = std::map<std::string, Scene*>();
SceneManager::Scenes[name] = new Scene(name);
*/
return new Scene(name);
}
Scene& SceneManager::GetScene(const std::string& name)
{
return Scene();
//return *SceneManager::Scenes[name];
}
void SceneManager::LoadScene( Scene& scene)
{
//SceneManager::CurrentScene = &scene;
}

View File

@ -1,17 +0,0 @@
#pragma once
#include <string>
#include <map>
#include "../Scene.h"
class SceneManager {
public:
static Scene* CreateScene(const std::string& name );
static Scene& GetScene(const std::string& name);
static void LoadScene(Scene& scene);
private:
static Scene* CurrentScene;
static std::map<std::string, Scene*> Scenes;
};

View File

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include "../Graphics/Transform.h"
#include "../../Graphics/Transform.h"
class Node {
public:

View File

@ -1,6 +1,6 @@
#pragma once
#include "../Graphics/Primitives/Camera.h"
#include "../Graphics/Renderable.h"
#include "../../Graphics/Primitives/Camera.h"
#include "../../Graphics/Renderable.h"
#include "Node.h"
namespace BarinkEngine {

View File

@ -1,6 +0,0 @@
#pragma once
#include "../../BarinkEngine/src/BarinkEngine.h"
void PrintSceneTree(Node& node, int depth);
glm::mat4 CalculateModelMat(Transform& transform);

View File

@ -25,8 +25,8 @@ includedirs{
"./../libs/glm",
"./../libs/ImGui",
"../libs/entt/src",
"./include"
}
@ -35,6 +35,6 @@ libdirs {
}
files {
"./include/*.h",
"./src/*.h",
"./src/*.cpp"
}

View File

@ -68,8 +68,8 @@ void SceneView(Framebuffer& framebuffer ) {
ImGui::End();
}
void transformWindow(Transform& transform, std::string PanelName) {
/*
* void transformWindow(Transform& transform, std::string PanelName) {
ImGui::Begin(PanelName.c_str());
ImGui::InputFloat3("Position:", (float*)&transform.Position[0]);
ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]);
@ -78,6 +78,8 @@ void transformWindow(Transform& transform, std::string PanelName) {
}
*/
void materialWindow(Material& material, std::string PanelName) {
ImGui::Begin(PanelName.c_str());
ImGui::ColorPicker3("Color:", &material.Color[0]);

View File

@ -1,11 +1,11 @@
#pragma once
#include "imgui.h"
#include "../../BarinkEngine/src/BarinkEngine.h"
#include "../../BarinkEngine/src/Graphics/Framebuffer.h"
#include "../../BarinkEngine/src/Graphics/Memory/Framebuffer.h"
void CameraTool();
void ScriptingTool(char* code);
void transformWindow(Transform& transform, std::string PanelName);
///void transformWindow(Transform& transform, std::string PanelName);
void materialWindow(Material& material, std::string PanelName);
void SceneExplorer(const std::string& PanelName);
void SceneView(Framebuffer& framebuffer);

View File

@ -1,56 +1,43 @@
#include "../../BarinkEngine/src/BarinkEngine.h"
#include "../../BarinkEngine/src/Scene/SceneManager.h"
#include "../../BarinkEngine/src/Scene/SceneNodeTypes.h"
#include "../../BarinkEngine/src/AssetManager/ModelImporter.h"
#include "../../BarinkEngine/src/Graphics/Framebuffer.h"
#include "../../BarinkEngine/src/Scene/Components.h"
#include "../../BarinkEngine/src/Scene/Scene.h"
#include "../../BarinkEngine/src/Scene/Entity.h"
#include <imgui.h>
#include "GUI.h"
#include "Util.h"
#include <entt/entt.hpp>
/*
* Define globals
*/
Shader* shader;
char* code = new char[254];
//Shader* shader;
const std::string vertexShaderSource = "build/Debug/test.vs";
const std::string fragmentShaderSource = "build/Debug/test.fs";
BarinkEngine::ModelImporter* MI = new BarinkEngine::ModelImporter();
Scene* Level1;
BarinkEngine::SceneObject* cube;
Scene scene;
/*
* Runs once at startup
* - USe to initialize the game/sandbox/demo
*/
void Start() {
// shader = new Shader(vertexShaderSource, fragmentShaderSource);
// Create a level and load it as the current level
std::string levelName("Test Level");
Level1 = SceneManager::CreateScene(levelName);
SceneManager::LoadScene(*Level1);
auto cube = scene.AddEntity((std::string&)"cube");
auto transform = cube.AddComponent<BarinkEngine::TransformComponent>();
auto render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
shader = new Shader(vertexShaderSource, fragmentShaderSource);
auto cube2 = scene.AddEntity((std::string&)"cube1");
// Create a cube node
auto cube3 = scene.AddEntity((std::string&)"cube2");
auto Transform2 = cube3.AddComponent<BarinkEngine::TransformComponent>();
auto render3DComponent2 = cube3.AddComponent<BarinkEngine::Render3DComponent>();
cube = MI->Import("build/Debug/Models/cube.obj");
cube->renderable->material = new Material(*shader);
cube->renderable->material->Color = glm::vec3(1.0f, 0.0f, 0.0f);
// What is in cube now ??
std::cout << "mesh vertices: " << cube->renderable->mesh->vertices.size() << std::endl;
std::cout << "mesh elements: " << cube->renderable->mesh->elements.size() << std::endl;
// Run over every transform component
auto view = scene.getReg().view<BarinkEngine::TransformComponent, BarinkEngine::Render3DComponent>();
view.each([](auto entity, auto& transform) {
std::cout << "Found a transform !" << std::endl;
});
Level1->GetRoot().addChild(*cube);
memset(code, '\0', 254);
// TODO: Move to runtime/ Engine
// NOTE: Submits should later be done through walking the sceneTree
renderer.Submit(cube->renderable);
}
@ -71,7 +58,7 @@ void ImmediateGraphicsDraw()
void Update()
{
renderer.Render();
renderer.Render(scene);
}
@ -81,6 +68,4 @@ void Update()
*/
void Stop()
{
delete MI;
delete shader;
}

View File

@ -1,6 +1,7 @@
#include "Util.h"
void PrintSceneTree(Node& node, int depth) {
/*
* void PrintSceneTree(Node& node, int depth) {
// Indent name based on depth
std::cout << " ";
for (int i = 0; i < depth; i++) {
@ -30,3 +31,5 @@ glm::mat4 CalculateModelMat(Transform& transform) {
return tran * rot * scale;
}
*/

6
SandboxApp/src/Util.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "../../BarinkEngine/src/BarinkEngine.h"
//void PrintSceneTree(Node& node, int depth);
//glm::mat4 CalculateModelMat(Transform& transform);