From 8c6d4a4c85c182e4e9bf8aa893fd111962980bf1 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Fri, 26 May 2017 09:45:26 -0700 Subject: [PATCH] also initialize near/far for depth shader in TinyRenderer --- examples/TinyRenderer/TinyRenderer.cpp | 4 +++- examples/TinyRenderer/our_gl.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 5930151a4..23f9fda10 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -38,6 +38,9 @@ struct DepthShader : public IShader { m_lightModelView(lightModelView), m_lightDistance(lightDistance) { + m_nearPlane = m_projectionMat.col(3)[2]/(m_projectionMat.col(2)[2]-1); + m_farPlane = m_projectionMat.col(3)[2]/(m_projectionMat.col(2)[2]+1); + m_invModelMat = m_modelMat.invert_transpose(); } virtual Vec4f vertex(int iface, int nthvert) { @@ -580,7 +583,6 @@ void TinyRenderer::renderObjectDepth(TinyRenderObjectData& renderData) Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]); DepthShader shader(model, lightModelViewMatrix, lightViewProjectionMatrix,renderData.m_modelMatrix, localScaling, light_distance); - shader.m_farPlane=1e30; for (int i=0; infaces(); i++) { for (int j=0; j<3; j++) { diff --git a/examples/TinyRenderer/our_gl.cpp b/examples/TinyRenderer/our_gl.cpp index c9c1adf8a..a47bfe2cb 100644 --- a/examples/TinyRenderer/our_gl.cpp +++ b/examples/TinyRenderer/our_gl.cpp @@ -180,10 +180,11 @@ void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zb zbuffer[P.x+P.y*image.get_width()]>frag_depth) continue; bool discard = shader.fragment(bc_clip, color); - if (frag_depth<-shader.m_farPlane) - discard=true; - if (frag_depth>-shader.m_nearPlane) - discard=true; + if (frag_depth<-shader.m_farPlane) + discard=true; + if (frag_depth>-shader.m_nearPlane) + discard=true; + if (!discard) { zbuffer[P.x+P.y*image.get_width()] = frag_depth; if (segmentationMaskBuffer)