initial commit

This commit is contained in:
2025-05-01 23:03:47 +02:00
commit a23fbff52a
200 changed files with 434542 additions and 0 deletions

45
ScopEngine/Runtime/Includes/Graphics/Actor.h git.filemode.normal_file
View File

@@ -0,0 +1,45 @@
#ifndef __SCOP_RENDERER_ACTOR__
#define __SCOP_RENDERER_ACTOR__
#include <Maths/Vec3.h>
#include <Maths/Vec4.h>
#include <Maths/Quaternions.h>
#include <Core/Script.h>
#include <Graphics/Model.h>
namespace Scop
{
class Actor
{
public:
Actor();
Actor(Model model);
inline void AttachScript(std::shared_ptr<ActorScript> script) { p_script = script; }
void Update(NonOwningPtr<class Scene> scene, class Inputs& input, float timestep);
inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec3f position) noexcept { m_position = position; }
inline void SetScale(Vec3f scale) noexcept { m_scale = scale; }
inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; }
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline const Vec3f& GetScale() const noexcept { return m_scale; }
[[nodiscard]] inline const Quatf& GetOrientation() const noexcept { return m_orientation; }
[[nodiscard]] inline const Model& GetModel() const noexcept { return m_model; }
[[nodiscard]] inline Model& GetModelRef() noexcept { return m_model; }
~Actor();
private:
Model m_model;
Quatf m_orientation = Quatf::Identity();
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f };
Vec3f m_scale = Vec3f{ 1.0f, 1.0f, 1.0f };
std::shared_ptr<ActorScript> p_script;
};
}
#endif

View File

@@ -0,0 +1,30 @@
#ifndef __SCOP_CAMERAS_BASE__
#define __SCOP_CAMERAS_BASE__
#include <Maths/Mat4.h>
namespace Scop
{
class BaseCamera
{
public:
BaseCamera() = default;
virtual void Update(class Inputs& input, float aspect, float timestep) {};
[[nodiscard]] inline const Mat4f& GetView() const noexcept { return m_view; }
[[nodiscard]] inline const Mat4f& GetProj() const noexcept { return m_proj; }
[[nodiscard]] virtual const Vec3f& GetPosition() const noexcept = 0;
virtual constexpr std::string GetCameraType() = 0;
virtual ~BaseCamera() = default;
protected:
Mat4f m_view;
Mat4f m_proj;
};
}
#endif

View File

@@ -0,0 +1,46 @@
#ifndef __SCOP_CAMERAS_FIRST_PERSON_3D__
#define __SCOP_CAMERAS_FIRST_PERSON_3D__
#include <Graphics/Cameras/Base.h>
#include <Maths/Vec3.h>
namespace Scop
{
class FirstPerson3D : public BaseCamera
{
public:
FirstPerson3D();
FirstPerson3D(Vec3f position, float fov = 90.0f);
void Update(class Inputs& input, float aspect, float timestep) override;
[[nodiscard]] inline constexpr std::string GetCameraType() override { return "FirstPerson3D"; }
[[nodiscard]] const Vec3f& GetPosition() const noexcept override { return m_position; }
~FirstPerson3D() = default;
private:
void UpdateView();
private:
const Vec3f m_up;
Vec3f m_position;
Vec3f m_left;
Vec3f m_forward;
Vec3f m_target;
Vec3f m_direction;
Vec3f m_mov;
float m_theta = 0.0;
float m_phi = 0.0;
const float m_speed = 50.0f;
const float m_sensivity = 0.7f;
float m_speed_factor = 1.0f;
float m_fov = 90.0f;
bool m_inputs_blocked = false;
};
}
#endif

View File

@@ -0,0 +1,14 @@
#ifndef __SCOP_BMP_LOADER__
#define __SCOP_BMP_LOADER__
#include <filesystem>
#include <Maths/Vec2.h>
#include <Utils/Buffer.h>
namespace Scop
{
CPUBuffer LoadBMPFile(const std::filesystem::path& path, Vec2ui32& dimensions);
}
#endif

View File

