Finality Engine
Loading...
Searching...
No Matches
MainScene.h
Go to the documentation of this file.
1#pragma once
2#include <Finality.h>
8
10class MainScene : public FINALITY::Scene
11{
12public:
13 void OnInit() override
14 {
16
17 auto cameraEntity = CreateEntity("Main Camera");
18 auto& camComp = cameraEntity.AddComponent<FINALITY::CameraComponent>(45.0f, 1280.0f / 720.0f, 0.01f, 100.0f);
19
20 auto& cameraTransform = cameraEntity.GetComponent<FINALITY::TransformComponent>();
21 cameraTransform.Position = glm::vec3(0.0f, 1.5f, 5.0f);
22 cameraTransform.Rotation = glm::vec3(-15.0f, 0.0f, 0.0f);
23
24 FINALITY::Renderer::SetActiveCamera(camComp.CameraInstance.get());
25 cameraEntity.AddScript<FINALITY::CameraMovementScript>();
26
27 std::vector<FINALITY::Vertex> cubeVertices = {
28 { { -0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
29 { { 0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
30 { { 0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
31 { { -0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } },
32 { { 0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
33 { { -0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
34 { { -0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
35 { { 0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } },
36 { { -0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
37 { { 0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
38 { { 0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
39 { { -0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } },
40 { { -0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
41 { { 0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
42 { { 0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
43 { { -0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } },
44 { { 0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
45 { { 0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
46 { { 0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
47 { { 0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } },
48 { { -0.3f, -0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } },
49 { { -0.3f, -0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
50 { { -0.3f, 0.3f, 0.3f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f } },
51 { { -0.3f, 0.3f, -0.3f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f } }
52 };
53
54 std::vector<uint32_t> cubeIndices = {
55 0, 1, 2, 2, 3, 0,
56 4, 5, 6, 6, 7, 4,
57 8, 9, 10, 10, 11, 8,
58 12, 13, 14, 14, 15, 12,
59 16, 17, 18, 18, 19, 16,
60 20, 21, 22, 22, 23, 20
61 };
62
63 std::shared_ptr<FINALITY::Mesh> cubeMesh = FINALITY::Mesh::Create(cubeVertices, cubeIndices);
65
66 auto vertexShader = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/shader.vert", device);
67 auto fragmentShader = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/shader.frag", device);
68
69 FINALITY::PipelineConfig cubeConfig;
70 cubeConfig.VertexShader = std::move(vertexShader);
71 cubeConfig.FragmentShader = std::move(fragmentShader);
72 cubeConfig.EnableBlending = false;
73
74 std::shared_ptr<FINALITY::Pipeline> materialPipeline = FINALITY::Pipeline::Create(cubeConfig);
75
76 auto cube1Entity = CreateEntity("PNG Textured Cube");
77 auto& cube1Transform = cube1Entity.GetComponent<FINALITY::TransformComponent>();
78 cube1Transform.Position = glm::vec3(-1.0f, 0.5f, 0.0f);
79 cube1Transform.Rotation = glm::vec3(0.0f);
80 cube1Transform.Scale = glm::vec3(1.0f);
81
82 auto pngMaterial = std::make_shared<FINALITY::Material>(materialPipeline);
83 auto pngTexture = FINALITY::AssetManager::CreateAsset<FINALITY::Texture>("assets/textures/test.png");
84 pngMaterial->SetTexture("textureSampler", pngTexture);
85
86 cube1Entity.AddComponent<FINALITY::MeshComponent>(cubeMesh);
87 cube1Entity.AddComponent<FINALITY::MaterialComponent>(pngMaterial);
88 cube1Entity.AddScript<FINALITY::TestScript>();
89 cube1Entity.AddScript<FINALITY::KeyPrintScript>();
90
91 auto cube3Entity = CreateEntity("PNG Textured Cube2");
92 auto& cube3Transform = cube3Entity.GetComponent<FINALITY::TransformComponent>();
93 cube3Transform.Position = glm::vec3(-2.0f, 0.5f, 0.0f);
94 cube3Transform.Rotation = glm::vec3(0.0f);
95 cube3Transform.Scale = glm::vec3(1.0f);
96
97 cube3Entity.AddComponent<FINALITY::MeshComponent>(cubeMesh);
98 cube3Entity.AddComponent<FINALITY::MaterialComponent>(pngMaterial);
99
100 auto cube2Entity = CreateEntity("KTX Textured Cube");
101 auto& cube2Transform = cube2Entity.GetComponent<FINALITY::TransformComponent>();
102 cube2Transform.Position = glm::vec3(1.0f, 0.5f, 0.0f);
103 cube2Transform.Rotation = glm::vec3(0.0f);
104 cube2Transform.Scale = glm::vec3(1.0f);
105
106 auto ktxMaterial = std::make_shared<FINALITY::Material>(materialPipeline);
107 auto ktxTexture = FINALITY::AssetManager::CreateAsset<FINALITY::Texture>("assets/textures/metal.dds");
108 ktxMaterial->SetTexture("textureSampler", ktxTexture);
109
110 cube2Entity.AddComponent<FINALITY::MeshComponent>(cubeMesh);
111 cube2Entity.AddComponent<FINALITY::MaterialComponent>(ktxMaterial);
112 cube2Entity.AddScript<FINALITY::TestScript>();
113
114 auto planeEntity = CreateEntity("Lava Ground Plane");
115 auto& planeTransform = planeEntity.GetComponent<FINALITY::TransformComponent>();
116 planeTransform.Position = glm::vec3(0.0f, -0.5f, 0.0f);
117 planeTransform.Rotation = glm::vec3(0.0f);
118 planeTransform.Scale = glm::vec3(1.0f);
119
120 std::vector<FINALITY::Vertex> planeVertices = {
121 { { -5.0f, 0.0f, -5.0f }, { 0.5f, 0.5f, 0.5f }, { 0.0f, 0.0f } },
122 { { 5.0f, 0.0f, -5.0f }, { 0.5f, 0.5f, 0.5f }, { 1.0f, 0.0f } },
123 { { 5.0f, 0.0f, 5.0f }, { 0.5f, 0.5f, 0.5f }, { 1.0f, 1.0f } },
124 { { -5.0f, 0.0f, 5.0f }, { 0.5f, 0.5f, 0.5f }, { 0.0f, 1.0f } }
125 };
126
127 std::vector<uint32_t> planeIndices = {
128 0, 3, 2, 2, 1, 0
129 };
130
131 std::shared_ptr<FINALITY::Mesh> planeMesh = FINALITY::Mesh::Create(planeVertices, planeIndices);
132
133 auto lavaVert = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/lava.vert", device);
134 auto lavaFrag = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/lava.frag", device);
135
136 FINALITY::PipelineConfig lavaConfig;
137 lavaConfig.VertexShader = std::move(lavaVert);
138 lavaConfig.FragmentShader = std::move(lavaFrag);
139 lavaConfig.EnableBlending = false;
140
141 std::shared_ptr<FINALITY::Pipeline> lavaPipeline = FINALITY::Pipeline::Create(lavaConfig);
142 auto lavaMaterial = std::make_shared<FINALITY::Material>(lavaPipeline);
143
144 planeEntity.AddComponent<FINALITY::MeshComponent>(planeMesh);
145 planeEntity.AddComponent<FINALITY::MaterialComponent>(lavaMaterial);
146 planeEntity.AddScript<FINALITY::LavaScript>();
147
148 auto ppVert = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/postprocess.vert", device);
149 auto ppFrag = FINALITY::AssetManager::CreateAsset<FINALITY::Shader>("assets/shaders/postprocess.frag", device);
150
152 ppConfig.VertexShader = std::move(ppVert);
153 ppConfig.FragmentShader = std::move(ppFrag);
154 ppConfig.EnableBlending = false;
156
157 std::shared_ptr<FINALITY::Pipeline> postProcessPipeline = FINALITY::Renderer::CreatePostProcessPipeline(ppConfig);
159 }
160
161 void OnUpdate(float ts) override
162 {
164 FINALITY::RenderCommand::SetClearColor(0.05f, 0.02f, 0.02f, 1.0f);
165
166 {
167 FINALITY::ImGUIPanel panel("Panel1");
168 panel.Text("Basic Text & Styling");
169 panel.Separator();
170 panel.TextColored({ 0.2f, 1.0f, 0.2f, 1.0f }, "This is colored text.");
171
172 static bool enableFeature = true;
173 panel.Checkbox("Enable Advanced Settings", &enableFeature);
174
175 static int radioValue = 0;
176 panel.RadioButton("Option A", &radioValue, 0);
177 panel.SameLine();
178 panel.RadioButton("Option B", &radioValue, 1);
179
180 static float sliderFloat = 0.5f;
181 panel.SliderFloat("Float Slider", &sliderFloat, 0.0f, 1.0f, "Value: %.3f");
182
183 static int sliderInt = 10;
184 panel.DragInt("Drag Int", &sliderInt, 1, 0, 100);
185
186 static int currentItem = 0;
187 const char* items[] = { "Item 1", "Item 2", "Item 3" };
188 panel.Combo("Dropdown Combo", &currentItem, items, 3);
189
190 static float col[4] = { 0.4f, 0.7f, 0.0f, 1.0f };
191 panel.ColorEdit4("Color Picker", col);
192
193 if (panel.Button("Action Button")) {
194 // Button clicked logic
195 }
196 }
197
198 {
199 FINALITY::ImGUIPanel panel("Tabs & Graphs Panel");
200
201 if (panel.BeginTabBar("TestTabBar")) {
202 if (panel.BeginTabItem("Plot Tab")) {
203 panel.Text("Real-time data visualization simulation:");
204 static float values[90] = { 0 };
205 static int values_offset = 0;
206 static float refresh_time = 0.0f;
207
208 // Simulate frame data updates
209 values[values_offset] = cosf((float)values_offset * 0.1f) * 2.0f + 3.0f;
210 values_offset = (values_offset + 1) % 90;
211
212 // Pass glm::vec2 instead of ImVec2 for the size
213 panel.PlotLines("Waveform", values, 90, values_offset, "Sine-like", 0.0f, 6.0f, glm::vec2(0.0f, 80.0f));
214 panel.EndTabItem();
215 }
216
217 if (panel.BeginTabItem("Collapsing Tree Tab")) {
218 if (panel.TreeNode("Parent Node")) {
219 panel.Text("Child node leaf contents.");
220 panel.TreePop();
221 }
222 panel.EndTabItem();
223 }
224 panel.EndTabBar();
225 }
226
227 }
228 }
229
230 void OnDestroy() override
231 {
233 }
234};
static std::shared_ptr< T > CreateAsset(const std::string &filePath, Args &&... args)
bool TreeNode(std::string_view label)
bool RadioButton(std::string_view label, int *v, int v_button)
bool Checkbox(std::string_view label, bool *v)
bool BeginTabItem(std::string_view label, bool *p_open=(bool *) 0)
bool BeginTabBar(std::string_view str_id)
void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
bool ColorEdit4(std::string_view label, float col[4])
void Text(std::string_view text)
bool Combo(std::string_view label, int *current_item, const char *const items[], int items_count)
bool SliderFloat(std::string_view label, float *v, float v_min, float v_max, std::string_view format="%.3f")
bool Button(std::string_view label, const glm::vec2 &size=glm::vec2(0, 0))
bool DragInt(std::string_view label, int *v, float v_speed=1.0f, int v_min=0, int v_max=0)
void PlotLines(std::string_view label, const float *values, int values_count, int values_offset=0, std::string_view overlay_text="", float scale_min=FLT_MAX, float scale_max=FLT_MAX, glm::vec2 graph_size=glm::vec2(0.0f, 0.0f))
void TextColored(const glm::vec4 &col, std::string_view text)
static std::shared_ptr< Mesh > Create(const std::vector< Vertex > &vertices)
static std::shared_ptr< Pipeline > Create(const PipelineConfig &config)
static void SetClearColor(float r, float g, float b, float a)
Defines the graphics backend boundary and render submission contract.
static void SetActiveCamera(Camera *camera)
Definition Renderer.h:59
static std::shared_ptr< Pipeline > CreatePostProcessPipeline(const PipelineConfig &config)
static void SetPostProcessPipeline(std::shared_ptr< Pipeline > pipeline)
static RenderDevice * GetDevice()
Definition Renderer.h:57
Owns entities and dispatches script simulation.
Definition Scene.h:36
virtual void OnDestroy()
virtual void OnInit()=0
Entity CreateEntity(const std::string &name=std::string())
virtual void OnUpdate(float ts)
Dispatches activation and simulation callbacks for a scene update.
void OnInit() override
Definition MainScene.h:13
void OnDestroy() override
Definition MainScene.h:230
void OnUpdate(float ts) override
Dispatches activation and simulation callbacks for a scene update.
Definition MainScene.h:161
std::shared_ptr< Shader > VertexShader
Definition Pipeline.h:26
std::shared_ptr< Shader > FragmentShader
Definition Pipeline.h:27