source: libcaca/trunk/caca/triangle.c @ 4369

Last change on this file since 4369 was 4369, checked in by Sam Hocevar, 11 years ago

Add the copyright unit test and update copyright information everywhere.

  • Property svn:keywords set to Id
File size: 11.2 KB
RevLine 
[3994]1/*
[2821]2 *  libcaca       Colour ASCII-Art library
[4369]3 *  Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
[268]4 *                All Rights Reserved
[105]5 *
[1462]6 *  This library is free software. It comes without any warranty, to
[1452]7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
[522]10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[105]11 */
12
[3994]13/*
[268]14 *  This file contains triangle drawing functions, both filled and outline.
[205]15 */
16
[105]17#include "config.h"
18
[568]19#if !defined(__KERNEL__)
20#   include <stdlib.h>
21#endif
[105]22
[2821]23#include "caca.h"
24#include "caca_internals.h"
[105]25
[773]26/** \brief Draw a triangle on the canvas using the given character.
[257]27 *
[870]28 *  This function never fails.
29 *
[2821]30 *  \param cv The handle to the libcaca canvas.
[773]31 *  \param x1 X coordinate of the first point.
32 *  \param y1 Y coordinate of the first point.
33 *  \param x2 X coordinate of the second point.
34 *  \param y2 Y coordinate of the second point.
35 *  \param x3 X coordinate of the third point.
36 *  \param y3 Y coordinate of the third point.
[1330]37 *  \param ch UTF-32 character to be used to draw the triangle outline.
[870]38 *  \return This function always returns 0.
[257]39 */
[3987]40int caca_draw_triangle(caca_canvas_t * cv, int x1, int y1, int x2, int y2,
41                       int x3, int y3, uint32_t ch)
[151]42{
[2821]43    caca_draw_line(cv, x1, y1, x2, y2, ch);
44    caca_draw_line(cv, x2, y2, x3, y3, ch);
45    caca_draw_line(cv, x3, y3, x1, y1, ch);
[870]46
47    return 0;
[151]48}
49
[773]50/** \brief Draw a thin triangle on the canvas.
[257]51 *
[870]52 *  This function never fails.
53 *
[2821]54 *  \param cv The handle to the libcaca canvas.
[773]55 *  \param x1 X coordinate of the first point.
56 *  \param y1 Y coordinate of the first point.
57 *  \param x2 X coordinate of the second point.
58 *  \param y2 Y coordinate of the second point.
59 *  \param x3 X coordinate of the third point.
60 *  \param y3 Y coordinate of the third point.
[870]61 *  \return This function always returns 0.
[257]62 */
[3987]63int caca_draw_thin_triangle(caca_canvas_t * cv, int x1, int y1,
64                            int x2, int y2, int x3, int y3)
[151]65{
[2821]66    caca_draw_thin_line(cv, x1, y1, x2, y2);
67    caca_draw_thin_line(cv, x2, y2, x3, y3);
68    caca_draw_thin_line(cv, x3, y3, x1, y1);
[870]69
70    return 0;
[151]71}
72
[773]73/** \brief Fill a triangle on the canvas using the given character.
[257]74 *
[870]75 *  This function never fails.
76 *
[2821]77 *  \param cv The handle to the libcaca canvas.
[773]78 *  \param x1 X coordinate of the first point.
79 *  \param y1 Y coordinate of the first point.
80 *  \param x2 X coordinate of the second point.
81 *  \param y2 Y coordinate of the second point.
82 *  \param x3 X coordinate of the third point.
83 *  \param y3 Y coordinate of the third point.
[1330]84 *  \param ch UTF-32 character to be used to fill the triangle.
[870]85 *  \return This function always returns 0.
[257]86 */
[3987]87int caca_fill_triangle(caca_canvas_t * cv, int x1, int y1, int x2, int y2,
88                       int x3, int y3, uint32_t ch)
[105]89{
[1173]90    int x, y, xmin, xmax, ymin, ymax;
[2313]91    int xx1, xx2, xa, xb, sl21, sl31, sl32;
[105]92
93    /* Bubble-sort y1 <= y2 <= y3 */
[3987]94    if (y1 > y2)
[2821]95        return caca_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
[105]96
[3987]97    if (y2 > y3)
[2821]98        return caca_fill_triangle(cv, x1, y1, x3, y3, x2, y2, ch);
[105]99
[1172]100    /* Compute slopes and promote precision */
[1174]101    sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x10000 / (y2 - y1);
102    sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x10000 / (y3 - y1);
103    sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x10000 / (y3 - y2);
[1172]104
[1174]105    x1 *= 0x10000;
106    x2 *= 0x10000;
107    x3 *= 0x10000;
[1172]108
[1173]109    ymin = y1 < 0 ? 0 : y1;
[2313]110    ymax = y3 + 1 < cv->height ? y3 + 1 : cv->height;
[105]111
[3987]112    if (ymin < y2)
[1173]113    {
114        xa = x1 + sl21 * (ymin - y1);
115        xb = x1 + sl31 * (ymin - y1);
116    }
[3987]117    else if (ymin == y2)
[1173]118    {
119        xa = x2;
120        xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
121    }
[3987]122    else                        /* (ymin > y2) */
[1173]123    {
124        xa = x3 + sl32 * (ymin - y3);
125        xb = x3 + sl31 * (ymin - y3);
126    }
127
[105]128    /* Rasterize our triangle */
[3987]129    for (y = ymin; y < ymax; y++)
[105]130    {
[1173]131        /* Rescale xa and xb, recentering the division */
[3987]132        if (xa < xb)
[105]133        {
[1174]134            xx1 = (xa + 0x800) / 0x10000;
135            xx2 = (xb + 0x801) / 0x10000;
[105]136        }
[1173]137        else
[105]138        {
[1174]139            xx1 = (xb + 0x800) / 0x10000;
140            xx2 = (xa + 0x801) / 0x10000;
[105]141        }
142
[1173]143        xmin = xx1 < 0 ? 0 : xx1;
[2313]144        xmax = xx2 + 1 < cv->width ? xx2 + 1 : cv->width;
[105]145
[3987]146        for (x = xmin; x < xmax; x++)
[2821]147            caca_put_char(cv, x, y, ch);
[105]148
[1173]149        xa += y < y2 ? sl21 : sl32;
150        xb += sl31;
[105]151    }
[870]152
153    return 0;
[105]154}
155
[3987]156/* This function actually renders the triangle, but is not exported due to
157   sam's pedantic will. */
158static int caca_fill_triangle_textured_l(caca_canvas_t * cv,
159                                         int x1, int y1,
160                                         int x2, int y2,
161                                         int x3, int y3,
162                                         caca_canvas_t * tex,
163                                         float u1, float v1,
164                                         float u2, float v2,
165                                         float u3, float v3)
[3909]166{
[3925]167    uint32_t savedattr;
168
[3987]169#define SWAP_F(a, b) {float c = a; a = b; b = c; }
[3909]170
[3987]171    /* (very) Naive and (very) float-based affine and (very) non-clipped and
172       (very) non-corrected triangle mapper Accepts arbitrary texture sizes
173       Coordinates clamped to [0.0 - 1.0] (no repeat) */
174    if (!cv || !tex)
175        return -1;
176
[3909]177    /* Bubble-sort y1 <= y2 <= y3 */
[3987]178    if (y1 > y2)
179        return caca_fill_triangle_textured_l(cv,
[3926]180                                             x2, y2, x1, y1, x3, y3,
[3987]181                                             tex, u2, v2, u1, v1, u3, v3);
182    if (y2 > y3)
183        return caca_fill_triangle_textured_l(cv,
[3926]184                                             x1, y1, x3, y3, x2, y2,
[3987]185                                             tex, u1, v1, u3, v3, u2, v2);
[3926]186
[3925]187    savedattr = caca_get_attr(cv, -1, -1);
[3926]188
[3909]189    /* Clip texture coordinates */
[3987]190    if (u1 < 0.0f)
191        u1 = 0.0f;
192    if (v1 < 0.0f)
193        v1 = 0.0f;
194    if (u2 < 0.0f)
195        u2 = 0.0f;
196    if (v2 < 0.0f)
197        v2 = 0.0f;
198    if (u3 < 0.0f)
199        u3 = 0.0f;
200    if (v3 < 0.0f)
201        v3 = 0.0f;
202    if (u1 > 1.0f)
203        u1 = 1.0f;
204    if (v1 > 1.0f)
205        v1 = 1.0f;
206    if (u2 > 1.0f)
207        u2 = 1.0f;
208    if (v2 > 1.0f)
209        v2 = 1.0f;
210    if (u3 > 1.0f)
211        u3 = 1.0f;
212    if (v3 > 1.0f)
213        v3 = 1.0f;
214
[3909]215    /* Convert relative tex coordinates to absolute */
[3926]216    int tw = caca_get_canvas_width(tex);
[3909]217    int th = caca_get_canvas_height(tex);
[3987]218
219    u1 *= (float)tw;
220    u2 *= (float)tw;
221    u3 *= (float)tw;
222    v1 *= (float)th;
223    v2 *= (float)th;
224    v3 *= (float)th;
225
[3912]226    int x, y;
[3987]227    float y2y1 = y2 - y1;
228    float y3y1 = y3 - y1;
229    float y3y2 = y3 - y2;
230
[3909]231    /* Compute slopes, making sure we don't divide by zero */
232    /* (in this case, we don't need the value anyway) */
233    /* FIXME : only compute needed slopes */
[3987]234    float sl12 = ((float)x2 - x1) / (y2y1 == 0 ? 1 : y2y1);
235    float sl13 = ((float)x3 - x1) / (y3y1 == 0 ? 1 : y3y1);
236    float sl23 = ((float)x3 - x2) / (y3y2 == 0 ? 1 : y3y2);
237
238    float usl12 = (u2 - u1) / (y2y1 == 0 ? 1 : y2y1);
239    float usl13 = (u3 - u1) / (y3y1 == 0 ? 1 : y3y1);
240    float usl23 = (u3 - u2) / (y3y2 == 0 ? 1 : y3y2);
241    float vsl12 = (v2 - v1) / (y2y1 == 0 ? 1 : y2y1);
242    float vsl13 = (v3 - v1) / (y3y1 == 0 ? 1 : y3y1);
243    float vsl23 = (v3 - v2) / (y3y2 == 0 ? 1 : y3y2);
244
245    float xa = (float)x1, xb = (float)x1;
[3909]246    float ua = u1, ub = u1;
247    float va = v1, vb = v1;
248    float u, v;
[3987]249
[3926]250    int s = 0;
[3987]251
[3909]252    /* Top */
[3987]253    for (y = y1; y < y2; y++)
[3909]254    {
[3987]255
256        if (xb < xa)
257        {
258            SWAP_F(xb, xa);
[3909]259            SWAP_F(sl13, sl12);
260            SWAP_F(ua, ub);
[3987]261            SWAP_F(va, vb);
[3909]262            SWAP_F(usl13, usl12);
263            SWAP_F(vsl13, vsl12);
[3987]264            s = 1;
[3909]265        }
[3987]266
[3909]267        float tus = (ub - ua) / (xb - xa);
268        float tvs = (vb - va) / (xb - xa);
[3987]269        v = va;
270        u = ua;
271
[3909]272        /* scanline */
[3987]273        for (x = xa; x < xb; x++)
[3909]274        {
[3987]275            u += tus;
276            v += tvs;
277            /* FIXME: use caca_get_canvas_attrs / caca_get_canvas_chars */
[3926]278            uint32_t attr = caca_get_attr(tex, u, v);
[3987]279            uint32_t c = caca_get_char(tex, u, v);
[3909]280            caca_set_attr(cv, attr);
281            caca_put_char(cv, x, y, c);
282        }
[3987]283
284        xa += sl13;
285        xb += sl12;
286
287        ua += usl13;
288        va += vsl13;
289        ub += usl12;
290        vb += vsl12;
[3909]291    }
[3987]292
293    if (s)
[3909]294    {
295        SWAP_F(xb, xa);
296        SWAP_F(sl13, sl12);
297        SWAP_F(ua, ub);
[3926]298        SWAP_F(va, vb);
[3909]299        SWAP_F(usl13, usl12);
300        SWAP_F(vsl13, vsl12);
301    }
[3987]302
303
[3909]304    /* Bottom */
[3987]305    xb = (float)x2;
[3909]306
[3994]307    /* These variables are set by 'top' routine and are in an incorrect state
[3987]308       if we only draw the bottom part */
309    if (y1 == y2)
310    {
[3909]311        ua = u1;
312        ub = u2;
313        va = v1;
314        vb = v2;
315    }
[3987]316
317    for (y = y2; y < y3; y++)
[3909]318    {
[3987]319        if (xb <= xa)
[3909]320        {
[3926]321            SWAP_F(xb, xa);
[3909]322            SWAP_F(sl13, sl23);
323            SWAP_F(ua, ub);
[3926]324            SWAP_F(va, vb);
[3909]325            SWAP_F(usl13, usl23);
326            SWAP_F(vsl13, vsl23);
[3987]327        }
328
[3909]329        float tus = (ub - ua) / ((float)xb - xa);
330        float tvs = (vb - va) / ((float)xb - xa);
[3987]331        u = ua;
332        v = va;
333
[3909]334        /* scanline */
[3987]335        for (x = xa; x < xb; x++)
336        {
337            u += tus;
338            v += tvs;
339            /* FIXME, can be heavily optimised */
[3909]340            uint32_t attr = caca_get_attr(tex, u, v);
[3987]341            uint32_t c = caca_get_char(tex, u, v);
[3926]342            caca_set_attr(cv, attr);
343            caca_put_char(cv, x, y, c);
[3909]344        }
[3926]345
[3987]346        xa += sl13;
347        xb += sl23;
348
349        ua += usl13;
350        va += vsl13;
351        ub += usl23;
352        vb += vsl23;
[3909]353    }
[3925]354
355    caca_set_attr(cv, savedattr);
356
357    return 0;
[3909]358}
359
[3926]360/** \brief Fill a triangle on the canvas using an arbitrary-sized texture.
361 *
362 *  This function fails if one or both the canvas are missing
363 *
364 *  \param cv     The handle to the libcaca canvas.
365 *  \param coords The coordinates of the triangle (3{x,y})
366 *  \param tex    The handle of the canvas texture.
367 *  \param uv     The coordinates of the texture (3{u,v})
368 *  \return This function return 0 if ok, -1 if canvas or texture are missing.
369 */
[3987]370int caca_fill_triangle_textured(caca_canvas_t * cv,
[3926]371                                int coords[6],
[3987]372                                caca_canvas_t * tex, float uv[6])
373{
374
[3926]375    return caca_fill_triangle_textured_l(cv,
376                                         coords[0], coords[1],
377                                         coords[2], coords[3],
378                                         coords[4], coords[5],
379                                         tex,
[3987]380                                         uv[0], uv[1],
381                                         uv[2], uv[3], uv[4], uv[5]);
[3926]382}
[3909]383
[3926]384
385
[3994]386/*
[2826]387 * XXX: The following functions are aliases.
388 */
389
390int cucul_draw_triangle(cucul_canvas_t *, int, int, int, int, int,
[3987]391                        int, uint32_t) CACA_ALIAS(caca_draw_triangle);
392     int cucul_draw_thin_triangle(cucul_canvas_t *, int, int, int, int,
393                                  int,
394                                  int) CACA_ALIAS(caca_draw_thin_triangle);
395     int cucul_fill_triangle(cucul_canvas_t *, int, int, int, int, int, int,
396                             uint32_t) CACA_ALIAS(caca_fill_triangle);
Note: See TracBrowser for help on using the repository browser.