Skip to content
Snippets Groups Projects
Commit 1b0bdc75 authored by Katharina Krämer's avatar Katharina Krämer
Browse files

[#69] screen coordinates to world for particles

parent f16cf13c
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #25892 failed
...@@ -29,4 +29,4 @@ private: ...@@ -29,4 +29,4 @@ private:
float m_lifeTime; float m_lifeTime;
glm::vec3 m_velocity; glm::vec3 m_velocity;
float padding_2 = 0.f; float padding_2 = 0.f;
}; };
\ No newline at end of file
...@@ -5,9 +5,10 @@ ...@@ -5,9 +5,10 @@
#include <chrono> #include <chrono>
#include "ParticleSystem.hpp" #include "ParticleSystem.hpp"
#include <random> #include <random>
#include <glm/gtc/matrix_access.hpp>
int main(int argc, const char** argv) { int main(int argc, const char **argv) {
const char* applicationName = "Particlesystem"; const char *applicationName = "Particlesystem";
uint32_t windowWidth = 800; uint32_t windowWidth = 800;
uint32_t windowHeight = 600; uint32_t windowHeight = 600;
...@@ -27,13 +28,14 @@ int main(int argc, const char** argv) { ...@@ -27,13 +28,14 @@ int main(int argc, const char** argv) {
window, window,
applicationName, applicationName,
VK_MAKE_VERSION(0, 0, 1), VK_MAKE_VERSION(0, 0, 1),
{ vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute }, {vk::QueueFlagBits::eTransfer, vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute},
{}, {},
{ "VK_KHR_swapchain" } {"VK_KHR_swapchain"}
); );
auto particleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3, vkcv::BufferMemoryType::DEVICE_LOCAL); auto particleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3,
uint16_t indices[3] = { 0, 1, 2 }; vkcv::BufferMemoryType::DEVICE_LOCAL);
uint16_t indices[3] = {0, 1, 2};
particleIndexBuffer.fill(&indices[0], sizeof(indices)); particleIndexBuffer.fill(&indices[0], sizeof(indices));
...@@ -44,7 +46,7 @@ int main(int argc, const char** argv) { ...@@ -44,7 +46,7 @@ int main(int argc, const char** argv) {
core.getSwapchainImageFormat()); core.getSwapchainImageFormat());
vkcv::PassConfig particlePassDefinition({ present_color_attachment }); vkcv::PassConfig particlePassDefinition({present_color_attachment});
vkcv::PassHandle particlePass = core.createPass(particlePassDefinition); vkcv::PassHandle particlePass = core.createPass(particlePassDefinition);
vkcv::PassConfig computePassDefinition({}); vkcv::PassConfig computePassDefinition({});
...@@ -73,7 +75,8 @@ int main(int argc, const char** argv) { ...@@ -73,7 +75,8 @@ int main(int argc, const char** argv) {
particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv")); particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv"));
particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv")); particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv"));
vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(particleShaderProgram.getReflectedDescriptors()[0]); vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(
particleShaderProgram.getReflectedDescriptors()[0]);
vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>( vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>(
vkcv::BufferType::VERTEX, vkcv::BufferType::VERTEX,
...@@ -86,7 +89,7 @@ int main(int argc, const char** argv) { ...@@ -86,7 +89,7 @@ int main(int argc, const char** argv) {
std::vector<vkcv::VertexBinding> bindings; std::vector<vkcv::VertexBinding> bindings;
for (size_t i = 0; i < vertexAttachments.size(); i++) { for (size_t i = 0; i < vertexAttachments.size(); i++) {
bindings.push_back(vkcv::VertexBinding(i, { vertexAttachments[i] })); bindings.push_back(vkcv::VertexBinding(i, {vertexAttachments[i]}));
} }
const vkcv::VertexLayout particleLayout(bindings); const vkcv::VertexLayout particleLayout(bindings);
...@@ -97,12 +100,12 @@ int main(int argc, const char** argv) { ...@@ -97,12 +100,12 @@ int main(int argc, const char** argv) {
UINT32_MAX, UINT32_MAX,
particlePass, particlePass,
{particleLayout}, {particleLayout},
{ core.getDescriptorSet(descriptorSet).layout }, {core.getDescriptorSet(descriptorSet).layout},
true); true);
const std::vector<glm::vec3> vertices = {glm::vec3(-0.02, 0.02, 0), const std::vector<glm::vec3> vertices = {glm::vec3(-0.005, 0.005, 0),
glm::vec3( 0.02, 0.02, 0), glm::vec3(0.005, 0.005, 0),
glm::vec3(0, -0.02, 0)}; glm::vec3(0, -0.005, 0)};
vertexBuffer.fill(vertices); vertexBuffer.fill(vertices);
...@@ -113,7 +116,7 @@ int main(int argc, const char** argv) { ...@@ -113,7 +116,7 @@ int main(int argc, const char** argv) {
vkcv::Buffer<glm::vec4> color = core.createBuffer<glm::vec4>( vkcv::Buffer<glm::vec4> color = core.createBuffer<glm::vec4>(
vkcv::BufferType::UNIFORM, vkcv::BufferType::UNIFORM,
1 1
); );
vkcv::Buffer<glm::vec2> position = core.createBuffer<glm::vec2>( vkcv::Buffer<glm::vec2> position = core.createBuffer<glm::vec2>(
vkcv::BufferType::UNIFORM, vkcv::BufferType::UNIFORM,
...@@ -150,27 +153,65 @@ int main(int argc, const char** argv) { ...@@ -150,27 +153,65 @@ int main(int argc, const char** argv) {
const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle();
const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount()); const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(),
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); particleIndexBuffer.getCount());
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle);
glm::vec2 pos = glm::vec2(1.f); //vkcv::DrawcallInfo drawcalls(renderMesh, {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle)});
window.e_mouseMove.add([&]( double offsetX, double offsetY) { std::uniform_real_distribution<float> rdmVel(-0.1f, 0.1f);
std::default_random_engine rdmEngine;
ParticleSystem particleSystem;
particleSystem.setMaxLifeTime(3.f);
glm::vec3 vel0 = glm::vec3(rdmVel(rdmEngine), rdmVel(rdmEngine), 0.0f);
glm::vec3 vel1 = glm::vec3(rdmVel(rdmEngine), rdmVel(rdmEngine), 0.0f);
glm::vec3 vel2 = glm::vec3(rdmVel(rdmEngine), rdmVel(rdmEngine), 0.0f);
particleSystem.addParticles({
Particle(glm::vec3(0.f, 1.f, 0.0f), vel0, 1.f),
Particle(glm::vec3(0.2f, 0.1f, 0.0f), vel1, 1.5f),
Particle(glm::vec3(0.15f, 0.f, 0.0f), vel2, 2.f),
Particle(glm::vec3(-0.15f, 0.1f, 0.0f), vel2, 2.5f),
Particle(glm::vec3(0.25f, 0.f, 0.0f), vel2, 3.f),
Particle(glm::vec3(-0.15f, 0.2f, 0.0f), vel2, 3.5f)});
glm::vec2 pos = glm::vec2(0.f);
glm::vec3 spawnPosition = glm::vec3(0.f);
glm::vec4 tempPosition = glm::vec4(0.f);
window.e_mouseMove.add([&](double offsetX, double offsetY) {
pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY));
// borders are assumed to be 0.5
//pos = glm::vec2((pos.x -0.5f * static_cast<float>(window.getWidth()))/static_cast<float>(window.getWidth()), (pos.y -0.5f * static_cast<float>(window.getHeight()))/static_cast<float>(window.getHeight()));
//borders are assumed to be 1
pos.x = (2 * pos.x - static_cast<float>(window.getWidth())) / static_cast<float>(window.getWidth());
pos.y = (2 * pos.y - static_cast<float>(window.getHeight())) / static_cast<float>(window.getHeight());
glm::vec4 camera_pos = glm::column(cameraManager.getCamera().getView(), 3);
std::cout << "camerapos: " << camera_pos.x << ", " << camera_pos.y << ", " << camera_pos.z << std::endl;
//glm::vec4 view_axis = glm::row(cameraManager.getCamera().getView(), 2);
// std::cout << "view_axis: " << view_axis.x << ", " << view_axis.y << ", " << view_axis.z << std::endl;
//std::cout << "Front: " << cameraManager.getCamera().getFront().x << ", " << cameraManager.getCamera().getFront().z << ", " << cameraManager.getCamera().getFront().z << std::endl;
glm::mat4 viewmat = cameraManager.getCamera().getView();
spawnPosition = glm::vec3(pos.x, pos.y, 0.f);
tempPosition = glm::inverse(viewmat) * glm::vec4(spawnPosition, 1.0f);
spawnPosition = glm::vec3(tempPosition.x, tempPosition.y, tempPosition.z);
particleSystem.setRespawnPos(glm::vec3(-spawnPosition.x, -spawnPosition.y, -spawnPosition.z));
std::cout << "respawn pos: " << spawnPosition.x << ", " << spawnPosition.y << ", " << spawnPosition.z << std::endl;
}); });
std::vector<glm::mat4> modelMatrices; std::vector<glm::mat4> modelMatrices;
std::vector<vkcv::DrawcallInfo> drawcalls; std::vector<vkcv::DrawcallInfo> drawcalls;
for(auto particle : particleSystem.getParticles()) { for (auto particle : particleSystem.getParticles()) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage})); drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage}));
} }
auto start = std::chrono::system_clock::now(); auto start = std::chrono::system_clock::now();
glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f); glm::vec4 colorData = glm::vec4(1.0f, 1.0f, 0.0f, 1.0f);
cameraManager.getCamera().setPosition(glm::vec3(0.f, 0.f, 0.f));
std::cout << "FRONT: " << cameraManager.getCamera().getFront().x << ", " << cameraManager.getCamera().getFront().y << ", " << cameraManager.getCamera().getFront().z << std::endl;
while (window.isWindowOpen()) while (window.isWindowOpen()) {
{
window.pollEvents(); window.pollEvents();
uint32_t swapchainWidth, swapchainHeight; uint32_t swapchainWidth, swapchainHeight;
...@@ -180,25 +221,26 @@ int main(int argc, const char** argv) { ...@@ -180,25 +221,26 @@ int main(int argc, const char** argv) {
color.fill(&colorData); color.fill(&colorData);
position.fill(&pos); position.fill(&pos);
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
float deltatime = std::chrono::duration<float>(end - start).count(); float deltatime = std::chrono::duration<float>(end - start).count();
start = end; start = end;
particleSystem.updateParticles( deltatime ); particleSystem.updateParticles(deltatime);
modelMatrices.clear(); modelMatrices.clear();
for(Particle particle : particleSystem.getParticles()) { for (Particle particle : particleSystem.getParticles()) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition())); modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
} }
cameraManager.getCamera().updateView(deltatime); cameraManager.getCamera().updateView(deltatime);
std::vector<glm::mat4> mvp; std::vector<glm::mat4> mvp;
mvp.clear(); mvp.clear();
for(const auto& m : modelMatrices){ for (const auto &m : modelMatrices) {
mvp.push_back(m * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView()); mvp.push_back(m * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView());
} }
vkcv::PushConstantData pushConstantData((void*)mvp.data(), sizeof(glm::mat4)); vkcv::PushConstantData pushConstantData((void *) mvp.data(), sizeof(glm::mat4));
auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics);
uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil(particleSystem.getParticles().size()/64.f)),1,1}; uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil(particleSystem.getParticles().size()/64.f)),1,1};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment