From 02727c74bb21b1b07b79fce0708b653095b1fe27 Mon Sep 17 00:00:00 2001 From: nigel Date: Sun, 29 May 2022 15:23:08 +0200 Subject: [PATCH] Clean up of sandbox * Added a statistics panel * Added comments to `Sandbox.cpp` * Moved utility functions and GUI code out of sandbox.cpp --- BarinkEngine/BarinkEngine.cpp | 47 ++++++- BarinkEngine/Include/BarinkEngine.h | 2 +- BarinkEngine/graphics/shaders/fragment.shader | 4 +- SandboxApplication/GUI.cpp | 31 +++++ SandboxApplication/GUI.h | 7 + SandboxApplication/Sandbox.cpp | 130 ++++++++---------- SandboxApplication/Util.cpp | 32 +++++ SandboxApplication/Util.h | 6 + 8 files changed, 183 insertions(+), 76 deletions(-) create mode 100644 SandboxApplication/GUI.cpp create mode 100644 SandboxApplication/GUI.h create mode 100644 SandboxApplication/Util.cpp create mode 100644 SandboxApplication/Util.h diff --git a/BarinkEngine/BarinkEngine.cpp b/BarinkEngine/BarinkEngine.cpp index c41e385..061ad1f 100644 --- a/BarinkEngine/BarinkEngine.cpp +++ b/BarinkEngine/BarinkEngine.cpp @@ -1,5 +1,7 @@ #include "BarinkEngine.h" #include +#include + extern void Start(); extern void Update(); @@ -9,7 +11,20 @@ extern void Stop(); using namespace BarinkEngine; +struct EngineStatistics{ + unsigned int lastSampleTime; + unsigned int frames; + float frameTime; + unsigned int FPS; +}; + +EngineStatistics* ES; + + int main(int argc, char* argv[]) { + ES = new EngineStatistics(); + ES->frames = 0; + ES->lastSampleTime = 0; // Startup services BarinkWindow MainWindow = BarinkWindow(800, 600); @@ -24,8 +39,27 @@ int main(int argc, char* argv[]) { // First call to setup game Start(); + + ES->lastSampleTime = std::chrono::duration_cast (std::chrono::system_clock::now().time_since_epoch()).count(); + // Runtime loop while (!MainWindow.WindowShouldClose()) { + + ES->frames++; + + unsigned int now = std::chrono::duration_cast (std::chrono::system_clock::now().time_since_epoch()).count(); + unsigned int MilliSecondsPast = ES->lastSampleTime - now; + + if ( MilliSecondsPast >= 1000) { + ES->FPS = 1000 / ES->frames ; + ES->frameTime = ES->frames / 1000; + ES->frames = 0; + ES->lastSampleTime = now; + } + + + + InputSystem.PollEvents(); Update(); @@ -35,6 +69,8 @@ int main(int argc, char* argv[]) { ImmediateGraphicsDraw(); GUISystem.Render(); + + MainWindow.SwapBuffers(); } @@ -45,9 +81,18 @@ int main(int argc, char* argv[]) { // Shutdown Services - + delete ES; return 0; } +void ShowStats() { + ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove ); + + ImGui::Text("Frames: %i", ES->frames); + ImGui::Text("FPS: %i", ES->FPS); + ImGui::Text("Frame Time: %f", ES->frameTime); + ImGui::End(); + +} \ No newline at end of file diff --git a/BarinkEngine/Include/BarinkEngine.h b/BarinkEngine/Include/BarinkEngine.h index 7251c27..4ed7551 100644 --- a/BarinkEngine/Include/BarinkEngine.h +++ b/BarinkEngine/Include/BarinkEngine.h @@ -16,6 +16,6 @@ #include "Graphics/GUI/GUIManager.h" #include "Scene.h" -void WARN(std::string message); +void ShowStats(); \ No newline at end of file diff --git a/BarinkEngine/graphics/shaders/fragment.shader b/BarinkEngine/graphics/shaders/fragment.shader index 7431644..a2320cb 100644 --- a/BarinkEngine/graphics/shaders/fragment.shader +++ b/BarinkEngine/graphics/shaders/fragment.shader @@ -2,6 +2,8 @@ out vec4 FragColor; +uniform vec3 MatColour; + void main(){ - FragColor = vec4(0.5f, 0.5f, 0.0f , 1.0f); + FragColor = vec4(MatColour, 1.0f); } \ No newline at end of file diff --git a/SandboxApplication/GUI.cpp b/SandboxApplication/GUI.cpp new file mode 100644 index 0000000..e10bfea --- /dev/null +++ b/SandboxApplication/GUI.cpp @@ -0,0 +1,31 @@ +#include "GUI.h" + +void CameraTool(Camera* cam) { + + ImGui::Begin("Camera"); + + ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190); + + ImGui::End(); +} + +void ScriptingTool(char* code) { + ImGui::Begin("Scripting"); + + ImGui::InputTextMultiline("Lua Script", code, 255); + bool runCode = ImGui::Button("Run"); + + + ImGui::End(); + +} + + +void transformWindow(Transform& transform, std::string PanelName) { + ImGui::Begin(PanelName.c_str()); + ImGui::InputFloat3("Position:", (float*)&transform.Position[0]); + ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]); + ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]); + ImGui::End(); + +} \ No newline at end of file diff --git a/SandboxApplication/GUI.h b/SandboxApplication/GUI.h new file mode 100644 index 0000000..a525de2 --- /dev/null +++ b/SandboxApplication/GUI.h @@ -0,0 +1,7 @@ +#pragma once +#include "imgui.h" +#include + +void CameraTool(Camera* camera); +void ScriptingTool(char* code); +void transformWindow(Transform& transform, std::string PanelName); \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index 90ab938..47a5b5f 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -1,32 +1,31 @@ #include "BarinkEngine.h" #include "imgui.h" -using namespace BarinkEngine; +#include "GUI.h" +#include "Util.h" +/* +* Define globals +*/ Camera* cam; Renderable* Cube; Renderable* Cube2; +Shader* shader; + char* code = new char[254]; +const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; +const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; -void PrintSceneTree(SceneNode& node, int depth ){ - // Indent name based on depth - std::cout << " "; - for (int i = 0; i < depth; i++) { - std::cout << "-"; - } - - std::cout << " " << node.name << std::endl; - - depth++; - for (auto child : node.children) { - PrintSceneTree(*child, depth); - } -} - - +/* +* Runs once at startup +* - USe to initialize the game/sandbox/demo +*/ void Start() { + /* + Building a very basic scene graph + */ SceneNode MyCube = SceneNode(); MyCube.name = "MyCube"; @@ -48,6 +47,13 @@ void Start() { // Walk scene graph PrintSceneTree(scene.GetRoot(),0); + + shader = new Shader(vertexShaderSource, fragmentShaderSource); + + + /* + * load meshes + */ Cube = Renderable::Load(); Cube2 = Renderable::Load(); Cube->addChild(*Cube2); @@ -56,96 +62,72 @@ void Start() { memset(code, '\0', 254); - } - - -void transformWindow(Transform& transform, std::string PanelName) { - ImGui::Begin(PanelName.c_str()); - ImGui::InputFloat3("Position:", (float*)&transform.Position[0]); - ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]); - ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]); - ImGui::End(); - -} - +/* +* Runs every frame +* - Use to draw Immediate mode graphics (Not meant for HUD's ) +*/ void ImmediateGraphicsDraw() { ImGui::NewFrame(); - - ImGui::Begin("Camera"); + // Show ImGui demo such that I can easily look + // at possible GUI elements to use + ImGui::ShowDemoWindow(); - ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190); - ImGui::End(); + // Show internal BarinkEngine stats + ShowStats(); + // Show different tooling for this specific sandbox + CameraTool(cam); + ScriptingTool(code); + transformWindow(Cube->transform, "Transform (Cube)"); transformWindow(Cube2->transform, "Transform (Cube2)"); - ImGui::Begin("Scripting!!"); - - ImGui::InputTextMultiline("Lua Script", code, 255); - bool runCode = ImGui::Button("Run"); - - - ImGui::End(); - - - ImGui::ShowDemoWindow(); } - - -glm::mat4 CalculateModelMat(Transform& transform) { - - glm::mat4 tran = glm::translate(glm::mat4(), transform.Position); - glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale); - glm::mat4 rot = - glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - - return tran * rot * scale; -} - - +/* +* Runs every frame +* - Meant for game logic ( non-physics related) +*/ void Update() { - - std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; - std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; - Shader shader = Shader(vertexShaderSource, fragmentShaderSource); - - + /* + * NOTE: this needs to move to the renderer + * Render code should not appear in the sandbox file + */ glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - shader.Use(); - shader.setUniformMat4("P", projection); - shader.setUniformMat4("M", CalculateModelMat(Cube->transform)); - shader.setUniformMat4("V", cam->GetViewMatrix()); - shader.setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f)); + shader->Use(); + shader->setUniformMat4("P", projection); + shader->setUniformMat4("M", CalculateModelMat(Cube->transform)); + shader->setUniformMat4("V", cam->GetViewMatrix()); + shader->setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f)); Cube->Draw(); - shader.setUniformMat4("M", CalculateModelMat(Cube2->transform)); - shader.setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f)); + shader->setUniformMat4("M", CalculateModelMat(Cube2->transform)); + shader->setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f)); Cube2->Draw(); } - +/* +* Runs at the end of the program +* - Meant for cleanup +*/ void Stop() { // Cleanup Cube->VAO.Delete(); @@ -156,4 +138,6 @@ void Stop() { delete Cube2; delete Cube; + + delete shader; } \ No newline at end of file diff --git a/SandboxApplication/Util.cpp b/SandboxApplication/Util.cpp new file mode 100644 index 0000000..aa9560a --- /dev/null +++ b/SandboxApplication/Util.cpp @@ -0,0 +1,32 @@ +#include "Util.h" + +void PrintSceneTree(SceneNode& node, int depth) { + // Indent name based on depth + std::cout << " "; + for (int i = 0; i < depth; i++) { + std::cout << "-"; + } + + std::cout << " " << node.name << std::endl; + + depth++; + for (auto child : node.children) { + PrintSceneTree(*child, depth); + } +} + + + +glm::mat4 CalculateModelMat(Transform& transform) { + + glm::mat4 tran = glm::translate(glm::mat4(), transform.Position); + glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale); + glm::mat4 rot = + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + + return tran * rot * scale; +} + diff --git a/SandboxApplication/Util.h b/SandboxApplication/Util.h new file mode 100644 index 0000000..3118ad0 --- /dev/null +++ b/SandboxApplication/Util.h @@ -0,0 +1,6 @@ +#pragma once +#include "BarinkEngine.h" + +void PrintSceneTree(SceneNode& node, int depth); + +glm::mat4 CalculateModelMat(Transform& transform); \ No newline at end of file