Merge pull request #1151 from erwincoumans/master

also discard pixels in front of nearplane
This commit is contained in:
erwincoumans
2017-05-26 10:12:36 -07:00
committed by GitHub
2 changed files with 7 additions and 3 deletions

View File

@@ -38,6 +38,9 @@ struct DepthShader : public IShader {
m_lightModelView(lightModelView), m_lightModelView(lightModelView),
m_lightDistance(lightDistance) 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(); m_invModelMat = m_modelMat.invert_transpose();
} }
virtual Vec4f vertex(int iface, int nthvert) { 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]); 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); DepthShader shader(model, lightModelViewMatrix, lightViewProjectionMatrix,renderData.m_modelMatrix, localScaling, light_distance);
shader.m_farPlane=1e30;
for (int i=0; i<model->nfaces(); i++) for (int i=0; i<model->nfaces(); i++)
{ {
for (int j=0; j<3; j++) { for (int j=0; j<3; j++) {

View File

@@ -180,8 +180,10 @@ void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zb
zbuffer[P.x+P.y*image.get_width()]>frag_depth) zbuffer[P.x+P.y*image.get_width()]>frag_depth)
continue; continue;
bool discard = shader.fragment(bc_clip, color); bool discard = shader.fragment(bc_clip, color);
if (frag_depth<-shader.m_farPlane) if (frag_depth<-shader.m_farPlane)
discard=true; discard=true;
if (frag_depth>-shader.m_nearPlane)
discard=true;
if (!discard) { if (!discard) {
zbuffer[P.x+P.y*image.get_width()] = frag_depth; zbuffer[P.x+P.y*image.get_width()] = frag_depth;