#include "demo.h" #include "direct.h" #include "util.h" #define dFixed(a, b) (((a) * 256) / ((b) / 256)) #define mFixed(a, b) (((((a) / 256) * (b)) / 256)) /*-------------------------------------------------------------------- * quadTex: Draw a texture mapped quadrangle * Fixed Point Math *-------------------------------------------------------------------*/ void quadTex( Quad *poly, Texture *tex ) { Int16 edgesLeft; Fixed32 m1, m2; /* inverse slopes */ Fixed32 fx1, fx2; Int16 x1, x2; Int16 ycount; Int16 dx1, dx2, dy1, dy2; /* x and y spans */ PIXEL *p, *q; /* Pixel pointer */ Int16 i; Int16 min_y = 1024; /* y value of top vertex */ Int16 top1_vert; /* Index of top vertex */ Int16 top2_vert; /* Index of top vertex */ Int16 vert_1, vert_2; /* Right and Left vertex */ Fixed32 du, dv; /* Texture differentials */ Fixed32 incrU1, incrU2; Fixed32 incrV1, incrV2; Fixed32 u1, u2; /* Texture u coord sides 1,2 */ Fixed32 v1, v2; /* Texture v coord sides 1,2 */ Fixed32 u, v; /* Texture u,v coords */ Fixed32 dy1_inv, dy2_inv; Fixed32 span_inv; /* reciprocal of scanline x span */ FixVert texPoints[4]; Uint16 width, height; /* Texture width and height */ PIXEL *texAddr; width = (tex->charSize & 0xff00) >> 5; height = (tex->charSize & 0x00ff); texAddr = tex->addr; texPoints[0].x = intToFixed(0); texPoints[0].y = intToFixed(0); texPoints[1].x = intToFixed(width-1); texPoints[1].y = intToFixed(0); texPoints[2].x = intToFixed(width-1); texPoints[2].y = intToFixed(height-1); texPoints[3].x = intToFixed(0); texPoints[3].y = intToFixed(height-1); /* Find top vertex */ for ( i=0; i<3; ++i ) { if ( poly->v[i].y < min_y ) { min_y = poly->v[i].y; top1_vert = i; } } top2_vert = top1_vert; vert_1 = (top1_vert + 3) % 4 ; vert_2 = (top2_vert + 1) % 4 ; edgesLeft = 3; dy1 = poly->v[vert_1].y - poly->v[top1_vert].y; if (dy1 == 0) { #ifdef DEBUG printf("detect horiz line (1)\n"); #endif edgesLeft--; top1_vert = vert_1; vert_1 = (vert_1 + 3) % 4 ; dy1 = poly->v[vert_1].y - poly->v[top1_vert].y; } dx1 = poly->v[vert_1].x - poly->v[top1_vert].x; dy2 = poly->v[vert_2].y - poly->v[top2_vert].y; if (dy2 == 0) { #ifdef DEBUG printf("detect horiz line (2)\n"); #endif edgesLeft--; top2_vert = vert_2; vert_2 = (vert_2 + 1) % 4 ; dy2 = poly->v[vert_2].y - poly->v[top2_vert].y; } dx2 = poly->v[vert_2].x - poly->v[top2_vert].x; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top1_vert].y * SCREEN_WIDTH); fx2 = intToFixed(poly->v[top2_vert].x) + FIXED(0.5) ; fx1 = intToFixed(poly->v[top1_vert].x) + FIXED(0.5) ; m1 = dFixed( intToFixed(dx1), intToFixed(dy1) ); m2 = dFixed( intToFixed(dx2), intToFixed(dy2) ); /* m1 = fixMul( intToFixed(dx1), fixURecip(intToFixed(dy1)) ); m2 = fixMul( intToFixed(dx2), fixURecip(intToFixed(dy2)) ); */ /* Texture Calculations */ /* */ dy1_inv = fixURecip(intToFixed(dy1)); dy2_inv = fixURecip(intToFixed(dy2)); incrU1 = fixMul(texPoints[vert_1].x - texPoints[top1_vert].x, dy1_inv); incrV1 = fixMul(texPoints[vert_1].y - texPoints[top1_vert].y, dy1_inv); incrU2 = fixMul(texPoints[vert_2].x - texPoints[top2_vert].x, dy2_inv); incrV2 = fixMul(texPoints[vert_2].y - texPoints[top2_vert].y, dy2_inv); u1 = texPoints[top1_vert].x; v1 = texPoints[top1_vert].y; u2 = texPoints[top2_vert].x; v2 = texPoints[top2_vert].y; if (dy1 > dy2) ycount = dy2; else ycount = dy1; do { while ( ycount-- > 0 ) { x1 = fixedToInt(fx1); /* line 1 x start */ x2 = fixedToInt(fx2); /* line 2 x start */ if (x1 <= x2) { u = u1 ; v = v1 ; if (x1 != x2) { span_inv = dFixed(intToFixed(1), (fx2 - fx1)); /* span_inv = fixURecip(fx2 - fx1); */ } else { span_inv = 0; } du = fixMul(u2 - u1, span_inv); dv = fixMul(v2 - v1, span_inv); for ( i = x1; i <= x2; i++ ) { q = (PIXEL *)(texAddr + (fixedToInt(v)* width)); *(p+i) = *(q + fixedToInt(u)); u += du; v += dv; } } else { u = u2 ; v = v2 ; if (x1 != x2) span_inv = dFixed(intToFixed(1), (fx1 - fx2)); /* span_inv = fixURecip(fx1 - fx2); */ else span_inv = 0; du = fixMul(u1 - u2, span_inv); dv = fixMul(v1 - v2, span_inv); for ( i = x2; i <= x1; i++ ) { q = (PIXEL *)(texAddr + (fixedToInt(v)* width)); *(p+i) = *(q + fixedToInt(u)); u += du; v += dv; } } fx1 += m1; /* inc scanline endpoints */ fx2 += m2; p += SCREEN_WIDTH; /* inc scanline */ u1 += incrU1; /* inc texture endpoints */ u2 += incrU2; v1 += incrV1; v2 += incrV2; } edgesLeft--; #ifdef DEBUG printf("ycount = %d\n", ycount); printf("edgesLeft = %d\n", edgesLeft); #endif if (edgesLeft < 1) { break; } if ( dy1 < dy2 ) { #ifdef DEBUG printf("endP1 \n"); #endif dy2 -= dy1; top1_vert = vert_1; vert_1 = (vert_1 + 3) % 4; fx1 = intToFixed(poly->v[top1_vert].x) ; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top1_vert].y * SCREEN_WIDTH); dx1 = poly->v[vert_1].x-poly->v[top1_vert].x; dy1 = poly->v[vert_1].y - poly->v[top1_vert].y; m1 = dFixed(intToFixed(dx1),intToFixed(dy1) ); /* m1 = fixMul(intToFixed(dx1), fixURecip(intToFixed(dy1)) ); */ ycount = dy1; if ( ycount > dy2 ) ycount = dy2; /* Texture Calculations */ dy1_inv = dFixed(intToFixed(1), intToFixed(dy1)); /* dy1_inv = fixURecip(intToFixed(dy1)); */ incrU1 = fixMul(texPoints[vert_1].x - texPoints[top1_vert].x, dy1_inv); incrV1 = fixMul(texPoints[vert_1].y - texPoints[top1_vert].y, dy1_inv); u1 = texPoints[top1_vert].x; v1 = texPoints[top1_vert].y; } else if ( dy1 > dy2 ) { #ifdef DEBUG printf("endP2 \n"); #endif dy1 -= dy2; top2_vert = vert_2; vert_2 = (vert_2 + 1) % 4; fx2 = intToFixed(poly->v[top2_vert].x) ; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top2_vert].y * SCREEN_WIDTH); dx2 = poly->v[vert_2].x-poly->v[top2_vert].x; dy2 = poly->v[vert_2].y-poly->v[top2_vert].y; m2 = dFixed( intToFixed(dx2), intToFixed(dy2) ); /* m2 = fixMul( intToFixed(dx2), fixURecip(intToFixed(dy2)) ); */ ycount = dy2; if ( ycount > dy1 ) ycount = dy1; /* Texture Calculations */ dy2_inv = dFixed(intToFixed(1), intToFixed(dy2)); /* dy2_inv = fixURecip(intToFixed(dy2)); */ incrU2 = fixMul(texPoints[vert_2].x - texPoints[top2_vert].x, dy2_inv); incrV2 = fixMul(texPoints[vert_2].y - texPoints[top2_vert].y, dy2_inv); u2 = texPoints[top2_vert].x; v2 = texPoints[top2_vert].y; } else { #ifdef DEBUG printf("break\n"); #endif break; } } while (True); }