Skip to content

Commit 80b4bb7

Browse files
committed
fix pick depth for pretty transparency, read depth from pick buffer, deprecate old functions
1 parent cdb92b7 commit 80b4bb7

File tree

5 files changed

+19
-31
lines changed

5 files changed

+19
-31
lines changed

examples/demo-app/demo_app.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ void callback() {
807807
std::tie(xInd, yInd) = polyscope::view::screenCoordsToBufferInds(screenCoords);
808808

809809
glm::vec3 worldRay = polyscope::view::screenCoordsToWorldRay(screenCoords);
810-
glm::vec3 worldPos = polyscope::view::screenCoordsToWorldPosition(screenCoords);
811810
polyscope::PickResult pickResult = polyscope::queryPickAtScreenCoords(screenCoords);
812811

813812
std::cout << "Polyscope scene test click " << std::endl;
@@ -817,7 +816,7 @@ void callback() {
817816
std::cout << " worldRay: ";
818817
polyscope::operator<<(std::cout, worldRay) << std::endl;
819818
std::cout << " worldPos: ";
820-
polyscope::operator<<(std::cout, worldPos) << std::endl;
819+
polyscope::operator<<(std::cout, pickResult.position) << std::endl;
821820
if (pickResult.isHit) {
822821
std::cout << " structure: " << pickResult.structureType << " " << pickResult.structureName
823822
<< " local ind: " << pickResult.localIndex << std::endl;
@@ -827,7 +826,7 @@ void callback() {
827826
}
828827

829828
// Construct point at click location
830-
polyscope::registerPointCloud("click point", std::vector<glm::vec3>({worldPos}));
829+
polyscope::registerPointCloud("click point", std::vector<glm::vec3>({pickResult.position}));
831830

832831
// Construct unit-length vector pointing in the direction of the click
833832
// (this depends only on the camera parameters, and does not require accessing the depth buffer)

include/polyscope/view.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ bool getWindowResizable();
138138
// Get world geometry corresponding to a screen pixel (e.g. from a mouse click)
139139
glm::vec3 screenCoordsToWorldRay(glm::vec2 screenCoords);
140140
glm::vec3 bufferIndsToWorldRay(glm::ivec2 bufferInds);
141-
glm::vec3 screenCoordsToWorldPosition(glm::vec2 screenCoords); // queries the depth buffer to get full position
142-
glm::vec3 bufferIndsToWorldPosition(glm::ivec2 bufferInds);
141+
glm::vec3 screenCoordsAndDepthToWorldPosition(glm::vec2 screenCoords, float clipDepth);
143142

144143
// Get and set camera from json string
145144
std::string getViewAsJson();
@@ -181,7 +180,6 @@ void processKeyboardNavigation(ImGuiIO& io);
181180

182181
// deprecated, bad names, see variants above
183182
glm::vec3 bufferCoordsToWorldRay(glm::vec2 bufferCoords);
184-
glm::vec3 bufferCoordsToWorldPosition(int xPos, int yPos);
185183

186184

187185
} // namespace view

src/pick.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ PickResult queryPickAtScreenCoords(glm::vec2 screenCoords) {
1818
PickResult queryPickAtBufferInds(glm::ivec2 bufferInds) {
1919
PickResult result;
2020

21-
// Query the render buffer
22-
result.position = view::bufferIndsToWorldPosition(bufferInds);
23-
result.depth = glm::length(result.position - view::getCameraWorldPosition());
24-
2521
// Query the pick buffer
22+
// (this necessarily renders to pickFrameBuffer)
2623
std::pair<Structure*, size_t> rawPickResult = pick::pickAtBufferCoords(bufferInds.x, bufferInds.y);
2724

25+
// Query the depth buffer populated above
26+
render::FrameBuffer* pickFramebuffer = render::engine->pickFramebuffer.get();
27+
float clipDepth = pickFramebuffer->readDepth(bufferInds.x, view::bufferHeight - bufferInds.y);
28+
2829
// Transcribe result into return tuple
2930
result.structure = rawPickResult.first;
3031
result.bufferInds = bufferInds;
3132
result.screenCoords = view::bufferIndsToScreenCoords(bufferInds);
33+
result.position = view::screenCoordsAndDepthToWorldPosition(result.screenCoords, clipDepth);
34+
result.depth = glm::length(result.position - view::getCameraWorldPosition());
35+
3236
if (rawPickResult.first == nullptr) {
3337
result.isHit = false;
3438
result.structureType = "";

src/render/opengl/gl_engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ float GLFrameBuffer::readDepth(int xPos, int yPos) {
977977
bind();
978978

979979
// Read from the buffer
980-
float result;
980+
float result = 1.;
981981
glReadPixels(xPos, yPos, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &result);
982982

983983
return result;

src/view.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -538,39 +538,26 @@ glm::vec3 bufferCoordsToWorldRay(glm::vec2 bufferCoords) {
538538
return worldRayDir;
539539
}
540540

541-
glm::vec3 screenCoordsToWorldPosition(glm::vec2 screenCoords) {
542-
int xInd, yInd;
543-
std::tie(xInd, yInd) = screenCoordsToBufferInds(screenCoords);
544-
return bufferCoordsToWorldPosition(xInd, yInd);
545-
}
546541

547-
glm::vec3 bufferIndsToWorldPosition(glm::ivec2 bufferInds) {
548-
return bufferCoordsToWorldPosition(bufferInds.x, bufferInds.y);
549-
}
542+
glm::vec3 screenCoordsAndDepthToWorldPosition(glm::vec2 screenCoords, float clipDepth) {
550543

551-
glm::vec3 bufferCoordsToWorldPosition(int xInd, int yInd) {
544+
if (clipDepth == 1.) {
545+
// if we didn't hit anything in the depth buffer, just return infinity
546+
float inf = std::numeric_limits<float>::infinity();
547+
return glm::vec3{inf, inf, inf};
548+
}
552549

553-
glm::vec2 screenCoords = bufferIndsToScreenCoords(xInd, yInd);
554550

555551
glm::mat4 view = getCameraViewMatrix();
556552
glm::mat4 viewInv = glm::inverse(view);
557553
glm::mat4 proj = getCameraPerspectiveMatrix();
558554
glm::mat4 projInv = glm::inverse(proj);
559555
// glm::vec2 depthRange = {0., 1.}; // no support for nonstandard depth range, currently
560556

561-
// query the depth buffer to get depth
562-
render::FrameBuffer* sceneFramebuffer = render::engine->sceneBuffer.get();
563-
float depth = sceneFramebuffer->readDepth(xInd, view::bufferHeight - yInd);
564-
if (depth == 1.) {
565-
// if we didn't hit anything in the depth buffer, just return infinity
566-
float inf = std::numeric_limits<float>::infinity();
567-
return glm::vec3{inf, inf, inf};
568-
}
569-
570557
// convert depth to world units
571558
glm::vec2 screenPos{screenCoords.x / static_cast<float>(view::windowWidth),
572559
1.f - screenCoords.y / static_cast<float>(view::windowHeight)};
573-
float z = depth * 2.0f - 1.0f;
560+
float z = clipDepth * 2.0f - 1.0f;
574561
glm::vec4 clipPos = glm::vec4(screenPos * 2.0f - 1.0f, z, 1.0f);
575562
glm::vec4 viewPos = projInv * clipPos;
576563
viewPos /= viewPos.w;

0 commit comments

Comments
 (0)