From f5c951f1308cf8e07d7c0e61a2e305a0e9e85086 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Mon, 19 Oct 2020 22:09:10 +0200 Subject: [PATCH] Move cubes example app into it's own file --- .../ExampleApps/CubesExampleApp.cpp | 126 ++++++++++++++ .../ExampleApps/CubesExampleApp.hpp | 16 ++ openVulkanoCpp/main.cpp | 158 +----------------- 3 files changed, 151 insertions(+), 149 deletions(-) create mode 100644 openVulkanoCpp/ExampleApps/CubesExampleApp.cpp create mode 100644 openVulkanoCpp/ExampleApps/CubesExampleApp.hpp diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp new file mode 100644 index 0000000..41de265 --- /dev/null +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp @@ -0,0 +1,126 @@ +/* + * 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.hpp" +#include "../Input/InputManager.hpp" +#include "../Host/GraphicsAppManager.hpp" +#include +#include + +using namespace openVulkanoCpp::Scene; +using namespace openVulkanoCpp::Input; + +uint32_t GEOS = 3000, OBJECTS = 10000, DYNAMIC = 1000; + +class CubesExampleAppImpl final : public CubesExampleApp +{ + Scene scene; + PerspectiveCamera cam; + Material mat; + Shader shader; + std::vector drawablesPool; + std::vector nodesPool; + + InputAction* actionForward; + InputAction* actionSide; + InputAction* actionLookUp; + InputAction* actionLookSide; + +public: + std::string GetAppName() override { return "ExampleApp"; } + std::string GetAppVersion() override { return "v1.0"; } + int GetAppVersionAsInt() override { return 1; } + + void Init() override + { + std::srand(1); + scene.Init(); + cam.Init(70, 16, 9, 0.1f, 100); + scene.SetCamera(&cam); + cam.SetMatrix(glm::translate(glm::mat4(1), glm::vec3(0,0,-10))); + shader.Init("Shader/basic", "Shader/basic"); + drawablesPool.resize(GEOS); + for(int i = 0; i < GEOS; i++) + { + Geometry* geo = new Geometry(); + geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, glm::vec4((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); + drawablesPool[i] = new Drawable(); + drawablesPool[i]->Init(geo, &mat); + } + nodesPool.resize(OBJECTS); + for(int i = 0; i < OBJECTS; i++) + { + nodesPool[i] = new Node(); + 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(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); + } + + scene.shader = &shader; + + GetGraphicsAppManager()->GetRenderer()->SetScene(&scene); + + auto input = InputManager::GetInstace(); + actionForward = input->GetAction("forward"); + actionSide = input->GetAction("side"); + actionLookUp = input->GetAction("look up"); + actionLookSide = input->GetAction("look side"); + + actionForward->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_Y)); + actionForward->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_S), InputKey(InputKey::Keyboard::KEY_W)); + actionSide->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_X)); + actionSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_D), InputKey(InputKey::Keyboard::KEY_A)); + actionLookUp->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_Y)); + actionLookSide->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_X)); + actionLookUp->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_DOWN), InputKey(InputKey::Keyboard::KEY_UP)); + actionLookSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_RIGHT), InputKey(InputKey::Keyboard::KEY_LEFT)); + actionLookUp->BindKey(InputKey(InputKey::Mouse::AXIS_Y)); + actionLookSide->BindKey(InputKey(InputKey::Mouse::AXIS_X)); + } + + float yaw = 0, pitch = 0; + glm::vec3 position = {0,0,-10}; + + void Tick() override + { + for(int i = 0; i < DYNAMIC; i++) + { + nodesPool[i]->SetMatrix(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); + } + + auto input = InputManager::GetInstace(); + glm::vec3 vec(input->GetAxis(actionSide), 0, -input->GetAxis(actionForward)); + if(glm::length2(vec) > 1) + { + vec = glm::normalize(vec); + } + + float tiemScale = 0.004f; + + vec = vec * tiemScale; // scale vector + + yaw += input->GetAxis(actionLookSide) * tiemScale * 0.5f; + pitch += input->GetAxis(actionLookUp) * tiemScale * 0.5f; + + glm::quat rot(glm::vec3(pitch, yaw, 0)); + glm::mat4 rotMat = glm::toMat4(rot); + + vec = glm::vec3(rot * glm::vec4(vec, 1)); + position += vec; + cam.SetMatrix(glm::translate(glm::mat4(1), position) * rotMat); + } + + void Close() override{} +}; + +std::unique_ptr CubesExampleApp::Create() +{ + return std::make_unique(); +} diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.hpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.hpp new file mode 100644 index 0000000..893e196 --- /dev/null +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.hpp @@ -0,0 +1,16 @@ +/* + * 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 + +class CubesExampleApp : public openVulkanoCpp::IGraphicsApp +{ +public: + static std::unique_ptr Create(); +}; diff --git a/openVulkanoCpp/main.cpp b/openVulkanoCpp/main.cpp index d23a6d2..3da0eb9 100644 --- a/openVulkanoCpp/main.cpp +++ b/openVulkanoCpp/main.cpp @@ -1,159 +1,19 @@ +/* + * 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 "Host/GraphicsAppManager.hpp" -#include "Scene/Scene.hpp" -#include "Scene/Shader.hpp" #include "Base/EngineConfiguration.hpp" -#include "Input/InputManager.hpp" - -#include -#include - -using namespace openVulkanoCpp::Scene; -using namespace openVulkanoCpp::Input; - -uint32_t GEOS = 3000, OBJECTS = 10000, DYNAMIC = 1000; - -class ExampleApp : public openVulkanoCpp::IGraphicsApp -{ - Scene scene; - PerspectiveCamera cam; - Material mat; - Shader shader; - std::vector drawablesPool; - std::vector nodesPool; - - InputAction* actionForward; - InputAction* actionSide; - InputAction* actionLookUp; - InputAction* actionLookSide; - -public: - std::string GetAppName() override { return "ExampleApp"; } - std::string GetAppVersion() override { return "v1.0"; } - int GetAppVersionAsInt() override { return 1; } - - void Init() override - { - std::srand(1); - scene.Init(); - cam.Init(70, 16, 9, 0.1f, 100); - scene.SetCamera(&cam); - cam.SetMatrix(glm::translate(glm::mat4(1), glm::vec3(0,0,-10))); - shader.Init("Shader/basic", "Shader/basic"); - drawablesPool.resize(GEOS); - for(int i = 0; i < GEOS; i++) - { - Geometry* geo = new Geometry(); - geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, glm::vec4((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); - drawablesPool[i] = new Drawable(); - drawablesPool[i]->Init(geo, &mat); - } - nodesPool.resize(OBJECTS); - for(int i = 0; i < OBJECTS; i++) - { - nodesPool[i] = new Node(); - 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(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); - } - - scene.shader = &shader; - - GetGraphicsAppManager()->GetRenderer()->SetScene(&scene); - - auto input = InputManager::GetInstace(); - actionForward = input->GetAction("forward"); - actionSide = input->GetAction("side"); - actionLookUp = input->GetAction("look up"); - actionLookSide = input->GetAction("look side"); - - actionForward->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_Y)); - actionForward->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_S), InputKey(InputKey::Keyboard::KEY_W)); - actionSide->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_X)); - actionSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_D), InputKey(InputKey::Keyboard::KEY_A)); - actionLookUp->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_Y)); - actionLookSide->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_X)); - actionLookUp->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_DOWN), InputKey(InputKey::Keyboard::KEY_UP)); - actionLookSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_RIGHT), InputKey(InputKey::Keyboard::KEY_LEFT)); - actionLookUp->BindKey(InputKey(InputKey::Mouse::AXIS_Y)); - actionLookSide->BindKey(InputKey(InputKey::Mouse::AXIS_X)); - } - - float yaw = 0, pitch = 0; - glm::vec3 position = {0,0,10}; - - void Tick() override - { - for(int i = 0; i < DYNAMIC; i++) - { - nodesPool[i]->SetMatrix(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); - } - - auto input = InputManager::GetInstace(); - glm::vec3 vec(input->GetAxis(actionSide), 0, -input->GetAxis(actionForward)); - if(glm::length2(vec) > 1) - { - vec = glm::normalize(vec); - } - - float tiemScale = 0.004f; - - vec = vec * tiemScale; // scale vector - - yaw += input->GetAxis(actionLookSide) * tiemScale * 0.5f; - pitch += input->GetAxis(actionLookUp) * tiemScale * 0.5f; - - glm::quat rot(glm::vec3(pitch, yaw, 0)); - glm::mat4 rotMat = glm::toMat4(rot); - - vec = glm::vec3(rot * glm::vec4(vec, 1)); - position += vec; - cam.SetMatrix(glm::translate(glm::mat4(1), position) * rotMat); - } - - void Close() override{} -}; - -#include -#include -#include +#include "ExampleApps/CubesExampleApp.hpp" int main(int argc, char** argv) { - std::cout << "Amount of Threads to use [2]: "; + const auto cubesApp = CubesExampleApp::Create(); int threads = 2; - std::string input; - std::getline(std::cin, input); - if (!input.empty()) - { - std::istringstream stream(input); - stream >> threads; - } - std::cout << "Amount of geometries to produce [" << GEOS << "]: "; - std::getline(std::cin, input); - if (!input.empty()) - { - std::istringstream stream(input); - stream >> GEOS; - } - std::cout << "Amount of objects to render [" << OBJECTS << "]: "; - std::getline(std::cin, input); - if (!input.empty()) - { - std::istringstream stream(input); - stream >> OBJECTS; - } - std::cout << "Amount of moving objects [" << DYNAMIC << "]: "; - std::getline(std::cin, input); - if (!input.empty()) - { - std::istringstream stream(input); - stream >> DYNAMIC; - } - DYNAMIC = std::min(DYNAMIC, OBJECTS); openVulkanoCpp::EngineConfiguration::GetEngineConfiguration()->SetNumThreads(threads); - openVulkanoCpp::IGraphicsAppManager* manager = new openVulkanoCpp::GraphicsAppManager(new ExampleApp()); + openVulkanoCpp::IGraphicsAppManager* manager = new openVulkanoCpp::GraphicsAppManager(cubesApp.get()); manager->Run(); return 0; }