Skip to content
Snippets Groups Projects
Commit 809aa0e7 authored by Jonathan Kunstwald's avatar Jonathan Kunstwald
Browse files

Add ViewerRenderer::query3DPosition

parent 1cdd16bf
No related branches found
No related tags found
1 merge request!71Viewer picking
......@@ -159,6 +159,42 @@ void glow::viewer::ViewerRenderer::maximizeSamples()
mSSAOSamples = 256;
}
tg::optional<tg::pos3> glow::viewer::ViewerRenderer::query3DPosition(const tg::isize2& resolution,
tg::ipos2 pixel,
glow::viewer::SubViewData& subViewData,
glow::viewer::CameraController& cam) const
{
if (pixel.x >= resolution.width || pixel.y >= resolution.height)
return {};
// rescale position
pixel.y = resolution.height - pixel.y - 1;
float d;
{
auto fb = mFramebuffer->bind();
fb.attachDepth(subViewData.targetDepth);
glReadPixels(static_cast<int>(pixel.x), static_cast<int>(pixel.y), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &d);
}
if (d < 1.f)
{
// unproject (with viewport coords!)
tg::vec4 v{pixel.x / float(resolution.width) * 2 - 1, pixel.y / float(resolution.height) * 2 - 1, d * 2 - 1, 1.0};
v = tg::inverse(cam.computeProjMatrix()) * v;
v /= v.w;
v = tg::inverse(cam.computeViewMatrix()) * v;
return tg::pos3(v);
}
else
{
return {};
}
}
void glow::viewer::ViewerRenderer::renderSubview(tg::isize2 const& res, tg::ipos2 const& offset, SubViewData& subViewData, Scene const& scene, glow::viewer::CameraController& cam)
{
if (scene.clearAccumulation)
......
......@@ -80,6 +80,8 @@ public:
void endFrame(float approximateRenderTime = 0.f);
void maximizeSamples();
void set_2d_transform(tg::mat3x2 const& transform) { mVectorRenderer.set_global_transform(transform); }
tg::optional<tg::pos3> query3DPosition(tg::isize2 const& resolution, tg::ipos2 pixel, SubViewData& subViewData, CameraController& cam) const;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment