From 9787429186f1f5c0240b9cea1e109196946aaabe Mon Sep 17 00:00:00 2001 From: David Rose Date: Sun, 4 May 2008 23:44:49 +0000 Subject: [PATCH] fix texturing issues --- .../tinydisplay/tinyGraphicsStateGuardian.cxx | 1 - panda/src/tinydisplay/tinyXGraphicsWindow.cxx | 1 + panda/src/tinydisplay/zbuffer.h | 38 +++++++++++++++---- panda/src/tinydisplay/ztriangle_two.h | 15 +++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx b/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx index 6c24b50edb..fb3dae1d69 100644 --- a/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx +++ b/panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx @@ -1191,7 +1191,6 @@ begin_draw_primitives(const GeomPipelineReader *geom_reader, } _c->zb_fill_tri = fill_tri_funcs[depth_write_state][color_write_state][alpha_test_state][depth_test_state][shading_state][texturing_state]; - //_c->zb_fill_tri = ZB_fillTriangleFlat_zless; return true; } diff --git a/panda/src/tinydisplay/tinyXGraphicsWindow.cxx b/panda/src/tinydisplay/tinyXGraphicsWindow.cxx index 6f0b20d8bb..ba0f50fb67 100644 --- a/panda/src/tinydisplay/tinyXGraphicsWindow.cxx +++ b/panda/src/tinydisplay/tinyXGraphicsWindow.cxx @@ -224,6 +224,7 @@ begin_flip() { XPutImage(_display, _xwindow, _gc, _ximage, 0, 0, 0, 0, _properties.get_x_size(), _properties.get_y_size()); + XFlush(_display); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/tinydisplay/zbuffer.h b/panda/src/tinydisplay/zbuffer.h index 80303fa0dc..4449d6c2b1 100644 --- a/panda/src/tinydisplay/zbuffer.h +++ b/panda/src/tinydisplay/zbuffer.h @@ -11,19 +11,41 @@ #define ZB_POINT_Z_FRAC_BITS 14 -#define ZB_POINT_S_MIN ( (1<<13) ) -#define ZB_POINT_S_MAX ( (1<<22)-(1<<13) ) -#define ZB_POINT_T_MIN ( (1<<21) ) -#define ZB_POINT_T_MAX ( (1<<30)-(1<<21) ) +/* The number of bits for lookup for S and T texture coords. This is + based on a fixed texture size of 256x256. */ +#define ZB_POINT_ST_BITS 8 + +/* The number of fractional bits below the S and T texture coords. + The more we have, the more precise the texel calculation will be + when we zoom into small details of a texture; but the greater + chance we'll overflow our 32-bit integer if the T texcoord gets + large. */ +#define ZB_POINT_ST_FRAC_BITS 10 + +/* Various parameters and accessors based on the above bits. */ +#define ZB_POINT_S_LOW ZB_POINT_ST_FRAC_BITS +#define ZB_POINT_S_MIN 0 +#define ZB_POINT_S_MAX (1 << (ZB_POINT_ST_BITS + ZB_POINT_S_LOW)) +#define ZB_POINT_S_MASK ((1 << (ZB_POINT_ST_BITS + ZB_POINT_S_LOW)) - (1 << ZB_POINT_S_LOW)) + +#define ZB_POINT_T_LOW (ZB_POINT_ST_BITS + ZB_POINT_S_LOW) +#define ZB_POINT_T_MIN 0 +#define ZB_POINT_T_MAX (1 << (ZB_POINT_ST_BITS + ZB_POINT_T_LOW)) +#define ZB_POINT_T_MASK ((1 << (ZB_POINT_ST_BITS + ZB_POINT_T_LOW)) - (1 << ZB_POINT_T_LOW)) + +// Returns the index within a 256x256 texture for the given (s, t) +// texel. +#define ZB_TEXEL(s, t) \ + ((((t) & ZB_POINT_T_MASK) | ((s) & ZB_POINT_S_MASK)) >> ZB_POINT_ST_FRAC_BITS) #define ZB_POINT_RED_MIN 0x0000 -#define ZB_POINT_RED_MAX 0xff00 +#define ZB_POINT_RED_MAX 0xffff #define ZB_POINT_GREEN_MIN 0x0000 -#define ZB_POINT_GREEN_MAX 0xff00 +#define ZB_POINT_GREEN_MAX 0xffff #define ZB_POINT_BLUE_MIN 0x0000 -#define ZB_POINT_BLUE_MAX 0xff00 +#define ZB_POINT_BLUE_MAX 0xffff #define ZB_POINT_ALPHA_MIN 0x0000 -#define ZB_POINT_ALPHA_MAX 0xff00 +#define ZB_POINT_ALPHA_MAX 0xffff /* display modes */ #define ZB_MODE_5R6G5B 1 /* true color 16 bits */ diff --git a/panda/src/tinydisplay/ztriangle_two.h b/panda/src/tinydisplay/ztriangle_two.h index eb2a8c6870..30a49fb567 100644 --- a/panda/src/tinydisplay/ztriangle_two.h +++ b/panda/src/tinydisplay/ztriangle_two.h @@ -82,7 +82,7 @@ void FNAME(ZB_fillTriangleMapping) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp=texture[((t & 0x3FC00000) | (s & 0x3fc000)) >> 14]; \ + tmp = texture[ZB_TEXEL(s, t)]; \ if (ACMP(zb, PIXEL_A(tmp))) { \ STORE_PIX(pp[_a], tmp, PIXEL_R(tmp), PIXEL_G(tmp), PIXEL_B(tmp), PIXEL_A(tmp)); \ STORE_Z(pz[_a], zz); \ @@ -118,7 +118,7 @@ void FNAME(ZB_fillTriangleMappingFlat) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp=texture[((t & 0x3FC00000) | (s & 0x3fc000)) >> 14]; \ + tmp = texture[ZB_TEXEL(s, t)]; \ int a = oa * PIXEL_A(tmp) >> 16; \ if (ACMP(zb, a)) { \ STORE_PIX(pp[_a], \ @@ -159,7 +159,7 @@ void FNAME(ZB_fillTriangleMappingSmooth) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp=texture[((t & 0x3FC00000) | (s & 0x3fc000)) >> 14]; \ + tmp = texture[ZB_TEXEL(s, t)]; \ int a = oa1 * PIXEL_A(tmp) >> 16; \ if (ACMP(zb, a)) { \ STORE_PIX(pp[_a], \ @@ -216,8 +216,7 @@ void FNAME(ZB_fillTriangleMappingPerspective) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp = *(PIXEL *)((char *)texture+ \ - (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH))); \ + tmp = texture[ZB_TEXEL(s, t)]; \ if (ACMP(zb, PIXEL_A(tmp))) { \ STORE_PIX(pp[_a], tmp, PIXEL_R(tmp), PIXEL_G(tmp), PIXEL_B(tmp), PIXEL_A(tmp)); \ STORE_Z(pz[_a], zz); \ @@ -322,8 +321,7 @@ void FNAME(ZB_fillTriangleMappingPerspectiveFlat) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp=*(PIXEL *)((char *)texture+ \ - (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH))); \ + tmp = texture[ZB_TEXEL(s, t)]; \ int a = oa * PIXEL_A(tmp) >> 16; \ if (ACMP(zb, a)) { \ STORE_PIX(pp[_a], \ @@ -436,8 +434,7 @@ void FNAME(ZB_fillTriangleMappingPerspectiveSmooth) (ZBuffer *zb, { \ zz=z >> ZB_POINT_Z_FRAC_BITS; \ if (ZCMP(pz[_a], zz)) { \ - tmp=*(PIXEL *)((char *)texture+ \ - (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH))); \ + tmp = texture[ZB_TEXEL(s, t)]; \ int a = oa1 * PIXEL_A(tmp) >> 16; \ if (ACMP(zb, a)) { \ STORE_PIX(pp[_a], \