Compare commits
6 Commits
vulkan
...
7da00008e0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7da00008e0 | ||
|
|
b7e5ceb1d1 | ||
|
|
f26ee2bca8 | ||
|
|
c189b12365 | ||
|
|
03bca252d3 | ||
|
|
90a6ea00c0 |
93
assets/shaders/color3d.glsl
Normal file
93
assets/shaders/color3d.glsl
Normal file
@@ -0,0 +1,93 @@
|
||||
#type vertex
|
||||
#version 450 core
|
||||
|
||||
layout(location = 0) in vec3 a_Position;
|
||||
layout(location = 1) in vec4 a_Color;
|
||||
layout(location = 2) in vec3 a_Normal;
|
||||
layout(location = 3) in vec2 a_TexCoord;
|
||||
layout(location = 4) in int a_EntityID;
|
||||
|
||||
layout(std140, binding = 0) uniform Camera
|
||||
{
|
||||
mat4 u_ViewProjection;
|
||||
vec3 u_ViewPosition;
|
||||
};
|
||||
|
||||
layout(std140, binding = 1) uniform Transform
|
||||
{
|
||||
mat4 u_Transform;
|
||||
};
|
||||
|
||||
struct VertexOutput
|
||||
{
|
||||
vec4 Color;
|
||||
vec2 TexCoord;
|
||||
vec3 Normal;
|
||||
vec3 ViewPos;
|
||||
};
|
||||
|
||||
layout (location = 0) out VertexOutput Output;
|
||||
layout (location = 4) out flat int v_EntityID;
|
||||
layout (location = 5) out vec4 v_Pos;
|
||||
|
||||
void main()
|
||||
{
|
||||
Output.Color = a_Color;
|
||||
Output.TexCoord = a_TexCoord;
|
||||
Output.Normal = mat3(transpose(inverse(u_Transform))) * a_Normal;
|
||||
Output.ViewPos = u_ViewPosition;
|
||||
|
||||
v_EntityID = a_EntityID;
|
||||
|
||||
v_Pos = u_Transform * vec4(a_Position, 1.0);
|
||||
gl_Position = u_ViewProjection * v_Pos;
|
||||
}
|
||||
|
||||
#type fragment
|
||||
#version 450 core
|
||||
|
||||
layout (location = 0) out vec4 color;
|
||||
layout (location = 1) out int color2;
|
||||
|
||||
struct VertexOutput
|
||||
{
|
||||
vec4 Color;
|
||||
vec2 TexCoord;
|
||||
vec3 Normal;
|
||||
vec3 ViewPos;
|
||||
};
|
||||
|
||||
layout (location = 0) in VertexOutput Input;
|
||||
layout (location = 4) in flat int v_EntityID;
|
||||
layout (location = 5) in vec4 v_Pos;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lightPos = vec3(0.0f, 1.0f, 0.0f);
|
||||
vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);
|
||||
|
||||
// Ambiant lighting
|
||||
float ambientStrength = 0.8;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
|
||||
// Diffuse lighting
|
||||
vec3 norm = normalize(Input.Normal);
|
||||
vec3 lightDir = normalize(lightPos - v_Pos.xyz);
|
||||
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
// Specular highlight
|
||||
float specularStrength = 0.5;
|
||||
vec3 viewDir = normalize(Input.ViewPos - v_Pos.xyz); // .xyz here too
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * lightColor;
|
||||
|
||||
vec3 result = (ambient + diffuse + specular) * Input.Color.rgb; // objectColor → Input.Color.rgb
|
||||
|
||||
// Output
|
||||
color = vec4(result, 1.0); // vec3 → vec4
|
||||
color2 = v_EntityID;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ imgui/1.92.5-docking
|
||||
spdlog/1.16.0
|
||||
entt/3.16.0
|
||||
yaml-cpp/0.8.0
|
||||
joltphysics/5.2.0
|
||||
|
||||
[generators]
|
||||
CMakeDeps
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
#ifndef EDITOR_HPP
|
||||
#define EDITOR_HPP
|
||||
|
||||
#include <filesystem>
|
||||
#include <X11/X.h>
|
||||
#include <open_engine.hpp>
|
||||
|
||||
#include "open_engine/application.hpp"
|
||||
#include "open_engine/logging.hpp"
|
||||
#include "open_engine/renderer/texture.hpp"
|
||||
#include "open_engine/scene/entity.hpp"
|
||||
#include "open_engine/renderer/renderer3d.hpp"
|
||||
#include "open_engine/scene/components.hpp"
|
||||
#include "open_engine/scene/scene_serializer.hpp"
|
||||
#include "panels/content_browser.hpp"
|
||||
#include "panels/scene_hierarchy.hpp"
|
||||
@@ -19,6 +17,7 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <ImGuizmo.h>
|
||||
#include <filesystem>
|
||||
#include <cstdint>
|
||||
#include <imgui.h>
|
||||
#include <vector>
|
||||
@@ -95,6 +94,41 @@ namespace OpenEngine {
|
||||
|
||||
icons["play"] = Texture2D::Create("resources/textures/icons/play.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 quad = scene->CreateEntity("quad");
|
||||
quad.AddComponent<TransformComponent>();
|
||||
quad.GetComponents<TransformComponent>().translation = {0, 0, 0};
|
||||
|
||||
Ref<Mesh> quad_mesh = CreateQuad(quad, true);
|
||||
|
||||
quad.AddComponent<MeshComponent>(quad_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);
|
||||
|
||||
/*
|
||||
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
|
||||
//icons["folder"] = Texture2D::Create("resources/textures/icons/folder.png");
|
||||
/*
|
||||
@@ -140,7 +174,7 @@ namespace OpenEngine {
|
||||
}
|
||||
|
||||
framebuffer->Bind();
|
||||
Renderer2D::ResetStats();
|
||||
//Renderer2D::ResetStats();
|
||||
RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f});
|
||||
RenderCommand::Clear();
|
||||
framebuffer->ClearBufferI(1, -1);
|
||||
@@ -193,6 +227,8 @@ namespace OpenEngine {
|
||||
}
|
||||
|
||||
framebuffer->Unbind();
|
||||
|
||||
scene->UpdateEntities();
|
||||
}
|
||||
|
||||
bool EditorKeyBinds(KeyPressedEvent& event)
|
||||
@@ -203,6 +239,29 @@ namespace OpenEngine {
|
||||
bool shift = Input::IsKeyPressed(Key::LeftShift) || Input::IsKeyPressed(Key::RightShift);
|
||||
|
||||
switch(event.GetKeyCode()) {
|
||||
case KeyCode::S: {
|
||||
if (control) {
|
||||
std::string file = FileDialogs::SaveFile("useless");
|
||||
OE_TRACE("saving to filename: {}", file);
|
||||
SaveScene(file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KeyCode::N: {
|
||||
if (control) {
|
||||
scene = CreateRef<Scene>();
|
||||
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
|
||||
scene_hierarchy.Init(scene);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KeyCode::O: {
|
||||
if (control) {
|
||||
std::string file = FileDialogs::OpenFile("useless");
|
||||
OE_DEBUG("loading scene: {}", file);
|
||||
OpenScene(file);}
|
||||
break;
|
||||
}
|
||||
case KeyCode::Q: {
|
||||
guizmo_operation = -1;
|
||||
break;
|
||||
@@ -219,6 +278,12 @@ namespace OpenEngine {
|
||||
guizmo_operation = ImGuizmo::OPERATION::SCALE;
|
||||
break;
|
||||
}
|
||||
case KeyCode::Delete: {
|
||||
scene->MarkEntityForDeletion(selected_entity);
|
||||
scene_hierarchy.ClearSelection();
|
||||
selected_entity = {};
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -282,12 +347,16 @@ namespace OpenEngine {
|
||||
void DrawViewport()
|
||||
{
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0, 0 });
|
||||
|
||||
ImGui::Begin("Viewport");
|
||||
|
||||
bool was_focused = viewport_focused;
|
||||
viewport_focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow);
|
||||
viewport_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow);
|
||||
Application::Get().GetImGuiLayer()->SetBlockEvents((!viewport_focused && !viewport_hovered) || ImGui::IsAnyItemActive());
|
||||
|
||||
if (viewport_focused && !was_focused)
|
||||
editor_camera.ResetMousePosition();
|
||||
|
||||
ImVec2 viewport_panel_size = ImGui::GetContentRegionAvail();
|
||||
|
||||
viewport_size = { viewport_panel_size.x, viewport_panel_size.y };
|
||||
@@ -318,7 +387,7 @@ namespace OpenEngine {
|
||||
|
||||
void DrawStats()
|
||||
{
|
||||
auto stats = Renderer2D::GetStats();
|
||||
//auto stats = Renderer2D::GetStats();
|
||||
static float time = 0;
|
||||
|
||||
time += Time::DeltaTime();
|
||||
@@ -330,17 +399,17 @@ namespace OpenEngine {
|
||||
ImGui::Begin("Statistics");
|
||||
ImGui::SeparatorText("Performance");
|
||||
ImGui::Text("FPS: %0.0f", 1 / Time::DeltaTime());
|
||||
ImGui::Text("Renderer2D:");
|
||||
ImGui::Text("\t\tDraw calls: %d", stats.draw_calls);
|
||||
ImGui::Text("\t\tQuad count: %d", stats.quad_count);
|
||||
ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount());
|
||||
ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount());
|
||||
if (selected_entity) {
|
||||
ImGui::SeparatorText("Entities");
|
||||
ImGui::Text("Selected entity:");
|
||||
ImGui::Text("\t\tname: %s", selected_entity.GetComponents<TagComponent>().tag.c_str());
|
||||
ImGui::Text("\t\tid: %d", (uint32_t)selected_entity);
|
||||
}
|
||||
//ImGui::Text("Renderer2D:");
|
||||
//ImGui::Text("\t\tDraw calls: %d", stats.draw_calls);
|
||||
//ImGui::Text("\t\tQuad count: %d", stats.quad_count);
|
||||
//ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount());
|
||||
//ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount());
|
||||
//if (selected_entity) {
|
||||
// ImGui::SeparatorText("Entities");
|
||||
// ImGui::Text("Selected entity:");
|
||||
// ImGui::Text("\t\tname: %s", selected_entity.GetComponents<TagComponent>().tag.c_str());
|
||||
// ImGui::Text("\t\tid: %d", (uint32_t)selected_entity);
|
||||
//}
|
||||
ImGui::End();
|
||||
};
|
||||
|
||||
@@ -400,6 +469,14 @@ namespace OpenEngine {
|
||||
}
|
||||
}
|
||||
|
||||
void SaveScene(const std::string& file)
|
||||
{
|
||||
if (!file.empty()) {
|
||||
SceneSerializer serializer(scene);
|
||||
serializer.Serialize(file);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawPlayBar()
|
||||
{
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 2 });
|
||||
@@ -450,10 +527,7 @@ namespace OpenEngine {
|
||||
if (ImGui::MenuItem("Save Scene As", "Ctrl+S")) {
|
||||
std::string file = FileDialogs::SaveFile("useless");
|
||||
OE_TRACE("saving to filename: {}", file);
|
||||
if (!file.empty()) {
|
||||
SceneSerializer serializer(scene);
|
||||
serializer.Serialize(file);
|
||||
}
|
||||
SaveScene(file);
|
||||
}
|
||||
if (ImGui::MenuItem("Open Scene", "Ctrl+O")) {
|
||||
std::string file = FileDialogs::OpenFile("useless");
|
||||
@@ -532,24 +606,3 @@ class EditorApp : public OpenEngine::Application
|
||||
};
|
||||
|
||||
#endif // EDITOR_HPP
|
||||
|
||||
/*
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
std::string OpenFile() {
|
||||
char buffer[1024];
|
||||
// This command opens a native GTK file picker and returns the path
|
||||
FILE* pipe = popen("zenity --file-selection", "r");
|
||||
if (!pipe) return "";
|
||||
|
||||
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
||||
std::string path = buffer;
|
||||
path.erase(path.find_last_not_of("\n") + 1); // Clean newline
|
||||
pclose(pipe);
|
||||
return path;
|
||||
}
|
||||
pclose(pipe);
|
||||
return "";
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -13,12 +13,10 @@ namespace OpenEngine {
|
||||
const std::array<const char*, 3> labels = {"x", "y", "z"});
|
||||
|
||||
void TagOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||
|
||||
void TransformOnImGuiRender(entt::registry& registry, entt::entity entity);
|
||||
|
||||
void SpriteOnImGuiRender(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
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
class EditorLayer;
|
||||
|
||||
class ContentBrowserPanel
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace OpenEngine {
|
||||
|
||||
Entity GetSelectedEntity() const { return selected_context; };
|
||||
void SetSelectedEntity(Entity entity) { selected_context = entity; };
|
||||
void ClearSelection() { selected_context = {}; };
|
||||
|
||||
private:
|
||||
void DrawEntityNode(Entity& entity);
|
||||
|
||||
@@ -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<SpriteRendererComponent>("Sprite Renderer", &SpriteOnImGuiRender);
|
||||
RegisterDrawer<CameraComponent>("Camera", &CameraOnImGuiRender);
|
||||
RegisterDrawer<MeshComponent>("Mesh", &MeshOnImGuiRender);
|
||||
|
||||
scene = context;
|
||||
selected_context = {};
|
||||
@@ -32,17 +33,22 @@ namespace OpenEngine {
|
||||
ImGui::SeparatorText("Add");
|
||||
if (!selected_context.HasComponent<TransformComponent>())
|
||||
if (ImGui::MenuItem("Transform")) {
|
||||
selected_context.AddComponents<TransformComponent>();
|
||||
selected_context.AddComponent<TransformComponent>();
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
if (!selected_context.HasComponent<SpriteRendererComponent>())
|
||||
if (ImGui::MenuItem("Sprite")) {
|
||||
selected_context.AddComponents<SpriteRendererComponent>();
|
||||
selected_context.AddComponent<SpriteRendererComponent>();
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
if (!selected_context.HasComponent<CameraComponent>())
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -127,7 +133,6 @@ namespace OpenEngine {
|
||||
{
|
||||
auto& tag = entity.GetComponents<TagComponent>().tag;
|
||||
|
||||
bool entity_marked_deletion = false;
|
||||
if (renamed_entity == entity && selected_context == entity) {
|
||||
char buffer[255];
|
||||
std::memset(buffer, 0, sizeof(buffer));
|
||||
@@ -155,8 +160,10 @@ namespace OpenEngine {
|
||||
bool opened = ImGui::TreeNodeEx((void*)(uint64_t)(uint32_t)entity, flags, "%s", tag.c_str());
|
||||
|
||||
if (ImGui::BeginPopupContextItem()) {
|
||||
if (ImGui::MenuItem("Delete entity"))
|
||||
entity_marked_deletion = true;
|
||||
if (ImGui::MenuItem("Delete entity")) {
|
||||
scene->MarkEntityForDeletion(entity);
|
||||
selected_context = {};
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
@@ -183,12 +190,6 @@ namespace OpenEngine {
|
||||
if (opened)
|
||||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
if (entity_marked_deletion) {
|
||||
scene->DeleteEntity(entity);
|
||||
if (selected_context == entity)
|
||||
selected_context = {};
|
||||
}
|
||||
}
|
||||
|
||||
void SceneHierarchy::DrawComponents(Entity& entity)
|
||||
@@ -202,6 +203,7 @@ namespace OpenEngine {
|
||||
entity.GetComponents<TagComponent>();
|
||||
TagOnImGuiRender(reg, entity);
|
||||
|
||||
|
||||
std::vector<entt::id_type> component_to_delete; // 0 is null/invalid in entt usually
|
||||
// Iterate through every component type entt knows about
|
||||
for (auto [id, storage] : reg.storage()) {
|
||||
|
||||
@@ -13,6 +13,7 @@ add_definitions( -DOE_ENABLE_ASSERTS )
|
||||
find_package(imgui REQUIRED)
|
||||
find_package(EnTT REQUIRED)
|
||||
find_package(yaml-cpp REQUIRED)
|
||||
find_package(Jolt REQUIRED)
|
||||
|
||||
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
|
||||
file(GLOB IMGUIZMO_SRC_FILES "${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/*.cpp")
|
||||
@@ -52,6 +53,16 @@ target_link_libraries(${PROJECT_EXECUTABLE_NAME} PUBLIC
|
||||
X11
|
||||
yaml-cpp::yaml-cpp
|
||||
nfd
|
||||
shaderc_combined
|
||||
glslang
|
||||
glslang-default-resource-limits
|
||||
SPIRV
|
||||
SPIRV-Tools
|
||||
SPIRV-Tools-opt
|
||||
spirv-cross-core
|
||||
spirv-cross-glsl
|
||||
spirv-cross-reflect
|
||||
Jolt::Jolt
|
||||
)
|
||||
|
||||
#target_link_directories(${PROJECT_EXECUTABLE_NAME} PRIVATE
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "open_engine/renderer/subtexture2d.hpp"
|
||||
#include "open_engine/renderer/framebuffer.hpp"
|
||||
#include "open_engine/renderer/renderer2d.hpp"
|
||||
#include "open_engine/renderer/renderer3d.hpp"
|
||||
#include "open_engine/renderer/renderer.hpp"
|
||||
#include "open_engine/renderer/texture.hpp"
|
||||
#include "open_engine/renderer/buffer.hpp"
|
||||
|
||||
@@ -29,13 +29,14 @@ namespace OpenEngine {
|
||||
class OpenGLIndexBuffer : public IndexBuffer
|
||||
{
|
||||
public:
|
||||
OpenGLIndexBuffer(uint32_t* indices, uint32_t count);
|
||||
OpenGLIndexBuffer(const uint32_t* indices, uint32_t count);
|
||||
virtual ~OpenGLIndexBuffer();
|
||||
|
||||
virtual void Bind() const override;
|
||||
virtual void UnBind() const override;
|
||||
|
||||
virtual uint32_t GetCount() const override { return count; };
|
||||
virtual uint32_t GetID() const override { return id; };
|
||||
|
||||
private:
|
||||
unsigned int id;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef BUFFER_HPP
|
||||
#define BUFFER_HPP
|
||||
|
||||
#include "open_engine/ref_scope.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@@ -82,8 +84,9 @@ namespace OpenEngine {
|
||||
virtual void UnBind() 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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace OpenEngine {
|
||||
glm::vec3 GetForwardDirection() const;
|
||||
const glm::vec3& GetPosition() const { return position; }
|
||||
glm::quat GetOrientation() const;
|
||||
void ResetMousePosition();
|
||||
|
||||
float GetPitch() const { return pitch; }
|
||||
float GetYaw() const { return yaw; }
|
||||
@@ -61,6 +62,7 @@ namespace OpenEngine {
|
||||
glm::vec3 focal_point = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
glm::vec2 initial_mouse_position = { 0.0f, 0.0f };
|
||||
bool mouse_button_was_pressed = false;
|
||||
|
||||
float distance = 10.0f;
|
||||
float pitch = 0.0f, yaw = 0.0f;
|
||||
|
||||
53
open_engine/include/open_engine/renderer/renderer3d.hpp
Normal file
53
open_engine/include/open_engine/renderer/renderer3d.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef RENDERER3D_HPP
|
||||
#define RENDERER3D_HPP
|
||||
|
||||
#include "open_engine/renderer/editor_camera.hpp"
|
||||
#include "open_engine/renderer/vertex_array.hpp"
|
||||
#include "open_engine/renderer/buffer.hpp"
|
||||
#include "open_engine/scene/scene_camera.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);
|
||||
Ref<Mesh> CreateQuad(uint32_t id = -1, bool back_face = false);
|
||||
// ==================================================
|
||||
|
||||
class Renderer3D
|
||||
{
|
||||
public:
|
||||
static void Init();
|
||||
static void Shutdown();
|
||||
|
||||
static void BeginScene(const SceneCamera& 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);
|
||||
};
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
#include "open_engine/renderer/buffer.hpp"
|
||||
#include "open_engine/ref_scope.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
#define COMPONENTS_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/renderer/texture.hpp"
|
||||
#include "open_engine/renderer/renderer3d.hpp"
|
||||
|
||||
#include <glm/ext/matrix_transform.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
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace OpenEngine {
|
||||
Entity(const Entity& other) = default;
|
||||
|
||||
template<typename T, typename ... Args>
|
||||
T& AddComponents(Args&&... args)
|
||||
T& AddComponent(Args&&... args)
|
||||
{
|
||||
OE_ASSERT(!HasComponent<T>(), "Entity already has component.");
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "open_engine/renderer/editor_camera.hpp"
|
||||
|
||||
#include <entt/entity/fwd.hpp>
|
||||
#include <entt/entt.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
@@ -13,11 +14,14 @@ namespace OpenEngine {
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene() {};
|
||||
Scene() = default;
|
||||
~Scene() = default;
|
||||
|
||||
Entity CreateEntity(const std::string& name = std::string());
|
||||
void DeleteEntity(Entity entity);
|
||||
void DeleteEntity(entt::entity entity);
|
||||
void MarkEntityForDeletion(Entity entity);
|
||||
|
||||
void UpdateEntities();
|
||||
|
||||
void OnUpdateRuntime();
|
||||
void OnUpdateEditor(EditorCamera& camera);
|
||||
@@ -36,6 +40,8 @@ namespace OpenEngine {
|
||||
|
||||
uint32_t viewport_width = 0, viewport_height = 0;
|
||||
|
||||
std::vector<entt::entity> entities_to_be_deleted;
|
||||
|
||||
friend class SceneSerializer;
|
||||
friend class Entity;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <core.hpp>
|
||||
#include <events/application_event.hpp>
|
||||
#include <renderer/renderer3d.hpp>
|
||||
#include <renderer/renderer2d.hpp>
|
||||
#include <input/input_system.hpp>
|
||||
#include <imgui/imgui_layer.hpp>
|
||||
@@ -28,7 +29,8 @@ namespace OpenEngine {
|
||||
{
|
||||
OE_PROFILE_SCOPE("Initializing Renderers");
|
||||
Renderer::Init();
|
||||
Renderer2D::Init();
|
||||
//Renderer2D::Init();
|
||||
Renderer3D::Init();
|
||||
}
|
||||
|
||||
imgui_layer = std::make_shared<ImGuiLayer>();
|
||||
@@ -39,7 +41,8 @@ namespace OpenEngine {
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
OpenEngine::Renderer2D::Shutdown();
|
||||
//OpenEngine::Renderer2D::Shutdown();
|
||||
Renderer3D::Shutdown();
|
||||
}
|
||||
|
||||
void Application::Run()
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace OpenEngine {
|
||||
// Index Buffer =====================================================
|
||||
// ==================================================================
|
||||
|
||||
OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count)
|
||||
OpenGLIndexBuffer::OpenGLIndexBuffer(const uint32_t* indices, uint32_t count)
|
||||
: count(count)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
@@ -68,7 +68,7 @@ namespace OpenEngine {
|
||||
|
||||
void OpenGLIndexBuffer::Bind() const
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||
}
|
||||
void OpenGLIndexBuffer::UnBind() const
|
||||
{
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace OpenEngine {
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
}
|
||||
|
||||
void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
|
||||
@@ -66,6 +67,9 @@ namespace OpenEngine {
|
||||
{
|
||||
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();
|
||||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace OpenEngine {
|
||||
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();
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include "logging.hpp"
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <renderer/editor_camera.hpp>
|
||||
@@ -63,10 +62,11 @@ namespace OpenEngine {
|
||||
|
||||
void EditorCamera::OnUpdate()
|
||||
{
|
||||
|
||||
if (Input::IsKeyPressed(Key::LeftAlt)) {
|
||||
const glm::vec2& mouse{ Input::GetMouseX(), Input::GetMouseY() };
|
||||
glm::vec2 delta = (mouse - initial_mouse_position) * 0.003f;
|
||||
initial_mouse_position = mouse;
|
||||
const glm::vec2& mouse{ Input::GetMouseX(), Input::GetMouseY() };
|
||||
glm::vec2 delta = (mouse - initial_mouse_position) * 0.003f;
|
||||
initial_mouse_position = mouse;
|
||||
|
||||
if (Input::IsMouseButtonPressed(Mouse::ButtonMiddle)) {
|
||||
MousePan(delta);
|
||||
@@ -151,4 +151,10 @@ namespace OpenEngine {
|
||||
return glm::quat(glm::vec3(-pitch, -yaw, 0.0f));
|
||||
}
|
||||
|
||||
void EditorCamera::ResetMousePosition()
|
||||
{
|
||||
initial_mouse_position = { Input::GetMouseX(), Input::GetMouseY() };
|
||||
mouse_button_was_pressed = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "renderer/editor_camera.hpp"
|
||||
#include "renderer/uniform_buffer.hpp"
|
||||
#include <pch.hpp>
|
||||
|
||||
#include "renderer/editor_camera.hpp"
|
||||
#include "renderer/uniform_buffer.hpp"
|
||||
#include <renderer/render_command.hpp>
|
||||
#include <renderer/vertex_array.hpp>
|
||||
#include <renderer/renderer2d.hpp>
|
||||
@@ -121,6 +121,8 @@ namespace OpenEngine {
|
||||
|
||||
void Renderer2D::Shutdown()
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
renderer_data.white_texture.reset();
|
||||
renderer_data.vertex_array.reset();
|
||||
renderer_data.vertex_buffer.reset();
|
||||
@@ -130,8 +132,6 @@ namespace OpenEngine {
|
||||
|
||||
for (uint32_t i = 0; i < renderer_data.texture_slot_index; i++)
|
||||
renderer_data.texture_slots[i].reset();
|
||||
|
||||
OE_PROFILE_FUNCTION();
|
||||
}
|
||||
|
||||
void Renderer2D::BeginScene(const OrthographicCamera& camera)
|
||||
|
||||
246
open_engine/src/open_engine/renderer/renderer3d.cpp
Normal file
246
open_engine/src/open_engine/renderer/renderer3d.cpp
Normal file
@@ -0,0 +1,246 @@
|
||||
#include "logging.hpp"
|
||||
#include "scene/scene_camera.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->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->index_buffer = IndexBuffer::Create(indices.data(), indices.size());
|
||||
mesh->vertex_array->SetIndexBuffer(mesh->index_buffer);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Ref<Mesh> CreateQuad(uint32_t id, bool back_face)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
OE_CORE_DEBUG("Creating quad with id {}.", id);
|
||||
|
||||
std::vector<MeshVertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
if (back_face) {
|
||||
vertices = {
|
||||
// Front face (normal 0, 0, 1)
|
||||
{{-0.5f, -0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {0,0}, id},
|
||||
{{-0.5f, 0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {0,1}, id},
|
||||
{{ 0.5f, 0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {1,1}, id},
|
||||
{{ 0.5f, -0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {1,0}, id},
|
||||
|
||||
// Back face (normal 0, 0, -1)
|
||||
{{-0.5f, -0.5f, -0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,-1}, {0,0}, id},
|
||||
{{-0.5f, 0.5f, -0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,-1}, {0,1}, id},
|
||||
{{ 0.5f, 0.5f, -0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,-1}, {1,1}, id},
|
||||
{{ 0.5f, -0.5f, -0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,-1}, {1,0}, id}
|
||||
};
|
||||
|
||||
indices = {
|
||||
// Front face (z=0.5)
|
||||
0, 2, 1, 0, 3, 2,
|
||||
// Back face (z=-0.5)
|
||||
4, 5, 6, 4, 6, 7
|
||||
};
|
||||
} else {
|
||||
vertices = {
|
||||
// Front face (normal 0, 0, 1)
|
||||
{{-0.5f, -0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {0,0}, id},
|
||||
{{-0.5f, 0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {0,1}, id},
|
||||
{{ 0.5f, 0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {1,1}, id},
|
||||
{{ 0.5f, -0.5f, 0.0f}, { 0.5f, 0.5f, 0.5f, 1 }, {0,0,1}, {1,0}, id}
|
||||
};
|
||||
|
||||
indices = {
|
||||
// Front face (z=0.5)
|
||||
0, 2, 1, 0, 3, 2
|
||||
};
|
||||
}
|
||||
|
||||
return CreateMesh(vertices, indices);
|
||||
}
|
||||
|
||||
Ref<Mesh> CreateCube(uint32_t id)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
OE_CORE_DEBUG("Creating cube with id {}.", id);
|
||||
std::vector<MeshVertex> vertices = {
|
||||
// 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},
|
||||
|
||||
// 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},
|
||||
|
||||
// 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 = {
|
||||
// Front face (z=-0.5)
|
||||
0, 2, 1, 0, 3, 2,
|
||||
// Back face (z=0.5)
|
||||
4, 5, 6, 4, 6, 7,
|
||||
// Right face
|
||||
12, 13, 14, 12, 14, 15,
|
||||
// Left face
|
||||
8, 10, 9, 8, 11, 10,
|
||||
// Top face
|
||||
18, 16, 17, 18, 19, 16,
|
||||
// Bottom face
|
||||
20, 22, 21, 20, 23, 22
|
||||
};
|
||||
|
||||
return CreateMesh(vertices, indices);
|
||||
}
|
||||
|
||||
// ==================================================
|
||||
struct Renderer3DData
|
||||
{
|
||||
Ref<Shader> color_shader_3d;
|
||||
|
||||
struct CameraData
|
||||
{
|
||||
glm::mat4 view_projection;
|
||||
glm::vec3 view_position;
|
||||
};
|
||||
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();
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
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 SceneCamera& camera, const glm::mat4& transform)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
renderer_data.camera_buffer.view_projection = camera.GetProjection() * glm::inverse(transform);
|
||||
renderer_data.camera_buffer.view_position = transform[3];
|
||||
|
||||
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_buffer.view_position = camera.GetPosition();
|
||||
|
||||
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,26 +1,42 @@
|
||||
#include <cstdint>
|
||||
#include <entt/entity/fwd.hpp>
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <renderer/renderer2d.hpp>
|
||||
#include <renderer/renderer3d.hpp>
|
||||
#include <scene/components.hpp>
|
||||
#include <scene/entity.hpp>
|
||||
#include <scene/scene.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
Entity Scene::CreateEntity(const std::string& name)
|
||||
{
|
||||
Entity entity = { registry.create(), this };
|
||||
|
||||
auto& tag = entity.AddComponents<TagComponent>();
|
||||
auto& tag = entity.AddComponent<TagComponent>();
|
||||
tag.tag = name.empty() ? "Entity" : name;
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
void Scene::DeleteEntity(Entity entity)
|
||||
void Scene::DeleteEntity(entt::entity entity)
|
||||
{
|
||||
registry.destroy(entity);
|
||||
}
|
||||
|
||||
void Scene::MarkEntityForDeletion(Entity entity)
|
||||
{
|
||||
entities_to_be_deleted.emplace_back(entity);
|
||||
}
|
||||
|
||||
void Scene::UpdateEntities()
|
||||
{
|
||||
for (auto& entity : entities_to_be_deleted)
|
||||
DeleteEntity(entity);
|
||||
|
||||
entities_to_be_deleted.clear();
|
||||
}
|
||||
|
||||
void Scene::OnUpdateRuntime()
|
||||
{
|
||||
{
|
||||
@@ -53,44 +69,42 @@ namespace OpenEngine {
|
||||
}
|
||||
|
||||
if (main_camera) {
|
||||
Renderer2D::BeginScene(main_camera->GetProjection(), main_transform);
|
||||
Renderer3D::BeginScene(*main_camera, main_transform);
|
||||
|
||||
auto view = registry.view<TransformComponent, SpriteRendererComponent>();
|
||||
auto view = registry.view<TransformComponent, MeshComponent>();
|
||||
|
||||
for (const auto& entity : view) {
|
||||
auto [transform, sprite] = view.get<TransformComponent, SpriteRendererComponent>(entity);
|
||||
auto [transform, mesh] = view.get<TransformComponent, MeshComponent>(entity);
|
||||
|
||||
Renderer3D::DrawMesh(mesh.mesh, GetTransformFromComp(transform));
|
||||
/*
|
||||
if (sprite.texture)
|
||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
||||
sprite.texture, sprite.color, (int)entity, sprite.tiling_factor);
|
||||
else
|
||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
||||
sprite.color, (int)entity);
|
||||
*/
|
||||
}
|
||||
|
||||
Renderer2D::EndScene();
|
||||
Renderer3D::EndScene();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::OnUpdateEditor(EditorCamera& camera)
|
||||
{
|
||||
Renderer2D::BeginScene(camera);
|
||||
Renderer3D::BeginScene(camera);
|
||||
|
||||
auto group = registry.group<TransformComponent>(entt::get<SpriteRendererComponent>);
|
||||
for (auto entity : group)
|
||||
{
|
||||
auto [transform, sprite] = group.get<TransformComponent, SpriteRendererComponent>(entity);
|
||||
auto view = registry.view<TransformComponent, MeshComponent>();
|
||||
|
||||
if (sprite.texture)
|
||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
||||
sprite.texture, sprite.color, (int)entity, sprite.tiling_factor);
|
||||
else
|
||||
Renderer2D::DrawQuad(GetTransformFromComp(transform),
|
||||
sprite.color, (int)entity);
|
||||
}
|
||||
for (const auto& entity : view) {
|
||||
auto [transform, mesh] = view.get<TransformComponent, MeshComponent>(entity);
|
||||
|
||||
Renderer2D::EndScene();
|
||||
Renderer3D::DrawMesh(mesh.mesh, GetTransformFromComp(transform));
|
||||
}
|
||||
|
||||
Renderer3D::EndScene();
|
||||
}
|
||||
|
||||
void Scene::OnViewportResize(uint32_t width, uint32_t height)
|
||||
@@ -151,4 +165,9 @@ namespace OpenEngine {
|
||||
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"];
|
||||
if (transformComponent)
|
||||
{
|
||||
auto& tc = deserializedEntity.AddComponents<TransformComponent>();
|
||||
auto& tc = deserializedEntity.AddComponent<TransformComponent>();
|
||||
tc.translation = transformComponent["Translation"].as<glm::vec3>();
|
||||
tc.rotation = transformComponent["Rotation"].as<glm::vec3>();
|
||||
tc.scale = transformComponent["Scale"].as<glm::vec3>();
|
||||
@@ -218,7 +218,7 @@ namespace OpenEngine {
|
||||
auto cameraComponent = entity["CameraComponent"];
|
||||
if (cameraComponent)
|
||||
{
|
||||
auto& cc = deserializedEntity.AddComponents<CameraComponent>();
|
||||
auto& cc = deserializedEntity.AddComponent<CameraComponent>();
|
||||
|
||||
auto camera_props = cameraComponent["Camera"];
|
||||
cc.camera.SetProjectionType((SceneCamera::ProjectionType)camera_props["ProjectionType"].as<int>());
|
||||
@@ -238,7 +238,7 @@ namespace OpenEngine {
|
||||
auto spriteRendererComponent = entity["SpriteRendererComponent"];
|
||||
if (spriteRendererComponent)
|
||||
{
|
||||
auto& src = deserializedEntity.AddComponents<SpriteRendererComponent>();
|
||||
auto& src = deserializedEntity.AddComponent<SpriteRendererComponent>();
|
||||
src.color = spriteRendererComponent["Color"].as<glm::vec4>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user