Add textured cube example app
This commit is contained in:
98
examples/ExampleApps/TexturedCubeExampleApp.cpp
Normal file
98
examples/ExampleApps/TexturedCubeExampleApp.cpp
Normal 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>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
27
examples/ExampleApps/TexturedCubeExampleApp.hpp
Normal file
27
examples/ExampleApps/TexturedCubeExampleApp.hpp
Normal 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"}; }
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "Host/GraphicsAppManager.hpp"
|
#include "Host/GraphicsAppManager.hpp"
|
||||||
#include "ExampleApps/CubesExampleApp.hpp"
|
#include "ExampleApps/CubesExampleApp.hpp"
|
||||||
#include "ExampleApps/MovingCubeApp.hpp"
|
#include "ExampleApps/MovingCubeApp.hpp"
|
||||||
|
#include "ExampleApps/TexturedCubeExampleApp.hpp"
|
||||||
|
|
||||||
#include <ftxui/component/captured_mouse.hpp>
|
#include <ftxui/component/captured_mouse.hpp>
|
||||||
#include <ftxui/component/component.hpp>
|
#include <ftxui/component/component.hpp>
|
||||||
@@ -23,6 +24,7 @@ int main(int argc, char** argv)
|
|||||||
std::vector<std::string> examples = {
|
std::vector<std::string> examples = {
|
||||||
"Cubes Example App",
|
"Cubes Example App",
|
||||||
"Moving Cube Example App",
|
"Moving Cube Example App",
|
||||||
|
"Textured Cube Example App",
|
||||||
};
|
};
|
||||||
|
|
||||||
int selectedExample = 0;
|
int selectedExample = 0;
|
||||||
@@ -38,6 +40,7 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
case 0: app = CubesExampleApp::CreateUnique(); break;
|
case 0: app = CubesExampleApp::CreateUnique(); break;
|
||||||
case 1: app = MovingCubeApp::CreateUnique(); break;
|
case 1: app = MovingCubeApp::CreateUnique(); break;
|
||||||
|
case 2: app = TexturedCubeExampleApp::CreateUnique(); break;
|
||||||
default: throw std::runtime_error("Invalid menu selection!"); break;
|
default: throw std::runtime_error("Invalid menu selection!"); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user