/* * 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 "CubesExampleApp.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 #pragma clang diagnostic push #pragma ide diagnostic ignored "cert-msc50-cpp" #pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions" uint32_t GEOS = 3000, OBJECTS = 10000, DYNAMIC = 1000; namespace OpenVulkano { using namespace Scene; using namespace Input; using namespace Math; class CubesExampleAppImpl final : public CubesExampleApp { OpenVulkano::Scene::Scene scene; PerspectiveCamera cam; FreeCamCameraController camController; Material mat; Shader shader; std::vector drawablesPool; std::vector nodesPool; Vector3f_SIMD position = {0, 0, -10}; std::vector m_geos; UI::SimpleUi m_ui; std::shared_ptr m_perfInfo; std::mt19937 random; std::uniform_real_distribution<> randDistPos {-5.0, 5.0}; Math::Vector3f_SIMD RandomPos() { return { randDistPos(random), randDistPos(random), randDistPos(random) }; } public: void Init() override { auto engineConfig = EngineConfiguration::GetEngineConfiguration(); //engineConfig->SetNumThreads(4); engineConfig->SetPreferFramebufferFormatSRGB(false); std::srand(1); // Fix seed for random numbers scene.Init(); cam.Init(70, 16, 9, 0.1f, 100); scene.SetCamera(&cam); shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/basic"); shader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/basic"); shader.AddVertexInputDescription(Vertex::GetVertexInputDescription()); drawablesPool.resize(GEOS); m_geos.reserve(GEOS); std::uniform_real_distribution<> randDistSize(0.1f, 1.1f); std::uniform_real_distribution<> randDistColor(0, 1); for (uint32_t i = 0; i < GEOS; i++) { m_geos.push_back(GeometryFactory::MakeCube(randDistSize(random), randDistSize(random), randDistSize(random), Vector4f(randDistColor(random), randDistColor(random), randDistColor(random), 1.0f) )); drawablesPool[i].Init(&shader, &m_geos[i], &mat); } nodesPool.resize(OBJECTS); for (uint32_t i = 0; i < OBJECTS; i++) { nodesPool[i].Init(); scene.GetRoot()->AddChild(&nodesPool[i]); if (i < DYNAMIC) nodesPool[i].SetUpdateFrequency(UpdateFrequency::Always); nodesPool[i].AddDrawable(&drawablesPool[std::rand() % GEOS]); nodesPool[i].SetMatrix(Math::Utils::translate(Math::Matrix4f(1), RandomPos())); } GetGraphicsAppManager()->GetRenderer()->SetScene(&scene); camController.Init(&cam); camController.SetDefaultKeybindings(); camController.SetPosition({0, 0, 10}); std::shared_ptr m_perfInfo = std::make_shared(); m_ui.AddElement(m_perfInfo); GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui); } void Tick() override { for (uint32_t i = 0; i < DYNAMIC; i++) { nodesPool[i].SetMatrix(Math::Utils::translate(Math::Matrix4f(1), RandomPos())); } camController.Tick(); } void Close() override {} }; IGraphicsApp* CubesExampleApp::Create() { return new CubesExampleAppImpl(); } std::unique_ptr CubesExampleApp::CreateUnique() { return std::make_unique(); } } #pragma clang diagnostic pop #pragma clang diagnostic pop