Browse Source

more tweaks to depth bits

David Rose 17 years ago
parent
commit
28e082c7ea

+ 11 - 1
panda/src/tinydisplay/clip.cxx

@@ -1,4 +1,5 @@
 #include "zgl.h"
 #include "zgl.h"
+#include <limits.h>
 
 
 /* fill triangle profile */
 /* fill triangle profile */
 /* #define PROFILE */
 /* #define PROFILE */
@@ -22,7 +23,16 @@ void gl_transform_to_viewport(GLContext *c,GLVertex *v)
                    + c->viewport.trans.v[1] );
                    + c->viewport.trans.v[1] );
   v->zp.z= (int) ( v->pc.v[2] * winv * c->viewport.scale.v[2] 
   v->zp.z= (int) ( v->pc.v[2] * winv * c->viewport.scale.v[2] 
                    + c->viewport.trans.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 */
   /* color */
   v->zp.r=(int)(v->color.v[0] * (ZB_POINT_RED_MAX - ZB_POINT_RED_MIN) 
   v->zp.r=(int)(v->color.v[0] * (ZB_POINT_RED_MAX - ZB_POINT_RED_MIN) 

+ 17 - 0
panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx

@@ -491,6 +491,23 @@ void TinyGraphicsStateGuardian::
 end_frame(Thread *current_thread) {
 end_frame(Thread *current_thread) {
   GraphicsStateGuardian::end_frame(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
 #ifdef DO_PSTATS
   // Flush any PCollectors specific to this kind of GSG.
   // Flush any PCollectors specific to this kind of GSG.
   _vertices_immediate_pcollector.flush_level();
   _vertices_immediate_pcollector.flush_level();