Changeset 3926


Ignore:
Timestamp:
Nov 18, 2009, 12:25:58 PM (14 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Make caca_fill_triangle_textured use less arguments, examples/trifiller modified accordingly
Location:
libcaca/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/XCode/libcacaXCode.xcodeproj/project.pbxproj

    r3911 r3926  
    6262                E6DB663610AECCD700B6F924 /* mygetopt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = mygetopt.h; path = ../src/mygetopt.h; sourceTree = SOURCE_ROOT; };
    6363                E6DB663710AECCD700B6F924 /* texture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = texture.h; path = ../src/texture.h; sourceTree = SOURCE_ROOT; };
    64                 E6DB663910AECD1F00B6F924 /* trifiller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = trifiller.c; path = ../examples/trifiller.c; sourceTree = SOURCE_ROOT; };
     64                E6DB663910AECD1F00B6F924 /* trifiller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = trifiller.c; path = ../examples/trifiller.c; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
    6565                E6DB663B10AECDF500B6F924 /* blit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = blit.c; path = ../examples/blit.c; sourceTree = SOURCE_ROOT; };
    6666                E6DB663C10AECDF500B6F924 /* canvas.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = canvas.c; path = ../examples/canvas.c; sourceTree = SOURCE_ROOT; };
  • libcaca/trunk/caca/caca.h

    r3909 r3926  
    339339__extern int caca_fill_triangle(caca_canvas_t *, int, int, int, int, int,
    340340                                 int, uint32_t);
    341 __extern int caca_fill_triangle_textured(caca_canvas_t *,
    342                                     int , int ,
    343                                     int , int ,
    344                                     int , int ,
    345                                     float , float ,
    346                                     float , float ,
    347                                     float , float ,
    348                                          caca_canvas_t *);
     341__extern int caca_fill_triangle_textured(caca_canvas_t *cv,
     342                                         int coords[6],
     343                                         caca_canvas_t *tex,
     344                                         float uv[6]);
    349345/*  @} */
    350346
  • libcaca/trunk/caca/triangle.c

    r3925 r3926  
    156156}
    157157
    158 /** \brief Fill a triangle on the canvas using an arbitrary-sized texture.
    159  *
    160  *  This function fails if one or both the canvas are missing
    161  *
    162  *  \param cv  The handle to the libcaca canvas.
    163  *  \param x1  X coordinate of the first point.
    164  *  \param y1  Y coordinate of the first point.
    165  *  \param x2  X coordinate of the second point.
    166  *  \param y2  Y coordinate of the second point.
    167  *  \param x3  X coordinate of the third point.
    168  *  \param y3  Y coordinate of the third point.
    169  *  \param u1  U texture coordinate of the first point.
    170  *  \param v1  V texture coordinate of the first point.
    171  *  \param u2  U texture coordinate of the second point.
    172  *  \param v2  V texture coordinate of the second point.
    173  *  \param u3  U texture coordinate of the third point.
    174  *  \param v3  V texture coordinate of the third point.
    175  *  \param tex The handle of the canvas texture.
    176  *  \return This function return 0 if ok, -1 if canvas or texture are missing.
    177  */
    178 int caca_fill_triangle_textured(caca_canvas_t *cv,
    179                                 int x1, int y1,
     158/* This function actually renders the triangle,
     159 * but is not exported due to sam's pedantic will. */
     160static int caca_fill_triangle_textured_l(caca_canvas_t *cv,
     161                                int x1, int y1,
    180162                                int x2, int y2,
    181                                         int x3, int y3,
    182                                 float u1, float v1,
     163                                        int x3, int y3,
     164                                caca_canvas_t *tex,
     165                                float u1, float v1,
    183166                                float u2, float v2,
    184                                         float u3, float v3,
    185                                 caca_canvas_t *tex)
     167                                        float u3, float v3)
     168                               
    186169{
    187170    uint32_t savedattr;
     
    201184    /* Bubble-sort y1 <= y2 <= y3 */
    202185    if(y1 > y2)
    203         return caca_fill_triangle_textured(cv,
    204                                            x2, y2, x1, y1, x3, y3,
    205                                            u2, v2, u1, v1, u3, v3,
    206                                            tex);
     186        return caca_fill_triangle_textured_l(cv,
     187                                             x2, y2, x1, y1, x3, y3,
     188                                             tex,
     189                                             u2, v2, u1, v1, u3, v3);
    207190    if(y2 > y3)
    208         return caca_fill_triangle_textured(cv,
    209                                            x1, y1, x3, y3, x2, y2,
    210                                            u1, v1, u3, v3, u2, v2,
    211                                            tex);
    212    
     191        return caca_fill_triangle_textured_l(cv,
     192                                             x1, y1, x3, y3, x2, y2,
     193                                             tex,
     194                                             u1, v1, u3, v3, u2, v2);
     195
    213196    savedattr = caca_get_attr(cv, -1, -1);
    214    
     197
    215198    /* Clip texture coordinates */
    216         if(u1<0.0f) u1 = 0.0f; if(v1<0.0f) v1 = 0.0f;
     199    if(u1<0.0f) u1 = 0.0f; if(v1<0.0f) v1 = 0.0f;
    217200    if(u2<0.0f) u2 = 0.0f; if(v2<0.0f) v2 = 0.0f;
    218201    if(u3<0.0f) u3 = 0.0f; if(v3<0.0f) v3 = 0.0f;
    219         if(u1>1.0f) u1 = 1.0f; if(v1>1.0f) v1 = 1.0f;
    220         if(u2>1.0f) u2 = 1.0f; if(v2>1.0f) v2 = 1.0f;
    221         if(u3>1.0f) u3 = 1.0f; if(v3>1.0f) v3 = 1.0f;
     202    if(u1>1.0f) u1 = 1.0f; if(v1>1.0f) v1 = 1.0f;
     203    if(u2>1.0f) u2 = 1.0f; if(v2>1.0f) v2 = 1.0f;
     204    if(u3>1.0f) u3 = 1.0f; if(v3>1.0f) v3 = 1.0f;
    222205   
    223206    /* Convert relative tex coordinates to absolute */
    224         int tw = caca_get_canvas_width(tex);
     207    int tw = caca_get_canvas_width(tex);
    225208    int th = caca_get_canvas_height(tex);
    226209   
    227     u1*=(float)tw; u2*=(float)tw; u3*=(float)tw; 
    228     v1*=(float)th; v2*=(float)th; v3*=(float)th; 
     210    u1*=(float)tw; u2*=(float)tw; u3*=(float)tw;
     211    v1*=(float)th; v2*=(float)th; v3*=(float)th;
    229212   
    230213    int x, y;
     
    252235    float u, v;
    253236   
    254         int s = 0;
     237    int s = 0;
    255238   
    256239    /* Top */
    257     for(y = y1 ; y < y2; y++) 
     240    for(y = y1 ; y < y2; y++)
    258241    {
    259242       
     
    273256       
    274257        /* scanline */
    275         for(x = xa ; x < xb; x++)
     258        for(x = xa ; x < xb; x++)
    276259        {
    277260            u+=tus;
    278261            v+=tvs;
    279262            /* FIXME: use caca_get_canvas_attrs / caca_get_canvas_chars  */
    280                 uint32_t attr = caca_get_attr(tex, u, v);
     263            uint32_t attr = caca_get_attr(tex, u, v);
    281264            uint32_t c    = caca_get_char(tex, u, v);
    282265            caca_set_attr(cv, attr);
     
    291274    }
    292275   
    293     if(s) 
     276    if(s)
    294277    {
    295278        SWAP_F(xb, xa);
    296279        SWAP_F(sl13, sl12);
    297280        SWAP_F(ua, ub);
    298         SWAP_F(va, vb);           
     281        SWAP_F(va, vb);
    299282        SWAP_F(usl13, usl12);
    300283        SWAP_F(vsl13, vsl12);
     
    315298    }
    316299   
    317     for(y = y2 ; y < y3; y++) 
    318     {
    319         if(xb <= xa) 
     300    for(y = y2 ; y < y3; y++)
     301    {
     302        if(xb <= xa)
    320303        {
    321             SWAP_F(xb, xa);   
     304            SWAP_F(xb, xa);
    322305            SWAP_F(sl13, sl23);
    323306            SWAP_F(ua, ub);
    324             SWAP_F(va, vb); 
     307            SWAP_F(va, vb);
    325308            SWAP_F(usl13, usl23);
    326309            SWAP_F(vsl13, vsl23);
     
    332315       
    333316        /* scanline */
    334         for(x = xa ; x < xb; x++)
     317        for(x = xa ; x < xb; x++)
    335318        {   
    336319            u+=tus;
     
    339322            uint32_t attr = caca_get_attr(tex, u, v);
    340323            uint32_t c    = caca_get_char(tex, u, v);
    341             caca_set_attr(cv, attr);   
    342             caca_put_char(cv, x, y, c);   
    343         }
    344        
     324            caca_set_attr(cv, attr);
     325            caca_put_char(cv, x, y, c);
     326        }
     327
    345328        xa+=sl13;
    346329        xb+=sl23;
    347330       
    348331        ua+=usl13; va+=vsl13;
    349         ub+=usl23; vb+=vsl23; 
     332        ub+=usl23; vb+=vsl23;
    350333    }
    351334
     
    354337    return 0;
    355338}
     339
     340/** \brief Fill a triangle on the canvas using an arbitrary-sized texture.
     341 *
     342 *  This function fails if one or both the canvas are missing
     343 *
     344 *  \param cv     The handle to the libcaca canvas.
     345 *  \param coords The coordinates of the triangle (3{x,y})
     346 *  \param tex    The handle of the canvas texture.
     347 *  \param uv     The coordinates of the texture (3{u,v})
     348 *  \return This function return 0 if ok, -1 if canvas or texture are missing.
     349 */
     350int caca_fill_triangle_textured(caca_canvas_t *cv,
     351                                int coords[6],
     352                                caca_canvas_t *tex,
     353                                float uv[6]) {
     354   
     355    return caca_fill_triangle_textured_l(cv,
     356                                         coords[0], coords[1],
     357                                         coords[2], coords[3],
     358                                         coords[4], coords[5],
     359                                         tex,
     360                                         uv[0],     uv[1],
     361                                         uv[2],     uv[3],
     362                                         uv[4],     uv[5]);
     363}
     364
    356365
    357366
  • libcaca/trunk/examples/trifiller.c

    r3909 r3926  
    5050   
    5151   
    52     float square[4][2] = {
     52    float square[6][2] = {
    5353        {-SQUARE_SIZE, -SQUARE_SIZE},
    5454        { SQUARE_SIZE, -SQUARE_SIZE},
     
    5656        {-SQUARE_SIZE,  SQUARE_SIZE},
    5757    };
     58    float uv1[6] = {
     59        0, 0,
     60        1, 0,
     61        1, 1
     62    };
     63    float uv2[6] = {
     64        0, 0,
     65        1, 1,
     66        0, 1
     67    };
     68   
    5869   
    5970    float rotated[4][2];
     71    int coords1[6], coords2[6];
    6072   
    6173    /* Create displayed canvas */
     
    7587    }
    7688   
    77         /* Open window */
     89    /* Open window */
    7890    dp = caca_create_display(cv);
    7991    if(!dp)
     
    194206        for(p=0; p<4; p++)
    195207        {
    196                 rotated[p][0] = square[p][0] * cos(angle*M_PI/180.0f) - square[p][1] * sin(angle*M_PI/180.0f);
    197                 rotated[p][1] = square[p][0] * sin(angle*M_PI/180.0f) + square[p][1] * cos(angle*M_PI/180.0f);
     208            rotated[p][0] = square[p][0] * cos(angle*M_PI/180.0f) - square[p][1] * sin(angle*M_PI/180.0f);
     209            rotated[p][1] = square[p][0] * sin(angle*M_PI/180.0f) + square[p][1] * cos(angle*M_PI/180.0f);
    198210           
    199211            rotated[p][0] += ww/2 + px;
     
    203215        angle+=1.0f;
    204216       
     217       
     218        /* Reaarange coordinates to fit libcaca's format */
     219        coords1[0] = rotated[0][0]; coords1[1] = rotated[0][1];
     220        coords1[2] = rotated[1][0]; coords1[3] = rotated[1][1];
     221        coords1[4] = rotated[2][0]; coords1[5] = rotated[2][1];
     222       
     223        coords2[0] = rotated[0][0]; coords2[1] = rotated[0][1];
     224        coords2[2] = rotated[2][0]; coords2[3] = rotated[2][1];
     225        coords2[4] = rotated[3][0]; coords2[5] = rotated[3][1];
     226       
    205227        /* Display two triangles */
     228        caca_fill_triangle_textured(cv,      /* canvas */
     229                                    coords1, /* triangle coordinates */
     230                                    tex,     /* texture canvas */
     231                                    uv1);    /* texture coordinates */
    206232        caca_fill_triangle_textured(cv,
    207                                                 /* triangle screen coordinates */
    208                                     rotated[0][0], rotated[0][1],
    209                                                 rotated[1][0], rotated[1][1],
    210                                     rotated[2][0], rotated[2][1],
    211                                     /* texture coordinates */
    212                                                 0, 0,
    213                                     1, 0,
    214                                     1, 1,
    215                                     tex);
    216        
    217         caca_fill_triangle_textured(cv,
    218                                                 /* triangle screen coordinates */
    219                                     rotated[0][0], rotated[0][1],
    220                                                 rotated[2][0], rotated[2][1],
    221                                     rotated[3][0], rotated[3][1],
    222                                     /* texture coordinates */
    223                                                 0, 0,
    224                                     1, 1,
    225                                     0, 1,
    226                                     tex);
     233                                    coords2,
     234                                    tex,
     235                                    uv2);
    227236       
    228237        /* Refresh display and clear for next frame */
Note: See TracChangeset for help on using the changeset viewer.