Added a prepare step to the renderer , removed transform object and GPUBucket object, Added a mesh to the Render3DComponent

The renderer prepare uploads the necessary vertex data to the GPU
This commit is contained in:
Nigel Barink 2022-10-23 14:02:13 +02:00
parent bc1254e427
commit 99eb5282e5
7 changed files with 60 additions and 132 deletions

View File

@ -1,36 +0,0 @@
#include "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);
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*)0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
vertexBuffer.Unbind(false);
vertexarray.Unbind();
}

View File

@ -1,32 +0,0 @@
#pragma once
#include "Memory/VertexArray.h"
#include "Memory/Buffer.h"
#include "Primitives/Mesh.h"
#include "Primitives/Material.h"
#include <glm/glm.hpp>
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

@ -6,33 +6,62 @@ float Angle = 0.0;
Camera cam = Camera(glm::vec3(16.0f, 0.0f, 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);
BarinkEngine::Renderer::Renderer()
{
}
BarinkEngine::Renderer::Renderer(){}
BarinkEngine::Renderer::~Renderer()
{
}
BarinkEngine::Renderer::~Renderer(){}
void BarinkEngine::Renderer::Prepare(Scene& scene ) {
auto group = scene.getReg().view<BarinkEngine::Render3DComponent>();
group.each([](auto enity, BarinkEngine::Render3DComponent& renderComponent) {
VertexArray va = VertexArray();
GpuBuffer vertexBuffer = GpuBuffer();
GpuBuffer elementBuffer = GpuBuffer();
va.Create();
va.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData((void*)&renderComponent.mesh.vertices[0], renderComponent.mesh.vertices.size() * sizeof(BarinkEngine::Vertex), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderComponent.mesh.elements[0], renderComponent.mesh.elements.size() * sizeof(unsigned int), true);
va.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex), (void*)0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
va.Unbind();
vertexBuffer.Unbind(false);
elementBuffer.Unbind(true);
renderComponent.VAO = va.getID();
renderComponent.IBO = elementBuffer.getBufferID();
});
}
void BarinkEngine::Renderer::Render(Scene& scene)
{
auto group = scene.getReg().view<TransformComponent, Render3DComponent>();
group.each([](auto entity , TransformComponent& trans, Render3DComponent& render)
group.each([](auto entity , TransformComponent& trans, Render3DComponent& renderComponent)
{
glBindVertexArray(render.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, render.IBO);
render.shader.Use();
glBindVertexArray(renderComponent.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO);
render.shader.setUniformVec3("Color", render.color);
renderComponent.shader.Use();
render.shader.setUniformMat4("M", glm::mat4(1.0f));
render.shader.setUniformMat4("V", cam.GetViewMatrix());
render.shader.setUniformMat4("P", projection);
renderComponent.shader.setUniformVec3("Color", renderComponent.color);
renderComponent.shader.setUniformMat4("M", glm::mat4(1.0f));
renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix());
renderComponent.shader.setUniformMat4("P", projection);
glDrawElements(GL_TRIANGLES, render.ElementCount,
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(renderComponent.mesh.elements.size()) ,
GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
@ -40,8 +69,5 @@ void BarinkEngine::Renderer::Render(Scene& scene)
}
void Render(Framebuffer& framebuffer) {
}
void Render(Framebuffer& framebuffer)
{}

View File

@ -20,7 +20,9 @@ namespace BarinkEngine {
Renderer();
~Renderer();
void Prepare(Scene& scene);
void Render(Scene& scene );
void Render(Framebuffer& framebuffer);
};
}

View File

@ -1,11 +0,0 @@
#pragma once
#include <glm/glm.hpp>
struct Transform {
glm::vec3 Position;
glm::vec3 Rotation;
glm::vec3 Scale;
glm::mat4 ModelMatrix;
};

View File

@ -1,7 +1,7 @@
#pragma once
#include <glm/glm.hpp>
#include "../Graphics/Primitives/Shader.h"
#include "../Graphics/Primitives/Mesh.h"
namespace BarinkEngine {
struct IdentifierComponent {
std::string name;
@ -19,13 +19,17 @@ namespace BarinkEngine {
};
struct Render3DComponent {
unsigned int VAO;
unsigned int IBO;
unsigned int ElementCount;
unsigned int VAO = 0;
unsigned int IBO = 0;
Mesh mesh;
// TODO: becomes a material
glm::vec3 color;
Shader shader;
Render3DComponent()
: shader(Shader("build/Debug/test.vs", "build/Debug/test.fs") )
: shader(Shader("build/Debug/test.vs", "build/Debug/test.fs")),
color(glm::vec3(1.0f, 0.0f, 0.0f))
{
}
};

View File

@ -12,9 +12,7 @@
* Define globals
*/
Scene scene;
VertexArray va = VertexArray();
GpuBuffer vertexBuffer = GpuBuffer();
GpuBuffer elementBuffer = GpuBuffer();
BarinkEngine::Renderable* renderable;
BarinkEngine::SceneObject* object;
Entity cube;
@ -28,35 +26,12 @@ void Start() {
auto& render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
auto importer = BarinkEngine::ModelImporter();
va.Create();
va.Bind();
object = importer.Import("build/Debug/Models/Cube.obj");
renderable = object->renderable;
render3DComponent.ElementCount = static_cast<unsigned int>(renderable->mesh->elements.size());
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData((void*)&renderable->mesh->vertices[0], renderable->mesh->vertices.size() * sizeof(BarinkEngine::Vertex), false);
render3DComponent.mesh = *renderable->mesh;
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderable->mesh->elements[0], renderable->mesh->elements.size() * sizeof(unsigned int), true);
va.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex), (void*)0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
va.Unbind();
vertexBuffer.Unbind(false);
elementBuffer.Unbind(true);
render3DComponent.VAO = va.getID();
render3DComponent.IBO = elementBuffer.getBufferID();
render3DComponent.color = glm::vec3(1.0f, 0.0f, 0.0f);
std::cout << render3DComponent.ElementCount << std::endl;
renderer.Prepare(scene);
}
/*