Skip to content

Commit 4d4c760

Browse files
committed
feat: Implement Picking
1 parent af3bc85 commit 4d4c760

30 files changed

Lines changed: 683 additions & 115 deletions

include/Game/Component/Camera.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Component.h"
44
#include "Game/Export.h"
5+
#include "Game/Vector.h"
56

67
#include "Render/Renderer.h"
78
#include "Render/RenderTexture.h"
@@ -31,22 +32,23 @@ namespace sh::game
3132
int depth;
3233

3334
float fovRadians;
34-
35-
glm::vec2 screenSize;
3635
protected:
3736
glm::mat4 matProj;
3837
glm::mat4 matView;
3938

4039
PROPERTY(lookPos)
41-
glm::vec3 lookPos;
42-
glm::vec3 up;
40+
Vec3 lookPos;
41+
Vec3 up;
42+
43+
Vec2 screenSize;
4344
public:
45+
PROPERTY(fov)
4446
float fov;
4547
float nearPlane;
4648
float farPlane;
4749

4850
const glm::mat4& worldToCameraMatrix;
49-
private:
51+
protected:
5052
inline void CalcMatrix();
5153
public:
5254
SH_GAME_API Camera(GameObject& owner);
@@ -67,7 +69,12 @@ namespace sh::game
6769

6870
SH_GAME_API auto GetNative() -> render::Camera&;
6971

70-
SH_GAME_API auto ScreenPointToRay(const glm::vec2& mousePos) const -> phys::Ray;
72+
SH_GAME_API auto ScreenPointToRay(const Vec2& mousePos) const -> phys::Ray;
73+
74+
SH_GAME_API void SetLookPos(const Vec3& pos);
75+
SH_GAME_API auto GetLookPos() const -> const Vec3&;
76+
SH_GAME_API void SetUpVector(const Vec3& up);
77+
SH_GAME_API auto GetUpVector() const -> const Vec3&;
7178
#ifdef SH_EDITOR
7279
SH_GAME_API void OnPropertyChanged(const core::reflection::Property& prop) override;
7380
#endif

include/Game/Component/MeshRenderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace sh::game
4646

4747
/// @brief 모든 Drawable들을 갱신하는 함수.
4848
void RebuildDrawables();
49+
void CleanDrawables();
4950
public:
5051
SH_GAME_API MeshRenderer(GameObject& owner);
5152
SH_GAME_API ~MeshRenderer();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include "Camera.h"
4+
#include "Game/Vector.h"
5+
6+
#include "Render/RenderTexture.h"
7+
#include "Render/IBuffer.h"
8+
9+
#include "Core/Observer.hpp"
10+
11+
#include <functional>
12+
#include <queue>
13+
14+
namespace sh::game
15+
{
16+
class PickingCamera : public Camera
17+
{
18+
COMPONENT(PickingCamera)
19+
private:
20+
PROPERTY(renderTex, core::PropertyOption::invisible)
21+
render::RenderTexture* renderTex = nullptr;
22+
PROPERTY(followCamera)
23+
Camera* followCamera = nullptr;
24+
25+
int x = 0, y = 0;
26+
27+
std::unique_ptr<render::IBuffer> buffer;
28+
29+
uint8_t* pixels = nullptr;
30+
public:
31+
struct PixelData
32+
{
33+
uint8_t r, g, b, a;
34+
};
35+
core::Observer<true, uint8_t, uint8_t, uint8_t, uint8_t> pickingCallback;
36+
public:
37+
SH_GAME_API PickingCamera(GameObject& owner);
38+
39+
SH_GAME_API void Awake() override;
40+
SH_GAME_API void BeginUpdate() override;
41+
42+
SH_GAME_API void SetPickingPos(const Vec2& pos);
43+
SH_GAME_API void SetTextureSize(const Vec2& size);
44+
SH_GAME_API void SetFollowCamera(Camera* follow);
45+
};
46+
}//namepspace
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include "MeshRenderer.h"
4+
#include "PickingCamera.h"
5+
6+
#include "Core/Singleton.hpp"
7+
8+
namespace sh::game
9+
{
10+
class PickingRenderer;
11+
12+
/// @brief PickingRenderer 객체의 아이디를 전역적으로 관리하는 클래스
13+
class PickingIdManager
14+
{
15+
private:
16+
SH_GAME_API static inline uint32_t nextId = 1;
17+
SH_GAME_API static inline core::SHashMap<uint32_t, PickingRenderer*> ids{};
18+
SH_GAME_API static inline std::queue<uint32_t> emptyId{};
19+
public:
20+
SH_GAME_API static auto AssignId(PickingRenderer* renderer) -> uint32_t;
21+
SH_GAME_API static auto Get(uint32_t id) -> PickingRenderer*;
22+
SH_GAME_API static void Erase(uint32_t id);
23+
};
24+
25+
class PickingRenderer : public MeshRenderer
26+
{
27+
COMPONENT(PickingRenderer)
28+
private:
29+
PROPERTY(camera)
30+
PickingCamera* camera = nullptr;
31+
32+
uint32_t id = 0;
33+
public:
34+
SH_GAME_API PickingRenderer(GameObject& owner);
35+
SH_GAME_API ~PickingRenderer();
36+
37+
SH_GAME_API void Awake() override;
38+
SH_GAME_API void Update() override;
39+
40+
SH_GAME_API void SetCamera(PickingCamera& camera);
41+
42+
SH_GAME_API void OnPropertyChanged(const core::reflection::Property& prop) override;
43+
};
44+
}//namespace

