From ce69f27f321d6d47a1d67f0b0517d88eee25cbe3 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Wed, 8 Feb 2017 11:34:38 -0800 Subject: [PATCH] Modify the depth buffer value in TinyRenderer to be consistent as in OpenGL. --- examples/CommonInterfaces/CommonCameraInterface.h | 3 +++ examples/OpenGLWindow/SimpleCamera.cpp | 10 ++++++++++ examples/OpenGLWindow/SimpleCamera.h | 5 ++++- .../SharedMemory/TinyRendererVisualShapeConverter.cpp | 11 ++++++++++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/CommonInterfaces/CommonCameraInterface.h b/examples/CommonInterfaces/CommonCameraInterface.h index 813aa4344..88db58685 100644 --- a/examples/CommonInterfaces/CommonCameraInterface.h +++ b/examples/CommonInterfaces/CommonCameraInterface.h @@ -36,6 +36,9 @@ struct CommonCameraInterface virtual void setAspectRatio(float ratio) = 0; virtual float getAspectRatio() const = 0; + + virtual float getCameraFrustumFar() const = 0; + virtual float getCameraFrustumNear() const = 0; }; #endif //COMMON_CAMERA_INTERFACE_H diff --git a/examples/OpenGLWindow/SimpleCamera.cpp b/examples/OpenGLWindow/SimpleCamera.cpp index 4b0b27164..93838a212 100644 --- a/examples/OpenGLWindow/SimpleCamera.cpp +++ b/examples/OpenGLWindow/SimpleCamera.cpp @@ -383,3 +383,13 @@ float SimpleCamera::getAspectRatio() const { return m_data->m_aspect; } + +float SimpleCamera::getCameraFrustumFar() const +{ + return m_data->m_frustumZFar; +} + +float SimpleCamera::getCameraFrustumNear() const +{ + return m_data->m_frustumZNear; +} diff --git a/examples/OpenGLWindow/SimpleCamera.h b/examples/OpenGLWindow/SimpleCamera.h index 5a61a729f..b186759c8 100644 --- a/examples/OpenGLWindow/SimpleCamera.h +++ b/examples/OpenGLWindow/SimpleCamera.h @@ -47,6 +47,9 @@ struct SimpleCamera : public CommonCameraInterface virtual void setAspectRatio(float ratio); virtual float getAspectRatio() const; + + virtual float getCameraFrustumFar() const; + virtual float getCameraFrustumNear() const; }; -#endif //SIMPLE_CAMERA_H \ No newline at end of file +#endif //SIMPLE_CAMERA_H diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index 5ae682fc2..cfe25c938 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -958,7 +958,16 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels { if (depthBuffer) { - depthBuffer[i] = m_data->m_depthBuffer[i+startPixelIndex]; + 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); + + // the depth buffer value is between 0 and 1 + float a = farPlane / (farPlane - nearPlane); + float b = farPlane * nearPlane / (nearPlane - farPlane); + depthBuffer[i] = a + b / distance; } if (segmentationMaskBuffer) {