diff --git a/panda/src/tinydisplay/clip.cxx b/panda/src/tinydisplay/clip.cxx index 72666fb63a..6ea46c6f48 100644 --- a/panda/src/tinydisplay/clip.cxx +++ b/panda/src/tinydisplay/clip.cxx @@ -1,4 +1,5 @@ #include "zgl.h" +#include /* fill triangle profile */ /* #define PROFILE */ @@ -22,7 +23,16 @@ void gl_transform_to_viewport(GLContext *c,GLVertex *v) + c->viewport.trans.v[1] ); v->zp.z= (int) ( v->pc.v[2] * winv * c->viewport.scale.v[2] + c->viewport.trans.v[2] ); - v->zp.z += (c->zbias << (ZB_Z_BITS - 8)); + + // Add the z-bias, if any. Be careful not to overflow the int. + int z = v->zp.z + (c->zbias << (ZB_POINT_Z_FRAC_BITS + 4)); + if (z < v->zp.z && c->zbias > 0) { + v->zp.z = INT_MAX; + } else if (z > v->zp.z && c->zbias < 0) { + v->zp.z = -INT_MAX; + } else { + v->zp.z = z; + } /* color */ v->zp.r=(int)(v->color.v[0] * (ZB_POINT_RED_MAX - ZB_POINT_RED_MIN) diff --git a/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx b/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx index 1af8413e02..b6dc116c50 100644 --- a/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx +++ b/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx @@ -491,6 +491,23 @@ void TinyGraphicsStateGuardian:: end_frame(Thread *current_thread) { GraphicsStateGuardian::end_frame(current_thread); +#ifndef NDEBUG + static ConfigVariableBool td_show_zbuffer + ("td-show-zbuffer", false, + PRC_DESC("Set this true to draw the ZBuffer instead of the visible buffer, when rendering with tinydisplay. This is useful to aid debugging the ZBuffer")); + if (td_show_zbuffer) { + PIXEL *tp = _current_frame_buffer->pbuf; + ZPOINT *tz = _current_frame_buffer->zbuf; + for (int yi = 0; yi < _current_frame_buffer->ysize; ++yi) { + for (int xi = 0; xi < _current_frame_buffer->xsize; ++xi) { + (*tp) = (int)(*tz); + ++tz; + ++tp; + } + } + } +#endif // NDEBUG + #ifdef DO_PSTATS // Flush any PCollectors specific to this kind of GSG. _vertices_immediate_pcollector.flush_level();