@@ -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