diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index db7bcbb35..5930151a4 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -91,7 +91,8 @@ struct Shader : public IShader { mat<4,3,float> varying_tri_light_view; mat<3,3,float> varying_nrm; // normal per vertex to be interpolated by FS mat<4,3,float> world_tri; // model triangle coordinates in the world space used for backface culling, written by VS - + + Shader(Model* model, Vec3f light_dir_local, Vec3f light_color, Matrix& modelView, Matrix& lightModelView, Matrix& projectionMat, Matrix& modelMat, Matrix& viewportMat, Vec3f localScaling, const Vec4f& colorRGBA, int width, int height, b3AlignedObjectArray* shadowBuffer, float ambient_coefficient=0.6, float diffuse_coefficient=0.35, float specular_coefficient=0.05) :m_model(model), m_light_dir_local(light_dir_local), @@ -112,6 +113,9 @@ struct Shader : public IShader { m_height(height) { + 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); + //printf("near=%f, far=%f\n", m_nearPlane, m_farPlane); m_invModelMat = m_modelMat.invert_transpose(); m_projectionModelViewMat = m_projectionMat*m_modelView1; m_projectionLightViewMat = m_projectionMat*m_lightModelView; @@ -576,7 +580,7 @@ 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 ea570386d..a050eda39 100644 --- a/examples/TinyRenderer/our_gl.cpp +++ b/examples/TinyRenderer/our_gl.cpp @@ -180,6 +180,9 @@ 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 (!discard) { zbuffer[P.x+P.y*image.get_width()] = frag_depth; if (segmentationMaskBuffer) diff --git a/examples/TinyRenderer/our_gl.h b/examples/TinyRenderer/our_gl.h index 17403764c..6a18cf1ae 100644 --- a/examples/TinyRenderer/our_gl.h +++ b/examples/TinyRenderer/our_gl.h @@ -11,6 +11,8 @@ Matrix projection(float coeff=0.f); // coeff = -1/c Matrix lookat(Vec3f eye, Vec3f center, Vec3f up); struct IShader { + float m_nearPlane; + float m_farPlane; virtual ~IShader(); virtual Vec4f vertex(int iface, int nthvert) = 0; virtual bool fragment(Vec3f bar, TGAColor &color) = 0; diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index e70395e7e..788bd151e 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -6463,7 +6463,7 @@ initpybullet(void) SpamError = PyErr_NewException("pybullet.error", NULL, NULL); Py_INCREF(SpamError); PyModule_AddObject(m, "error", SpamError); - + printf("pybullet build time: %s %s\n", __DATE__,__TIME__); Py_AtExit( b3pybulletExitFunc );