Moving to a single renderer instance system

This commit is contained in:
Nigel Barink 2022-10-08 15:34:02 +02:00
parent 3974889f7e
commit b03b82272f
19 changed files with 201 additions and 138 deletions

View File

@ -12,6 +12,7 @@
#include <string> #include <string>
#include "Scene/SceneNodeTypes.h" #include "Scene/SceneNodeTypes.h"
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_vertices); void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_vertices);
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices); void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices);
@ -20,7 +21,7 @@ namespace BarinkEngine {
public: public:
SceneObject* Import(const std::string path); BarinkEngine::SceneObject* Import(const std::string path);
private: private:

View File

@ -5,6 +5,7 @@
#include "graphics/Texture.h" #include "graphics/Texture.h"
#include "graphics/Camera.h" #include "graphics/Camera.h"
#include "graphics/Renderable.h" #include "graphics/Renderable.h"
#include "Graphics/Renderer.h"
#include "Graphics/Material.h" #include "Graphics/Material.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"

View File

@ -0,0 +1,30 @@
#pragma once
#include "VertexArray.h"
#include "Buffer.h"
#include "Mesh.h"
#include "Material.h"
namespace BarinkEngine {
class GPU_Bucket {
public:
GPU_Bucket();
~GPU_Bucket();
void Upload(const Mesh& renderable);
GpuBuffer vertexBuffer;
GpuBuffer elementBuffer;
VertexArray vertexarray;
const Mesh* mesh;
const Material* material;
private :
unsigned int uv_id;
};
};

View File

@ -7,7 +7,7 @@ class Material {
public: public:
Material(const Shader& shader); Material(const Shader& shader);
void Apply(); void Apply()const;
glm::vec3 Color; glm::vec3 Color;

View File

@ -1,26 +1,12 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "VertexArray.h" #include "Vertex.h"
#include "Buffer.h"
namespace BarinkEngine{ namespace BarinkEngine {
struct Mesh {
struct Vertex {
glm::vec3 vertices;
glm::vec2 uv;
};
class Mesh {
public:
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
std::vector<unsigned int> elements; std::vector<unsigned int> elements;
private:
GpuBuffer vertexBuffer;
GpuBuffer elementBuffer;
VertexArray VAO;
unsigned int UV_id;
}; };
} }

View File

@ -1,23 +1,14 @@
#pragma once #pragma once
#include <vector>
#include "Mesh.h" #include "Mesh.h"
#include "Material.h" #include "Material.h"
#include "Texture.h" #include "Texture.h"
#include "Scene.h" #include "Scene.h"
namespace BarinkEngine { namespace BarinkEngine {
class Renderable { struct Renderable {
public: BarinkEngine::Mesh* mesh;
Mesh mesh;
Material* material; Material* material;
Texture* texture; Texture* texture;
Shader* shader;
Renderable();
~Renderable();
}; };
} }

View File

@ -1,8 +1,16 @@
#pragma once #pragma once
#include "Graphics/Renderable.h"
#include <vector> #include <vector>
#include <glm/gtc/matrix_transform.hpp>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "PerfCounter.h"
#include "Graphics/Camera.h"
#include "Graphics/Renderable.h"
#include "Graphics/GPUBucket.h"
namespace BarinkEngine { namespace BarinkEngine {
class Renderer { class Renderer {
@ -15,7 +23,7 @@ namespace BarinkEngine {
void Submit(Renderable* model); void Submit(Renderable* model);
private: private:
std::vector<Renderable*> models; std::vector<GPU_Bucket*> models;
}; };
} }

View File

@ -14,7 +14,7 @@ class Shader {
char* readFile (const char* filePath); char* readFile (const char* filePath);
public: public:
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath); Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
void Use(); void Use() const;
void setUniformMat4(std::string uniformName, glm::mat4 matrix4)const; void setUniformMat4(std::string uniformName, glm::mat4 matrix4)const;
void setUniformVec4(std::string uniformName, glm::vec4 vector4)const; void setUniformVec4(std::string uniformName, glm::vec4 vector4)const;
void setUniformVec3(std::string uniformName, glm::vec3 vector3)const; void setUniformVec3(std::string uniformName, glm::vec3 vector3)const;

View File

@ -0,0 +1,9 @@
#pragma once
#include <glm/glm.hpp>
namespace BarinkEngine {
struct Vertex {
glm::vec3 vertices;
glm::vec2 uv;
};
}

View File

@ -0,0 +1,37 @@
#include "Graphics/GPUBucket.h"
BarinkEngine::GPU_Bucket::GPU_Bucket() {
}
BarinkEngine::GPU_Bucket::~GPU_Bucket() {
}
void BarinkEngine::GPU_Bucket::Upload(const Mesh& renderable) {
// keep a ref to the origin in main memory
mesh = &renderable;
// Upload gpu
vertexarray.Create();
vertexarray.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData((void*)&renderable.vertices[0], renderable.vertices.size() * sizeof(BarinkEngine::Vertex), false);
if (renderable.elements.empty() == false) {
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderable.elements[0], renderable.elements.size() * sizeof(unsigned int), true);
}
vertexarray.AttachAttribute(0, 3, sizeof(Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex), (void*)offsetof(BarinkEngine::Vertex, vertices));
glEnableVertexAttribArray(1);
vertexBuffer.Unbind(false);
vertexarray.Unbind();
}

View File

@ -4,7 +4,8 @@ Material::Material(const Shader& shader) :
shader(shader) { shader(shader) {
} }
void Material::Apply() const {
void Material::Apply() { shader.Use();
shader.setUniformVec3("Color", Color); shader.setUniformVec3("Color", Color);
} }

View File

@ -1,9 +1,8 @@
#include "AssetManager/ModelImporter.h" #include "AssetManager/ModelImporter.h"
#include "spdlog/spdlog.h"
BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string path) BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string path)
{ {
SceneObject* root = new SceneObject(std::string(path), nullptr); SceneObject* root = new SceneObject(std::string(path), nullptr);
Assimp::Importer importer; Assimp::Importer importer;
@ -13,6 +12,12 @@ BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string
std::vector<BarinkEngine::Mesh> meshes = processNode(currentNode, scene); std::vector<BarinkEngine::Mesh> meshes = processNode(currentNode, scene);
std::cout << "[DEBUG]: Loaded "<< meshes.size() << " meshes!" << std::endl;
// create a renderable (per mesh ?? )
root->renderable = new Renderable();
root->renderable->mesh = &(meshes[0]);
return root; return root;

View File

@ -1,53 +0,0 @@
#include "Graphics/Renderable.h"
#include "AssetManager/ModelImporter.h"
#include "PerfCounter.h"
BarinkEngine::Renderable::Renderable()
{
/*
VAO.Create();
VAO.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(BarinkEngine::Vertex), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
VAO.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex),(void* )offsetof(BarinkEngine::Vertex, vertices));
glEnableVertexAttribArray(1);
//vertexBuffer.Unbind(false);
VAO.Unbind();
*/
}
BarinkEngine::Renderable::~Renderable()
{
// glDeleteBuffers(1, &UV_id);
}
// Draw call Example !!
/*
VAO.Bind();
elementBuffer.Bind(true);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(shader->id, "Texture"), GL_TEXTURE0);
texture->Bind();
ES->verts = meshes[0].vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind();
*/

View File

@ -1,25 +1,77 @@
#include "Graphics/Renderer.h" #include "Graphics/Renderer.h"
BarinkEngine::Renderer::Renderer() BarinkEngine::Renderer::Renderer()
{ {
models = std::vector<Renderable*>(); models = std::vector<GPU_Bucket*>();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
BarinkEngine::Renderer::~Renderer() BarinkEngine::Renderer::~Renderer()
{ {
// CleanUp! // CleanUp!
// For each model submitted
for ( auto packet : models )
{
delete packet;
}
// glDeleteBuffers(1, &UV_id);
} }
void BarinkEngine::Renderer::Render() void BarinkEngine::Renderer::Render()
{ {
// This creation of the projection and camera is somewhat wastefull
Camera cam = Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
for (auto model : models) { for (auto model : models) {
//model->Draw(); // Push matrices etc ....
model->vertexarray.Bind();
model->elementBuffer.Bind(true);
if (model->material == nullptr) {
std::cout << "No material attached!" << std::endl;
}
else {
model->material->Apply();
}
// 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();
} }
} }
// Upload data to cpu and add object to render list
void BarinkEngine::Renderer::Submit(Renderable* model) void BarinkEngine::Renderer::Submit(Renderable* model)
{ {
models.push_back(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); // Maybe push a GPU packet or something instead
} }

View File

@ -1,5 +1,6 @@
#include "Graphics/Shader.h" #include "Graphics/Shader.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
Shader::Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath) Shader::Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath)
{ {
char infoLog[512]; char infoLog[512];
@ -57,6 +58,7 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha
} }
char* Shader::readFile (const char* filePath){ char* Shader::readFile (const char* filePath){
spdlog::info("Opening {} ", filePath); spdlog::info("Opening {} ", filePath);
@ -93,7 +95,7 @@ char* Shader::readFile (const char* filePath){
return FileBuffer; return FileBuffer;
} }
void Shader::Use() void Shader::Use() const
{ {
glUseProgram(id); glUseProgram(id);
} }

View File

@ -16,6 +16,15 @@ Width(width), Height(height), FullScreen(false){
exit(-1); exit(-1);
} }
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_TRUE);
// No window decorations such as a border, a close widget
//glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
//glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
// Disable resizing the window
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL); window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL);
if( !window) if( !window)

