Fix wrong depth values returned by TinyRenderer

This commit is contained in:
Michel Breyer
2017-11-17 14:45:37 +01:00
parent 6214ce5f93
commit 5138fe2c55

View File

@@ -1025,12 +1025,13 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels
{
if (depthBuffer)
{
float distance = -m_data->m_depthBuffer[i+startPixelIndex];
float farPlane = m_data->m_camera.getCameraFrustumFar();
float nearPlane = m_data->m_camera.getCameraFrustumNear();
btClamp(distance,nearPlane,farPlane);
// TinyRenderer returns clip coordinates, transform to eye coordinates first
float z_c = -m_data->m_depthBuffer[i+startPixelIndex];
float distance = (farPlane - nearPlane) / (farPlane + nearPlane) * (z_c + 2. * farPlane * nearPlane / (farPlane - nearPlane));
// the depth buffer value is between 0 and 1
float a = farPlane / (farPlane - nearPlane);
float b = farPlane * nearPlane / (nearPlane - farPlane);