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
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
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
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  This file contains triangle drawing functions, both filled and outline.
15 */
16
17#include "config.h"
18
19#if !defined(__KERNEL__)
20#   include <stdlib.h>
21#endif
22
23#include "caca.h"
24#include "caca_internals.h"
25
26/** \brief Draw a triangle on the canvas using the given character.
27 *
28 *  This function never fails.
29 *
30 *  \param cv The handle to the libcaca canvas.
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.
37 *  \param ch UTF-32 character to be used to draw the triangle outline.
38 *  \return This function always returns 0.
39 */
40int caca_draw_triangle(caca_canvas_t * cv, int x1, int y1, int x2, int y2,
41                       int x3, int y3, uint32_t ch)
42{
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);
46
47    return 0;
48}
49
50/** \brief Draw a thin triangle on the canvas.
51 *
52 *  This function never fails.
53 *
54 *  \param cv The handle to the libcaca canvas.
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.
61 *  \return This function always returns 0.
62 */
63int caca_draw_thin_triangle(caca_canvas_t * cv, int x1, int y1,
64                            int x2, int y2, int x3, int y3)
65{
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);
69
70    return 0;
71}
72
73/** \brief Fill a triangle on the canvas using the given character.
74 *
75 *  This function never fails.
76 *
77 *  \param cv The handle to the libcaca canvas.
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.
84 *  \param ch UTF-32 character to be used to fill the triangle.
85 *  \return This function always returns 0.
86 */
87int caca_fill_triangle(caca_canvas_t * cv, int x1, int y1, int x2, int y2,
88                       int x3, int y3, uint32_t ch)
89{
90    int x, y, xmin, xmax, ymin, ymax;
91    int xx1, xx2, xa, xb, sl21, sl31, sl32;
92
93    /* Bubble-sort y1 <= y2 <= y3 */
94    if (y1 > y2)
95        return caca_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
96
97    if (y2 > y3)
98        return caca_fill_triangle(cv, x1, y1, x3, y3, x2, y2, ch);
99
100    /* Compute slopes and promote precision */
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);
104
105    x1 *= 0x10000;
106    x2 *= 0x10000;
107    x3 *= 0x10000;
108
109    ymin = y1 < 0 ? 0 : y1;
110    ymax = y3 + 1 < cv->height ? y3 + 1 : cv->height;
111
112    if (ymin < y2)
113    {
114        xa = x1 + sl21 * (ymin - y1);
115        xb = x1 + sl31 * (ymin - y1);
116    }
117    else if (ymin == y2)
118    {
119        xa = x2;
120        xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
121    }
122    else                        /* (ymin > y2) */
123    {
124        xa = x3 + sl32 * (ymin - y3);
125        xb = x3 + sl31 * (ymin - y3);
126    }
127
128    /* Rasterize our triangle */
129    for (y = ymin; y < ymax; y++)
130    {
131        /* Rescale xa and xb, recentering the division */
132        if (xa < xb)
133        {
134            xx1 = (xa + 0x800) / 0x10000;
135            xx2 = (xb + 0x801) / 0x10000;
136        }
137        else
138        {
139            xx1 = (xb + 0x800) / 0x10000;
140            xx2 = (xa + 0x801) / 0x10000;
141        }
142
143        xmin = xx1 < 0 ? 0 : xx1;
144        xmax = xx2 + 1 < cv->width ? xx2 + 1 : cv->width;
145
146        for (x = xmin; x < xmax; x++)
147            caca_put_char(cv, x, y, ch);
148
149        xa += y < y2 ? sl21 : sl32;
150        xb += sl31;
151    }
152
153    return 0;
154}
155
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)
166{
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;
171    uint32_t savedattr;
172    int tw, th, x, y, s;
173
174#define SWAP_F(a, b) {float c = a; a = b; b = c; }
175
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
182    /* Bubble-sort y1 <= y2 <= y3 */
183    if (y1 > y2)
184        return caca_fill_triangle_textured_l(cv,
185                                             x2, y2, x1, y1, x3, y3,
186                                             tex, u2, v2, u1, v1, u3, v3);
187    if (y2 > y3)
188        return caca_fill_triangle_textured_l(cv,
189                                             x1, y1, x3, y3, x2, y2,
190                                             tex, u1, v1, u3, v3, u2, v2);
191
192    savedattr = caca_get_attr(cv, -1, -1);
193
194    /* Clip texture coordinates */
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;
201
202    /* Convert relative tex coordinates to absolute */
203    tw = caca_get_canvas_width(tex);
204    th = caca_get_canvas_height(tex);
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
213    y2y1 = (float)(y2 - y1);
214    y3y1 = (float)(y3 - y1);
215    y3y2 = (float)(y3 - y2);
216
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 */
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);
223
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);
230
231    xa = (float)x1;
232    xb = (float)x1;
233    ua = u1; ub = u1;
234    va = v1; vb = v1;
235
236    s = 0;
237
238    /* Top */
239    for (y = y1; y < y2; y++)
240    {
241        float tus, tvs;
242
243        if (xb < xa)
244        {
245            SWAP_F(xb, xa);
246            SWAP_F(sl13, sl12);
247            SWAP_F(ua, ub);
248            SWAP_F(va, vb);
249            SWAP_F(usl13, usl12);
250            SWAP_F(vsl13, vsl12);
251            s = 1;
252        }
253
254        tus = (ub - ua) / (xb - xa);
255        tvs = (vb - va) / (xb - xa);
256        v = va;
257        u = ua;
258
259        /* scanline */
260        for (x = xa; x < xb; x++)
261        {
262            uint32_t attr, c;
263            u += tus;
264            v += tvs;
265            /* FIXME: use caca_get_canvas_attrs / caca_get_canvas_chars */
266            attr = caca_get_attr(tex, u, v);
267            c = caca_get_char(tex, u, v);
268            caca_set_attr(cv, attr);
269            caca_put_char(cv, x, y, c);
270        }
271
272        xa += sl13;
273        xb += sl12;
274
275        ua += usl13;
276        va += vsl13;
277        ub += usl12;
278        vb += vsl12;
279    }
280
281    if (s)
282    {
283        SWAP_F(xb, xa);
284        SWAP_F(sl13, sl12);
285        SWAP_F(ua, ub);
286        SWAP_F(va, vb);
287        SWAP_F(usl13, usl12);
288        SWAP_F(vsl13, vsl12);
289    }
290
291
292    /* Bottom */
293    xb = (float)x2;
294
295    /* These variables are set by 'top' routine and are in an incorrect state
296       if we only draw the bottom part */
297    if (y1 == y2)
298    {
299        ua = u1;
300        ub = u2;
301        va = v1;
302        vb = v2;
303    }
304
305    for (y = y2; y < y3; y++)
306    {
307        float tus, tvs;
308
309        if (xb <= xa)
310        {
311            SWAP_F(xb, xa);
312            SWAP_F(sl13, sl23);
313            SWAP_F(ua, ub);
314            SWAP_F(va, vb);
315            SWAP_F(usl13, usl23);
316            SWAP_F(vsl13, vsl23);
317        }
318
319        tus = (ub - ua) / ((float)xb - xa);
320        tvs = (vb - va) / ((float)xb - xa);
321        u = ua;
322        v = va;
323
324        /* scanline */
325        for (x = xa; x < xb; x++)
326        {
327            uint32_t attr, c;
328            u += tus;
329            v += tvs;
330            /* FIXME, can be heavily optimised */
331            attr = caca_get_attr(tex, u, v);
332            c = caca_get_char(tex, u, v);
333            caca_set_attr(cv, attr);
334            caca_put_char(cv, x, y, c);
335        }
336
337        xa += sl13;
338        xb += sl23;
339
340        ua += usl13;
341        va += vsl13;
342        ub += usl23;
343        vb += vsl23;
344    }
345
346    caca_set_attr(cv, savedattr);
347
348    return 0;
349}
350
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 */
361int caca_fill_triangle_textured(caca_canvas_t * cv,
362                                int coords[6],
363                                caca_canvas_t * tex, float uv[6])
364{
365
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,
371                                         uv[0], uv[1],
372                                         uv[2], uv[3], uv[4], uv[5]);
373}
374
375
376
377/*
378 * XXX: The following functions are aliases.
379 */
380
381int cucul_draw_triangle(cucul_canvas_t *, int, int, int, int, int,
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.