diff --git a/XCode/libcacaXCode.xcodeproj/project.pbxproj b/XCode/libcacaXCode.xcodeproj/project.pbxproj index 777e62c..e0b5242 100644 --- a/XCode/libcacaXCode.xcodeproj/project.pbxproj +++ b/XCode/libcacaXCode.xcodeproj/project.pbxproj @@ -61,7 +61,7 @@ E6DB663510AECCD700B6F924 /* mygetopt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = mygetopt.c; path = ../src/mygetopt.c; sourceTree = SOURCE_ROOT; }; E6DB663610AECCD700B6F924 /* mygetopt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = mygetopt.h; path = ../src/mygetopt.h; sourceTree = SOURCE_ROOT; }; E6DB663710AECCD700B6F924 /* texture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = texture.h; path = ../src/texture.h; sourceTree = SOURCE_ROOT; }; - E6DB663910AECD1F00B6F924 /* trifiller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = trifiller.c; path = ../examples/trifiller.c; sourceTree = SOURCE_ROOT; }; + E6DB663910AECD1F00B6F924 /* trifiller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = trifiller.c; path = ../examples/trifiller.c; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; E6DB663B10AECDF500B6F924 /* blit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = blit.c; path = ../examples/blit.c; sourceTree = SOURCE_ROOT; }; E6DB663C10AECDF500B6F924 /* canvas.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = canvas.c; path = ../examples/canvas.c; sourceTree = SOURCE_ROOT; }; E6DB663D10AECDF500B6F924 /* colors.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = colors.c; path = ../examples/colors.c; sourceTree = SOURCE_ROOT; }; diff --git a/caca/caca.h b/caca/caca.h index baf82e6..d52b27a 100644 --- a/caca/caca.h +++ b/caca/caca.h @@ -338,14 +338,10 @@ __extern int caca_draw_thin_triangle(caca_canvas_t *, int, int, int, int, int, int); __extern int caca_fill_triangle(caca_canvas_t *, int, int, int, int, int, int, uint32_t); -__extern int caca_fill_triangle_textured(caca_canvas_t *, - int , int , - int , int , - int , int , - float , float , - float , float , - float , float , - caca_canvas_t *); +__extern int caca_fill_triangle_textured(caca_canvas_t *cv, + int coords[6], + caca_canvas_t *tex, + float uv[6]); /* @} */ /** \defgroup caca_frame libcaca canvas frame handling diff --git a/caca/triangle.c b/caca/triangle.c index ff336de..704fabf 100644 --- a/caca/triangle.c +++ b/caca/triangle.c @@ -155,34 +155,17 @@ int caca_fill_triangle(caca_canvas_t *cv, int x1, int y1, int x2, int y2, return 0; } -/** \brief Fill a triangle on the canvas using an arbitrary-sized texture. - * - * This function fails if one or both the canvas are missing - * - * \param cv The handle to the libcaca canvas. - * \param x1 X coordinate of the first point. - * \param y1 Y coordinate of the first point. - * \param x2 X coordinate of the second point. - * \param y2 Y coordinate of the second point. - * \param x3 X coordinate of the third point. - * \param y3 Y coordinate of the third point. - * \param u1 U texture coordinate of the first point. - * \param v1 V texture coordinate of the first point. - * \param u2 U texture coordinate of the second point. - * \param v2 V texture coordinate of the second point. - * \param u3 U texture coordinate of the third point. - * \param v3 V texture coordinate of the third point. - * \param tex The handle of the canvas texture. - * \return This function return 0 if ok, -1 if canvas or texture are missing. - */ -int caca_fill_triangle_textured(caca_canvas_t *cv, - int x1, int y1, +/* This function actually renders the triangle, + * but is not exported due to sam's pedantic will. */ +static int caca_fill_triangle_textured_l(caca_canvas_t *cv, + int x1, int y1, int x2, int y2, - int x3, int y3, - float u1, float v1, + int x3, int y3, + caca_canvas_t *tex, + float u1, float v1, float u2, float v2, - float u3, float v3, - caca_canvas_t *tex) + float u3, float v3) + { uint32_t savedattr; @@ -200,32 +183,32 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, /* Bubble-sort y1 <= y2 <= y3 */ if(y1 > y2) - return caca_fill_triangle_textured(cv, - x2, y2, x1, y1, x3, y3, - u2, v2, u1, v1, u3, v3, - tex); + return caca_fill_triangle_textured_l(cv, + x2, y2, x1, y1, x3, y3, + tex, + u2, v2, u1, v1, u3, v3); if(y2 > y3) - return caca_fill_triangle_textured(cv, - x1, y1, x3, y3, x2, y2, - u1, v1, u3, v3, u2, v2, - tex); - + return caca_fill_triangle_textured_l(cv, + x1, y1, x3, y3, x2, y2, + tex, + u1, v1, u3, v3, u2, v2); + savedattr = caca_get_attr(cv, -1, -1); - + /* Clip texture coordinates */ - if(u1<0.0f) u1 = 0.0f; if(v1<0.0f) v1 = 0.0f; + if(u1<0.0f) u1 = 0.0f; if(v1<0.0f) v1 = 0.0f; if(u2<0.0f) u2 = 0.0f; if(v2<0.0f) v2 = 0.0f; if(u3<0.0f) u3 = 0.0f; if(v3<0.0f) v3 = 0.0f; - if(u1>1.0f) u1 = 1.0f; if(v1>1.0f) v1 = 1.0f; - if(u2>1.0f) u2 = 1.0f; if(v2>1.0f) v2 = 1.0f; - if(u3>1.0f) u3 = 1.0f; if(v3>1.0f) v3 = 1.0f; + if(u1>1.0f) u1 = 1.0f; if(v1>1.0f) v1 = 1.0f; + if(u2>1.0f) u2 = 1.0f; if(v2>1.0f) v2 = 1.0f; + if(u3>1.0f) u3 = 1.0f; if(v3>1.0f) v3 = 1.0f; /* Convert relative tex coordinates to absolute */ - int tw = caca_get_canvas_width(tex); + int tw = caca_get_canvas_width(tex); int th = caca_get_canvas_height(tex); - u1*=(float)tw; u2*=(float)tw; u3*=(float)tw; - v1*=(float)th; v2*=(float)th; v3*=(float)th; + u1*=(float)tw; u2*=(float)tw; u3*=(float)tw; + v1*=(float)th; v2*=(float)th; v3*=(float)th; int x, y; float y2y1 = y2-y1; @@ -251,10 +234,10 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, float va = v1, vb = v1; float u, v; - int s = 0; + int s = 0; /* Top */ - for(y = y1 ; y < y2; y++) + for(y = y1 ; y < y2; y++) { if(xb < xa) { @@ -272,12 +255,12 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, v = va; u = ua; /* scanline */ - for(x = xa ; x < xb; x++) + for(x = xa ; x < xb; x++) { u+=tus; v+=tvs; /* FIXME: use caca_get_canvas_attrs / caca_get_canvas_chars */ - uint32_t attr = caca_get_attr(tex, u, v); + uint32_t attr = caca_get_attr(tex, u, v); uint32_t c = caca_get_char(tex, u, v); caca_set_attr(cv, attr); caca_put_char(cv, x, y, c); @@ -290,12 +273,12 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, ub+=usl12; vb+=vsl12; } - if(s) + if(s) { SWAP_F(xb, xa); SWAP_F(sl13, sl12); SWAP_F(ua, ub); - SWAP_F(va, vb); + SWAP_F(va, vb); SWAP_F(usl13, usl12); SWAP_F(vsl13, vsl12); } @@ -314,14 +297,14 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, vb = v2; } - for(y = y2 ; y < y3; y++) + for(y = y2 ; y < y3; y++) { - if(xb <= xa) + if(xb <= xa) { - SWAP_F(xb, xa); + SWAP_F(xb, xa); SWAP_F(sl13, sl23); SWAP_F(ua, ub); - SWAP_F(va, vb); + SWAP_F(va, vb); SWAP_F(usl13, usl23); SWAP_F(vsl13, vsl23); } @@ -331,22 +314,22 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, u = ua; v = va; /* scanline */ - for(x = xa ; x < xb; x++) + for(x = xa ; x < xb; x++) { u+=tus; v+=tvs; /* FIXME, can be heavily optimised */ uint32_t attr = caca_get_attr(tex, u, v); uint32_t c = caca_get_char(tex, u, v); - caca_set_attr(cv, attr); - caca_put_char(cv, x, y, c); + caca_set_attr(cv, attr); + caca_put_char(cv, x, y, c); } - + xa+=sl13; xb+=sl23; ua+=usl13; va+=vsl13; - ub+=usl23; vb+=vsl23; + ub+=usl23; vb+=vsl23; } caca_set_attr(cv, savedattr); @@ -354,6 +337,32 @@ int caca_fill_triangle_textured(caca_canvas_t *cv, return 0; } +/** \brief Fill a triangle on the canvas using an arbitrary-sized texture. + * + * This function fails if one or both the canvas are missing + * + * \param cv The handle to the libcaca canvas. + * \param coords The coordinates of the triangle (3{x,y}) + * \param tex The handle of the canvas texture. + * \param uv The coordinates of the texture (3{u,v}) + * \return This function return 0 if ok, -1 if canvas or texture are missing. + */ +int caca_fill_triangle_textured(caca_canvas_t *cv, + int coords[6], + caca_canvas_t *tex, + float uv[6]) { + + return caca_fill_triangle_textured_l(cv, + coords[0], coords[1], + coords[2], coords[3], + coords[4], coords[5], + tex, + uv[0], uv[1], + uv[2], uv[3], + uv[4], uv[5]); +} + + /* * XXX: The following functions are aliases. diff --git a/examples/trifiller.c b/examples/trifiller.c index 3b1de73..1683fa1 100644 --- a/examples/trifiller.c +++ b/examples/trifiller.c @@ -49,14 +49,26 @@ int main(int argc, char *argv[]) float angle = 0; - float square[4][2] = { + float square[6][2] = { {-SQUARE_SIZE, -SQUARE_SIZE}, { SQUARE_SIZE, -SQUARE_SIZE}, { SQUARE_SIZE, SQUARE_SIZE}, {-SQUARE_SIZE, SQUARE_SIZE}, }; + float uv1[6] = { + 0, 0, + 1, 0, + 1, 1 + }; + float uv2[6] = { + 0, 0, + 1, 1, + 0, 1 + }; + float rotated[4][2]; + int coords1[6], coords2[6]; /* Create displayed canvas */ cv = caca_create_canvas(0, 0); @@ -74,7 +86,7 @@ int main(int argc, char *argv[]) return 1; } - /* Open window */ + /* Open window */ dp = caca_create_display(cv); if(!dp) { @@ -193,8 +205,8 @@ int main(int argc, char *argv[]) int p; for(p=0; p<4; p++) { - rotated[p][0] = square[p][0] * cos(angle*M_PI/180.0f) - square[p][1] * sin(angle*M_PI/180.0f); - rotated[p][1] = square[p][0] * sin(angle*M_PI/180.0f) + square[p][1] * cos(angle*M_PI/180.0f); + rotated[p][0] = square[p][0] * cos(angle*M_PI/180.0f) - square[p][1] * sin(angle*M_PI/180.0f); + rotated[p][1] = square[p][0] * sin(angle*M_PI/180.0f) + square[p][1] * cos(angle*M_PI/180.0f); rotated[p][0] += ww/2 + px; rotated[p][1] += wh/2 + py; @@ -202,28 +214,25 @@ int main(int argc, char *argv[]) angle+=1.0f; - /* Display two triangles */ - caca_fill_triangle_textured(cv, - /* triangle screen coordinates */ - rotated[0][0], rotated[0][1], - rotated[1][0], rotated[1][1], - rotated[2][0], rotated[2][1], - /* texture coordinates */ - 0, 0, - 1, 0, - 1, 1, - tex); + /* Reaarange coordinates to fit libcaca's format */ + coords1[0] = rotated[0][0]; coords1[1] = rotated[0][1]; + coords1[2] = rotated[1][0]; coords1[3] = rotated[1][1]; + coords1[4] = rotated[2][0]; coords1[5] = rotated[2][1]; + + coords2[0] = rotated[0][0]; coords2[1] = rotated[0][1]; + coords2[2] = rotated[2][0]; coords2[3] = rotated[2][1]; + coords2[4] = rotated[3][0]; coords2[5] = rotated[3][1]; + + /* Display two triangles */ + caca_fill_triangle_textured(cv, /* canvas */ + coords1, /* triangle coordinates */ + tex, /* texture canvas */ + uv1); /* texture coordinates */ caca_fill_triangle_textured(cv, - /* triangle screen coordinates */ - rotated[0][0], rotated[0][1], - rotated[2][0], rotated[2][1], - rotated[3][0], rotated[3][1], - /* texture coordinates */ - 0, 0, - 1, 1, - 0, 1, - tex); + coords2, + tex, + uv2); /* Refresh display and clear for next frame */ caca_refresh_display(dp);