Add textured cube example app

This commit is contained in:
2024-07-04 12:34:22 +02:00
parent f39a628139
commit ebd1d85f39
3 changed files with 128 additions and 0 deletions

View File

@@ -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<OpenVulkano::Scene::UI::PerformanceInfo> 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<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo = std::make_shared<OpenVulkano::Scene::UI::PerformanceInfo>();
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<IGraphicsApp> TexturedCubeExampleApp::CreateUnique()
{
return std::make_unique<TexturedCubeExampleAppImpl>();
}
}

View File

@@ -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 <memory>
namespace OpenVulkano
{
class TexturedCubeExampleApp : public IGraphicsApp
{
public:
static IGraphicsApp* Create();
static std::unique_ptr<IGraphicsApp> CreateUnique();
[[nodiscard]] std::string GetAppName() const final
{ return "Textured Cube ExampleApp"; }
[[nodiscard]] OpenVulkano::Version GetAppVersion() const final
{ return {"v1.0"}; }
};
}