MCPcopy Create free account
hub / github.com/antopilo/Nuake / DepthToWorldPosition

Function DepthToWorldPosition

Editor/Source/Editor/Windows/EditorInterface.cpp:84–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82 Ref<FrameBuffer> virtualCamera;
83
84 glm::vec3 DepthToWorldPosition(const glm::vec2& pixelPos, float depth, const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix, const glm::vec2& viewportSize)
85 {
86 // Convert pixel position to normalized device coordinates (NDC)
87 glm::vec2 ndcPos;
88 ndcPos.x = (2.0f * pixelPos.x) / viewportSize.x - 1.0f;
89 ndcPos.y = 1.0f - (2.0f * pixelPos.y) / viewportSize.y; // Y-axis inversion
90
91 // Depth value ranges from 0 to 1 in NDC space
92 float ndcDepth = depth * 2.0f - 1.0f;
93
94 // Construct the NDC position
95 glm::vec4 ndcPos4(ndcPos.x, ndcPos.y, ndcDepth, 1.0f);
96
97 // Compute the inverse of the projection and view matrices
98 glm::mat4 invProj = glm::inverse(projectionMatrix);
99 glm::mat4 invView = glm::inverse(viewMatrix);
100
101 // Transform the NDC position to eye space
102 glm::vec4 eyePos4 = invProj * ndcPos4;
103 eyePos4.z = -1.0f; // Set Z to -1 as it's on the near plane
104 eyePos4 /= eyePos4.w;
105
106 //eyePos4.w = 0.0f;
107
108 // Transform the eye space position to world space
109 glm::vec4 worldPos4 = invView * eyePos4;
110
111 // Return the 3D world position
112 glm::vec3 worldPos(worldPos4.x, worldPos4.y, worldPos4.z);
113 return worldPos;
114 }
115
116 EditorInterface::EditorInterface(CommandBuffer& commandBuffer)
117 {

Callers 1

DrawViewportMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected