Rendering a cube basics through ENTT

This commit is contained in:
Nigel Barink 2022-10-23 12:57:58 +02:00
parent 7458254b2d
commit bc1254e427
7 changed files with 103 additions and 26 deletions

View File

@ -10,6 +10,8 @@ void Create();
void Bind();
void Unbind();
unsigned int getID() { return id; }
void Delete();
void AttachAttribute(unsigned int index, int size, int stride);

View File

@ -1,4 +1,6 @@
#include "Renderer.h"
#include "../Scene/Components.h"
#include <glad/glad.h>
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);
@ -14,7 +16,29 @@ BarinkEngine::Renderer::~Renderer()
void BarinkEngine::Renderer::Render(Scene& scene)
{}
{
auto group = scene.getReg().view<TransformComponent, Render3DComponent>();
group.each([](auto entity , TransformComponent& trans, Render3DComponent& render)
{
glBindVertexArray(render.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, render.IBO);
render.shader.Use();
render.shader.setUniformVec3("Color", render.color);
render.shader.setUniformMat4("M", glm::mat4(1.0f));
render.shader.setUniformMat4("V", cam.GetViewMatrix());
render.shader.setUniformMat4("P", projection);
glDrawElements(GL_TRIANGLES, render.ElementCount,
GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
});
}

View File

@ -1,7 +1,11 @@
#pragma once
#include <glm/glm.hpp>
#include "../Graphics/Primitives/Shader.h"
namespace BarinkEngine {
struct IdentifierComponent {
std::string name;
};
struct TransformComponent {
glm::mat4& transform = glm::mat4(1.0f);
@ -17,7 +21,13 @@ namespace BarinkEngine {
struct Render3DComponent {
unsigned int VAO;
unsigned int IBO;
unsigned int ElementCount;
glm::vec3 color;
Shader shader;
Render3DComponent()
: shader(Shader("build/Debug/test.vs", "build/Debug/test.fs") )
{
}
};
struct Render2DComponent {

View File

@ -10,10 +10,15 @@ public:
template<class T >
T AddComponent() {
T& AddComponent() {
return m_scene->m_registry.emplace<T>(m_entity);
}
template<class T>
T* GetComponent() {
return m_scene->m_registry.try_get<T>(m_entity);
}
private:
entt::entity m_entity;
Scene* m_scene;

View File

@ -1,5 +1,6 @@
#include "Scene.h"
#include "Entity.h"
#include "Components.h"
Scene::Scene()
{
}
@ -9,6 +10,12 @@ Scene::~Scene()
Entity Scene::AddEntity(std::string& name)
{
return { m_registry.create(), this };
Entity entity = { m_registry.create(), this };
entity.AddComponent<BarinkEngine::IdentifierComponent>();
entity.AddComponent<BarinkEngine::TransformComponent>();
return entity;
}

View File

@ -7,5 +7,5 @@ uniform sampler2D Texture;
void main(){
FragColor = vec4(1.0f, 0.0f, 0.0f , 1.0f); // mix ( texture(Texture, TexCoord), vec4(Color, 1.0f), 1.0f);
FragColor = vec4(color , 1.0f); // mix ( texture(Texture, TexCoord), vec4(Color, 1.0f), 1.0f);
}

View File

@ -2,43 +2,61 @@
#include "../../BarinkEngine/src/Scene/Components.h"
#include "../../BarinkEngine/src/Scene/Scene.h"
#include "../../BarinkEngine/src/Scene/Entity.h"
#include "../../BarinkEngine/src/AssetManager/ModelImporter.h"
#include <imgui.h>
#include "GUI.h"
#include "Util.h"
#include <entt/entt.hpp>
/*
* Define globals
*/
//Shader* shader;
const std::string vertexShaderSource = "build/Debug/test.vs";
const std::string fragmentShaderSource = "build/Debug/test.fs";
Scene scene;
VertexArray va = VertexArray();
GpuBuffer vertexBuffer = GpuBuffer();
GpuBuffer elementBuffer = GpuBuffer();
BarinkEngine::Renderable* renderable;
BarinkEngine::SceneObject* object;
Entity cube;
/*
* Runs once at startup
* - USe to initialize the game/sandbox/demo
*/
void Start() {
// shader = new Shader(vertexShaderSource, fragmentShaderSource);
auto cube = scene.AddEntity((std::string&)"cube");
auto transform = cube.AddComponent<BarinkEngine::TransformComponent>();
auto render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
cube = scene.AddEntity((std::string&)"cube");
auto& render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
auto importer = BarinkEngine::ModelImporter();
auto cube2 = scene.AddEntity((std::string&)"cube1");
va.Create();
va.Bind();
auto cube3 = scene.AddEntity((std::string&)"cube2");
auto Transform2 = cube3.AddComponent<BarinkEngine::TransformComponent>();
auto render3DComponent2 = cube3.AddComponent<BarinkEngine::Render3DComponent>();
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);
// 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;
});
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;
}
/*
@ -49,6 +67,18 @@ void ImmediateGraphicsDraw()
{
// Show internal BarinkEngine stats
ShowStats();
ImGui::Begin("Render edit");
auto a = cube.GetComponent<BarinkEngine::Render3DComponent>();
ImGui::DragFloat3("Color", &a->color[0], 0.01f, 0.0f, 1.0f);
ImGui::End();
}
/*
@ -57,7 +87,6 @@ void ImmediateGraphicsDraw()
*/
void Update()
{
renderer.Render(scene);
}