@@ -0,0 +1,72 @@
#ifndef __SCOP_OBJ_LOADER__
#define __SCOP_OBJ_LOADER__
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdint>
#include <optional>
#include <filesystem>
#include <Maths/Vec2.h>
#include <Maths/Vec3.h>
#include <Maths/Vec4.h>
namespace Scop
{
struct ObjData
{
struct FaceVertex
{
FaceVertex() : v(-1), t(-1), n(-1) {}
std::int32_t v;
std::int32_t t;
std::int32_t n;
inline bool operator<(const FaceVertex& rhs) const
{
return (v < rhs.v) || (v == rhs.v && t < rhs.t ) || (v == rhs.v && t == rhs.t && n < rhs.n);
}
inline bool operator==(const FaceVertex& rhs) const
{
return (v == rhs.v && t == rhs.t && n == rhs.n);
}
};
using FaceList = std::pair<std::vector<FaceVertex>, std::vector<std::uint32_t>>;
std::vector<Vec4f> color;
std::vector<Vec3f> vertex;
std::vector<Vec3f> normal;
std::vector<Vec2f> tex_coord;
std::map<std::string, FaceList> faces;
};
struct ObjModel
{
std::vector<Vec4f> color;
std::vector<Vec3f> vertex;
std::vector<Vec3f> normal;
std::vector<Vec2f> tex_coord;
std::map<std::string, std::vector<std::uint32_t>> faces;
};
std::optional<ObjData> LoadObjFromFile(const std::filesystem::path& path);
void TesselateObjData(ObjData& data);
ObjModel ConvertObjDataToObjModel(const ObjData& data);
template<typename T>
inline std::istream& operator>>(std::istream& in, std::vector<T>& vec);
template<typename T>
inline std::istream& operator>>(std::istream& in, std::set<T>& vec);
inline std::istream& operator>>(std::istream& in, ObjData::FaceVertex& f);
}
#include <Graphics/Loaders/OBJ.inl>
#endif

View File

@@ -0,0 +1,48 @@
#pragma once
#include <Graphics/Loaders/OBJ.h>
namespace Scop
{
template<typename T>
inline std::istream& operator>>(std::istream& in, std::vector<T>& vec)
{
T temp;
if(in >> temp)
vec.push_back(temp);
return in;
}
template<typename T>
inline std::istream& operator>>(std::istream& in, std::set<T>& vec)
{
T temp;
if(in >> temp)
vec.insert(temp);
return in;
}
inline std::istream& operator>>(std::istream& in, ObjData::FaceVertex& f)
{
std::int32_t val;
if(in >> f.v)
{
if(in.peek() == '/')
{
in.get();
in >> f.t;
in.clear();
if(in.peek() == '/')
{
in.get();
in >> f.n;
in.clear();
}
}
in.clear();
f.v--;
f.t--;
f.n--;
}
return in;
}
}

View File

@@ -0,0 +1,81 @@
#ifndef __SCOP_RENDERER_MATERIAL__
#define __SCOP_RENDERER_MATERIAL__
#include <memory>
#include <Core/EventBus.h>
#include <Renderer/Image.h>
#include <Renderer/Buffer.h>
#include <Renderer/Descriptor.h>
namespace Scop
{
struct MaterialTextures
{
std::shared_ptr<Texture> albedo;
};
struct MaterialData
{
float dissolve_texture_factor = 1.0f;
float dissolve_black_white_colors_factor = 1.0f;
float dissolve_normals_colors_factor = 0.0f;
std::uint8_t __padding[4];
};
class Material
{
friend class Model;
public:
Material() { m_data_buffer.Init(sizeof(m_data)); SetupEventListener(); }
Material(const MaterialTextures& textures) : m_textures(textures) { m_data_buffer.Init(sizeof(m_data)); SetupEventListener(); }
inline void SetMaterialData(const MaterialData& data) noexcept { m_data = data; }
~Material() { m_data_buffer.Destroy(); }
private:
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
inline void SetupEventListener()
{
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
if(event.What() == Event::FrameBeginEventCode)
m_have_been_updated_this_frame = false;
};
EventBus::RegisterListener({ functor, "__ScopMaterial" + std::to_string(reinterpret_cast<std::uintptr_t>(this)) });
}
inline void UpdateDescriptorSet(const DescriptorSet& set)
{
m_set = set.Duplicate();
}
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
{
if(m_have_been_updated_this_frame)
return;
m_set.SetImage(frame_index, 0, *m_textures.albedo);
m_set.SetUniformBuffer(frame_index, 1, m_data_buffer.Get(frame_index));
m_set.Update(frame_index, cmd);
static CPUBuffer buffer(sizeof(MaterialData));
std::memcpy(buffer.GetData(), &m_data, buffer.GetSize());
m_data_buffer.SetData(buffer, frame_index);
m_have_been_updated_this_frame = true;
}
private:
UniformBuffer m_data_buffer;
MaterialTextures m_textures;
MaterialData m_data;
DescriptorSet m_set;
bool m_have_been_updated_this_frame = false;
};
}
#endif

57
ScopEngine/Runtime/Includes/Graphics/Mesh.h git.filemode.normal_file
View File

