Commit 7e9b9b91 authored by Philip Trettner's avatar Philip Trettner

Added a read3D pos function

parent b3fb141c
......@@ -270,6 +270,41 @@ void RenderingPipeline::render(const std::function<void(RenderPass const& pass)>
}
}
bool RenderingPipeline::queryPosition3D(int x, int y, glm::vec3* pos, float* depth)
{
if (!mCamera)
return false;
if (x < 0 || y < 0 || x >= (int)mCamera->getViewportWidth() || y >= (int)mCamera->getViewportHeight())
return false;
// rescale position
x = x * mWidth / mCamera->getViewportWidth();
y = mHeight - y * mHeight / mCamera->getViewportHeight() - 1;
auto fb = mFboForward->bind();
float d;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &d);
if (depth)
*depth = d;
if (d < 1)
{
// unproject (with viewport coords!)
glm::vec4 v{x / float(mWidth) * 2 - 1, y / float(mHeight) * 2 - 1, d * 2 - 1, 1.0};
v = glm::inverse(mCamera->getProjectionMatrix()) * v;
v /= v.w;
v = glm::inverse(mCamera->getViewMatrix()) * v;
if (pos)
*pos = glm::vec3(v);
return true;
}
return false;
}
SharedRenderingPipeline RenderingPipeline::create(const camera::SharedGenericCamera& cam)
{
auto pipe = std::make_shared<RenderingPipeline>();
......
......@@ -126,6 +126,13 @@ public:
*/
void render(std::function<void(RenderPass const& pass)> const& renderFunc);
public:
/// Reads back part of the depth buffer and calculates the 3d position of the pixel
/// returns false if depth is background (== 1)
/// (x,y) are in camera pixel coords (they'll get scaled if internal size differs)
/// (0,0) is top-left
bool queryPosition3D(int x, int y, glm::vec3* pos, float* depth = nullptr);
public: // static creation
/// Creates a new rendering pipeline and optionally assigns a camera to it
static SharedRenderingPipeline create(camera::SharedGenericCamera const& cam = nullptr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment