From ebd1d85f398c2885cb19d9c82b9138c5a657cd84 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Thu, 4 Jul 2024 12:34:22 +0200 Subject: [PATCH] Add textured cube example app --- .../ExampleApps/TexturedCubeExampleApp.cpp | 98 +++++++++++++++++++ .../ExampleApps/TexturedCubeExampleApp.hpp | 27 +++++ examples/main.cpp | 3 + 3 files changed, 128 insertions(+) create mode 100644 examples/ExampleApps/TexturedCubeExampleApp.cpp create mode 100644 examples/ExampleApps/TexturedCubeExampleApp.hpp diff --git a/examples/ExampleApps/TexturedCubeExampleApp.cpp b/examples/ExampleApps/TexturedCubeExampleApp.cpp new file mode 100644 index 0000000..f63e958 --- /dev/null +++ b/examples/ExampleApps/TexturedCubeExampleApp.cpp @@ -0,0 +1,98 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "TexturedCubeExampleApp.hpp" +#include "Scene/Scene.hpp" +#include "Scene/Shader/Shader.hpp" +#include "Scene/Geometry.hpp" +#include "Scene/GeometryFactory.hpp" +#include "Scene/Material.hpp" +#include "Scene/Vertex.hpp" +#include "Scene/SimpleDrawable.hpp" +#include "Scene/UI/PerformanceInfo.hpp" +#include "Input/InputManager.hpp" +#include "Host/GraphicsAppManager.hpp" +#include "Math/Math.hpp" +#include "Base/EngineConfiguration.hpp" +#include "Controller/FreeCamCameraController.hpp" +#include "Base/FrameMetadata.hpp" + +namespace OpenVulkano +{ + using namespace Scene; + using namespace Input; + using namespace Math; + + class TexturedCubeExampleAppImpl final : public TexturedCubeExampleApp + { + OpenVulkano::Scene::Scene scene; + PerspectiveCamera cam; + OpenVulkano::FreeCamCameraController camController; + Material mat; + Shader shader; + SimpleDrawable drawable; + Node node; + Vector3f_SIMD position = {0, 0, -10}; + + OpenVulkano::Scene::UI::SimpleUi m_ui; + std::shared_ptr m_perfInfo; + + public: + void Init() override + { + std::srand(1); // Fix seed for random numbers + scene.Init(); + cam.Init(70, 16, 9, 0.1f, 100); + scene.SetCamera(&cam); + shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/basic"); + shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic"); + shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription()); + Geometry* geo = GeometryFactory::MakeCube(1, 1, 1, Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); + drawable.Init(&shader, geo, &mat); + node.Init(); + scene.GetRoot()->AddChild(&node); + node.SetUpdateFrequency(UpdateFrequency::Always); + node.AddDrawable(&drawable); + + GetGraphicsAppManager()->GetRenderer()->SetScene(&scene); + + camController.Init(&cam); + camController.SetDefaultKeybindings(); + camController.SetPosition({0, 0, 2}); + + std::shared_ptr m_perfInfo = std::make_shared(); + m_ui.AddElement(m_perfInfo); + GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui); + } + + float t = 0; + void Tick() override + { + t += CURRENT_FRAME.frameTime * 0.25; + + Math::Matrix4f rotation = Math::Utils::rotate(t, Math::Vector3f_SIMD{1.0f, 0.0f, 0.0f}); + rotation *= Math::Utils::rotate(t, Math::Vector3f_SIMD{0.0f, 1.0f, 0.0f}); + rotation *= Math::Utils::rotate(t, Math::Vector3f_SIMD{0.0f, 0.0f, 1.0f}); + node.SetMatrix(rotation); + + camController.Tick(); + } + + void Close() override + {} + }; + + IGraphicsApp* TexturedCubeExampleApp::Create() + { + return new TexturedCubeExampleAppImpl(); + } + + std::unique_ptr TexturedCubeExampleApp::CreateUnique() + { + return std::make_unique(); + } + +} \ No newline at end of file diff --git a/examples/ExampleApps/TexturedCubeExampleApp.hpp b/examples/ExampleApps/TexturedCubeExampleApp.hpp new file mode 100644 index 0000000..3e7ad4d --- /dev/null +++ b/examples/ExampleApps/TexturedCubeExampleApp.hpp @@ -0,0 +1,27 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Base/IGraphicsApp.hpp" +#include + +namespace OpenVulkano +{ + class TexturedCubeExampleApp : public IGraphicsApp + { + public: + static IGraphicsApp* Create(); + + static std::unique_ptr CreateUnique(); + + [[nodiscard]] std::string GetAppName() const final + { return "Textured Cube ExampleApp"; } + + [[nodiscard]] OpenVulkano::Version GetAppVersion() const final + { return {"v1.0"}; } + }; +} \ No newline at end of file diff --git a/examples/main.cpp b/examples/main.cpp index e81daae..18a8431 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -7,6 +7,7 @@ #include "Host/GraphicsAppManager.hpp" #include "ExampleApps/CubesExampleApp.hpp" #include "ExampleApps/MovingCubeApp.hpp" +#include "ExampleApps/TexturedCubeExampleApp.hpp" #include #include @@ -23,6 +24,7 @@ int main(int argc, char** argv) std::vector examples = { "Cubes Example App", "Moving Cube Example App", + "Textured Cube Example App", }; int selectedExample = 0; @@ -38,6 +40,7 @@ int main(int argc, char** argv) { case 0: app = CubesExampleApp::CreateUnique(); break; case 1: app = MovingCubeApp::CreateUnique(); break; + case 2: app = TexturedCubeExampleApp::CreateUnique(); break; default: throw std::runtime_error("Invalid menu selection!"); break; }