From e040fad807b33e76820e9da96ade0496f68f49d7 Mon Sep 17 00:00:00 2001 From: Alexander Gauggel <agauggel@uni-koblenz.de> Date: Wed, 8 Sep 2021 14:43:35 +0200 Subject: [PATCH] [#94] Add camera support --- projects/saf_r/shaders/raytracing.comp | 17 ++++++++++------- projects/saf_r/src/main.cpp | 26 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/projects/saf_r/shaders/raytracing.comp b/projects/saf_r/shaders/raytracing.comp index 4cb9ae9c..59a10c2a 100644 --- a/projects/saf_r/shaders/raytracing.comp +++ b/projects/saf_r/shaders/raytracing.comp @@ -34,8 +34,8 @@ layout(std430, binding = 1) coherent buffer spheres{ layout(set=0, binding = 2, rgba8) uniform image2D outImage; layout( push_constant ) uniform constants{ + mat4 viewToWorld; int lightCount; - int matCount; int sphereCount; }; @@ -160,11 +160,13 @@ vec3 castRay(const vec3 initialOrigin, const vec3 initialDirection, int max_dept vec3 computeDirection(ivec2 coord){ - ivec2 outImageRes = imageSize(outImage); - float fov = pi / 2.f; - float x = (2 * (float(coord.x) + 0.5f) / float(outImageRes.x) - 1) * tan(fov / 2.f) * outImageRes.x / float(outImageRes.y); - float y = -(2 * (float(coord.y) + 0.5f) / float(outImageRes.y) - 1) * tan(fov / 2.f); - return normalize(vec3(x, y, -1)); + ivec2 outImageRes = imageSize(outImage); + float fov = pi / 2.f; + float x = (2 * (float(coord.x) + 0.5f) / float(outImageRes.x) - 1) * tan(fov / 2.f) * outImageRes.x / float(outImageRes.y); + float y = -(2 * (float(coord.y) + 0.5f) / float(outImageRes.y) - 1) * tan(fov / 2.f); + vec3 directionViewSpace = normalize(vec3(x, y, 1)); + vec3 directionWorldSpace = mat3(viewToWorld) * directionViewSpace; + return directionWorldSpace; } @@ -172,7 +174,8 @@ void main(){ ivec2 coord = ivec2(gl_GlobalInvocationID.xy); int max_depth = 4; vec3 direction = computeDirection(coord); - vec3 color = castRay(vec3(0,0,0), direction, max_depth); + vec3 cameraPos = viewToWorld[3].xyz; + vec3 color = castRay(cameraPos, direction, max_depth); imageStore(outImage, coord, vec4(color, 0.f)); } \ No newline at end of file diff --git a/projects/saf_r/src/main.cpp b/projects/saf_r/src/main.cpp index 0d9e5c10..a6319ce3 100644 --- a/projects/saf_r/src/main.cpp +++ b/projects/saf_r/src/main.cpp @@ -92,10 +92,10 @@ int main(int argc, const char** argv) { //spheres for the scene std::vector<safrScene::Sphere> spheres; - spheres.push_back(safrScene::Sphere(glm::vec3(-3, 0, -16), 2, ivory)); - spheres.push_back(safrScene::Sphere(glm::vec3(-1.0, -1.5, -12), 2, mirror)); - spheres.push_back(safrScene::Sphere(glm::vec3(1.5, -0.5, -18), 3, red_rubber)); - spheres.push_back(safrScene::Sphere(glm::vec3(7, 5, -18), 4, mirror)); + spheres.push_back(safrScene::Sphere(glm::vec3(-3.0, 0.0, 16), 2, ivory)); + spheres.push_back(safrScene::Sphere(glm::vec3(-1.0, -1.5, 12), 2, mirror)); + spheres.push_back(safrScene::Sphere(glm::vec3( 1.5, -0.5, 18), 3, red_rubber)); + spheres.push_back(safrScene::Sphere(glm::vec3( 7.0, 5.0, 18), 4, mirror)); //lights for the scene std::vector<safrScene::Light> lights; @@ -133,10 +133,6 @@ int main(int argc, const char** argv) { ); sphereBuffer.fill(spheres); - glm::ivec3 pushData = glm::ivec3((lights.size()), (materials.size()), (spheres.size())); - vkcv::PushConstants pushConstantsCompute(sizeof(glm::ivec3)); - pushConstantsCompute.appendDrawcall(pushData); - vkcv::DescriptorWrites setWrites; setWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, texture.getHandle()) }; setWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, sampler) }; @@ -242,6 +238,20 @@ int main(int argc, const char** argv) { core.prepareImageForStorage (cmdStream, swapchainInput); + struct RaytracingPushConstantData { + glm::mat4 viewToWorld; + int32_t lightCount; + int32_t sphereCount; + }; + + RaytracingPushConstantData raytracingPushData; + raytracingPushData.lightCount = lights.size(); + raytracingPushData.sphereCount = spheres.size(); + raytracingPushData.viewToWorld = glm::inverse(cameraManager.getActiveCamera().getView()); + + vkcv::PushConstants pushConstantsCompute(sizeof(RaytracingPushConstantData)); + pushConstantsCompute.appendDrawcall(raytracingPushData); + uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil( swapchainWidth/16.f)), static_cast<uint32_t> (std::ceil(swapchainHeight/16.f)), 1 }; // Anzahl workgroups -- GitLab