View File

@ -10,7 +10,6 @@
/* /*
* Define globals * Define globals
*/ */
Camera* cam;
Shader* shader; Shader* shader;
char* code = new char[254]; char* code = new char[254];
@ -21,7 +20,8 @@ const std::string fragmentShaderSource = "../build/SandboxApplication/Debug/test
BarinkEngine::ModelImporter* MI = new BarinkEngine::ModelImporter(); BarinkEngine::ModelImporter* MI = new BarinkEngine::ModelImporter();
Scene* Level1; Scene* Level1;
// BarinkEngine::SceneObject* cube; BarinkEngine::Renderer* renderer;
BarinkEngine::SceneObject* cube;
/* /*
* Runs once at startup * Runs once at startup
* - USe to initialize the game/sandbox/demo * - USe to initialize the game/sandbox/demo
@ -35,29 +35,28 @@ void Start() {
Level1 = SceneManager::CreateScene(levelName); Level1 = SceneManager::CreateScene(levelName);
SceneManager::LoadScene(*Level1); SceneManager::LoadScene(*Level1);
shader = new Shader(vertexShaderSource, fragmentShaderSource);
// Create a cube node // Create a cube node
// Load a model cube = MI->Import("../build/SandboxApplication/Debug/Models/Cube.obj");
auto cube = MI->Import("../build/SandboxApplication/Debug/Models/Cube.obj"); cube->renderable->material = new Material(*shader);
// 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;
Level1->GetRoot().addChild(*cube); Level1->GetRoot().addChild(*cube);
std::string groupName("Nested-Group");
auto testGroup = new Group(groupName);
Level1->GetRoot().addChild( *testGroup);
std::string group2Name("Nested-Group2");
auto testGroup2 = new Group(group2Name);
Level1->GetRoot().addChild(*testGroup2);
// Walk scene graph
PrintSceneTree(Level1->GetRoot(),0);
shader = new Shader(vertexShaderSource, fragmentShaderSource);
cam = new Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
memset(code, '\0', 254); memset(code, '\0', 254);
}
// TODO: Move to runtime/ Engine
renderer = new BarinkEngine::Renderer();
// NOTE: Submits should later be done through walking the sceneTree
renderer->Submit(cube->renderable);
}
/* /*
* Runs every frame * Runs every frame
@ -74,7 +73,7 @@ void ImmediateGraphicsDraw() {
ShowStats(); ShowStats();
// Show different tooling for this specific sandbox // Show different tooling for this specific sandbox
CameraTool(cam); // CameraTool(cam);
ScriptingTool(code); ScriptingTool(code);
SceneExplorer(*Level1, "Scene Explorer"); SceneExplorer(*Level1, "Scene Explorer");
@ -87,25 +86,8 @@ void ImmediateGraphicsDraw() {
*/ */
void Update() void Update()
{ {
/*
* NOTE: this needs to move to the renderer
* Render code should not appear in the sandbox file
*/
//glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader->Use();
//shader->setUniformMat4("P", projection);
//shader->setUniformMat4("M", CalculateModelMat(cube->transform));
shader->setUniformMat4("V", cam->GetViewMatrix());
//cube->renderable->material->Apply();
//Cube->Draw();
renderer->Render();
} }
@ -115,5 +97,6 @@ void Update()
*/ */
void Stop() { void Stop() {
delete MI; delete MI;
delete renderer;
delete shader; delete shader;
} }

View File

@ -11,7 +11,7 @@
<input type="checkbox"></input> Basic Textures \ <input type="checkbox"></input> Basic Textures \
<input type="checkbox" checked></input> Link GLEW or GLAD \ <input type="checkbox" checked></input> Link GLEW or GLAD \
<input type="checkbox" checked></input> Work on basic logging \ <input type="checkbox" checked></input> Work on basic logging \
<input type="checkbox"></input> Input handling \ <input type="checkbox" checked></input> Input handling \
<input type="checkbox"></input> More shader work \ <input type="checkbox"></input> More shader work \
<input type="checkbox" checked></input> Load FBX model files \ <input type="checkbox" checked></input> Load FBX model files \
<input type="checkbox"></input> Basic Physics \ <input type="checkbox"></input> Basic Physics \
@ -21,5 +21,6 @@
## Resources ## Resources
https://renderdoc.org/ https://renderdoc.org/ \
https://api.projectchrono.org/tutorial_table_of_content_chrono.html https://api.projectchrono.org/tutorial_table_of_content_chrono.html \
https://github.com/epezent/implot -- Useful when displaying graphs of any kind \