3d renderer
This commit is contained in:
@@ -1,14 +1,9 @@
|
|||||||
#ifndef EDITOR_HPP
|
#ifndef EDITOR_HPP
|
||||||
#define EDITOR_HPP
|
#define EDITOR_HPP
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <open_engine.hpp>
|
#include <open_engine.hpp>
|
||||||
|
|
||||||
#include "open_engine/application.hpp"
|
#include "open_engine/scene/components.hpp"
|
||||||
#include "open_engine/logging.hpp"
|
|
||||||
#include "open_engine/renderer/texture.hpp"
|
|
||||||
#include "open_engine/scene/entity.hpp"
|
|
||||||
#include "open_engine/scene/scene_serializer.hpp"
|
|
||||||
#include "panels/content_browser.hpp"
|
#include "panels/content_browser.hpp"
|
||||||
#include "panels/scene_hierarchy.hpp"
|
#include "panels/scene_hierarchy.hpp"
|
||||||
|
|
||||||
@@ -19,6 +14,7 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <ImGuizmo.h>
|
#include <ImGuizmo.h>
|
||||||
|
#include <filesystem>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -95,6 +91,34 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
icons["play"] = Texture2D::Create("resources/textures/icons/play.png");
|
icons["play"] = Texture2D::Create("resources/textures/icons/play.png");
|
||||||
icons["stop"] = Texture2D::Create("resources/textures/icons/stop.png");
|
icons["stop"] = Texture2D::Create("resources/textures/icons/stop.png");
|
||||||
|
|
||||||
|
Entity cube = scene->CreateEntity("cube");
|
||||||
|
|
||||||
|
|
||||||
|
cube.AddComponent<TransformComponent>();
|
||||||
|
|
||||||
|
Ref<Mesh> mesh = CreateCube(cube);
|
||||||
|
|
||||||
|
cube.AddComponent<MeshComponent>(mesh);
|
||||||
|
|
||||||
|
Entity cube2 = scene->CreateEntity("cube2");
|
||||||
|
cube2.AddComponent<TransformComponent>();
|
||||||
|
cube2.GetComponents<TransformComponent>().translation = {2, 0, 0};
|
||||||
|
|
||||||
|
Ref<Mesh> mesh2 = CreateCube(cube2);
|
||||||
|
|
||||||
|
cube2.AddComponent<MeshComponent>(mesh2);
|
||||||
|
OE_DEBUG("cubes is like {} {}", (uint32_t)cube, (uint32_t)cube2);
|
||||||
|
|
||||||
|
/*
|
||||||
|
auto view = scene->GetRegistry().view<TagComponent>();
|
||||||
|
|
||||||
|
for (auto& entity : view) {
|
||||||
|
auto tag = view->get(entity);
|
||||||
|
OE_DEBUG("entity: {}, tag: {}", (uint32_t)entity, tag.tag);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// TODO: Add file texture. Get free icons and add license
|
// TODO: Add file texture. Get free icons and add license
|
||||||
//icons["folder"] = Texture2D::Create("resources/textures/icons/folder.png");
|
//icons["folder"] = Texture2D::Create("resources/textures/icons/folder.png");
|
||||||
/*
|
/*
|
||||||
@@ -140,7 +164,7 @@ namespace OpenEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
framebuffer->Bind();
|
framebuffer->Bind();
|
||||||
Renderer2D::ResetStats();
|
//Renderer2D::ResetStats();
|
||||||
RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f});
|
RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f});
|
||||||
RenderCommand::Clear();
|
RenderCommand::Clear();
|
||||||
framebuffer->ClearBufferI(1, -1);
|
framebuffer->ClearBufferI(1, -1);
|
||||||
@@ -318,7 +342,7 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
void DrawStats()
|
void DrawStats()
|
||||||
{
|
{
|
||||||
auto stats = Renderer2D::GetStats();
|
//auto stats = Renderer2D::GetStats();
|
||||||
static float time = 0;
|
static float time = 0;
|
||||||
|
|
||||||
time += Time::DeltaTime();
|
time += Time::DeltaTime();
|
||||||
@@ -330,17 +354,17 @@ namespace OpenEngine {
|
|||||||
ImGui::Begin("Statistics");
|
ImGui::Begin("Statistics");
|
||||||
ImGui::SeparatorText("Performance");
|
ImGui::SeparatorText("Performance");
|
||||||
ImGui::Text("FPS: %0.0f", 1 / Time::DeltaTime());
|
ImGui::Text("FPS: %0.0f", 1 / Time::DeltaTime());
|
||||||
ImGui::Text("Renderer2D:");
|
//ImGui::Text("Renderer2D:");
|
||||||
ImGui::Text("\t\tDraw calls: %d", stats.draw_calls);
|
//ImGui::Text("\t\tDraw calls: %d", stats.draw_calls);
|
||||||
ImGui::Text("\t\tQuad count: %d", stats.quad_count);
|
//ImGui::Text("\t\tQuad count: %d", stats.quad_count);
|
||||||
ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount());
|
//ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount());
|
||||||
ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount());
|
//ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount());
|
||||||
if (selected_entity) {
|
//if (selected_entity) {
|
||||||
ImGui::SeparatorText("Entities");
|
// ImGui::SeparatorText("Entities");
|
||||||
ImGui::Text("Selected entity:");
|
// ImGui::Text("Selected entity:");
|
||||||
ImGui::Text("\t\tname: %s", selected_entity.GetComponents<TagComponent>().tag.c_str());
|
// ImGui::Text("\t\tname: %s", selected_entity.GetComponents<TagComponent>().tag.c_str());
|
||||||
ImGui::Text("\t\tid: %d", (uint32_t)selected_entity);
|
// ImGui::Text("\t\tid: %d", (uint32_t)selected_entity);
|
||||||
}
|
//}
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,10 @@ namespace OpenEngine {
|
|||||||
const std::array<const char*, 3> labels = {"x", "y", "z"});
|
const std::array<const char*, 3> labels = {"x", "y", "z"});
|
||||||
|
|
||||||
void TagOnImGuiRender(entt::registry& registry, entt::entity entity);
|
void TagOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||||
|
|
||||||
void TransformOnImGuiRender(entt::registry& registry, entt::entity entity);
|
void TransformOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||||
|
|
||||||
void SpriteOnImGuiRender(entt::registry& registry, entt::entity entity);
|
void SpriteOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||||
|
|
||||||
void CameraOnImGuiRender(entt::registry& registry, entt::entity entity);
|
void CameraOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||||
|
void MeshOnImGuiRender(entt::registry®istry, entt::entity entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // EDITOR_COMPONENT_HPP
|
#endif // EDITOR_COMPONENT_HPP
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
namespace OpenEngine {
|
namespace OpenEngine {
|
||||||
|
|
||||||
|
class EditorLayer;
|
||||||
|
|
||||||
class ContentBrowserPanel
|
class ContentBrowserPanel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -170,4 +170,9 @@ namespace OpenEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void MeshOnImGuiRender(entt::registry®istry, entt::entity entity)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ namespace OpenEngine {
|
|||||||
RegisterDrawer<TransformComponent>("Transform", &TransformOnImGuiRender);
|
RegisterDrawer<TransformComponent>("Transform", &TransformOnImGuiRender);
|
||||||
RegisterDrawer<SpriteRendererComponent>("Sprite Renderer", &SpriteOnImGuiRender);
|
RegisterDrawer<SpriteRendererComponent>("Sprite Renderer", &SpriteOnImGuiRender);
|
||||||
RegisterDrawer<CameraComponent>("Camera", &CameraOnImGuiRender);
|
RegisterDrawer<CameraComponent>("Camera", &CameraOnImGuiRender);
|
||||||
|
RegisterDrawer<MeshComponent>("Mesh", &MeshOnImGuiRender);
|
||||||
|
|
||||||
scene = context;
|
scene = context;
|
||||||
selected_context = {};
|
selected_context = {};
|
||||||
@@ -32,17 +33,22 @@ namespace OpenEngine {
|
|||||||
ImGui::SeparatorText("Add");
|
ImGui::SeparatorText("Add");
|
||||||
if (!selected_context.HasComponent<TransformComponent>())
|
if (!selected_context.HasComponent<TransformComponent>())
|
||||||
if (ImGui::MenuItem("Transform")) {
|
if (ImGui::MenuItem("Transform")) {
|
||||||
selected_context.AddComponents<TransformComponent>();
|
selected_context.AddComponent<TransformComponent>();
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
}
|
}
|
||||||
if (!selected_context.HasComponent<SpriteRendererComponent>())
|
if (!selected_context.HasComponent<SpriteRendererComponent>())
|
||||||
if (ImGui::MenuItem("Sprite")) {
|
if (ImGui::MenuItem("Sprite")) {
|
||||||
selected_context.AddComponents<SpriteRendererComponent>();
|
selected_context.AddComponent<SpriteRendererComponent>();
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
}
|
}
|
||||||
if (!selected_context.HasComponent<CameraComponent>())
|
if (!selected_context.HasComponent<CameraComponent>())
|
||||||
if (ImGui::MenuItem("Camera")) {
|
if (ImGui::MenuItem("Camera")) {
|
||||||
selected_context.AddComponents<CameraComponent>();
|
selected_context.AddComponent<CameraComponent>();
|
||||||
|
ImGui::CloseCurrentPopup();
|
||||||
|
}
|
||||||
|
if (!selected_context.HasComponent<MeshComponent>())
|
||||||
|
if (ImGui::MenuItem("Mesh")) {
|
||||||
|
selected_context.AddComponent<MeshComponent>();
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,6 +208,7 @@ namespace OpenEngine {
|
|||||||
entity.GetComponents<TagComponent>();
|
entity.GetComponents<TagComponent>();
|
||||||
TagOnImGuiRender(reg, entity);
|
TagOnImGuiRender(reg, entity);
|
||||||
|
|
||||||
|
|
||||||
std::vector<entt::id_type> component_to_delete; // 0 is null/invalid in entt usually
|
std::vector<entt::id_type> component_to_delete; // 0 is null/invalid in entt usually
|
||||||
// Iterate through every component type entt knows about
|
// Iterate through every component type entt knows about
|
||||||
for (auto [id, storage] : reg.storage()) {
|
for (auto [id, storage] : reg.storage()) {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#include "open_engine/renderer/subtexture2d.hpp"
|
#include "open_engine/renderer/subtexture2d.hpp"
|
||||||
#include "open_engine/renderer/framebuffer.hpp"
|
#include "open_engine/renderer/framebuffer.hpp"
|
||||||
#include "open_engine/renderer/renderer2d.hpp"
|
#include "open_engine/renderer/renderer2d.hpp"
|
||||||
|
#include "open_engine/renderer/renderer3d.hpp"
|
||||||
#include "open_engine/renderer/renderer.hpp"
|
#include "open_engine/renderer/renderer.hpp"
|
||||||
#include "open_engine/renderer/texture.hpp"
|
#include "open_engine/renderer/texture.hpp"
|
||||||
#include "open_engine/renderer/buffer.hpp"
|
#include "open_engine/renderer/buffer.hpp"
|
||||||
|
|||||||
@@ -29,13 +29,14 @@ namespace OpenEngine {
|
|||||||
class OpenGLIndexBuffer : public IndexBuffer
|
class OpenGLIndexBuffer : public IndexBuffer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OpenGLIndexBuffer(uint32_t* indices, uint32_t count);
|
OpenGLIndexBuffer(const uint32_t* indices, uint32_t count);
|
||||||
virtual ~OpenGLIndexBuffer();
|
virtual ~OpenGLIndexBuffer();
|
||||||
|
|
||||||
virtual void Bind() const override;
|
virtual void Bind() const override;
|
||||||
virtual void UnBind() const override;
|
virtual void UnBind() const override;
|
||||||
|
|
||||||
virtual uint32_t GetCount() const override { return count; };
|
virtual uint32_t GetCount() const override { return count; };
|
||||||
|
virtual uint32_t GetID() const override { return id; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef BUFFER_HPP
|
#ifndef BUFFER_HPP
|
||||||
#define BUFFER_HPP
|
#define BUFFER_HPP
|
||||||
|
|
||||||
|
#include "open_engine/ref_scope.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -82,8 +84,9 @@ namespace OpenEngine {
|
|||||||
virtual void UnBind() const = 0;
|
virtual void UnBind() const = 0;
|
||||||
|
|
||||||
virtual uint32_t GetCount() const = 0;
|
virtual uint32_t GetCount() const = 0;
|
||||||
|
virtual uint32_t GetID() const = 0;
|
||||||
|
|
||||||
static Ref<IndexBuffer> Create(uint32_t* indices, uint32_t count);
|
static Ref<IndexBuffer> Create(const uint32_t* indices, uint32_t count);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
61
open_engine/include/open_engine/renderer/renderer3d.hpp
Normal file
61
open_engine/include/open_engine/renderer/renderer3d.hpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#ifndef RENDERER3D_HPP
|
||||||
|
#define RENDERER3D_HPP
|
||||||
|
|
||||||
|
#include "open_engine/renderer/editor_camera.hpp"
|
||||||
|
#include "open_engine/renderer/vertex_array.hpp"
|
||||||
|
#include "open_engine/renderer/camera.hpp"
|
||||||
|
#include "open_engine/renderer/buffer.hpp"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace OpenEngine {
|
||||||
|
|
||||||
|
// SHOULD BE MOVED ==================================
|
||||||
|
struct MeshVertex
|
||||||
|
{
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec4 color;
|
||||||
|
glm::vec3 normal;
|
||||||
|
glm::vec2 text_coords;
|
||||||
|
uint32_t id = -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Mesh
|
||||||
|
{
|
||||||
|
Ref<VertexArray> vertex_array;
|
||||||
|
Ref<VertexBuffer> vertex_buffer;
|
||||||
|
Ref<IndexBuffer> index_buffer;
|
||||||
|
|
||||||
|
// TODO: Make them a ptr or something
|
||||||
|
std::vector<MeshVertex> vertices;
|
||||||
|
std::vector<uint32_t> indices;
|
||||||
|
};
|
||||||
|
|
||||||
|
Ref<Mesh> CreateMesh(const std::vector<MeshVertex>& vertices,
|
||||||
|
const std::vector<uint32_t>& indices);
|
||||||
|
Ref<Mesh> CreateCube(uint32_t id = -1);
|
||||||
|
// ==================================================
|
||||||
|
|
||||||
|
class Renderer3D
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static void Init();
|
||||||
|
static void Shutdown();
|
||||||
|
|
||||||
|
static void BeginScene(const Camera& camera, const glm::mat4& transform);
|
||||||
|
static void BeginScene(const EditorCamera& camera);
|
||||||
|
static void EndScene();
|
||||||
|
|
||||||
|
static void DrawMesh(const Ref<Mesh>& mesh, const glm::mat4& transform);
|
||||||
|
|
||||||
|
/*
|
||||||
|
static void DrawCube(const glm::mat4& transform, const glm::vec4& color,
|
||||||
|
int entity_id);
|
||||||
|
static void DrawCube(const glm::mat4& transform, Ref<Texture2D>,
|
||||||
|
int entity_id);
|
||||||
|
static void DrawCube(const glm::mat4& transform, Ref<Texture2D>,
|
||||||
|
const glm::vec4& color, int entity_id);
|
||||||
|
*/
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // RENDERER3D_HPP
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#include <pch.hpp>
|
|
||||||
|
|
||||||
#include <renderer/uniform_buffer.hpp>
|
|
||||||
#include <opengl/opengl_uniform_buffer.hpp>
|
|
||||||
#include <renderer/renderer.hpp>
|
|
||||||
|
|
||||||
namespace OpenEngine {
|
|
||||||
|
|
||||||
Ref<UniformBuffer> UniformBuffer::Create(uint32_t size, uint32_t binding)
|
|
||||||
{
|
|
||||||
switch (Renderer::GetAPI())
|
|
||||||
{
|
|
||||||
case RendererAPI::API::None: OE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
|
|
||||||
case RendererAPI::API::OpenGL: return CreateRef<OpenGLUniformBuffer>(size, binding);
|
|
||||||
}
|
|
||||||
|
|
||||||
OE_CORE_ASSERT(false, "Unknown RendererAPI!");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#define VERTEX_ARRAY_HPP
|
#define VERTEX_ARRAY_HPP
|
||||||
|
|
||||||
#include "open_engine/renderer/buffer.hpp"
|
#include "open_engine/renderer/buffer.hpp"
|
||||||
|
#include "open_engine/ref_scope.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
#define COMPONENTS_HPP
|
#define COMPONENTS_HPP
|
||||||
|
|
||||||
#include "open_engine/scene/native_scriptable_entity.hpp"
|
#include "open_engine/scene/native_scriptable_entity.hpp"
|
||||||
|
#include "open_engine/renderer/renderer3d.hpp"
|
||||||
#include "open_engine/scene/scene_camera.hpp"
|
#include "open_engine/scene/scene_camera.hpp"
|
||||||
#include "open_engine/renderer/texture.hpp"
|
#include "open_engine/renderer/texture.hpp"
|
||||||
|
#include "open_engine/renderer/renderer3d.hpp"
|
||||||
|
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
@@ -87,6 +89,16 @@ namespace OpenEngine {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct MeshComponent
|
||||||
|
{
|
||||||
|
Ref<Mesh> mesh;
|
||||||
|
|
||||||
|
MeshComponent() = default;
|
||||||
|
MeshComponent(const MeshComponent&) = default;
|
||||||
|
MeshComponent(const Ref<Mesh>& mesh)
|
||||||
|
: mesh(mesh) {}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // COMPONENTS_HPP
|
#endif // COMPONENTS_HPP
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace OpenEngine {
|
|||||||
Entity(const Entity& other) = default;
|
Entity(const Entity& other) = default;
|
||||||
|
|
||||||
template<typename T, typename ... Args>
|
template<typename T, typename ... Args>
|
||||||
T& AddComponents(Args&&... args)
|
T& AddComponent(Args&&... args)
|
||||||
{
|
{
|
||||||
OE_ASSERT(!HasComponent<T>(), "Entity already has component.");
|
OE_ASSERT(!HasComponent<T>(), "Entity already has component.");
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <core.hpp>
|
#include <core.hpp>
|
||||||
#include <events/application_event.hpp>
|
#include <events/application_event.hpp>
|
||||||
|
#include <renderer/renderer3d.hpp>
|
||||||
#include <renderer/renderer2d.hpp>
|
#include <renderer/renderer2d.hpp>
|
||||||
#include <input/input_system.hpp>
|
#include <input/input_system.hpp>
|
||||||
#include <imgui/imgui_layer.hpp>
|
#include <imgui/imgui_layer.hpp>
|
||||||
@@ -28,7 +29,8 @@ namespace OpenEngine {
|
|||||||
{
|
{
|
||||||
OE_PROFILE_SCOPE("Initializing Renderers");
|
OE_PROFILE_SCOPE("Initializing Renderers");
|
||||||
Renderer::Init();
|
Renderer::Init();
|
||||||
Renderer2D::Init();
|
//Renderer2D::Init();
|
||||||
|
Renderer3D::Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
imgui_layer = std::make_shared<ImGuiLayer>();
|
imgui_layer = std::make_shared<ImGuiLayer>();
|
||||||
@@ -39,7 +41,8 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
Application::~Application()
|
Application::~Application()
|
||||||
{
|
{
|
||||||
OpenEngine::Renderer2D::Shutdown();
|
//OpenEngine::Renderer2D::Shutdown();
|
||||||
|
Renderer3D::Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::Run()
|
void Application::Run()
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace OpenEngine {
|
|||||||
// Index Buffer =====================================================
|
// Index Buffer =====================================================
|
||||||
// ==================================================================
|
// ==================================================================
|
||||||
|
|
||||||
OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count)
|
OpenGLIndexBuffer::OpenGLIndexBuffer(const uint32_t* indices, uint32_t count)
|
||||||
: count(count)
|
: count(count)
|
||||||
{
|
{
|
||||||
OE_PROFILE_FUNCTION();
|
OE_PROFILE_FUNCTION();
|
||||||
@@ -68,7 +68,7 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
void OpenGLIndexBuffer::Bind() const
|
void OpenGLIndexBuffer::Bind() const
|
||||||
{
|
{
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||||
}
|
}
|
||||||
void OpenGLIndexBuffer::UnBind() const
|
void OpenGLIndexBuffer::UnBind() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ namespace OpenEngine {
|
|||||||
{
|
{
|
||||||
OE_PROFILE_FUNCTION();
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
OE_CORE_ASSERT(vertex_array, "VertexArray is null!");
|
||||||
|
OE_CORE_ASSERT(vertex_array->GetIndexBuffer(), "IndexBuffer is null!");
|
||||||
|
|
||||||
uint32_t count = index_count? index_count : vertex_array->GetIndexBuffer()->GetCount();
|
uint32_t count = index_count? index_count : vertex_array->GetIndexBuffer()->GetCount();
|
||||||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
|
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ namespace OpenEngine {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<IndexBuffer> IndexBuffer::Create(uint32_t* indices, uint32_t count)
|
Ref<IndexBuffer> IndexBuffer::Create(const uint32_t* indices, uint32_t count)
|
||||||
{
|
{
|
||||||
OE_PROFILE_FUNCTION();
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "renderer/editor_camera.hpp"
|
|
||||||
#include "renderer/uniform_buffer.hpp"
|
|
||||||
#include <pch.hpp>
|
#include <pch.hpp>
|
||||||
|
|
||||||
|
#include "renderer/editor_camera.hpp"
|
||||||
|
#include "renderer/uniform_buffer.hpp"
|
||||||
#include <renderer/render_command.hpp>
|
#include <renderer/render_command.hpp>
|
||||||
#include <renderer/vertex_array.hpp>
|
#include <renderer/vertex_array.hpp>
|
||||||
#include <renderer/renderer2d.hpp>
|
#include <renderer/renderer2d.hpp>
|
||||||
@@ -121,6 +121,8 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
void Renderer2D::Shutdown()
|
void Renderer2D::Shutdown()
|
||||||
{
|
{
|
||||||
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
renderer_data.white_texture.reset();
|
renderer_data.white_texture.reset();
|
||||||
renderer_data.vertex_array.reset();
|
renderer_data.vertex_array.reset();
|
||||||
renderer_data.vertex_buffer.reset();
|
renderer_data.vertex_buffer.reset();
|
||||||
@@ -130,8 +132,6 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
for (uint32_t i = 0; i < renderer_data.texture_slot_index; i++)
|
for (uint32_t i = 0; i < renderer_data.texture_slot_index; i++)
|
||||||
renderer_data.texture_slots[i].reset();
|
renderer_data.texture_slots[i].reset();
|
||||||
|
|
||||||
OE_PROFILE_FUNCTION();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer2D::BeginScene(const OrthographicCamera& camera)
|
void Renderer2D::BeginScene(const OrthographicCamera& camera)
|
||||||
|
|||||||
185
open_engine/src/open_engine/renderer/renderer3d.cpp
Normal file
185
open_engine/src/open_engine/renderer/renderer3d.cpp
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
#include "logging.hpp"
|
||||||
|
#include <pch.hpp>
|
||||||
|
|
||||||
|
#include <renderer/render_command.hpp>
|
||||||
|
#include <renderer/uniform_buffer.hpp>
|
||||||
|
#include <renderer/editor_camera.hpp>
|
||||||
|
#include <renderer/vertex_array.hpp>
|
||||||
|
#include <renderer/renderer3d.hpp>
|
||||||
|
#include <renderer/shader.hpp>
|
||||||
|
#include <renderer/buffer.hpp>
|
||||||
|
#include <instrumentor.hpp>
|
||||||
|
#include <ref_scope.hpp>
|
||||||
|
|
||||||
|
#include <glm/ext/quaternion_common.hpp>
|
||||||
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
#include <glm/trigonometric.hpp>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
namespace OpenEngine {
|
||||||
|
|
||||||
|
// SHOULD BE MOVED ==================================
|
||||||
|
Ref<Mesh> CreateMesh(const std::vector<MeshVertex>& vertices,
|
||||||
|
const std::vector<uint32_t>& indices)
|
||||||
|
{
|
||||||
|
Ref<Mesh> mesh = CreateRef<Mesh>();
|
||||||
|
mesh->vertices = vertices;
|
||||||
|
mesh->indices = indices;
|
||||||
|
|
||||||
|
mesh->index_buffer = IndexBuffer::Create(indices.data(), indices.size());
|
||||||
|
mesh->vertex_array = VertexArray::Create();
|
||||||
|
mesh->vertex_buffer = VertexBuffer::Create(vertices.size() * sizeof(MeshVertex));
|
||||||
|
|
||||||
|
mesh->vertex_buffer->SetData(vertices.data(), vertices.size() * sizeof(MeshVertex));
|
||||||
|
|
||||||
|
BufferLayout layout = {
|
||||||
|
{ ShaderDataType::Float3, "a_Position" },
|
||||||
|
{ ShaderDataType::Float4, "a_Color" },
|
||||||
|
{ ShaderDataType::Float3, "a_Normal" },
|
||||||
|
{ ShaderDataType::Float2, "a_TexCoord" },
|
||||||
|
{ ShaderDataType::Int, "a_EntityID"}
|
||||||
|
};
|
||||||
|
|
||||||
|
mesh->vertex_buffer->SetLayout(layout);
|
||||||
|
|
||||||
|
mesh->vertex_array->AddVertexBuffer(mesh->vertex_buffer);
|
||||||
|
mesh->vertex_array->SetIndexBuffer(mesh->index_buffer);
|
||||||
|
|
||||||
|
return mesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ref<Mesh> CreateCube(uint32_t id)
|
||||||
|
{
|
||||||
|
OE_CORE_DEBUG("Creating cube with id {}.", id);
|
||||||
|
std::vector<MeshVertex> vertices = {
|
||||||
|
// Back face (normal 0, 0, -1)
|
||||||
|
{{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {0,0}, id},
|
||||||
|
{{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {0,1}, id},
|
||||||
|
{{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {1,1}, id},
|
||||||
|
{{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {1,0}, id},
|
||||||
|
|
||||||
|
// Front face (normal 0, 0, 1)
|
||||||
|
{{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {0,0}, id},
|
||||||
|
{{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {0,1}, id},
|
||||||
|
{{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {1,1}, id},
|
||||||
|
{{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {1,0}, id},
|
||||||
|
|
||||||
|
// Left face (normal -1, 0, 0)
|
||||||
|
{{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {0,0}, id},
|
||||||
|
{{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {0,1}, id},
|
||||||
|
{{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {1,1}, id},
|
||||||
|
{{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {1,0}, id},
|
||||||
|
|
||||||
|
// Right face (normal 1, 0, 0)
|
||||||
|
{{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {0,0}, id},
|
||||||
|
{{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {0,1}, id},
|
||||||
|
{{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {1,1}, id},
|
||||||
|
{{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {1,0}, id},
|
||||||
|
|
||||||
|
// Top face (normal 0, 1, 0)
|
||||||
|
{{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {0,0}, id},
|
||||||
|
{{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {0,1}, id},
|
||||||
|
{{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {1,1}, id},
|
||||||
|
{{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {1,0}, id},
|
||||||
|
|
||||||
|
// Bottom face (normal 0, -1, 0)
|
||||||
|
{{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {0,0}, id},
|
||||||
|
{{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {0,1}, id},
|
||||||
|
{{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {1,1}, id},
|
||||||
|
{{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {1,0}, id},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<uint32_t> indices = {
|
||||||
|
0, 1, 2, 0, 2, 3, // back
|
||||||
|
4, 6, 5, 4, 7, 6, // front
|
||||||
|
8, 9, 10, 8, 10, 11, // left
|
||||||
|
12, 14, 13, 12, 15, 14, // right
|
||||||
|
16, 17, 18, 16, 18, 19, // top
|
||||||
|
20, 23, 22, 20, 22, 21, // bottom
|
||||||
|
};
|
||||||
|
|
||||||
|
return CreateMesh(vertices, indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================================================
|
||||||
|
struct Renderer3DData
|
||||||
|
{
|
||||||
|
Ref<Shader> color_shader_3d;
|
||||||
|
|
||||||
|
struct CameraData
|
||||||
|
{
|
||||||
|
glm::mat4 view_projection;
|
||||||
|
};
|
||||||
|
CameraData camera_buffer;
|
||||||
|
Ref<UniformBuffer> camera_uniform_buffer;
|
||||||
|
|
||||||
|
struct TransformData
|
||||||
|
{
|
||||||
|
glm::mat4 transform;
|
||||||
|
};
|
||||||
|
TransformData transform_buffer;
|
||||||
|
Ref<UniformBuffer> transform_uniform_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Renderer3DData renderer_data;
|
||||||
|
|
||||||
|
void Renderer3D::Init()
|
||||||
|
{
|
||||||
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
renderer_data.color_shader_3d = Shader::Create("assets/shaders/color3d.glsl");
|
||||||
|
renderer_data.camera_uniform_buffer = UniformBuffer::Create(sizeof(Renderer3DData::CameraData), 0);
|
||||||
|
renderer_data.transform_uniform_buffer = UniformBuffer::Create(sizeof(Renderer3DData::TransformData), 1);
|
||||||
|
|
||||||
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||||
|
glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity,
|
||||||
|
GLsizei length, const GLchar* message, const void* userParam) {
|
||||||
|
OE_CORE_ERROR("GL: {0}", message);
|
||||||
|
}, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer3D::Shutdown()
|
||||||
|
{
|
||||||
|
renderer_data.camera_uniform_buffer.reset();
|
||||||
|
renderer_data.color_shader_3d.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer3D::BeginScene(const Camera& camera, const glm::mat4& transform)
|
||||||
|
{
|
||||||
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
renderer_data.camera_buffer.view_projection = camera.GetProjection() * glm::inverse(transform);
|
||||||
|
renderer_data.camera_uniform_buffer->SetData(&renderer_data.camera_buffer, sizeof(Renderer3DData::CameraData));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer3D::BeginScene(const EditorCamera& camera)
|
||||||
|
{
|
||||||
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
renderer_data.camera_buffer.view_projection = camera.GetViewProjection();
|
||||||
|
renderer_data.camera_uniform_buffer->SetData(&renderer_data.camera_buffer, sizeof(Renderer3DData::CameraData));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer3D::EndScene()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer3D::DrawMesh(const Ref<Mesh>& mesh, const glm::mat4& transform)
|
||||||
|
{
|
||||||
|
OE_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
renderer_data.color_shader_3d->Bind();
|
||||||
|
|
||||||
|
renderer_data.transform_buffer.transform = transform;
|
||||||
|
renderer_data.transform_uniform_buffer->SetData(&renderer_data.transform_buffer, sizeof(Renderer3DData::TransformData));
|
||||||
|
|
||||||
|
mesh->vertex_array->Bind();
|
||||||
|
|
||||||
|
RenderCommand::DrawIndexed(mesh->vertex_array, mesh->indices.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,18 @@
|
|||||||
|
#include <cstdint>
|
||||||
#include <pch.hpp>
|
#include <pch.hpp>
|
||||||
|
|
||||||
#include <renderer/renderer2d.hpp>
|
#include <renderer/renderer3d.hpp>
|
||||||
#include <scene/components.hpp>
|
#include <scene/components.hpp>
|
||||||
#include <scene/entity.hpp>
|
#include <scene/entity.hpp>
|
||||||
#include <scene/scene.hpp>
|
#include <scene/scene.hpp>
|
||||||
|
|
||||||
namespace OpenEngine {
|
namespace OpenEngine {
|
||||||
|
|
||||||
Entity Scene::CreateEntity(const std::string& name)
|
Entity Scene::CreateEntity(const std::string& name)
|
||||||
{
|
{
|
||||||
Entity entity = { registry.create(), this };
|
Entity entity = { registry.create(), this };
|
||||||
|
|
||||||
auto& tag = entity.AddComponents<TagComponent>();
|
auto& tag = entity.AddComponent<TagComponent>();
|
||||||
tag.tag = name.empty() ? "Entity" : name;
|
tag.tag = name.empty() ? "Entity" : name;
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
@@ -52,6 +54,7 @@ namespace OpenEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
if (main_camera) {
|
if (main_camera) {
|
||||||
Renderer2D::BeginScene(main_camera->GetProjection(), main_transform);
|
Renderer2D::BeginScene(main_camera->GetProjection(), main_transform);
|
||||||
|
|
||||||
@@ -70,27 +73,23 @@ namespace OpenEngine {
|
|||||||
|
|
||||||
Renderer2D::EndScene();
|
Renderer2D::EndScene();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::OnUpdateEditor(EditorCamera& camera)
|
void Scene::OnUpdateEditor(EditorCamera& camera)
|
||||||
{
|
{
|
||||||
Renderer2D::BeginScene(camera);
|
Renderer3D::BeginScene(camera);
|
||||||
|
|
||||||
auto group = registry.group<TransformComponent>(entt::get<SpriteRendererComponent>);
|
auto view = registry.view<TransformComponent, MeshComponent>();
|
||||||
for (auto entity : group)
|
|
||||||
{
|
|
||||||
auto [transform, sprite] = group.get<TransformComponent, SpriteRendererComponent>(entity);
|
|
||||||
|
|
||||||
if (sprite.texture)
|
for (const auto& entity : view) {
|
||||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
auto [transform, mesh] = view.get<TransformComponent, MeshComponent>(entity);
|
||||||
sprite.texture, sprite.color, (int)entity, sprite.tiling_factor);
|
|
||||||
else
|
|
||||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
|
||||||
sprite.color, (int)entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
Renderer2D::EndScene();
|
Renderer3D::DrawMesh(mesh.mesh, GetTransformFromComp(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
Renderer3D::EndScene();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::OnViewportResize(uint32_t width, uint32_t height)
|
void Scene::OnViewportResize(uint32_t width, uint32_t height)
|
||||||
@@ -151,4 +150,9 @@ namespace OpenEngine {
|
|||||||
void Scene::OnComponentAdded<NativeScriptComponent>(Entity entity, NativeScriptComponent& component)
|
void Scene::OnComponentAdded<NativeScriptComponent>(Entity entity, NativeScriptComponent& component)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
void Scene::OnComponentAdded<MeshComponent>(Entity entity, MeshComponent& component)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ namespace OpenEngine {
|
|||||||
auto transformComponent = entity["TransformComponent"];
|
auto transformComponent = entity["TransformComponent"];
|
||||||
if (transformComponent)
|
if (transformComponent)
|
||||||
{
|
{
|
||||||
auto& tc = deserializedEntity.AddComponents<TransformComponent>();
|
auto& tc = deserializedEntity.AddComponent<TransformComponent>();
|
||||||
tc.translation = transformComponent["Translation"].as<glm::vec3>();
|
tc.translation = transformComponent["Translation"].as<glm::vec3>();
|
||||||
tc.rotation = transformComponent["Rotation"].as<glm::vec3>();
|
tc.rotation = transformComponent["Rotation"].as<glm::vec3>();
|
||||||
tc.scale = transformComponent["Scale"].as<glm::vec3>();
|
tc.scale = transformComponent["Scale"].as<glm::vec3>();
|
||||||
@@ -218,7 +218,7 @@ namespace OpenEngine {
|
|||||||
auto cameraComponent = entity["CameraComponent"];
|
auto cameraComponent = entity["CameraComponent"];
|
||||||
if (cameraComponent)
|
if (cameraComponent)
|
||||||
{
|
{
|
||||||
auto& cc = deserializedEntity.AddComponents<CameraComponent>();
|
auto& cc = deserializedEntity.AddComponent<CameraComponent>();
|
||||||
|
|
||||||
auto camera_props = cameraComponent["Camera"];
|
auto camera_props = cameraComponent["Camera"];
|
||||||
cc.camera.SetProjectionType((SceneCamera::ProjectionType)camera_props["ProjectionType"].as<int>());
|
cc.camera.SetProjectionType((SceneCamera::ProjectionType)camera_props["ProjectionType"].as<int>());
|
||||||
@@ -238,7 +238,7 @@ namespace OpenEngine {
|
|||||||
auto spriteRendererComponent = entity["SpriteRendererComponent"];
|
auto spriteRendererComponent = entity["SpriteRendererComponent"];
|
||||||
if (spriteRendererComponent)
|
if (spriteRendererComponent)
|
||||||
{
|
{
|
||||||
auto& src = deserializedEntity.AddComponents<SpriteRendererComponent>();
|
auto& src = deserializedEntity.AddComponent<SpriteRendererComponent>();
|
||||||
src.color = spriteRendererComponent["Color"].as<glm::vec4>();
|
src.color = spriteRendererComponent["Color"].as<glm::vec4>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user