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

Last change on this file since 4389 was 4389, checked in by Sam Hocevar, 10 years ago

A few C89 compilation fixes.

  • Property svn:keywords set to Id
File size: 11.3 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{
[4389]167    float y2y1, y3y1, y3y2;
168    float sl12, sl13, sl23;
169    float usl12, usl13, usl23, vsl12, vsl13, vsl23;
170    float xa, xb, ua, va, ub, vb, u, v;
[3925]171    uint32_t savedattr;
[4389]172    int tw, th, x, y, s;
[3925]173
[3987]174#define SWAP_F(a, b) {float c = a; a = b; b = c; }
[3909]175
[3987]176    /* (very) Naive and (very) float-based affine and (very) non-clipped and
177       (very) non-corrected triangle mapper Accepts arbitrary texture sizes
178       Coordinates clamped to [0.0 - 1.0] (no repeat) */
179    if (!cv || !tex)
180        return -1;
181
[3909]182    /* Bubble-sort y1 <= y2 <= y3 */
[3987]183    if (y1 > y2)
184        return caca_fill_triangle_textured_l(cv,
[3926]185                                             x2, y2, x1, y1, x3, y3,
[3987]186                                             tex, u2, v2, u1, v1, u3, v3);
187    if (y2 > y3)
188        return caca_fill_triangle_textured_l(cv,
[3926]189                                             x1, y1, x3, y3, x2, y2,
[3987]190                                             tex, u1, v1, u3, v3, u2, v2);
[3926]191
[3925]192    savedattr = caca_get_attr(cv, -1, -1);
[3926]193
[3909]194    /* Clip texture coordinates */
[4389]195    if (u1 < 0.0f) u1 = 0.0f; else if (u1 > 1.0f) u1 = 1.0f;
196    if (u2 < 0.0f) u2 = 0.0f; else if (u2 > 1.0f) u2 = 1.0f;
197    if (u3 < 0.0f) u3 = 0.0f; else if (u3 > 1.0f) u3 = 1.0f;
198    if (v1 < 0.0f) v1 = 0.0f; else if (v1 > 1.0f) v1 = 1.0f;
199    if (v2 < 0.0f) v2 = 0.0f; else if (v2 > 1.0f) v2 = 1.0f;
200    if (v3 < 0.0f) v3 = 0.0f; else if (v3 > 1.0f) v3 = 1.0f;
[3987]201
[3909]202    /* Convert relative tex coordinates to absolute */
[4389]203    tw = caca_get_canvas_width(tex);
204    th = caca_get_canvas_height(tex);
[3987]205
206    u1 *= (float)tw;
207    u2 *= (float)tw;
208    u3 *= (float)tw;
209    v1 *= (float)th;
210    v2 *= (float)th;
211    v3 *= (float)th;
212
[4389]213    y2y1 = (float)(y2 - y1);
214    y3y1 = (float)(y3 - y1);
215    y3y2 = (float)(y3 - y2);
[3987]216
[3909]217    /* Compute slopes, making sure we don't divide by zero */
218    /* (in this case, we don't need the value anyway) */
219    /* FIXME : only compute needed slopes */
[4389]220    sl12 = ((float)x2 - x1) / (y2y1 == 0 ? 1 : y2y1);
221    sl13 = ((float)x3 - x1) / (y3y1 == 0 ? 1 : y3y1);
222    sl23 = ((float)x3 - x2) / (y3y2 == 0 ? 1 : y3y2);
[3987]223
[4389]224    usl12 = (u2 - u1) / (y2y1 == 0 ? 1 : y2y1);
225    usl13 = (u3 - u1) / (y3y1 == 0 ? 1 : y3y1);
226    usl23 = (u3 - u2) / (y3y2 == 0 ? 1 : y3y2);
227    vsl12 = (v2 - v1) / (y2y1 == 0 ? 1 : y2y1);
228    vsl13 = (v3 - v1) / (y3y1 == 0 ? 1 : y3y1);
229    vsl23 = (v3 - v2) / (y3y2 == 0 ? 1 : y3y2);
[3987]230
[4389]231    xa = (float)x1;
232    xb = (float)x1;
233    ua = u1; ub = u1;
234    va = v1; vb = v1;
[3987]235
[4389]236    s = 0;
[3987]237
[3909]238    /* Top */
[3987]239    for (y = y1; y < y2; y++)
[3909]240    {
[4389]241        float tus, tvs;
[3987]242
243        if (xb < xa)
244        {
245            SWAP_F(xb, xa);
[3909]246            SWAP_F(sl13, sl12);
247            SWAP_F(ua, ub);
[3987]248            SWAP_F(va, vb);
[3909]249            SWAP_F(usl13, usl12);
250            SWAP_F(vsl13, vsl12);
[3987]251            s = 1;
[3909]252        }
[3987]253
[4389]254        tus = (ub - ua) / (xb - xa);
255        tvs = (vb - va) / (xb - xa);
[3987]256        v = va;
257        u = ua;
258
[3909]259        /* scanline */
[3987]260        for (x = xa; x < xb; x++)
[3909]261        {
[4389]262            uint32_t attr, c;
[3987]263            u += tus;
264            v += tvs;
265            /* FIXME: use caca_get_canvas_attrs / caca_get_canvas_chars */
[4389]266            attr = caca_get_attr(tex, u, v);
267            c = caca_get_char(tex, u, v);
[3909]268            caca_set_attr(cv, attr);
269            caca_put_char(cv, x, y, c);
270        }
[3987]271
272        xa += sl13;
273        xb += sl12;
274
275        ua += usl13;
276        va += vsl13;
277        ub += usl12;
278        vb += vsl12;
[3909]279    }
[3987]280
281    if (s)
[3909]282    {
283        SWAP_F(xb, xa);
284        SWAP_F(sl13, sl12);
285        SWAP_F(ua, ub);
[3926]286        SWAP_F(va, vb);
[3909]287        SWAP_F(usl13, usl12);
288        SWAP_F(vsl13, vsl12);
289    }
[3987]290
291
[3909]292    /* Bottom */
[3987]293    xb = (float)x2;
[3909]294
[3994]295    /* These variables are set by 'top' routine and are in an incorrect state
[3987]296       if we only draw the bottom part */
297    if (y1 == y2)
298    {
[3909]299        ua = u1;
300        ub = u2;
301        va = v1;
302        vb = v2;
303    }
[3987]304
305    for (y = y2; y < y3; y++)
[3909]306    {
[4389]307        float tus, tvs;
308
[3987]309        if (xb <= xa)
[3909]310        {
[3926]311            SWAP_F(xb, xa);
[3909]312            SWAP_F(sl13, sl23);
313            SWAP_F(ua, ub);
[3926]314            SWAP_F(va, vb);
[3909]315            SWAP_F(usl13, usl23);
316            SWAP_F(vsl13, vsl23);
[3987]317        }
318
[4389]319        tus = (ub - ua) / ((float)xb - xa);
320        tvs = (vb - va) / ((float)xb - xa);
[3987]321        u = ua;
322        v = va;
323
[3909]324        /* scanline */
[3987]325        for (x = xa; x < xb; x++)
326        {
[4389]327            uint32_t attr, c;
[3987]328            u += tus;
329            v += tvs;
330            /* FIXME, can be heavily optimised */
[4389]331            attr = caca_get_attr(tex, u, v);
332            c = caca_get_char(tex, u, v);
[3926]333            caca_set_attr(cv, attr);
334            caca_put_char(cv, x, y, c);
[3909]335        }
[3926]336
[3987]337        xa += sl13;
338        xb += sl23;
339
340        ua += usl13;
341        va += vsl13;
342        ub += usl23;
343        vb += vsl23;
[3909]344    }
[3925]345
346    caca_set_attr(cv, savedattr);
347
348    return 0;
[3909]349}
350
[3926]351/** \brief Fill a triangle on the canvas using an arbitrary-sized texture.
352 *
353 *  This function fails if one or both the canvas are missing
354 *
355 *  \param cv     The handle to the libcaca canvas.
356 *  \param coords The coordinates of the triangle (3{x,y})
357 *  \param tex    The handle of the canvas texture.
358 *  \param uv     The coordinates of the texture (3{u,v})
359 *  \return This function return 0 if ok, -1 if canvas or texture are missing.
360 */
[3987]361int caca_fill_triangle_textured(caca_canvas_t * cv,
[3926]362                                int coords[6],
[3987]363                                caca_canvas_t * tex, float uv[6])
364{
365
[3926]366    return caca_fill_triangle_textured_l(cv,
367                                         coords[0], coords[1],
368                                         coords[2], coords[3],
369                                         coords[4], coords[5],
370                                         tex,
[3987]371                                         uv[0], uv[1],
372                                         uv[2], uv[3], uv[4], uv[5]);
[3926]373}
[3909]374
[3926]375
376
[3994]377/*
[2826]378 * XXX: The following functions are aliases.
379 */
380
381int cucul_draw_triangle(cucul_canvas_t *, int, int, int, int, int,
[3987]382                        int, uint32_t) CACA_ALIAS(caca_draw_triangle);
383     int cucul_draw_thin_triangle(cucul_canvas_t *, int, int, int, int,
384                                  int,
385                                  int) CACA_ALIAS(caca_draw_thin_triangle);
386     int cucul_fill_triangle(cucul_canvas_t *, int, int, int, int, int, int,
387                             uint32_t) CACA_ALIAS(caca_fill_triangle);
Note: See TracBrowser for help on using the repository browser.