Skip to content
Snippets Groups Projects
Commit 71808c60 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

Merge branch 'lensDistortion' into 'master'

ENH: added lens distortion to Vulkan engine

See merge request iMSTK/iMSTK!282
parents fe4fdef0 2fe81476
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,7 @@ function(CopyAndCompileShaders)
compileShaders(PostProcessing/postprocess_vert.vert PostProcessing/postprocess_vert.spv)
compileShaders(PostProcessing/postprocess_frag.frag PostProcessing/postprocess_frag.spv)
compileShaders(PostProcessing/sss_frag.frag PostProcessing/sss_frag.spv)
compileShaders(PostProcessing/lens_distortion_frag.frag PostProcessing/lens_distortion_frag.spv)
compileShaders(PostProcessing/composite_frag.frag PostProcessing/composite_frag.spv)
compileShaders(PostProcessing/bloom_threshold_frag.frag PostProcessing/bloom_threshold_frag.spv)
compileShaders(PostProcessing/blur_horizontal_frag.frag PostProcessing/blur_horizontal_frag.spv)
......
......@@ -130,6 +130,19 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
m_postProcesses.push_back(bloomCompositePass);
this->incrementBufferNumbers();
}
// Lens distortion
if (renderer->m_enableLensDistortion)
{
auto lensDistortionPass = std::make_shared<VulkanPostProcess>(renderer);
lensDistortionPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastOutput][0]);
lensDistortionPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastInput][0]);
lensDistortionPass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastInput][0], VK_FORMAT_R16G16B16A16_SFLOAT);
lensDistortionPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/lens_distortion_frag.spv");
lensDistortionPass->m_pushConstantData[0] = renderer->m_lensDistortionFactor;
m_postProcesses.push_back(lensDistortionPass);
this->incrementBufferNumbers();
}
}
std::vector<std::shared_ptr<VulkanPostProcess>>&
......
#version 460
layout (set = 0, binding = 0) uniform sampler2D colorTexture;
layout (location = 0) out vec4 finalColor;
layout (location = 3) in vertexData{
vec2 uv;
}vertex;
layout (push_constant) uniform pushConstants
{
float distortion;
}constants;
// Equation based on:
// Fitzgibbon, Andrew W. "Simulataneous linear estimation of multiple view
// geometry and lens distortion." Proceedings of the 2001 IEEE Computer Society
// Conference on Computer Vision and Pattern Recognition. CVPR 2001. Vol. 1,
// 2001.
vec2 distortCoordinate(vec2 coordinate, float distortion)
{
// Center coordinate and normalize between -1 and 1
vec2 c = (coordinate * 2.0) - 1.0;
// Compute undistorted distance
float d1 = distance(c, vec2(0));
// Compute distorted distance
float d2 = d1 / (1 - (distortion * d1 * d1));
// Apply distortion
c *= (d2 / d1);
// Restore original coordinate space
c = (c + 1.0) / 2.0;
return c;
}
void main(void)
{
vec2 totalLength = distortCoordinate(vec2(0.5, 1.0), constants.distortion);
vec2 distortedCoordinate = distortCoordinate(vertex.uv, constants.distortion) - vec2(0.5);
distortedCoordinate /= (distance(totalLength, vec2(0.5)) * 2.0);
distortedCoordinate += vec2(0.5);
finalColor = texture(colorTexture, distortedCoordinate);
}
\ No newline at end of file
......@@ -1500,6 +1500,26 @@ VulkanRenderer::setBloomOff()
m_postProcessingChain->m_bloom = false;
}
void
VulkanRenderer::enableLensDistortion(const float distortion)
{
float d = distortion;
if (distortion >= 1.0f)
{
d = 0.99f;
LOG(WARNING) << "Distortion invalid (>= 1.0f), clamped to 0.99f";
}
if (distortion <= -1.0f)
{
d = -0.99f;
LOG(WARNING) << "Distortion invalid (<= -1.0f), clamped to -0.99f";
}
m_enableLensDistortion = true;
m_lensDistortionFactor = d;
}
void
VulkanRenderer::setCommandBufferState(VkCommandBuffer * commandBuffer, uint32_t width, uint32_t height)
{
......
......@@ -76,6 +76,8 @@ public:
void setBloomOn();
void setBloomOff();
void enableLensDistortion(const float distortion);
protected:
friend class VulkanViewer;
friend class VulkanMaterialDelegate;
......@@ -313,6 +315,9 @@ protected:
uint32_t m_shadowMapResolution = 2048;
std::vector<glm::mat4> m_lightMatrices;
bool m_enableLensDistortion = false;
float m_lensDistortionFactor = 0.0f;
std::vector<std::shared_ptr<VulkanRenderDelegate>> m_renderDelegates;
uint32_t m_renderQueueFamily = 0;
......
......@@ -86,6 +86,12 @@ VulkanViewer::setResolution(unsigned int width, unsigned int height)
m_height = height;
}
void
VulkanViewer::enableLensDistortion(const float distortion)
{
m_renderer->enableLensDistortion(distortion);
}
void
VulkanViewer::startRenderingLoop()
{
......
......@@ -88,6 +88,14 @@ public:
///
void setResolution(unsigned int width, unsigned int height);
///
/// \brief Enable lens distortion
/// \param distortion Distortion factor to apply: range is (-1.0, 1.0)
/// A negative distortion value will cause pincushion distortion. A
/// positive value will cause barrel distortion.
///
void enableLensDistortion(const float distortion);
protected:
friend class VulkanInteractorStyle;
friend class VulkanInteractorStyleFreeCamera;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment