CSEngine
Loading...
Searching...
No Matches
CameraComponent.h
1#pragma once
2
3#include "SComponent.h"
4#include "../Util/Vector.h"
5#include "../Util/Render/CameraBase.h"
6
7namespace CSE {
9 mat4 camera;
10 mat4 projection;
11 vec3 cameraPosition;
12 bool isCube;
13
14 CameraMatrixStruct(const mat4& camera, const mat4& projection, const vec3& cameraPosition, bool isCube = false)
15 : camera(camera), projection(projection), cameraPosition(cameraPosition), isCube(isCube) {}
16 };
17
18 static mat4 CubeCameraMatrix[] = {
19 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
20 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(-1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
21 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f)),
22 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f)),
23 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), vec3(0.0f, -1.0f, 0.0f)),
24 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f), vec3(0.0f, -1.0f, 0.0f))
25 };
26
27 class SFrameBuffer;
28 class STexture;
29
30 class CameraComponent : public SComponent, public CameraBase {
31 public:
32 enum CAMERATYPE {
33 PERSPECTIVE = 1, ORTHO = 2, CUBE = 3
34 };
35 public:
36 COMPONENT_DEFINE_CONSTRUCTOR(CameraComponent);
37
38 ~CameraComponent() override;
39
40 void Exterminate() override;
41
42 void Init() override;
43
44 void Tick(float elapsedTime) override;
45
46 SComponent* Clone(SGameObject* object) override;
47
48 void CopyReference(SComponent* src, std::map<SGameObject*, SGameObject*> lists_obj,
49 std::map<SComponent*, SComponent*> lists_comp) override;
50
51 void SetValue(std::string name_str, Arguments value) override;
52
53 std::string PrintValue() const override;
54
55 mat4 GetCameraMatrix() const;
56
57 vec3 GetCameraPosition() const;
58
59 mat4 GetProjectionMatrix() const {
60 if (!m_isProjectionInited) {
61 SetProjectionMatrix();
62 }
63 return m_projectionMatrix;
64 }
65
66 CameraMatrixStruct GetCameraMatrixStruct() const override;
67
68 void SetTargetVector(const vec3& target);
69
70 void SetTarget(SGameObject* gameObject);
71
72 void SetUp(const vec3& up);
73
74 void SetCameraType(CAMERATYPE type);
75
76 void SetPerspectiveFov(float fov);
77
78 void SetZDepthRange(float near, float far);
79
80 void SetPerspective(float fov, float near, float far);
81
82 void SetOrtho(float left, float right, float top, float bottom);
83
84 void SetCubeCamera();
85
86 void SetProjectionMatrix() const;
87
88 SFrameBuffer* GetFrameBuffer() const override;
89
90 void SetFrameBuffer(SFrameBuffer* frameBuffer);
91
92 BackgroundType GetBackgroundType() override;
93
94 void RenderBackground() const override;
95
96 void SetBackgroundSkybox(STexture* skyboxTexture = nullptr);
97
98 void SetBackgroundColor(vec3&& color);
99
100 void SetBackgroundType(BackgroundType type);
101
102 private:
103 struct BackgroundMapStruct {
104 STexture* map = nullptr;
105 unsigned short mapId = 0;
106 unsigned short viewId = 0;
107 unsigned short projectionId = 0;
108 };
109
110 private:
111 void SetCameraMatrix();
112
113 private:
114 vec3* m_eye;
115 vec3 m_target = vec3(0, 0, -1);
116 vec3 m_up = vec3(0, 1, 0);
117 SGameObject* m_targetObject;
118
119 mutable mat4 m_cameraMatrix;
120 mutable mat4 m_projectionMatrix;
121 vec3 m_resultTarget;
122 SFrameBuffer* m_frameBuffer = nullptr;
123
124 CAMERATYPE m_type = PERSPECTIVE;
125 mutable bool m_isProjectionInited = false;
126
127 //perspective
128 float m_pFov = 45.f;
129 float* m_pRatio;
130
131 //Orthographic
132 float m_oLeft = -1.f;
133 float m_oRight = 1.f;
134 float m_oBottom = -1.f;
135 float m_oTop = 1.f;
136
137 float m_Near = 0.1f;
138 float m_Far = 100.f;
139
140 //Background
141 BackgroundType m_backgroundType = SOLID;
142 vec3 m_backgroundColor = vec3(0.4f, 0.4f, 0.4f);
143 BackgroundMapStruct* m_backgroundMap = nullptr;
144 };
145}