diff --git a/s3l.h b/s3l.h index 759958a..001ce34 100644 --- a/s3l.h +++ b/s3l.h @@ -12,13 +12,14 @@ typedef struct S3L_COORD x; ///< Screen X coordinate. S3L_COORD y; ///< Screen Y coordinate. - S3L_UNIT barycentricA; /**< Barycentric coord A. Together with B and C coords - these serve to locate the pixel on a triangle and - interpolate values between it's three points. The - sum of the three coordinates will always be - exactly S3L_FRACTIONS_PER_UNIT. */ - S3L_UNIT barycentricB; ///< Baryc. coord B, same semantics as barycentricA. - S3L_UNIT barycentricC; ///< Baryc. coord C, same semantics as barycentricA. + S3L_UNIT barycentric0; /**< Barycentric coord A (corresponds to 1st vertex). + Together with B and C coords these serve to + locate the pixel on a triangle and interpolate + values between it's three points. The sum of the + three coordinates will always be exactly + S3L_FRACTIONS_PER_UNIT. */ + S3L_UNIT barycentric1; ///< Baryc. coord B (corresponds to 2nd vertex). + S3L_UNIT barycentric2; ///< Baryc. coord C (corresponds to 3rd vertex). } S3L_PixelInfo; typedef struct @@ -136,6 +137,20 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 lPointX, lPointY, // left triangle point coords rPointX, rPointY; // right triangle point coords + S3L_PixelInfo p; + + p.barycentric0 = 0; + p.barycentric1 = 0; + p.barycentric2 = 0; + + S3L_UNIT *barycentric0; // bar. coord that gets higher from L to R + S3L_UNIT *barycentric1; // bar. coord that gets higher from R to L + S3L_UNIT *barycentric2; // bar. coord that is computed from previous two + +barycentric0 = &p.barycentric0; +barycentric1 = &p.barycentric1; +barycentric2 = &p.barycentric2; + // Sort the points. #define handleLR(a,b)\ @@ -254,16 +269,6 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 initSide(r,t,r,1) initSide(l,t,l,1) - S3L_PixelInfo p; - - p.barycentricA = 0; - p.barycentricB = 0; - p.barycentricC = 0; - - S3L_UNIT *barycentric1 = &p.barycentricA; - S3L_UNIT *barycentric2 = &p.barycentricB; - S3L_UNIT *barycentric3 = &p.barycentricC; - while (currentY <= endY) { if (currentY == splitY) @@ -272,8 +277,9 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 { initSide(l,l,r,0); - barycentric1 = &p.barycentricC; - barycentric3 = &p.barycentricA; + S3L_UNIT *tmp = barycentric0; + barycentric0 = barycentric2; + barycentric2 = tmp; rSideUnitPos = S3L_FRACTIONS_PER_UNIT - rSideUnitPos; rSideUnitStep *= -1; @@ -282,8 +288,9 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 { initSide(r,r,l,0); - barycentric2 = &p.barycentricC; - barycentric3 = &p.barycentricB; + S3L_UNIT *tmp = barycentric1; + barycentric1 = barycentric2; + barycentric2 = tmp; lSideUnitPos = S3L_FRACTIONS_PER_UNIT - lSideUnitPos; lSideUnitStep *= -1; @@ -302,9 +309,9 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 for (S3L_COORD x = lX; x <= rX; ++x) { - *barycentric1 = S3L_interpolateFrom0(rSideUnitPos,t1,tMax); - *barycentric2 = S3L_interpolateFrom0(lSideUnitPos,t2,tMax); - *barycentric3 = S3L_FRACTIONS_PER_UNIT - *barycentric1 - *barycentric2; + *barycentric0 = S3L_interpolateFrom0(rSideUnitPos,t1,tMax); + *barycentric1 = S3L_interpolateFrom0(lSideUnitPos,t2,tMax); + *barycentric2 = S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1; p.x = x; S3L_PIXEL_FUNCTION(&p); diff --git a/testSDL.c b/testSDL.c index c4ccdbd..482f37c 100644 --- a/testSDL.c +++ b/testSDL.c @@ -37,9 +37,9 @@ void drawPixel(S3L_PixelInfo *p) setPixel(p->x,p->y, - p->barycentricA / ((float) S3L_FRACTIONS_PER_UNIT) * 255, - p->barycentricB / ((float) S3L_FRACTIONS_PER_UNIT) * 255, - p->barycentricC / ((float) S3L_FRACTIONS_PER_UNIT) * 255); + p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255, + p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255, + p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255); } @@ -58,6 +58,7 @@ void draw() { clearScreen(); +/* for (int c = 0; c < 7; ++c) { int @@ -69,12 +70,26 @@ void draw() y2 = test_coords[6 * c + 5]; S3L_drawTriangle(x0,y0,x1,y1,x2,y2); -/* + setPixel(x0,y0,255,0,0); setPixel(x1,y1,255,0,0); setPixel(x2,y2,255,0,0); -*/ } +*/ + +int16_t rotX0 = 200 + sin(frame * 0.01) * 50; +int16_t rotY0 = 200 + cos(frame * 0.01) * 50; + +int16_t rotX1 = 200 + sin((frame + 300) * 0.01) * 50; +int16_t rotY1 = 200 + cos((frame + 300) * 0.01) * 50; + +int16_t rotX2 = 200 + sin((frame + 500) * 0.01) * 50; +int16_t rotY2 = 200 + cos((frame + 500) * 0.01) * 50; + +S3L_drawTriangle(rotX0,rotY0,rotX1,rotY1,rotX2,rotY2); + + + } int main()