include/Render/BufferFactory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace sh::render
1515
class BufferFactory
1616
{
1717
private:
18-
static auto CreateVkUniformBuffer(const VulkanRenderer& renderer, std::size_t size) -> std::unique_ptr<IBuffer>;
18+
static auto CreateVkUniformBuffer(const VulkanRenderer& renderer, std::size_t size, bool bTransferDst) -> std::unique_ptr<IBuffer>;
1919
public:
20-
SH_RENDER_API static auto Create(const Renderer& renderer, std::size_t size) -> std::unique_ptr<IBuffer>;
20+
SH_RENDER_API static auto Create(const Renderer& renderer, std::size_t size, bool bTransferDst = false) -> std::unique_ptr<IBuffer>;
2121
SH_RENDER_API static auto CreateUniformBuffer(const Renderer& renderer, const Shader& shader, Shader::UniformType type) -> std::unique_ptr<IUniformBuffer>;
2222
};
2323
}//namespace

include/Render/RenderTexture.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ namespace sh::render
1919
uint32_t height;
2020

2121
core::SyncArray<std::unique_ptr<Framebuffer>> framebuffer;
22+
23+
bool bReadUsage = false;
2224
public:
23-
SH_RENDER_API RenderTexture();
25+
SH_RENDER_API RenderTexture(Texture::TextureFormat format = Texture::TextureFormat::SRGBA32);
2426
SH_RENDER_API RenderTexture(RenderTexture&& other) noexcept;
2527
SH_RENDER_API ~RenderTexture();
26-
28+
29+
/// @brief CPU에서 텍스쳐를 읽을 목적으로 쓰는지 정하는 함수
30+
/// @param bReadUsage true면 CPU에서 읽을 수 있게 한다.
31+
SH_RENDER_API void SetReadUsage(bool bReadUsage) noexcept;
2732
SH_RENDER_API void Build(Renderer& renderer) override;
2833

2934
/// @brief [렌더 스레드용] 프레임 버퍼를 가져온다.
3035
/// @return 프레임버퍼 포인터
3136
SH_RENDER_API auto GetFramebuffer() const -> Framebuffer*;
32-
37+
SH_RENDER_API auto GetPixelData() const -> const std::vector<Byte>& override;
3338
SH_RENDER_API void SetSize(uint32_t width, uint32_t height);
3439
SH_RENDER_API auto GetSize() const -> glm::vec2;
3540

include/Render/Texture.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
#include "Export.h"
44

5+
#include "Core/ISyncable.h"
56
#include "Core/NonCopyable.h"
67
#include "Core/SObject.h"
7-
#include "Core/ISyncable.h"
88

99
#include <cstdint>
10-
#include <vector>
1110
#include <memory>
12-
#include <array>
11+
#include <vector>
1312

1413
namespace sh::render
1514
{
@@ -27,6 +26,8 @@ namespace sh::render
2726
public:
2827
enum class TextureFormat
2928
{
29+
SRGB24,
30+
SRGBA32,
3031
RGB24,
3132
RGBA32
3233
};

include/Render/VulkanImpl/VulkanBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "VulkanConfig.h"
4-
#include "IBuffer.h"
4+
#include "Render/IBuffer.h"
55

66
#include "Render/Export.h"
77

include/Render/VulkanImpl/VulkanFramebuffer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace sh::render::impl
2929

3030
uint32_t width, height;
3131
VkFormat format;
32+
33+
bool bTransferSrc = false;
3234
private:
3335
void CreateRenderPass();
3436
auto FindSupportedDepthFormat() -> VkFormat;
@@ -41,8 +43,9 @@ namespace sh::render::impl
4143
SH_RENDER_API auto operator=(VulkanFramebuffer&& other) noexcept -> VulkanFramebuffer&;
4244

4345
SH_RENDER_API auto Create(uint32_t width, uint32_t height, VkImageView img, VkFormat format) -> VkResult;
44-
SH_RENDER_API auto CreateOffScreen(uint32_t width, uint32_t height) -> VkResult;
46+
SH_RENDER_API auto CreateOffScreen(uint32_t width, uint32_t height, VkFormat format = VkFormat::VK_FORMAT_R8G8B8A8_SRGB, bool bTransferSrc = false) -> VkResult;
4547
SH_RENDER_API void Clean();
48+
SH_RENDER_API void TransferImageToBuffer(VulkanCommandBuffer* cmd, VkQueue queue, VkBuffer buffer, int x, int y);
4649

4750
SH_RENDER_API auto GetRenderPass() const -> VkRenderPass;
4851
SH_RENDER_API auto GetVkFramebuffer() const -> VkFramebuffer;

include/Render/VulkanImpl/VulkanImageBuffer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "VulkanConfig.h"
3+
#include "VulkanCommandBuffer.h"
44
#include "Render/Export.h"
55

66
#include "Core/NonCopyable.h"
@@ -37,6 +37,8 @@ namespace sh::render::impl
3737
VkPhysicalDeviceProperties* gpuProp = nullptr) -> VkResult;
3838
SH_RENDER_API void Clean();
3939

40+
SH_RENDER_API void TransitionImageLayout(VkQueue queue, VulkanCommandBuffer* cmd, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
41+
4042
SH_RENDER_API auto GetImage() const ->VkImage;
4143
SH_RENDER_API auto GetImageView() const -> VkImageView;
4244
SH_RENDER_API auto GetSampler() const -> VkSampler;

0 commit comments

Comments
 (0)