@@ -0,0 +1,57 @@
#ifndef __SCOPE_RENDERER_MESH__
#define __SCOPE_RENDERER_MESH__
#include <vector>
#include <cstdint>
#include <cstring>
#include <Renderer/Vertex.h>
#include <Renderer/Buffer.h>
#include <Utils/Buffer.h>
namespace Scop
{
class Mesh
{
public:
struct SubMesh
{
VertexBuffer vbo;
IndexBuffer ibo;
std::size_t triangle_count = 0;
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
{
CPUBuffer vb(vertices.size() * sizeof(Vertex));
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
vbo.Init(vb.GetSize());
vbo.SetData(std::move(vb));
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
ibo.Init(ib.GetSize());
ibo.SetData(std::move(ib));
triangle_count = vertices.size() / 3;
}
};
public:
Mesh() = default;
void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn) const noexcept;
void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t submesh_index) const noexcept;
inline std::size_t GetSubMeshCount() const { return m_sub_meshes.size(); }
inline void AddSubMesh(SubMesh mesh) { m_sub_meshes.emplace_back(std::move(mesh)); }
[[nodiscard]] inline SubMesh& GetSubMesh(std::size_t index) { return m_sub_meshes.at(index); }
~Mesh();
private:
std::vector<SubMesh> m_sub_meshes;
};
}
#endif

View File

@@ -0,0 +1,21 @@
#ifndef __SCOP_MESH_FACTORY__
#define __SCOP_MESH_FACTORY__
#include <memory>
#include <Maths/Vec2.h>
#include <Maths/Vec3.h>
namespace Scop
{
std::shared_ptr<class Mesh> CreateQuad();
std::shared_ptr<class Mesh> CreateQuad(float x, float y, float width, float height);
std::shared_ptr<class Mesh> CreateQuad(const Vec2f& position, const Vec2f& size);
std::shared_ptr<class Mesh> CreateCube();
std::shared_ptr<class Mesh> CreatePyramid();
std::shared_ptr<class Mesh> CreateSphere(std::uint32_t x_segments = 32, std::uint32_t y_segments = 32);
std::shared_ptr<class Mesh> CreateCapsule(float radius = 0.5f, float mid_height = 2.0f, int radial_degments = 64, int rings = 8);
std::shared_ptr<class Mesh> CreatePlane(float width, float height, const Vec3f& normal);
}
#endif

44
ScopEngine/Runtime/Includes/Graphics/Model.h git.filemode.normal_file
View File

@@ -0,0 +1,44 @@
#ifndef __SCOPE_RENDERER_MODEL__
#define __SCOPE_RENDERER_MODEL__
#include <memory>
#include <filesystem>
#include <kvf.h>
#include <Maths/Vec3.h>
#include <Graphics/Mesh.h>
#include <Graphics/Material.h>
namespace Scop
{
// Only static meshes for now
class Model
{
friend Model LoadModelFromObjFile(std::filesystem::path path) noexcept;
public:
Model() = default;
Model(std::shared_ptr<Mesh> mesh);
inline void SetMaterial(std::shared_ptr<Material> material, std::size_t mesh_index) { m_materials[mesh_index] = material; }
inline std::size_t GetSubMeshCount() const { return p_mesh->GetSubMeshCount(); }
[[nodiscard]] inline std::shared_ptr<Material> GetMaterial(std::size_t mesh_index) { return m_materials[mesh_index]; }
[[nodiscard]] inline std::vector<std::shared_ptr<Material>>& GetAllMaterials() { return m_materials; }
[[nodiscard]] inline Vec3f GetCenter() const noexcept { return m_center; }
void Draw(VkCommandBuffer cmd, const DescriptorSet& matrices_set, const class GraphicPipeline& pipeline, DescriptorSet& set, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t frame_index) const;
~Model() = default;
private:
Vec3f m_center = { 0.0f, 0.0f, 0.0f };
std::vector<std::shared_ptr<Material>> m_materials;
std::shared_ptr<Mesh> p_mesh;
};
Model LoadModelFromObjFile(std::filesystem::path path) noexcept;
}
#endif

91
ScopEngine/Runtime/Includes/Graphics/Scene.h git.filemode.normal_file
View File

