Basic Entity Components implementation

This commit is contained in:
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,35 +6,26 @@ int main(int argc, char* argv[]) {
// Setup performance sampler
PerfomanceSamplerInit();
// Startup services
BarinkWindow MainWindow = BarinkWindow(800, 600);
renderer = BarinkEngine::Renderer();
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
);
}
void Render(Framebuffer& framebuffer) {
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);
}
}

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 {