implement billboard shader and add new example

This commit is contained in:
ohyzha
2024-07-18 14:48:50 +03:00
parent 5023fe64ce
commit cfb8b76801
14 changed files with 406 additions and 12 deletions

View File

@@ -0,0 +1,168 @@
/*
* 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 "BillboardExampleApp.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 "Scene/UniformBuffer.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp"
#include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-msc50-cpp"
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
namespace OpenVulkano
{
using namespace Scene;
using namespace Input;
using namespace Math;
class BillboardExampleAppImpl final : public BillboardExampleApp
{
public:
struct BillboardControlBlock
{
glm::vec2 quadSize;
glm::vec2 windowSize;
bool isFixedSize;
};
void Init() override
{
auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
engineConfig->SetNumThreads(4);
engineConfig->SetPreferFramebufferFormatSRGB(false);
std::srand(1); // Fix seed for random numbers
m_scene.Init();
m_cam.Init(70, 16, 9, 0.1f, 100);
m_scene.SetCamera(&m_cam);
m_quadBillboardShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboardFromSinglePoint");
m_quadBillboardShader.AddShaderProgram(OpenVulkano::ShaderProgramType::GEOMETRY, "Shader/billboardFromSinglePoint");
m_quadBillboardShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/billboard");
m_quadBillboardShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_quadBillboardShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
m_quadBillboardShader.topology = Topology::POINT_LIST;
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/billboard");
m_shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_shader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
m_shader.cullMode = CullMode::NONE;
constexpr int quadsCnt = 7;
constexpr int otherCnt = 2;
constexpr int cntDrawables = quadsCnt + otherCnt;
m_bbContolBlock.quadSize = { 100.f, 100.f };
m_bbContolBlock.windowSize = { 0, 0 }; // will be initialized on first frame
m_bbContolBlock.isFixedSize = false;
m_uniBuffer.Init(sizeof(BillboardControlBlock), &m_bbContolBlock);
m_drawablesPool.resize(cntDrawables);
m_nodesPool.resize(cntDrawables);
m_geo.reserve(cntDrawables);
for (uint32_t i = 0; i < cntDrawables; i++)
{
Geometry* geo = nullptr;
m_nodesPool[i].Init();
if (i < quadsCnt)
{
geo = new Geometry();
geo->Init(1, 0);
geo->vertices[0].position = glm::vec3(1 + i, i, 0);
geo->vertices[0].color = glm::vec4(1, 0, 0, 1);
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, std::rand() % 5)));
m_drawablesPool[i].Init(&m_quadBillboardShader, geo, &m_mat, &m_uniBuffer);
}
else
{
geo = GeometryFactory::MakePyramid(1, 1, glm::vec4(0, 1, 0, 1));
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, -std::rand() % 10)));
m_drawablesPool[i].Init(&m_shader, geo, &m_mat, &m_uniBuffer);
}
m_geo.push_back(geo);
m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
m_nodesPool[i].AddDrawable(&m_drawablesPool[i]);
m_nodesPool[i].SetUpdateFrequency(UpdateFrequency::Always);
}
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
m_camController.Init(&m_cam);
m_camController.SetDefaultKeybindings();
m_camController.SetPosition({ 0, 0, 5 });
m_camController.SetBoostFactor(5);
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);
}
void Tick() override
{
static bool firstFrame = true;
if (firstFrame)
{
glm::vec2 sz = GetGraphicsAppManager()->GetWindow()->GetSize();
m_bbContolBlock.windowSize = sz;
m_uniBuffer.updated = true;
firstFrame = false;
}
m_camController.Tick();
}
void Close() override
{
for (Geometry* g: m_geo)
{
delete g;
}
}
private:
OpenVulkano::Scene::Scene m_scene;
BillboardControlBlock m_bbContolBlock;
PerspectiveCamera m_cam;
UniformBuffer m_uniBuffer;
OpenVulkano::FreeCamCameraController m_camController;
Material m_mat;
Shader m_shader;
Shader m_quadBillboardShader;
std::vector<SimpleDrawable> m_drawablesPool;
std::vector<Node> m_nodesPool;
Vector3f_SIMD m_position = { 0, 0, -10 };
OpenVulkano::Scene::UI::SimpleUi m_ui;
std::vector<Geometry*> m_geo;
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo;
};
IGraphicsApp* BillboardExampleApp::Create() { return new BillboardExampleAppImpl(); }
std::unique_ptr<IGraphicsApp> BillboardExampleApp::CreateUnique()
{
return std::make_unique<BillboardExampleAppImpl>();
}
}
#pragma clang diagnostic pop
#pragma clang diagnostic pop

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

View File

@@ -8,6 +8,7 @@
#include "ExampleApps/CubesExampleApp.hpp" #include "ExampleApps/CubesExampleApp.hpp"
#include "ExampleApps/MovingCubeApp.hpp" #include "ExampleApps/MovingCubeApp.hpp"
#include "ExampleApps/TexturedCubeExampleApp.hpp" #include "ExampleApps/TexturedCubeExampleApp.hpp"
#include "ExampleApps/BillboardExampleApp.hpp"
#include <ftxui/component/captured_mouse.hpp> #include <ftxui/component/captured_mouse.hpp>
#include <ftxui/component/component.hpp> #include <ftxui/component/component.hpp>
@@ -25,6 +26,7 @@ int main(int argc, char** argv)
"Cubes Example App", "Cubes Example App",
"Moving Cube Example App", "Moving Cube Example App",
"Textured Cube Example App", "Textured Cube Example App",
"Billboard Example App"
}; };
int selectedExample = 0; int selectedExample = 0;
@@ -41,6 +43,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; case 2: app = TexturedCubeExampleApp::CreateUnique(); break;
case 3: app = BillboardExampleApp::CreateUnique(); break;
default: throw std::runtime_error("Invalid menu selection!"); break; default: throw std::runtime_error("Invalid menu selection!"); break;
} }

View File

@@ -16,6 +16,7 @@ namespace OpenVulkano
virtual RenderAPI::RenderApi GetRenderApi() const = 0; virtual RenderAPI::RenderApi GetRenderApi() const = 0;
virtual IGraphicsApp* GetGraphicsApp() const = 0; virtual IGraphicsApp* GetGraphicsApp() const = 0;
virtual IRenderer* GetRenderer() const = 0; virtual IRenderer* GetRenderer() const = 0;
virtual IWindow* GetWindow() const = 0;
virtual bool IsRunning() const = 0; virtual bool IsRunning() const = 0;
virtual bool IsPaused() const = 0; virtual bool IsPaused() const = 0;
virtual void Stop() = 0; virtual void Stop() = 0;

View File

@@ -32,8 +32,7 @@ namespace OpenVulkano
vec = Math::Utils::normalize(vec); vec = Math::Utils::normalize(vec);
} }
float timeScale = CURRENT_FRAME.frameTime; //TODO float timeScale = 5 * CURRENT_FRAME.frameTime; //TODO
vec = vec * timeScale * 3.0f; // scale vector vec = vec * timeScale * 3.0f; // scale vector
if (input->GetButton(m_actionBoost)) if (input->GetButton(m_actionBoost))
{ {

View File

@@ -18,7 +18,7 @@ namespace OpenVulkano
class FreeCamCameraController final : public CameraController class FreeCamCameraController final : public CameraController
{ {
float m_yaw = 0, m_pitch = 0, m_boostFactor = 2; float m_yaw = 0, m_pitch = 0, m_boostFactor = 1.5;
Math::Vector3f_SIMD m_position = { 0, 0, 0 }; Math::Vector3f_SIMD m_position = { 0, 0, 0 };
Input::InputAction* m_actionForward; Input::InputAction* m_actionForward;

View File

@@ -50,6 +50,8 @@ namespace OpenVulkano
[[nodiscard]] IRenderer* GetRenderer() const override { return renderer.get(); } [[nodiscard]] IRenderer* GetRenderer() const override { return renderer.get(); }
[[nodiscard]] IWindow* GetWindow() const override { return window; }
[[nodiscard]] bool IsRunning() const override { return running; } [[nodiscard]] bool IsRunning() const override { return running; }
[[nodiscard]] bool IsPaused() const override { return paused; } [[nodiscard]] bool IsPaused() const override { return paused; }

View File

@@ -9,19 +9,21 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void SimpleDrawable::Init(Shader* shader, Geometry* mesh, Material* material) void SimpleDrawable::Init(Shader* shader, Geometry* mesh, Material* material, UniformBuffer* uniBuffer)
{ {
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); if (m_mesh || m_material || m_uniBuffer) throw std::runtime_error("Drawable is already initialized.");
m_mesh = mesh; m_mesh = mesh;
m_material = material; m_material = material;
m_uniBuffer = uniBuffer;
SetShader(shader); SetShader(shader);
} }
void SimpleDrawable::Init(SimpleDrawable* drawable) void SimpleDrawable::Init(SimpleDrawable* drawable)
{ {
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); if (m_mesh || m_material || m_uniBuffer) throw std::runtime_error("Drawable is already initialized.");
m_mesh = drawable->m_mesh; m_mesh = drawable->m_mesh;
m_material = drawable->m_material; m_material = drawable->m_material;
m_uniBuffer = drawable->m_uniBuffer;
SetShader(drawable->GetShader()); SetShader(drawable->GetShader());
} }
} }

View File

@@ -12,20 +12,22 @@ namespace OpenVulkano::Scene
{ {
class Geometry; class Geometry;
class Material; class Material;
class UniformBuffer;
class SimpleDrawable final : public Drawable class SimpleDrawable final : public Drawable
{ {
Geometry* m_mesh = nullptr; Geometry* m_mesh = nullptr;
Material* m_material = nullptr; Material* m_material = nullptr;
UniformBuffer* m_uniBuffer = nullptr;
public: public:
SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>()) SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>()) {}
{}
explicit SimpleDrawable(const SimpleDrawable* toCopy) explicit SimpleDrawable(const SimpleDrawable* toCopy)
: Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>()) : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
, m_mesh(toCopy->m_mesh) , m_mesh(toCopy->m_mesh)
, m_material(toCopy->m_material) , m_material(toCopy->m_material)
, m_uniBuffer(toCopy->m_uniBuffer)
{ {
SetShader(toCopy->GetShader()); SetShader(toCopy->GetShader());
} }
@@ -35,12 +37,14 @@ namespace OpenVulkano::Scene
//if (m_mesh) SimpleDrawable::Close(); //if (m_mesh) SimpleDrawable::Close();
} }
void Init(Shader* shader, Geometry* mesh, Material* material); void Init(Shader* shader, Geometry* mesh, Material* material, UniformBuffer* uniBuffer = nullptr);
void Init(SimpleDrawable* drawable); void Init(SimpleDrawable* drawable);
[[nodiscard]] Geometry* GetMesh() const { return m_mesh; } [[nodiscard]] Geometry* GetMesh() const { return m_mesh; }
[[nodiscard]] Material* GetMaterial() const { return m_material; } [[nodiscard]] Material* GetMaterial() const { return m_material; }
[[nodiscard]] UniformBuffer* GetBuffer() const { return m_uniBuffer; }
}; };
} }

View File

@@ -0,0 +1,9 @@
#version 450
layout(location = 0) in vec4 color;
layout(location = 0) out vec4 outColor;
void main()
{
outColor = color;
}

View File

@@ -0,0 +1,55 @@
#version 450
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 5) in vec4 color;
layout(location = 0) out vec4 outColor;
layout(set = 0, binding = 0) uniform NodeData
{
mat4 world;
} node;
layout(set = 1, binding = 0) uniform CameraData
{
mat4 viewProjection;
mat4 view;
mat4 projection;
vec4 camPos;
} cam;
layout(set = 2, binding = 0) uniform BillboardData
{
vec2 size;
vec2 windowSize;
bool isFixedSize;
} billboardInfo;
void main() {
if (!billboardInfo.isFixedSize)
{
mat4 mv = cam.view * node.world;
mv[0][0] = 1;
mv[0][1] = 0;
mv[0][2] = 0;
mv[1][0] = 0;
mv[1][1] = 1;
mv[1][2] = 0;
mv[2][0] = 0;
mv[2][1] = 0;
mv[2][2] = 1;
gl_Position = cam.projection * mv * vec4(pos, 1);
}
else
{
vec4 billboardPos = vec4(0, 0, 0, 1);
vec4 viewPos = cam.view * node.world * billboardPos;
float dist = -viewPos.z;
gl_Position = cam.projection * (viewPos + vec4(pos.xy*dist*0.2,0,0));
}
outColor = color;
}

View File

@@ -0,0 +1,75 @@
#version 450
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
layout(set = 0, binding = 0) uniform NodeData
{
mat4 world;
} node;
layout(set = 1, binding = 0) uniform CameraData
{
mat4 viewProjection;
mat4 view;
mat4 projection;
vec4 camPos;
} cam;
layout(set = 2, binding = 0) uniform BillboardData
{
vec2 size;
vec2 windowSize;
bool isFixedSize;
} billboardInfo;
layout(location = 0) out vec4 color;
layout(location = 0) in VS_OUT {
vec4 color;
} gs_in[];
void main() {
// The desired point for the billboard
vec3 pos = gl_in[0].gl_Position.xyz;
if(!billboardInfo.isFixedSize)
{
vec3 cameraRight = normalize(vec3(cam.view[0][0], cam.view[1][0], cam.view[2][0]));
vec3 cameraUp = normalize(vec3(cam.view[0][1], cam.view[1][1], cam.view[2][1]));
const vec2 offsets[4] = {
vec2(0.5, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, -0.5),
vec2(-0.5, 0.5)
};
for (int i = 0; i < 4; i++)
{
vec2 scaledSize = billboardInfo.size / length(billboardInfo.size);
gl_Position = cam.viewProjection * vec4(pos + cameraRight * offsets[i].x * scaledSize.x + cameraUp * offsets[i].y * scaledSize.y, 1.0);
color = gs_in[0].color;
EmitVertex();
}
EndPrimitive();
}
else
{
const vec2 offsets[4] = {
vec2(-0.5, -0.5),
vec2(-0.5, 0.5),
vec2(0.5, -0.5),
vec2(0.5, 0.5)
};
for (int i = 0; i < 4; i++)
{
gl_Position = cam.viewProjection * vec4(pos, 1);
gl_Position /= gl_Position.w;
gl_Position.xy += offsets[i] * vec2(billboardInfo.size.x/billboardInfo.windowSize.x, billboardInfo.size.x/billboardInfo.windowSize.y);
color = gs_in[0].color;
EmitVertex();
}
EndPrimitive();
}
}

View File

@@ -0,0 +1,35 @@
#version 450
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 5) in vec4 color;
layout(location = 0) out VS_OUT {
vec4 color;
} vs_out;
layout(set = 0, binding = 0) uniform NodeData
{
mat4 world;
} node;
layout(set = 1, binding = 0) uniform CameraData
{
mat4 viewProjection;
mat4 view;
mat4 projection;
vec4 camPos;
} cam;
layout(set = 2, binding = 0) uniform BillboardData
{
vec2 size;
vec2 windowSize;
bool isFixedSize;
} billboardInfo;
void main() {
// single point
gl_Position = node.world * vec4(pos, 1);
vs_out.color = color;
}

View File

@@ -6,9 +6,11 @@
#include "Scene/SimpleDrawable.hpp" #include "Scene/SimpleDrawable.hpp"
#include "Scene/Material.hpp" #include "Scene/Material.hpp"
#include "Scene/UniformBuffer.hpp"
#include "VulkanGeometry.hpp" #include "VulkanGeometry.hpp"
#include "VulkanNode.hpp" #include "VulkanNode.hpp"
#include "Vulkan/VulkanDrawContext.hpp" #include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanUniformBuffer.hpp"
#include "VulkanTexture.hpp" #include "VulkanTexture.hpp"
using namespace OpenVulkano::Scene; using namespace OpenVulkano::Scene;
@@ -22,6 +24,18 @@ namespace OpenVulkano::Vulkan
VulkanGeometry* renderGeo = static_cast<VulkanGeometry*>(mesh->renderGeo); VulkanGeometry* renderGeo = static_cast<VulkanGeometry*>(mesh->renderGeo);
if (!renderGeo) renderGeo = drawContext->renderer->GetResourceManager().PrepareGeometry(mesh); if (!renderGeo) renderGeo = drawContext->renderer->GetResourceManager().PrepareGeometry(mesh);
renderGeo->RecordBind(drawContext->commandBuffer); renderGeo->RecordBind(drawContext->commandBuffer);
if (drawable->GetBuffer())
{
VulkanUniformBuffer* vkBuffer = static_cast<VulkanUniformBuffer*>(drawable->GetBuffer()->renderBuffer);
if (!vkBuffer)
{
vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(drawable->GetBuffer());
}
vkBuffer->Record(drawContext);
}
if (Material* material = drawable->GetMaterial()) if (Material* material = drawable->GetMaterial())
{ {
if (Texture* texture = material->texture) if (Texture* texture = material->texture)