#include "shmath.h" #include "demo.h" #include "direct.h" /*-------------------------------------------------------------------- * lineDraw: Draw a solid line * Bresenham's algorithm (adapted from Graphics Gems 1) * Midpoint optimization *-------------------------------------------------------------------*/ void cLineDraw( Int16 ax, Int16 ay, Int16 bx, Int16 by, Int16 color ) { Int16 d; /* Error term */ Int16 abs_x, abs_y; Int16 pIncrXY; /* Pointer increment, x and y step */ Int16 incrXY; /* Error increment, x and y step */ Int16 line; /* Pointer increment for screen line */ Int16 dx, dy; /* x and y spans */ Int16 sgn_x; /* Sign of dx */ PIXEL *p; /* Pixel pointer */ PIXEL *endP; /* Pointer at end of line */ dx = bx - ax; dy = by - ay; abs_x = ABS(dx) * 2; abs_y = ABS(dy) * 2; sgn_x = SGN(dx); endP = ((PIXEL *)FRAME_BUF_ADDR) + ( by * SCREEN_WIDTH ) + bx ; line = SGN(dy) * SCREEN_WIDTH; p = ((PIXEL *)FRAME_BUF_ADDR) + ( ay * SCREEN_WIDTH ) + ax ; /* |slope| < 1, increment in x */ if ( abs_x > abs_y ) { d = abs_y - ( abs_x >> 2); incrXY = abs_y - abs_x; pIncrXY = line + sgn_x; do { *p = (PIXEL) color; if ( d >= 0 ) { p += pIncrXY; d += incrXY; } else { p += sgn_x; d += abs_y; } } while (p != endP); /* |slope| > 1, increment in y */ } else { d = abs_x - ( abs_y >> 2); incrXY = abs_x - abs_y; pIncrXY = line + sgn_x; do { *p = (PIXEL) color; if ( d >= 0 ) { p += pIncrXY; d += incrXY; } else { p += line; d += abs_x; } } while (p != endP); } } /*-------------------------------------------------------------------- * cTriFill: Draw a solid triangle * Fixed Point Math *-------------------------------------------------------------------*/ void cTriFill( Triangle *poly, Int16 color ) { 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; /* Pixel pointer */ Int16 i; Int16 min_y = 1024; /* y value of top vertex */ Int16 top_vert; /* Index of top vertex */ Int16 top1_vert; /* Index of top vertex */ Int16 vert_1, vert_2; /* Right and Left vertex /* Find top vertex */ for ( i=0; i<3; ++i ) { if ( poly->v[i].y < min_y ) { min_y = poly->v[i].y; top_vert = i; } } vert_1 = (top_vert + 2) % 3 ; vert_2 = (top_vert + 1) % 3 ; edgesLeft = 2; dy1 = poly->v[vert_1].y - poly->v[top_vert].y; if (dy1 == 0) { #ifdef DEBUG printf("detect horiz line (1)\n"); #endif edgesLeft--; top1_vert = vert_1; vert_1 = vert_2; dy1 = poly->v[vert_1].y - poly->v[top1_vert].y; } else top1_vert = top_vert; dx1 = poly->v[vert_1].x - poly->v[top1_vert].x; dy2 = poly->v[vert_2].y - poly->v[top_vert].y; if (dy2 == 0) { #ifdef DEBUG printf("detect horiz line (2)\n"); #endif edgesLeft--; top_vert = vert_2; vert_2 = vert_1; dy2 = (poly->v[vert_2].y - poly->v[top_vert].y); } dx2 = poly->v[vert_2].x - poly->v[top_vert].x; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top_vert].y * SCREEN_WIDTH); fx2 = intToFixed(poly->v[top_vert].x) + FIXED(0.5) ; fx1 = intToFixed(poly->v[top1_vert].x) + FIXED(0.5) ; m1 = divFixed( intToFixed(dx1), intToFixed(dy1) ); m2 = divFixed( intToFixed(dx2), intToFixed(dy2) ); if (dy1 > dy2) ycount = dy2; else ycount = dy1; do { while ( ycount-- > 0 ) { x1 = fixedToInt(fx1); x2 = fixedToInt(fx2); if (x1 <= x2) { for ( i = x1; i <= x2; i++ ) *(p+i) = (PIXEL) color; } else { for ( i = x2; i <= x1; i++ ) *(p+i) = (PIXEL) color; } fx1 += m1; fx2 += m2; p += SCREEN_WIDTH; } 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 top1_vert = vert_1; vert_1 = vert_2; 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 = divFixed( intToFixed(dx1), intToFixed(dy1) ); ycount = dy1; } else if ( dy1 > dy2 ) { #ifdef DEBUG printf("endP2 \n"); #endif top_vert = vert_2; vert_2 = vert_1; fx2 = intToFixed(poly->v[top_vert].x) ; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top_vert].y * SCREEN_WIDTH); dx2 = poly->v[vert_2].x - poly->v[top_vert].x; dy2 = poly->v[vert_2].y - poly->v[top_vert].y; m2 = divFixed( intToFixed(dx2), intToFixed(dy2) ); ycount = dy2; } else { #ifdef DEBUG printf("break\n"); #endif break; } } while (True); } /*-------------------------------------------------------------------- * cQuadFill: Draw a solid quadrangle * Fixed Point Math *-------------------------------------------------------------------*/ void cQuadFill( Quad *poly, Int16 color ) { 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; /* Pixel pointer */ Int16 i; Int16 min_y = 1024; /* y value of top vertex */ Int16 top_vert; /* Index of top vertex */ Int16 top1_vert; /* Index of top vertex */ Int16 vert_1, vert_2; /* Right and Left vertex /* Find top vertex */ for ( i=0; i<3; ++i ) { if ( poly->v[i].y < min_y ) { min_y = poly->v[i].y; top_vert = i; } } vert_1 = (top_vert + 3) % 4 ; vert_2 = (top_vert + 1) % 4 ; edgesLeft = 3; dy1 = poly->v[vert_1].y - poly->v[top_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; } else top1_vert = top_vert; dx1 = poly->v[vert_1].x - poly->v[top1_vert].x; dy2 = poly->v[vert_2].y - poly->v[top_vert].y; if (dy2 == 0) { #ifdef DEBUG printf("detect horiz line (2)\n"); #endif edgesLeft--; top_vert = vert_2; vert_2 = (vert_2 + 1) % 4 ; dy2 = poly->v[vert_2].y - poly->v[top_vert].y; } dx2 = poly->v[vert_2].x - poly->v[top_vert].x; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top_vert].y * SCREEN_WIDTH); fx2 = intToFixed(poly->v[top_vert].x) + FIXED(0.5) ; fx1 = intToFixed(poly->v[top1_vert].x) + FIXED(0.5) ; m1 = divFixed( intToFixed(dx1), intToFixed(dy1) ); m2 = divFixed( intToFixed(dx2), intToFixed(dy2) ); if (dy1 > dy2) ycount = dy2; else ycount = dy1; do { while ( ycount-- > 0 ) { x1 = fixedToInt(fx1); x2 = fixedToInt(fx2); if (x1 <= x2) { for ( i = x1; i <= x2; i++ ) *(p+i) = (PIXEL) color; } else { for ( i = x2; i <= x1; i++ ) *(p+i) = (PIXEL) color; } fx1 += m1; fx2 += m2; p += SCREEN_WIDTH; } 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) + FIXED(0.5) ; 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 = divFixed( intToFixed(dx1), intToFixed(dy1) ); ycount = dy1; if ( ycount > dy2 ) ycount = dy2; } else if ( dy1 > dy2 ) { #ifdef DEBUG printf("endP2 \n"); #endif dy1 -= dy2; top_vert = vert_2; vert_2 = (vert_2 + 1) % 4; fx2 = intToFixed(poly->v[top_vert].x) + FIXED(0.5) ; p = ((PIXEL *)FRAME_BUF_ADDR) + (poly->v[top_vert].y * SCREEN_WIDTH); dx2 = poly->v[vert_2].x - poly->v[top_vert].x; dy2 = poly->v[vert_2].y - poly->v[top_vert].y; m2 = divFixed( intToFixed(dx2), intToFixed(dy2) ); ycount = dy2; if ( ycount > dy1 ) ycount = dy1; } else { #ifdef DEBUG printf("break\n"); #endif break; } } while (True); }