@@ -0,0 +1,91 @@
#ifndef __SCOP_SCENE__
#define __SCOP_SCENE__
#include <memory>
#include <string>
#include <string_view>
#include <Utils/NonOwningPtr.h>
#include <Graphics/Actor.h>
#include <Graphics/Sprite.h>
#include <Renderer/Buffer.h>
#include <Renderer/Descriptor.h>
#include <Renderer/RenderCore.h>
#include <Graphics/Cameras/Base.h>
#include <Renderer/Pipelines/Shader.h>
#include <Renderer/Pipelines/Graphics.h>
namespace Scop
{
struct SceneDescriptor
{
std::shared_ptr<Shader> fragment_shader;
std::shared_ptr<BaseCamera> camera;
bool render_3D_enabled = true;
bool render_2D_enabled = true;
bool render_skybox_enabled = true;
};
class Scene
{
friend class ScopEngine;
public:
struct ForwardData
{
std::shared_ptr<DescriptorSet> matrices_set;
std::shared_ptr<DescriptorSet> albedo_set;
std::shared_ptr<UniformBuffer> matrices_buffer;
bool wireframe = false;
};
public:
Scene(std::string_view name, SceneDescriptor desc);
Scene(std::string_view name, SceneDescriptor desc, NonOwningPtr<Scene> parent);
Actor& CreateActor(Model model) noexcept;
Actor& CreateActor(std::string_view name, Model model);
Sprite& CreateSprite(std::shared_ptr<Texture> texture) noexcept;
Sprite& CreateSprite(std::string_view name, std::shared_ptr<Texture> texture);
[[nodiscard]] inline Scene& AddChildScene(std::string_view name, SceneDescriptor desc) { return m_scene_children.emplace_back(name, std::move(desc), this); }
inline void AddSkybox(std::shared_ptr<CubeTexture> cubemap) { p_skybox = cubemap; }
void SwitchToChild(std::string_view name) const noexcept;
void SwitchToParent() const noexcept;
[[nodiscard]] inline ForwardData& GetForwardData() noexcept { return m_forward; }
[[nodiscard]] inline const std::vector<std::shared_ptr<Actor>>& GetActors() const noexcept { return m_actors; }
[[nodiscard]] inline const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
[[nodiscard]] inline const std::string& GetName() const noexcept { return m_name; }
[[nodiscard]] inline GraphicPipeline& GetPipeline() noexcept { return m_pipeline; }
[[nodiscard]] inline std::shared_ptr<BaseCamera> GetCamera() const { return m_descriptor.camera; }
[[nodiscard]] inline DepthImage& GetDepth() noexcept { return m_depth; }
[[nodiscard]] inline std::shared_ptr<Shader> GetFragmentShader() const { return m_descriptor.fragment_shader; }
[[nodiscard]] inline std::shared_ptr<CubeTexture> GetSkybox() const { return p_skybox; }
[[nodiscard]] inline const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }
~Scene() = default;
private:
Scene() = default;
void Init(NonOwningPtr<class Renderer> renderer);
void Update(class Inputs& input, float delta, float aspect);
void Destroy();
private:
GraphicPipeline m_pipeline;
ForwardData m_forward;
DepthImage m_depth;
SceneDescriptor m_descriptor;
std::shared_ptr<CubeTexture> p_skybox;
std::vector<std::shared_ptr<Actor>> m_actors;
std::vector<std::shared_ptr<Sprite>> m_sprites;
std::vector<Scene> m_scene_children;
std::string m_name;
NonOwningPtr<Scene> p_parent;
};
}
#endif

63
ScopEngine/Runtime/Includes/Graphics/Sprite.h git.filemode.normal_file
View File

@@ -0,0 +1,63 @@
#ifndef __SCOP_RENDERER_SPRITE__
#define __SCOP_RENDERER_SPRITE__
#include <memory>
#include <Maths/Vec2.h>
#include <Maths/Vec4.h>
#include <Core/Script.h>
#include <Graphics/Mesh.h>
#include <Renderer/Descriptor.h>
#include <Renderer/Image.h>
namespace Scop
{
class Sprite
{
friend class Render2DPass;
public:
Sprite(std::shared_ptr<Texture> texture);
inline void AttachScript(std::shared_ptr<SpriteScript> script) { p_script = script; }
void Update(NonOwningPtr<class Scene> scene, class Inputs& input, float timestep);
inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec2ui position) noexcept { m_position = position; }
inline void SetScale(Vec2f scale) noexcept { m_scale = scale; }
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec2ui& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline const Vec2f& GetScale() const noexcept { return m_scale; }
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] inline std::shared_ptr<Texture> GetTexture() const { return p_texture; }
~Sprite();
private:
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
inline void UpdateDescriptorSet(const DescriptorSet& set)
{
m_set = set.Duplicate();
}
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
{
m_set.SetImage(frame_index, 0, *p_texture);
m_set.Update(frame_index, cmd);
}
private:
DescriptorSet m_set;
std::shared_ptr<Texture> p_texture;
std::shared_ptr<class SpriteScript> p_script;
std::shared_ptr<Mesh> p_mesh;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec2ui m_position = Vec2ui{ 0, 0 };
Vec2f m_scale = Vec2f{ 1.0f, 1.0f };
};
}
#endif