14 int pixel_count_white_untextured;
15 int pixel_count_flat_untextured;
16 int pixel_count_smooth_untextured;
17 int pixel_count_white_textured;
18 int pixel_count_flat_textured;
19 int pixel_count_smooth_textured;
20 int pixel_count_white_perspective;
21 int pixel_count_flat_perspective;
22 int pixel_count_smooth_perspective;
23 int pixel_count_smooth_multitex2;
24 int pixel_count_smooth_multitex3;
28 ZB_open(
int xsize,
int ysize,
int mode,
30 unsigned char *color_indexes,
31 unsigned int *color_table,
42 xsize = (xsize + 3) & ~3;
47 zb->linesize = (xsize * PSZB + 3) & ~3;
50 #ifdef TGL_FEATURE_8_BITS
52 ZB_initDither(zb, nb_colors, color_indexes, color_table);
55 #ifdef TGL_FEATURE_32_BITS
58 #ifdef TGL_FEATURE_24_BITS
68 size = zb->xsize * zb->ysize *
sizeof(ZPOINT);
70 zb->zbuf = (ZPOINT *)gl_malloc(size);
74 if (frame_buffer == NULL) {
75 zb->pbuf = (PIXEL *)gl_malloc(zb->ysize * zb->linesize);
76 if (zb->pbuf == NULL) {
80 zb->frame_buffer_allocated = 1;
82 zb->frame_buffer_allocated = 0;
83 zb->pbuf = (PIXEL *)frame_buffer;
94 #ifdef TGL_FEATURE_8_BITS
95 if (zb->mode == ZB_MODE_INDEX)
99 if (zb->frame_buffer_allocated)
107 ZB_resize(
ZBuffer * zb,
void *frame_buffer,
int xsize,
int ysize) {
110 nassertv(zb != NULL);
113 xsize = (xsize + 3) & ~3;
117 zb->linesize = (xsize * PSZB + 3) & ~3;
119 size = zb->xsize * zb->ysize *
sizeof(ZPOINT);
121 zb->zbuf = (ZPOINT *)gl_malloc(size);
123 if (zb->frame_buffer_allocated)
126 if (frame_buffer == NULL) {
127 zb->pbuf = (PIXEL *)gl_malloc(zb->ysize * zb->linesize);
128 zb->frame_buffer_allocated = 1;
130 zb->pbuf = (PIXEL *)frame_buffer;
131 zb->frame_buffer_allocated = 0;
136 ZB_copyBuffer(
const ZBuffer * zb,
144 p1 = (
unsigned char *)buf;
145 n = zb->xsize * PSZB;
146 for (y = 0; y < zb->ysize; y++) {
149 q = (PIXEL *) ((
char *) q + zb->linesize);
154 ZB_copyBufferNoAlpha(
const ZBuffer * zb,
void *buf,
int linesize) {
155 const PIXEL *q = zb->pbuf;
156 PIXEL *p = (PIXEL *)buf;
157 int xsize = zb->xsize;
158 for (
int y = 0; y < zb->ysize; ++y) {
161 PIXEL *p2 = p1 + xsize;
164 #ifdef WORDS_BIGENDIAN
165 *p1 = *q1 | 0x000000ff;
167 *p1 = *q1 | 0xff000000;
172 p = (PIXEL *) ((
char *) p + linesize);
173 q = (
const PIXEL *) ((
const char *) q + zb->linesize);
177 #define RGB32_TO_RGB16(v) \
178 (((v >> 8) & 0xf800) | (((v) >> 5) & 0x07e0) | (((v) & 0xff) >> 3))
181 static void ZB_copyFrameBuffer5R6G5B(
const ZBuffer * zb,
182 void *buf,
int linesize)
185 unsigned short *p, *p1;
189 p1 = (
unsigned short *) buf;
191 for (y = 0; y < zb->ysize; y++) {
195 p[0] = RGB32_TO_RGB16(q[0]);
196 p[1] = RGB32_TO_RGB16(q[1]);
197 p[2] = RGB32_TO_RGB16(q[2]);
198 p[3] = RGB32_TO_RGB16(q[3]);
202 p1 = (
unsigned short *)((
char *)p1 + linesize);
207 static void ZB_copyFrameBufferRGB24(
const ZBuffer * zb,
208 void *buf,
int linesize)
211 unsigned char *p, *p1;
214 fprintf(stderr,
"copyFrameBufferRGB24\n");
217 p1 = (
unsigned char *) buf;
219 for (y = 0; y < zb->ysize; y++) {
234 ZB_copyFrameBuffer(
const ZBuffer * zb,
void *buf,
237 #ifdef TGL_FEATURE_16_BITS
239 ZB_copyFrameBuffer5R6G5B(zb, buf, linesize);
242 #ifdef TGL_FEATURE_24_BITS
244 ZB_copyFrameBufferRGB24(zb, buf, linesize);
247 #ifdef TGL_FEATURE_32_BITS
249 ZB_copyBuffer(zb, buf, linesize);
258 ZB_copyFrameBufferNoAlpha(
const ZBuffer * zb,
void *buf,
261 #ifdef TGL_FEATURE_16_BITS
263 ZB_copyFrameBuffer5R6G5B(zb, buf, linesize);
266 #ifdef TGL_FEATURE_24_BITS
268 ZB_copyFrameBufferRGB24(zb, buf, linesize);
271 #ifdef TGL_FEATURE_32_BITS
273 ZB_copyBufferNoAlpha(zb, buf, linesize);
283 void ZB_zoomFrameBuffer(
ZBuffer *dest,
int dest_xmin,
int dest_ymin,
int dest_xsize,
int dest_ysize,
284 const ZBuffer *source,
int source_xmin,
int source_ymin,
int source_xsize,
int source_ysize) {
285 int tyinc = dest->linesize / PSZB;
286 int fyinc = source->linesize / PSZB;
289 for (
int ty = 0; ty < dest_ysize; ++ty) {
290 int fy = fyt / dest_ysize;
293 PIXEL *tp = dest->pbuf + dest_xmin + (dest_ymin + ty) * tyinc;
294 PIXEL *fp = source->pbuf + source_xmin + (source_ymin + fy) * fyinc;
295 ZPOINT *tz = dest->zbuf + dest_xmin + (dest_ymin + ty) * dest->xsize;
296 ZPOINT *fz = source->zbuf + source_xmin + (source_ymin + fy) * source->xsize;
298 for (
int tx = 0; tx < dest_xsize; ++tx) {
299 int fx = fxt / dest_xsize;
313 memset_s(
void *adr,
int val,
int count) {
318 p = (
unsigned int *)adr;
319 v = val | (val << 16);
322 for (i = 0; i < n; i++) {
330 q = (
unsigned short *) p;
332 for (i = 0; i < n; i++)
337 memset_l(
void *adr,
int val,
int count) {
341 p = (
unsigned int *)adr;
344 for (i = 0; i < n; i++) {
353 for (i = 0; i < n; i++)
359 memset_RGB24(
void *adr,
int r,
int v,
int b,
long count) {
361 long v1,v2,v3,*pt=(
long *)(adr);
362 unsigned char *p,R=(
unsigned char)r,V=(
unsigned char)v,B=(
unsigned char)b;
364 p=(
unsigned char *)adr;
389 ZB_clear(
ZBuffer * zb,
int clear_z, ZPOINT z,
int clear_color, PIXEL
color) {
394 memset(zb->zbuf, 0, zb->xsize * zb->ysize *
sizeof(ZPOINT));
398 for (y = 0; y < zb->ysize; y++) {
399 memset_l(pp, color, zb->xsize);
400 pp = (PIXEL *) ((
char *) pp + zb->linesize);
406 ZB_clear_viewport(
ZBuffer * zb,
int clear_z, ZPOINT z,
int clear_color, PIXEL color,
407 int xmin,
int ymin,
int xsize,
int ysize) {
412 nassertv(xmin >= 0 && xmin < zb->xsize &&
413 ymin >= 0 && ymin < zb->ysize &&
414 xmin + xsize >= 0 && xmin + xsize <= zb->xsize &&
415 ymin + ysize >= 0 && ymin + ysize <= zb->ysize);
418 zz = zb->zbuf + xmin + ymin * zb->xsize;
419 for (y = 0; y < ysize; ++y) {
420 memset(zz, 0, xsize *
sizeof(ZPOINT));
425 pp = zb->pbuf + xmin + ymin * (zb->linesize / PSZB);
426 for (y = 0; y < ysize; ++y) {
427 memset_l(pp, color, xsize);
433 #define ZB_ST_FRAC_HIGH (1 << ZB_POINT_ST_FRAC_BITS)
434 #define ZB_ST_FRAC_MASK (ZB_ST_FRAC_HIGH - 1)
436 #define LINEAR_FILTER_BITSIZE(c1, c2, f, bitsize) \
437 ((((c2) * (f)) >> bitsize) + (((c1) * ((1 << bitsize) - (f))) >> bitsize))
439 #define LINEAR_FILTER(c1, c2, f) \
440 LINEAR_FILTER_BITSIZE(c1, c2, f, ZB_POINT_ST_FRAC_BITS)
442 #define BILINEAR_FILTER(c1, c2, c3, c4, sf, tf) \
443 (LINEAR_FILTER(LINEAR_FILTER(c1, c2, sf), LINEAR_FILTER(c3, c4, sf), tf))
448 lookup_texture_nearest(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
449 return ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s, t);
454 lookup_texture_bilinear(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
455 PIXEL p1, p2, p3, p4;
459 p1 = ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH);
460 p2 = ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH);
461 sf = s & ZB_ST_FRAC_MASK;
463 p3 = ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t);
464 p4 = ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s, t);
465 tf = t & ZB_ST_FRAC_MASK;
467 r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
468 g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
469 b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
470 a = BILINEAR_FILTER(PIXEL_A(p1), PIXEL_A(p2), PIXEL_A(p3), PIXEL_A(p4), sf, tf);
472 return RGBA_TO_PIXEL(r, g, b, a);
478 lookup_texture_mipmap_nearest(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
479 return ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
484 lookup_texture_mipmap_linear(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
488 p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
489 level = max((
int)level - 1, 0);
490 p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
492 unsigned int bitsize = level + ZB_POINT_ST_FRAC_BITS;
493 r = LINEAR_FILTER_BITSIZE(PIXEL_R(p2), PIXEL_R(p1), level_dx, bitsize);
494 g = LINEAR_FILTER_BITSIZE(PIXEL_G(p2), PIXEL_G(p1), level_dx, bitsize);
495 b = LINEAR_FILTER_BITSIZE(PIXEL_B(p2), PIXEL_B(p1), level_dx, bitsize);
496 a = LINEAR_FILTER_BITSIZE(PIXEL_A(p2), PIXEL_A(p1), level_dx, bitsize);
498 return RGBA_TO_PIXEL(r, g, b, a);
503 lookup_texture_mipmap_bilinear(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
504 PIXEL p1, p2, p3, p4;
508 p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level);
509 p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level);
510 sf = (s >> level) & ZB_ST_FRAC_MASK;
512 p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level);
513 p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
514 tf = (t >> level) & ZB_ST_FRAC_MASK;
516 r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
517 g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
518 b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
519 a = BILINEAR_FILTER(PIXEL_A(p1), PIXEL_A(p2), PIXEL_A(p3), PIXEL_A(p4), sf, tf);
521 return RGBA_TO_PIXEL(r, g, b, a);
527 lookup_texture_mipmap_trilinear(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
531 PIXEL p1, p2, p3, p4;
535 p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level);
536 p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level);
537 sf = (s >> level) & ZB_ST_FRAC_MASK;
539 p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level);
540 p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
541 tf = (t >> level) & ZB_ST_FRAC_MASK;
543 r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
544 g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
545 b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
546 a = BILINEAR_FILTER(PIXEL_A(p1), PIXEL_A(p2), PIXEL_A(p3), PIXEL_A(p4), sf, tf);
547 p1a = RGBA_TO_PIXEL(r, g, b, a);
550 level = max((
int)level - 1, 0);
553 PIXEL p1, p2, p3, p4;
557 p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level);
558 p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level);
559 sf = (s >> level) & ZB_ST_FRAC_MASK;
561 p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level);
562 p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
563 tf = (t >> level) & ZB_ST_FRAC_MASK;
565 r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
566 g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
567 b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
568 a = BILINEAR_FILTER(PIXEL_A(p1), PIXEL_A(p2), PIXEL_A(p3), PIXEL_A(p4), sf, tf);
569 p2a = RGBA_TO_PIXEL(r, g, b, a);
573 unsigned int bitsize = level + ZB_POINT_ST_FRAC_BITS;
574 r = LINEAR_FILTER_BITSIZE(PIXEL_R(p2a), PIXEL_R(p1a), level_dx, bitsize);
575 g = LINEAR_FILTER_BITSIZE(PIXEL_G(p2a), PIXEL_G(p1a), level_dx, bitsize);
576 b = LINEAR_FILTER_BITSIZE(PIXEL_B(p2a), PIXEL_B(p1a), level_dx, bitsize);
577 a = LINEAR_FILTER_BITSIZE(PIXEL_A(p2a), PIXEL_A(p1a), level_dx, bitsize);
579 return RGBA_TO_PIXEL(r, g, b, a);
586 apply_wrap_general_minfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
587 s = (*texture_def->tex_wrap_u_func)(s, texture_def->s_max);
588 t = (*texture_def->tex_wrap_v_func)(t, texture_def->t_max);
589 return (*texture_def->tex_minfilter_func_impl)(texture_def, s, t, level, level_dx);
593 apply_wrap_general_magfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
594 s = (*texture_def->tex_wrap_u_func)(s, texture_def->s_max);
595 t = (*texture_def->tex_wrap_v_func)(t, texture_def->t_max);
596 return (*texture_def->tex_magfilter_func_impl)(texture_def, s, t, level, level_dx);
602 apply_wrap_border_color_minfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
603 if (s < 0 || t < 0 || s > texture_def->s_max || t > texture_def->t_max) {
604 return texture_def->border_color;
606 return (*texture_def->tex_minfilter_func_impl)(texture_def, s, t, level, level_dx);
610 apply_wrap_border_color_magfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
611 if (s < 0 || t < 0 || s > texture_def->s_max || t > texture_def->t_max) {
612 return texture_def->border_color;
614 return (*texture_def->tex_magfilter_func_impl)(texture_def, s, t, level, level_dx);
621 apply_wrap_clamp_minfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
622 s = min(max(s, 0), texture_def->s_max);
623 t = min(max(t, 0), texture_def->t_max);
624 return (*texture_def->tex_minfilter_func_impl)(texture_def, s, t, level, level_dx);
628 apply_wrap_clamp_magfilter(
ZTextureDef *texture_def,
int s,
int t,
unsigned int level,
unsigned int level_dx) {
629 s = min(max(s, 0), texture_def->s_max);
630 t = min(max(t, 0), texture_def->t_max);
631 return (*texture_def->tex_magfilter_func_impl)(texture_def, s, t, level, level_dx);
635 texcoord_clamp(
int coord,
int max_coord) {
636 return min(max(coord, 0), max_coord);
640 texcoord_repeat(
int coord,
int max_coord) {
645 texcoord_mirror(
int coord,
int max_coord) {
646 if ((coord & ((max_coord << 1) - 1)) > max_coord) {
647 coord = (max_coord << 1) - coord;
653 texcoord_mirror_once(
int coord,
int max_coord) {
654 if (coord > max_coord) {
655 coord = (max_coord << 1) - coord;
657 return max(coord, 0);