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