Debian Codegraph @

addons/primitives/high_primitives.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Some high level routines, provided for user's convinience.
12 *
13 *
14 * By Pavel Sountsov.
15 *
16 *
17 * Bezier spline plotter By Seymour Shlien.
18 *
19 * Optimised version by Sven Sandberg.
20 *
21 * I'm not sure wether or not we still use the Castelau Algorithm
22 * described in the book :o)
23 *
24 * Interactive Computer Graphics
25 * by Peter Burger and Duncan Gillies
26 * Addison-Wesley Publishing Co 1989
27 * ISBN 0-201-17439-1
28 *
29 * The 4 th order Bezier curve is a cubic curve passing
30 * through the first and fourth point. The curve does
31 * not pass through the middle two points. They are merely
32 * guide points which control the shape of the curve. The
33 * curve is tangent to the lines joining points 1 and 2
34 * and points 3 and 4.
35 *
36 * See readme.txt for copyright information.
37 */
38
39
40 #define ALLEGRO_INTERNAL_UNSTABLE
41 #include "allegro5/allegro_primitives.h"
42 #ifdef ALLEGRO_CFG_OPENGL
43 #include "allegro5/allegro_opengl.h"
44 #endif
45 #include "allegro5/internal/aintern_bitmap.h"
46 #include "allegro5/debug.h"
47 #include <math.h>
48
49 ALLEGRO_DEBUG_CHANNEL("primitives")
50
51 #ifdef ALLEGRO_MSVC
52 #define hypotf(x, y) _hypotf((x), (y))
53 #endif
54
55 #define LOCAL_VERTEX_CACHE ALLEGRO_VERTEX vertex_cache[ALLEGRO_VERTEX_CACHE_SIZE]
56
57 /*
58 * Make an estimate of the scale of the current transformation.
59 * We do this by computing the determinants of the 2D section of the transformation matrix.
60 */
61 static float get_scale(void)
62 {
63 #define DET2D(T) (fabs((T)->m[0][0] * (T)->m[1][1] - (T)->m[0][1] * (T)->m[1][0]))
64
65 const ALLEGRO_TRANSFORM* t = al_get_current_transform();
66 float scale_sq = DET2D(t);
67 ALLEGRO_BITMAP* b = al_get_target_bitmap();
68 if (b) {
69 const ALLEGRO_TRANSFORM* p = al_get_current_projection_transform();
70 /* Divide by 4.0f as the screen coordinates range from -1 to 1 on both axes. */
71 scale_sq *= DET2D(p) * al_get_bitmap_width(b) * al_get_bitmap_height(b) / 4.0f;
72 }
73
74 return sqrtf(scale_sq);
75
76 #undef DET2D
77 }
78
79 /* Function: al_draw_line
80 */
81 void al_draw_line(float x1, float y1, float x2, float y2,
82 ALLEGRO_COLOR color, float thickness)
83 {
84 float len = hypotf(x2 - x1, y2 - y1);
85 if (thickness > 0) {
86 int ii;
87 float tx, ty;
88
89 ALLEGRO_VERTEX vtx[4];
90
91 if (len == 0)
92 return;
93
94 tx = 0.5f * thickness * (y2 - y1) / len;
95 ty = 0.5f * thickness * -(x2 - x1) / len;
96
97 vtx[0].x = x1 + tx; vtx[0].y = y1 + ty; vtx[0].u = 0.0f; vtx[0].v = -thickness / 2.;
98 vtx[1].x = x1 - tx; vtx[1].y = y1 - ty; vtx[1].u = 0.0f; vtx[1].v = thickness / 2.;
99 vtx[2].x = x2 - tx; vtx[2].y = y2 - ty; vtx[2].u = len; vtx[2].v = thickness / 2.;
100 vtx[3].x = x2 + tx; vtx[3].y = y2 + ty; vtx[3].u = len; vtx[3].v = -thickness / 2.;
101
102 for (ii = 0; ii < 4; ii++) {
103 vtx[ii].color = color;
104 vtx[ii].z = 0;
105 }
106
107 al_draw_prim(vtx, 0, 0, 0, 4, ALLEGRO_PRIM_TRIANGLE_FAN);
108
109 } else {
110 ALLEGRO_VERTEX vtx[2];
111
112 vtx[0].x = x1; vtx[0].y = y1; vtx[0].u = 0.0f; vtx[0].v = 0.0f;
113 vtx[1].x = x2; vtx[1].y = y2; vtx[1].u = len; vtx[1].v = 0.0f;
114
115 vtx[0].color = color;
116 vtx[1].color = color;
117 vtx[0].z = 0;
118 vtx[1].z = 0;
119
120 al_draw_prim(vtx, 0, 0, 0, 2, ALLEGRO_PRIM_LINE_LIST);
121 }
122 }
123
124 /* Function: al_draw_triangle
125 */
126 void al_draw_triangle(float x1, float y1, float x2, float y2,
127 float x3, float y3, ALLEGRO_COLOR color, float thickness)
128 {
129
130 float min_x = fmin(x1, fmin(x2, x3));
131 float min_y = fmin(y1, fmin(y2, y3));
132
133 if (thickness > 0) {
134 int ii = 0;
135 float side1, side2, side3;
136 float perimeter, semi_perimeter;
137 float outer_frac, inner_frac;
138 float incenter_x, incenter_y;
139 float incircle_rad;
140 int idx = 0;
141 ALLEGRO_VERTEX vtx[5];
142 float x[3] = {x1, x2, x3};
143 float y[3] = {y1, y2, y3};
144 ALLEGRO_VERTEX first_inner_vtx;
145 ALLEGRO_VERTEX first_outer_vtx;
146 ALLEGRO_VERTEX ini_vtx;
147 float cross = (x[1] - x[0]) * (y[2] - y[0]) - (x[2] - x[0]) * (y[1] - y[0]);
148
149 ini_vtx.x = ini_vtx.y = ini_vtx.z = ini_vtx.u = ini_vtx.v = 0;
150 ini_vtx.color = color;
151 first_inner_vtx = ini_vtx;
152 first_outer_vtx = ini_vtx;
153
154 /*
155 * If the triangle is very flat, draw it as a line
156 */
157 if(fabsf(cross) < 0.0001f) {
158 /*
159 * Find the obtuse vertex via two dot products
160 */
161 float dot = (x[1] - x[0]) * (x[2] - x[0]) + (y[1] - y[0]) * (y[2] - y[0]);
162 if(dot < 0) {
163 x1 = x[1]; y1 = y[1];
164 x2 = x[2]; y2 = y[2];
165 } else {
166 dot = (x[0] - x[1]) * (x[2] - x[1]) + (y[0] - y[1]) * (y[2] - y[1]);
167 if(dot < 0) {
168 x1 = x[0]; y1 = y[0];
169 x2 = x[2]; y2 = y[2];
170 } else {
171 x1 = x[0]; y1 = y[0];
172 x2 = x[1]; y2 = y[1];
173 }
174 }
175
176 al_draw_line(x1, y1, x2, y2, color, thickness);
177 return;
178 }
179 else if(cross > 0) {
180 /*
181 * Points need to be wound correctly for the algorithm to work
182 */
183 float t;
184 t = x[1];
185 x[1] = x[2];
186 x[2] = t;
187
188 t = y[1];
189 y[1] = y[2];
190 y[2] = t;
191 }
192
193 side1 = hypotf(x[1] - x[0], y[1] - y[0]);
194 side2 = hypotf(x[2] - x[0], y[2] - y[0]);
195 side3 = hypotf(x[2] - x[1], y[2] - y[1]);
196
197 perimeter = side1 + side2 + side3;
198 semi_perimeter = perimeter / 2.0f;
199 if (semi_perimeter < 0.00001f)
200 return;
201
202 incircle_rad = sqrtf((semi_perimeter - side1) * (semi_perimeter - side2) * (semi_perimeter - side3) / semi_perimeter);
203
204 if (incircle_rad < 0.00001f)
205 return;
206
207 outer_frac = (incircle_rad + thickness / 2) / incircle_rad;
208 inner_frac = (incircle_rad - thickness / 2) / incircle_rad;
209
210 if(inner_frac < 0)
211 inner_frac = 0;
212
213 incenter_x = (side1 * x[2] + side2 * x[1] + side3 * x[0]) / perimeter;
214 incenter_y = (side1 * y[2] + side2 * y[1] + side3 * y[0]) / perimeter;
215
216 #define DRAW \
217 if(ii != 0) { \
218 vtx[idx++] = outer_vtx; \
219 vtx[idx++] = inner_vtx; \
220 \
221 al_draw_prim(vtx, 0, 0, 0, idx, ALLEGRO_PRIM_TRIANGLE_FAN); \
222 \
223 idx = 0; \
224 }
225
226 /*
227 * Iterate across the vertices, and draw each side of the triangle separately
228 */
229 for(ii = 0; ii < 3; ii++)
230 {
231 float vert_x = x[ii] - incenter_x;
232 float vert_y = y[ii] - incenter_y;
233
234 float o_dx = vert_x * outer_frac;
235 float o_dy = vert_y * outer_frac;
236
237 float i_dx = vert_x * inner_frac;
238 float i_dy = vert_y * inner_frac;
239
240 float tdx = o_dx - i_dx;
241 float tdy = o_dy - i_dy;
242
243 ALLEGRO_VERTEX inner_vtx = ini_vtx;
244 ALLEGRO_VERTEX outer_vtx = ini_vtx;
245
246 if(tdx * tdx + tdy * tdy > 16 * thickness * thickness) {
247 float x_pos = x[(ii + 1) % 3];
248 float y_pos = y[(ii + 1) % 3];
249
250 float x_neg = x[(ii + 2) % 3];
251 float y_neg = y[(ii + 2) % 3];
252
253 float x1_x2 = x[ii] - x_pos;
254 float y1_y2 = y[ii] - y_pos;
255
256 float x1_x3 = x[ii] - x_neg;
257 float y1_y3 = y[ii] - y_neg;
258
259 float mag_1_2 = hypotf(x1_x2, y1_y2);
260 float mag_1_3 = hypotf(x1_x3, y1_y3);
261
262 ALLEGRO_VERTEX next_vtx = ini_vtx;
263
264 x1_x2 *= thickness / 2 / mag_1_2;
265 y1_y2 *= thickness / 2 / mag_1_2;
266
267 x1_x3 *= thickness / 2 / mag_1_3;
268 y1_y3 *= thickness / 2 / mag_1_3;
269
270 outer_vtx.x = x[ii] + x1_x3 - y1_y3; outer_vtx.y = y[ii] + y1_y3 + x1_x3;
271 outer_vtx.u = outer_vtx.x - min_x; outer_vtx.v = outer_vtx.y - min_y;
272 inner_vtx.x = incenter_x + i_dx; inner_vtx.y = incenter_y + i_dy;
273 inner_vtx.u = inner_vtx.x - min_x; inner_vtx.v = inner_vtx.y - min_y;
274 next_vtx.x = x[ii] + x1_x2 + y1_y2; next_vtx.y = y[ii] + y1_y2 - x1_x2;
275 next_vtx.u = next_vtx.x - min_x; next_vtx.v = next_vtx.y - min_y;
276
277 DRAW
278
279 vtx[idx++] = inner_vtx;
280 vtx[idx++] = outer_vtx;
281 vtx[idx++] = next_vtx;
282 } else {
283 inner_vtx.x = incenter_x + i_dx; inner_vtx.y = incenter_y + i_dy;
284 inner_vtx.u = inner_vtx.x - min_x; inner_vtx.v = inner_vtx.y - min_y;
285 outer_vtx.x = incenter_x + o_dx; outer_vtx.y = incenter_y + o_dy;
286 outer_vtx.u = outer_vtx.x - min_x; outer_vtx.v = outer_vtx.y - min_y;
287
288 DRAW
289
290 vtx[idx++] = inner_vtx;
291 vtx[idx++] = outer_vtx;
292 }
293
294 if(ii == 0) {
295 first_inner_vtx = inner_vtx;
296 first_outer_vtx = outer_vtx;
297 }
298 }
299
300 vtx[idx++] = first_outer_vtx;
301 vtx[idx++] = first_inner_vtx;
302
303 al_draw_prim(vtx, 0, 0, 0, idx, ALLEGRO_PRIM_TRIANGLE_FAN);
304
305 #undef DRAW
306 } else {
307 ALLEGRO_VERTEX vtx[3];
308
309 vtx[0].x = x1; vtx[0].y = y1; vtx[0].u = x1 - min_x; vtx[0].v = y1 - min_y;
310 vtx[1].x = x2; vtx[1].y = y2; vtx[1].u = x2 - min_x; vtx[1].v = y2 - min_y;
311 vtx[2].x = x3; vtx[2].y = y3; vtx[2].u = x3 - min_x; vtx[2].v = y3 - min_y;
312
313 vtx[0].color = color;
314 vtx[1].color = color;
315 vtx[2].color = color;
316
317 vtx[0].z = 0;
318 vtx[1].z = 0;
319 vtx[2].z = 0;
320
321 al_draw_prim(vtx, 0, 0, 0, 3, ALLEGRO_PRIM_LINE_LOOP);
322 }
323 }
324
325 /* Function: al_draw_filled_triangle
326 */
327 void al_draw_filled_triangle(float x1, float y1, float x2, float y2,
328 float x3, float y3, ALLEGRO_COLOR color)
329 {
330 ALLEGRO_VERTEX vtx[3];
331
332 float min_x = fmin(x1, fmin(x2, x3));
333 float min_y = fmin(y1, fmin(y2, y3));
334
335 vtx[0].x = x1; vtx[0].y = y1; vtx[0].u = x1 - min_x; vtx[0].v = y1 - min_y;
336 vtx[1].x = x2; vtx[1].y = y2; vtx[1].u = x2 - min_x; vtx[1].v = y2 - min_y;
337 vtx[2].x = x3; vtx[2].y = y3; vtx[2].u = x3 - min_x; vtx[2].v = y3 - min_y;
338
339 vtx[0].color = color;
340 vtx[1].color = color;
341 vtx[2].color = color;
342
343 vtx[0].z = 0;
344 vtx[1].z = 0;
345 vtx[2].z = 0;
346
347 al_draw_prim(vtx, 0, 0, 0, 3, ALLEGRO_PRIM_TRIANGLE_LIST);
348 }
349
350 /* Function: al_draw_rectangle
351 */
352 void al_draw_rectangle(float x1, float y1, float x2, float y2,
353 ALLEGRO_COLOR color, float thickness)
354 {
355 int ii;
356
357 if (thickness > 0) {
358 float t = thickness / 2;
359 ALLEGRO_VERTEX vtx[10];
360
361 vtx[0].x = x1 - t; vtx[0].y = y1 - t; vtx[0].u = -t; vtx[0].v = -t;
362 vtx[1].x = x1 + t; vtx[1].y = y1 + t; vtx[1].u = t; vtx[1].v = t;
363 vtx[2].x = x2 + t; vtx[2].y = y1 - t; vtx[2].u = x2 - x1 + t; vtx[2].v = -t;
364 vtx[3].x = x2 - t; vtx[3].y = y1 + t; vtx[3].u = x2 - x1 - t; vtx[3].v = t;
365 vtx[4].x = x2 + t; vtx[4].y = y2 + t; vtx[4].u = x2 - x1 + t; vtx[4].v = y2 - y1 + t;
366 vtx[5].x = x2 - t; vtx[5].y = y2 - t; vtx[5].u = x2 - x1 - t; vtx[5].v = y2 - y1 - t;
367 vtx[6].x = x1 - t; vtx[6].y = y2 + t; vtx[6].u = -t; vtx[6].v = y2 - y1 + t;
368 vtx[7].x = x1 + t; vtx[7].y = y2 - t; vtx[7].u = t; vtx[7].v = y2 - y1 - t;
369 vtx[8].x = x1 - t; vtx[8].y = y1 - t; vtx[8].u = -t; vtx[8].v = -t;
370 vtx[9].x = x1 + t; vtx[9].y = y1 + t; vtx[9].u = t; vtx[9].v = t;
371
372 for (ii = 0; ii < 10; ii++) {
373 vtx[ii].color = color;
374 vtx[ii].z = 0;
375 }
376
377 al_draw_prim(vtx, 0, 0, 0, 10, ALLEGRO_PRIM_TRIANGLE_STRIP);
378 } else {
379 ALLEGRO_VERTEX vtx[4];
380
381 vtx[0].x = x1; vtx[0].y = y1;
382 vtx[1].x = x2; vtx[1].y = y1;
383 vtx[2].x = x2; vtx[2].y = y2;
384 vtx[3].x = x1; vtx[3].y = y2;
385
386 for (ii = 0; ii < 4; ii++) {
387 vtx[ii].color = color;
388 vtx[ii].z = 0;
389 vtx[ii].u = vtx[ii].x - x1;
390 vtx[ii].v = vtx[ii].y - y1;
391 }
392
393 al_draw_prim(vtx, 0, 0, 0, 4, ALLEGRO_PRIM_LINE_LOOP);
394 }
395 }
396
397 /* Function: al_draw_filled_rectangle
398 */
399 void al_draw_filled_rectangle(float x1, float y1, float x2, float y2,
400 ALLEGRO_COLOR color)
401 {
402 ALLEGRO_VERTEX vtx[4];
403 int ii;
404
405 vtx[0].x = x1; vtx[0].y = y1; vtx[0].u = 0.0f; vtx[0].v = 0.0f;
406 vtx[1].x = x1; vtx[1].y = y2; vtx[1].u = 0.0f; vtx[1].v = y2 - y1;
407 vtx[2].x = x2; vtx[2].y = y2; vtx[2].u = x2 - x1; vtx[2].v = y2 - y1;
408 vtx[3].x = x2; vtx[3].y = y1; vtx[3].u = x2 - x1; vtx[3].v = 0.0f;
409
410 for (ii = 0; ii < 4; ii++) {
411 vtx[ii].color = color;
412 vtx[ii].z = 0;
413 }
414
415 al_draw_prim(vtx, 0, 0, 0, 4, ALLEGRO_PRIM_TRIANGLE_FAN);
416 }
417
418 static void calculate_arc(float* dest, float* tex_dest, int stride, float cx, float cy,
419 float rx, float ry, float start_theta, float delta_theta, float thickness,
420 int num_points)
421 {
422 float theta;
423 float u;
424 float du;
425 float c;
426 float s;
427 float x, y, ox, oy;
428 int ii;
429
430 ASSERT(dest);
431 ASSERT(num_points > 1);
432 ASSERT(rx >= 0);
433 ASSERT(ry >= 0);
434
435 theta = delta_theta / ((float)num_points - 1);
436 c = cosf(theta);
437 s = sinf(theta);
438 x = cosf(start_theta);
439 y = sinf(start_theta);
440 u = rx * start_theta;
441 du = rx * theta;
442
443 if (thickness > 0.0f) {
444 if (rx == ry) {
445 /*
446 The circle case is particularly simple
447 */
448 float r1 = rx - thickness / 2.0f;
449 float r2 = rx + thickness / 2.0f;
450 for (ii = 0; ii < num_points; ii ++) {
451 *dest = r2 * x + cx;
452 *(dest + 1) = r2 * y + cy;
453 dest = (float*)(((char*)dest) + stride);
454
455 *dest = r1 * x + cx;
456 *(dest + 1) = r1 * y + cy;
457 dest = (float*)(((char*)dest) + stride);
458
459 if (tex_dest) {
460 *tex_dest = u;
461 *(tex_dest + 1) = thickness / 2.0f;
462 tex_dest = (float*)(((char*)tex_dest) + stride);
463
464 *tex_dest = u;
465 *(tex_dest + 1) = -thickness / 2.0f;
466 tex_dest = (float*)(((char*)tex_dest) + stride);
467 }
468
469 ox = x;
470 oy = y;
471 x = c * ox - s * oy;
472 y = s * ox + c * oy;
473 u += du;
474 }
475 }
476 else {
477 if (rx != 0 && ry != 0) {
478 for (ii = 0; ii < num_points; ii++) {
479 float denom = hypotf(ry * x, rx * y);
480 float nx = thickness / 2 * ry * x / denom;
481 float ny = thickness / 2 * rx * y / denom;
482
483 *dest = rx * x + cx + nx;
484 *(dest + 1) = ry * y + cy + ny;
485 dest = (float*)(((char*)dest) + stride);
486
487 if (tex_dest) {
488 *tex_dest = u;
489 *(tex_dest + 1) = thickness / 2.0f;
490 tex_dest = (float*)(((char*)tex_dest) + stride);
491
492 *tex_dest = u;
493 *(tex_dest + 1) = -thickness / 2.0f;
494 tex_dest = (float*)(((char*)tex_dest) + stride);
495 }
496
497 *dest = rx * x + cx - nx;
498 *(dest + 1) = ry * y + cy - ny;
499 dest = (float*)(((char*)dest) + stride);
500
501 ox = x;
502 oy = y;
503 x = c * ox - s * oy;
504 y = s * ox + c * oy;
505 u += hypotf(rx * (x - ox), ry * (y - oy));
506 }
507 }
508 }
509 }
510 else {
511 for (ii = 0; ii < num_points; ii++) {
512 *dest = rx * x + cx;
513 *(dest + 1) = ry * y + cy;
514 dest = (float*)(((char*)dest) + stride);
515
516 if (tex_dest) {
517 *tex_dest = u;
518 *(tex_dest + 1) = 0.;
519 tex_dest = (float*)(((char*)tex_dest) + stride);
520 }
521
522 ox = x;
523 oy = y;
524 x = c * ox - s * oy;
525 y = s * ox + c * oy;
526 if (rx == ry) {
527 u += du;
528 }
529 else {
530 u += hypotf(rx * (x - ox), ry * (y - oy));
531 }
532 }
533 }
534 }
535
536 /* Function: al_calculate_arc
537 */
538 void al_calculate_arc(float* dest, int stride, float cx, float cy,
539 float rx, float ry, float start_theta, float delta_theta, float thickness,
540 int num_points)
541 {
542 calculate_arc(dest, NULL, stride, cx, cy, rx, ry, start_theta, delta_theta,
543 thickness, num_points);
544 }
545
546 /* Function: al_draw_pieslice
547 */
548 void al_draw_pieslice(float cx, float cy, float r, float start_theta,
549 float delta_theta, ALLEGRO_COLOR color, float thickness)
550 {
551 LOCAL_VERTEX_CACHE;
552 float scale = get_scale();
553 int num_segments, ii;
554
555 ASSERT(r >= 0);
556
557 /* Just makes things a bit easier */
558 if(delta_theta < 0) {
559 delta_theta = -delta_theta;
560 start_theta -= delta_theta;
561 }
562
563 if (thickness <= 0) {
564 num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * r));
565
566 if (num_segments < 2)
567 num_segments = 2;
568
569 if (num_segments + 1 >= ALLEGRO_VERTEX_CACHE_SIZE) {
570 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1 - 1;
571 }
572
573 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, r, r, start_theta, delta_theta, 0, num_segments);
574 vertex_cache[0].x = cx; vertex_cache[0].y = cy;
575
576 for (ii = 0; ii < num_segments + 1; ii++) {
577 vertex_cache[ii].color = color;
578 vertex_cache[ii].z = 0;
579 vertex_cache[ii].u = vertex_cache[ii].x - cx;
580 vertex_cache[ii].v = vertex_cache[ii].y - cy;
581 }
582
583 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + 1, ALLEGRO_PRIM_LINE_LOOP);
584 } else {
585 float ht = thickness / 2;
586 float inner_side_angle = asinf(ht / (r - ht));
587 float outer_side_angle = asinf(ht / (r + ht));
588 float central_angle = delta_theta - 2 * inner_side_angle;
589 bool inverted_winding = ((int)(delta_theta / ALLEGRO_PI)) % 2 == 1;
590 float midangle = start_theta + (fmodf(delta_theta + ALLEGRO_PI, 2 * ALLEGRO_PI) - ALLEGRO_PI) / 2;
591 float midpoint_dir_x = cosf(midangle);
592 float midpoint_dir_y = sinf(midangle);
593 float side_dir_x = cosf(start_theta);
594 float side_dir_y = sinf(start_theta);
595 float sine_half_delta = fabsf(side_dir_x * midpoint_dir_y - side_dir_y * midpoint_dir_x); /* Cross product */
596 float connect_len = ht / sine_half_delta;
597 bool blunt_tip = connect_len > 2 * thickness;
598
599 /* The angle is big enough for there to be a hole in the middle */
600 if (central_angle > 0) {
601 float central_start_angle = start_theta + inner_side_angle;
602 size_t vtx_id;
603 int vtx_delta;
604 /* Two inner hole vertices and the apex (2 vertices if the apex is blunt) */
605 int extra_vtx = blunt_tip ? 4 : 3;
606
607 num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * r));
608
609 if (num_segments < 2)
610 num_segments = 2;
611
612 if (2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
613 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 2;
614 }
615
616 al_calculate_arc(&vertex_cache[0].x, sizeof(ALLEGRO_VERTEX),
617 cx, cy, r, r, central_start_angle, central_angle, thickness, num_segments);
618
619 for (ii = 0; ii < 2 * num_segments; ii++) {
620 vertex_cache[ii].color = color;
621 vertex_cache[ii].z = 0;
622 vertex_cache[ii].u = vertex_cache[ii].x - cx;
623 vertex_cache[ii].v = vertex_cache[ii].y - cy;
624 }
625
626 al_draw_prim(vertex_cache, 0, 0, 0, 2 * num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP);
627
628 vertex_cache[0].x = cx + (r - thickness / 2) * cosf(central_start_angle);
629 vertex_cache[0].y = cy + (r - thickness / 2) * sinf(central_start_angle);
630 vertex_cache[0].u = (r - thickness / 2) * cosf(central_start_angle);
631 vertex_cache[0].v = (r - thickness / 2) * sinf(central_start_angle);
632
633 num_segments = (inner_side_angle + outer_side_angle) / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * (r + ht));
634
635 if (num_segments < 2)
636 num_segments = 2;
637
638 if (num_segments + extra_vtx >= ALLEGRO_VERTEX_CACHE_SIZE)
639 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1 - extra_vtx;
640
641 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, r + ht, r + ht, central_start_angle, -(outer_side_angle + inner_side_angle), 0, num_segments);
642
643 /* Do the tip */
644 vtx_id = num_segments + 1 + (inverted_winding ? (1 + (blunt_tip ? 1 : 0)) : 0);
645 vtx_delta = inverted_winding ? -1 : 1;
646 if (blunt_tip) {
647 float vx = ht * (side_dir_y * (inverted_winding ? -1 : 1) - side_dir_x);
648 float vy = ht * (-side_dir_x * (inverted_winding ? -1 : 1) - side_dir_y);
649 float dot = vx * midpoint_dir_x + vy * midpoint_dir_y;
650
651 vertex_cache[vtx_id].x = cx + vx;
652 vertex_cache[vtx_id].y = cy + vy;
653 vtx_id += vtx_delta;
654
655 vertex_cache[vtx_id].x = cx + dot * midpoint_dir_x;
656 vertex_cache[vtx_id].y = cy + dot * midpoint_dir_y;
657 } else {
658 vertex_cache[vtx_id].x = cx - connect_len * midpoint_dir_x;
659 vertex_cache[vtx_id].y = cy - connect_len * midpoint_dir_y;
660 }
661 vtx_id += vtx_delta;
662
663 if(connect_len > r - ht)
664 connect_len = r - ht;
665 vertex_cache[vtx_id].x = cx + connect_len * midpoint_dir_x;
666 vertex_cache[vtx_id].y = cy + connect_len * midpoint_dir_y;
667
668 for (ii = 0; ii < num_segments + extra_vtx; ii++) {
669 vertex_cache[ii].color = color;
670 vertex_cache[ii].z = 0;
671 vertex_cache[ii].u = vertex_cache[ii].x - cx;
672 vertex_cache[ii].v = vertex_cache[ii].y - cy;
673 }
674
675 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + extra_vtx, ALLEGRO_PRIM_TRIANGLE_FAN);
676
677 /* Mirror the vertices and draw them again */
678 for (ii = 0; ii < num_segments + extra_vtx; ii++) {
679 float dot = (vertex_cache[ii].x - cx) * midpoint_dir_x + (vertex_cache[ii].y - cy) * midpoint_dir_y;
680 vertex_cache[ii].x = 2 * cx + 2 * dot * midpoint_dir_x - vertex_cache[ii].x;
681 vertex_cache[ii].y = 2 * cy + 2 * dot * midpoint_dir_y - vertex_cache[ii].y;
682 vertex_cache[ii].u = vertex_cache[ii].x - cx;
683 vertex_cache[ii].v = vertex_cache[ii].y - cy;
684 }
685
686 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + extra_vtx, ALLEGRO_PRIM_TRIANGLE_FAN);
687 } else {
688 /* Apex: 2 vertices if the apex is blunt) */
689 int extra_vtx = blunt_tip ? 2 : 1;
690
691 num_segments = (2 * outer_side_angle) / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * (r + ht));
692
693 if (num_segments < 2)
694 num_segments = 2;
695
696 if (num_segments + extra_vtx >= ALLEGRO_VERTEX_CACHE_SIZE)
697 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1 - extra_vtx;
698
699 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, r + ht, r + ht, start_theta - outer_side_angle, 2 * outer_side_angle + delta_theta, 0, num_segments);
700
701 if (blunt_tip) {
702 float vx = ht * (side_dir_y - side_dir_x);
703 float vy = ht * (-side_dir_x - side_dir_y);
704 float dot = vx * midpoint_dir_x + vy * midpoint_dir_y;
705
706 vertex_cache[0].x = cx + vx;
707 vertex_cache[0].y = cy + vy;
708
709 vx = 2 * dot * midpoint_dir_x - vx;
710 vy = 2 * dot * midpoint_dir_y - vy;
711
712 vertex_cache[num_segments + 1].x = cx + vx;
713 vertex_cache[num_segments + 1].y = cy + vy;
714 } else {
715 vertex_cache[0].x = cx - connect_len * midpoint_dir_x;
716 vertex_cache[0].y = cy - connect_len * midpoint_dir_y;
717 }
718
719 for (ii = 0; ii < num_segments + extra_vtx; ii++) {
720 vertex_cache[ii].color = color;
721 vertex_cache[ii].z = 0;
722 vertex_cache[ii].u = vertex_cache[ii].x - cx;
723 vertex_cache[ii].v = vertex_cache[ii].y - cy;
724 }
725
726 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + extra_vtx, ALLEGRO_PRIM_TRIANGLE_FAN);
727 }
728 }
729 }
730
731 /* Function: al_draw_filled_pieslice
732 */
733 void al_draw_filled_pieslice(float cx, float cy, float r, float start_theta,
734 float delta_theta, ALLEGRO_COLOR color)
735 {
736 LOCAL_VERTEX_CACHE;
737 float scale = get_scale();
738 int num_segments, ii;
739
740 ASSERT(r >= 0);
741
742 num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * r));
743
744 if (num_segments < 2)
745 num_segments = 2;
746
747 if (num_segments + 1 >= ALLEGRO_VERTEX_CACHE_SIZE) {
748 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1 - 1;
749 }
750
751 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, r, r, start_theta, delta_theta, 0, num_segments);
752 vertex_cache[0].x = cx; vertex_cache[0].y = cy;
753
754 for (ii = 0; ii < num_segments + 1; ii++) {
755 vertex_cache[ii].color = color;
756 vertex_cache[ii].z = 0;
757 vertex_cache[ii].u = vertex_cache[ii].x - cx;
758 vertex_cache[ii].v = vertex_cache[ii].y - cy;
759 }
760
761 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + 1, ALLEGRO_PRIM_TRIANGLE_FAN);
762 }
763
764 /* Function: al_draw_ellipse
765 */
766 void al_draw_ellipse(float cx, float cy, float rx, float ry,
767 ALLEGRO_COLOR color, float thickness)
768 {
769 LOCAL_VERTEX_CACHE;
770 float scale = get_scale();
771
772 ASSERT(rx >= 0);
773 ASSERT(ry >= 0);
774
775 if (thickness > 0) {
776 int num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f);
777 int ii;
778
779 /* In case rx and ry are both 0. */
780 if (num_segments < 2)
781 return;
782
783 if (2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
784 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 2;
785 }
786
787 al_calculate_arc(&(vertex_cache[0].x), sizeof(ALLEGRO_VERTEX), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, thickness, num_segments);
788 for (ii = 0; ii < 2 * num_segments; ii++) {
789 vertex_cache[ii].color = color;
790 vertex_cache[ii].z = 0;
791 vertex_cache[ii].u = vertex_cache[ii].x - cx;
792 vertex_cache[ii].v = vertex_cache[ii].y - cy;
793 }
794
795 al_draw_prim(vertex_cache, 0, 0, 0, 2 * num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP);
796 } else {
797 int num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f);
798 int ii;
799
800 /* In case rx and ry are both 0. */
801 if (num_segments < 2)
802 return;
803
804 if (num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
805 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1;
806 }
807
808 al_calculate_arc(&(vertex_cache[0].x), sizeof(ALLEGRO_VERTEX), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, 0, num_segments);
809 for (ii = 0; ii < num_segments; ii++) {
810 vertex_cache[ii].color = color;
811 vertex_cache[ii].z = 0;
812 vertex_cache[ii].u = vertex_cache[ii].x - cx;
813 vertex_cache[ii].v = vertex_cache[ii].y - cy;
814 }
815
816 al_draw_prim(vertex_cache, 0, 0, 0, num_segments - 1, ALLEGRO_PRIM_LINE_LOOP);
817 }
818 }
819
820 /* Function: al_draw_filled_ellipse
821 */
822 void al_draw_filled_ellipse(float cx, float cy, float rx, float ry,
823 ALLEGRO_COLOR color)
824 {
825 LOCAL_VERTEX_CACHE;
826 int num_segments, ii;
827 float scale = get_scale();
828
829 ASSERT(rx >= 0);
830 ASSERT(ry >= 0);
831
832 num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f);
833
834 /* In case rx and ry are both close to 0. If al_calculate_arc is passed
835 * 0 or 1 it will assert.
836 */
837 if (num_segments < 2)
838 return;
839
840 if (num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
841 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1;
842 }
843
844 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, 0, num_segments);
845 vertex_cache[0].x = cx; vertex_cache[0].y = cy;
846
847 for (ii = 0; ii < num_segments + 1; ii++) {
848 vertex_cache[ii].color = color;
849 vertex_cache[ii].z = 0;
850 vertex_cache[ii].u = vertex_cache[ii].x - cx;
851 vertex_cache[ii].v = vertex_cache[ii].y - cy;
852 }
853
854 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + 1, ALLEGRO_PRIM_TRIANGLE_FAN);
855 }
856
857 /* Function: al_draw_circle
858 */
859 void al_draw_circle(float cx, float cy, float r, ALLEGRO_COLOR color,
860 float thickness)
861 {
862 al_draw_ellipse(cx, cy, r, r, color, thickness);
863 }
864
865 /* Function: al_draw_filled_circle
866 */
867 void al_draw_filled_circle(float cx, float cy, float r, ALLEGRO_COLOR color)
868 {
869 al_draw_filled_ellipse(cx, cy, r, r, color);
870 }
871
872 /* Function: al_draw_elliptical_arc
873 */
874 void al_draw_elliptical_arc(float cx, float cy, float rx, float ry, float start_theta,
875 float delta_theta, ALLEGRO_COLOR color, float thickness)
876 {
877 LOCAL_VERTEX_CACHE;
878 float scale = get_scale();
879
880 ASSERT(rx >= 0 && ry >= 0);
881 if (thickness > 0) {
882 int num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f));
883 int ii;
884
885 if (num_segments < 2)
886 num_segments = 2;
887
888 if (2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
889 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 2;
890 }
891
892 calculate_arc(&vertex_cache[0].x, &vertex_cache[0].u, sizeof(ALLEGRO_VERTEX),
893 cx, cy, rx, ry, start_theta, delta_theta, thickness, num_segments);
894
895 for (ii = 0; ii < 2 * num_segments; ii++) {
896 vertex_cache[ii].color = color;
897 vertex_cache[ii].z = 0;
898 }
899
900 al_draw_prim(vertex_cache, 0, 0, 0, 2 * num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP);
901 } else {
902 int num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f));
903 int ii;
904
905 if (num_segments < 2)
906 num_segments = 2;
907
908 if (num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
909 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1;
910 }
911
912 calculate_arc(&vertex_cache[0].x, &vertex_cache[0].u, sizeof(ALLEGRO_VERTEX), cx, cy, rx, ry, start_theta, delta_theta, 0, num_segments);
913
914 for (ii = 0; ii < num_segments; ii++) {
915 vertex_cache[ii].color = color;
916 vertex_cache[ii].z = 0;
917 }
918
919 al_draw_prim(vertex_cache, 0, 0, 0, num_segments, ALLEGRO_PRIM_LINE_STRIP);
920 }
921 }
922
923 /* Function: al_draw_arc
924 */
925 void al_draw_arc(float cx, float cy, float r, float start_theta,
926 float delta_theta, ALLEGRO_COLOR color, float thickness)
927 {
928 al_draw_elliptical_arc(cx, cy, r, r, start_theta, delta_theta, color, thickness);
929 }
930
931 /* Function: al_draw_rounded_rectangle
932 */
933 void al_draw_rounded_rectangle(float x1, float y1, float x2, float y2,
934 float rx, float ry, ALLEGRO_COLOR color, float thickness)
935 {
936 LOCAL_VERTEX_CACHE;
937 float scale = get_scale();
938
939 ASSERT(rx >= 0);
940 ASSERT(ry >= 0);
941
942 if (thickness > 0) {
943 int num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f) / 4;
944 int ii;
945
946 /* In case rx and ry are both 0. */
947 if (num_segments < 2) {
948 al_draw_rectangle(x1, y1, x2, y2, color, thickness);
949 return;
950 }
951
952 if (8 * num_segments + 2 >= ALLEGRO_VERTEX_CACHE_SIZE) {
953 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 3) / 8;
954 }
955
956 al_calculate_arc(&(vertex_cache[0].x), sizeof(ALLEGRO_VERTEX), 0, 0, rx, ry, 0, ALLEGRO_PI / 2, thickness, num_segments);
957
958 for (ii = 0; ii < 2 * num_segments; ii += 2) {
959 vertex_cache[ii + 2 * num_segments + 1].x = x1 + rx - vertex_cache[2 * num_segments - 1 - ii].x;
960 vertex_cache[ii + 2 * num_segments + 1].y = y1 + ry - vertex_cache[2 * num_segments - 1 - ii].y;
961 vertex_cache[ii + 2 * num_segments].x = x1 + rx - vertex_cache[2 * num_segments - 1 - ii - 1].x;
962 vertex_cache[ii + 2 * num_segments].y = y1 + ry - vertex_cache[2 * num_segments - 1 - ii - 1].y;
963
964 vertex_cache[ii + 4 * num_segments].x = x1 + rx - vertex_cache[ii].x;
965 vertex_cache[ii + 4 * num_segments].y = y2 - ry + vertex_cache[ii].y;
966 vertex_cache[ii + 4 * num_segments + 1].x = x1 + rx - vertex_cache[ii + 1].x;
967 vertex_cache[ii + 4 * num_segments + 1].y = y2 - ry + vertex_cache[ii + 1].y;
968
969 vertex_cache[ii + 6 * num_segments + 1].x = x2 - rx + vertex_cache[2 * num_segments - 1 - ii].x;
970 vertex_cache[ii + 6 * num_segments + 1].y = y2 - ry + vertex_cache[2 * num_segments - 1 - ii].y;
971 vertex_cache[ii + 6 * num_segments].x = x2 - rx + vertex_cache[2 * num_segments - 1 - ii - 1].x;
972 vertex_cache[ii + 6 * num_segments].y = y2 - ry + vertex_cache[2 * num_segments - 1 - ii - 1].y;
973 }
974 for (ii = 0; ii < 2 * num_segments; ii += 2) {
975 vertex_cache[ii].x = x2 - rx + vertex_cache[ii].x;
976 vertex_cache[ii].y = y1 + ry - vertex_cache[ii].y;
977 vertex_cache[ii + 1].x = x2 - rx + vertex_cache[ii + 1].x;
978 vertex_cache[ii + 1].y = y1 + ry - vertex_cache[ii + 1].y;
979 }
980 vertex_cache[8 * num_segments] = vertex_cache[0];
981 vertex_cache[8 * num_segments + 1] = vertex_cache[1];
982
983 for (ii = 0; ii < 8 * num_segments + 2; ii++) {
984 vertex_cache[ii].color = color;
985 vertex_cache[ii].z = 0;
986 vertex_cache[ii].u = vertex_cache[ii].x - x1;
987 vertex_cache[ii].v = vertex_cache[ii].y - y1;
988 }
989
990 al_draw_prim(vertex_cache, 0, 0, 0, 8 * num_segments + 2, ALLEGRO_PRIM_TRIANGLE_STRIP);
991 } else {
992 int num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f) / 4;
993 int ii;
994
995 /* In case rx and ry are both 0. */
996 if (num_segments < 2) {
997 al_draw_rectangle(x1, y1, x2, y2, color, thickness);
998 return;
999 }
1000
1001 if (num_segments * 4 >= ALLEGRO_VERTEX_CACHE_SIZE) {
1002 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 4;
1003 }
1004
1005 al_calculate_arc(&(vertex_cache[0].x), sizeof(ALLEGRO_VERTEX), 0, 0, rx, ry, 0, ALLEGRO_PI / 2, 0, num_segments + 1);
1006
1007 for (ii = 0; ii < num_segments; ii++) {
1008 vertex_cache[ii + 1 * num_segments].x = x1 + rx - vertex_cache[num_segments - 1 - ii].x;
1009 vertex_cache[ii + 1 * num_segments].y = y1 + ry - vertex_cache[num_segments - 1 - ii].y;
1010
1011 vertex_cache[ii + 2 * num_segments].x = x1 + rx - vertex_cache[ii].x;
1012 vertex_cache[ii + 2 * num_segments].y = y2 - ry + vertex_cache[ii].y;
1013
1014 vertex_cache[ii + 3 * num_segments].x = x2 - rx + vertex_cache[num_segments - 1 - ii].x;
1015 vertex_cache[ii + 3 * num_segments].y = y2 - ry + vertex_cache[num_segments - 1 - ii].y;
1016 }
1017 for (ii = 0; ii < num_segments; ii++) {
1018 vertex_cache[ii].x = x2 - rx + vertex_cache[ii].x;
1019 vertex_cache[ii].y = y1 + ry - vertex_cache[ii].y;
1020 }
1021
1022 for (ii = 0; ii < 4 * num_segments; ii++) {
1023 vertex_cache[ii].color = color;
1024 vertex_cache[ii].z = 0;
1025 vertex_cache[ii].u = vertex_cache[ii].x - x1;
1026 vertex_cache[ii].v = vertex_cache[ii].y - y1;
1027 }
1028
1029 al_draw_prim(vertex_cache, 0, 0, 0, 4 * num_segments, ALLEGRO_PRIM_LINE_LOOP);
1030 }
1031 }
1032
1033 static void calculate_ribbon(float *dest, float *tex_dest, int dest_stride, const float *points,
1034 int points_stride, float thickness, int num_segments)
1035 {
1036 ASSERT(points);
1037 ASSERT(num_segments >= 2);
1038
1039 if (thickness > 0) {
1040 int ii = 0;
1041 float x, y;
1042
1043 float cur_dir_x;
1044 float cur_dir_y;
1045 float prev_dir_x = 0;
1046 float prev_dir_y = 0;
1047 float t = thickness / 2;
1048 float tx, ty;
1049 float nx, ny;
1050 float sign = 1;
1051 float u = 0.;
1052
1053 for (ii = 0; ii < 2 * num_segments - 2; ii += 2) {
1054 float dir_len;
1055 x = *points;
1056 y = *(points + 1);
1057
1058 points = (float*)(((char*)points) + points_stride);
1059
1060 cur_dir_x = *(points) - x;
1061 cur_dir_y = *(points + 1) - y;
1062
1063 dir_len = hypotf(cur_dir_x, cur_dir_y);
1064
1065 if(dir_len > 0.000001f) {
1066 cur_dir_x /= dir_len;
1067 cur_dir_y /= dir_len;
1068 } else if (ii == 0) {
1069 cur_dir_x = 1;
1070 cur_dir_y = 0;
1071 } else {
1072 cur_dir_x = prev_dir_x;
1073 cur_dir_y = prev_dir_y;
1074 }
1075
1076 if (ii == 0) {
1077 tx = -t * cur_dir_y;
1078 ty = t * cur_dir_x;
1079 nx = 0;
1080 ny = 0;
1081 } else {
1082 float dot = cur_dir_x * prev_dir_x + cur_dir_y * prev_dir_y;
1083 float norm_len, cosine;
1084 if(dot < 0) {
1085 /*
1086 * This is by no means exact, but seems to produce acceptable results
1087 */
1088 float tx_;
1089 tx = cur_dir_x - prev_dir_x;
1090 ty = cur_dir_y - prev_dir_y;
1091 norm_len = hypotf(tx, ty);
1092
1093 tx /= norm_len;
1094 ty /= norm_len;
1095
1096 cosine = tx * cur_dir_x + ty * cur_dir_y;
1097
1098 nx = -t * tx / cosine;
1099 ny = -t * ty / cosine;
1100 tx_ = tx;
1101 tx = -t * ty * cosine;
1102 ty = t * tx_ * cosine;
1103 sign = -sign;
1104 } else {
1105 float new_norm_len;
1106 tx = cur_dir_y + prev_dir_y;
1107 ty = -(cur_dir_x + prev_dir_x);
1108 norm_len = hypotf(tx, ty);
1109
1110 tx /= norm_len;
1111 ty /= norm_len;
1112 cosine = tx * (-cur_dir_y) + ty * (cur_dir_x);
1113 new_norm_len = t / cosine;
1114
1115 tx *= new_norm_len;
1116 ty *= new_norm_len;
1117 nx = 0;
1118 ny = 0;
1119 }
1120 }
1121
1122 *dest = x - sign * tx + nx;
1123 *(dest + 1) = y - sign * ty + ny;
1124 dest = (float*)(((char*)dest) + dest_stride);
1125 *dest = x + sign * tx + nx;
1126 *(dest + 1) = y + sign * ty + ny;
1127 dest = (float*)(((char*)dest) + dest_stride);
1128 if (tex_dest) {
1129 *tex_dest = u;
1130 *(tex_dest + 1) = -thickness / 2;
1131 tex_dest = (float*)(((char*)tex_dest) + dest_stride);
1132 *tex_dest = u;
1133 *(tex_dest + 1) = thickness / 2;
1134 tex_dest = (float*)(((char*)tex_dest) + dest_stride);
1135 }
1136
1137 prev_dir_x = cur_dir_x;
1138 prev_dir_y = cur_dir_y;
1139 u += dir_len;
1140 }
1141 tx = -t * prev_dir_y;
1142 ty = t * prev_dir_x;
1143
1144 x = *points;
1145 y = *(points + 1);
1146
1147 *dest = x - sign * tx;
1148 *(dest + 1) = y - sign * ty;
1149 dest = (float*)(((char*)dest) + dest_stride);
1150 *dest = x + sign * tx;
1151 *(dest + 1) = y + sign * ty;
1152 if (tex_dest) {
1153 *tex_dest = u;
1154 *(tex_dest + 1) = -thickness / 2;
1155 tex_dest = (float*)(((char*)tex_dest) + dest_stride);
1156 *tex_dest = u;
1157 *(tex_dest + 1) = thickness / 2;
1158 tex_dest = (float*)(((char*)tex_dest) + dest_stride);
1159 }
1160 } else {
1161 int ii;
1162 float u = 0.;
1163 float old_x = *points;
1164 float old_y = *(points + 1);
1165 for (ii = 0; ii < num_segments; ii++) {
1166 float x = *points;
1167 float y = *(points + 1);
1168 *dest = x;
1169 *(dest + 1) = y;
1170 if (tex_dest) {
1171 u += hypotf(old_x - x, old_y - y);
1172 *tex_dest = u;
1173 *(tex_dest + 1) = 0.0f;
1174 tex_dest = (float*)(((char*)tex_dest) + dest_stride);
1175 }
1176 old_x = x;
1177 old_y = y;
1178 dest = (float*)(((char*)dest) + dest_stride);
1179 points = (float*)(((char*)points) + points_stride);
1180 }
1181 }
1182 }
1183
1184 /* Function: al_calculate_ribbon
1185 */
1186 void al_calculate_ribbon(float* dest, int dest_stride, const float *points,
1187 int points_stride, float thickness, int num_segments)
1188 {
1189 calculate_ribbon(dest, NULL, dest_stride, points, points_stride,
1190 thickness, num_segments);
1191 }
1192
1193 /* Function: al_draw_filled_rounded_rectangle
1194 */
1195 void al_draw_filled_rounded_rectangle(float x1, float y1, float x2, float y2,
1196 float rx, float ry, ALLEGRO_COLOR color)
1197 {
1198 LOCAL_VERTEX_CACHE;
1199 int ii;
1200 float scale = get_scale();
1201 int num_segments = ALLEGRO_PRIM_QUALITY * sqrtf(scale * (rx + ry) / 2.0f) / 4;
1202
1203 ASSERT(rx >= 0);
1204 ASSERT(ry >= 0);
1205
1206 /* In case rx and ry are both 0. */
1207 if (num_segments < 2) {
1208 al_draw_filled_rectangle(x1, y1, x2, y2, color);
1209 return;
1210 }
1211
1212 if (num_segments * 4 >= ALLEGRO_VERTEX_CACHE_SIZE) {
1213 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 4;
1214 }
1215
1216 al_calculate_arc(&(vertex_cache[0].x), sizeof(ALLEGRO_VERTEX), 0, 0, rx, ry, 0, ALLEGRO_PI / 2, 0, num_segments + 1);
1217
1218 for (ii = 0; ii < num_segments; ii++) {
1219 vertex_cache[ii + 1 * num_segments].x = x1 + rx - vertex_cache[num_segments - 1 - ii].x;
1220 vertex_cache[ii + 1 * num_segments].y = y1 + ry - vertex_cache[num_segments - 1 - ii].y;
1221
1222 vertex_cache[ii + 2 * num_segments].x = x1 + rx - vertex_cache[ii].x;
1223 vertex_cache[ii + 2 * num_segments].y = y2 - ry + vertex_cache[ii].y;
1224
1225 vertex_cache[ii + 3 * num_segments].x = x2 - rx + vertex_cache[num_segments - 1 - ii].x;
1226 vertex_cache[ii + 3 * num_segments].y = y2 - ry + vertex_cache[num_segments - 1 - ii].y;
1227 }
1228 for (ii = 0; ii < num_segments; ii++) {
1229 vertex_cache[ii].x = x2 - rx + vertex_cache[ii].x;
1230 vertex_cache[ii].y = y1 + ry - vertex_cache[ii].y;
1231 }
1232
1233 for (ii = 0; ii < 4 * num_segments; ii++) {
1234 vertex_cache[ii].color = color;
1235 vertex_cache[ii].z = 0;
1236 vertex_cache[ii].u = vertex_cache[ii].x - x1;
1237 vertex_cache[ii].v = vertex_cache[ii].y - y1;
1238 }
1239
1240 /*
1241 TODO: Doing this as a triangle fan just doesn't sound all that great, perhaps shuffle the vertices somehow to at least make it a strip
1242 */
1243 al_draw_prim(vertex_cache, 0, 0, 0, 4 * num_segments, ALLEGRO_PRIM_TRIANGLE_FAN);
1244 }
1245
1246 static void calculate_spline(float *dest, float *tex_dest, int stride, const float points[8],
1247 float thickness, int num_segments)
1248 {
1249 /* Derivatives of x(t) and y(t). */
1250 float x, dx, ddx, dddx;
1251 float y, dy, ddy, dddy;
1252 int ii = 0;
1253
1254 /* Temp variables used in the setup. */
1255 float dt, dt2, dt3;
1256 float xdt2_term, xdt3_term;
1257 float ydt2_term, ydt3_term;
1258
1259 /* This is enough to avoid malloc in ex_prim, which I take as a reasonable
1260 * guess to what a common number of segments might be. To be honest, it
1261 * probably makes no difference.
1262 */
1263 float cache_point_buffer_storage[150];
1264 float* cache_point_buffer = cache_point_buffer_storage;
1265
1266 ASSERT(num_segments > 1);
1267 ASSERT(points);
1268
1269 if (num_segments > (int)(sizeof(cache_point_buffer_storage) / sizeof(float) / 2)) {
1270 cache_point_buffer = al_malloc(2 * sizeof(float) * num_segments);
1271 }
1272
1273 dt = 1.0 / (num_segments - 1);
1274 dt2 = (dt * dt);
1275 dt3 = (dt2 * dt);
1276
1277 /* x coordinates. */
1278 xdt2_term = 3 * (points[4] - 2 * points[2] + points[0]);
1279 xdt3_term = points[6] + 3 * (-points[4] + points[2]) - points[0];
1280
1281 xdt2_term = dt2 * xdt2_term;
1282 xdt3_term = dt3 * xdt3_term;
1283
1284 dddx = 6 * xdt3_term;
1285 ddx = -6 * xdt3_term + 2 * xdt2_term;
1286 dx = xdt3_term - xdt2_term + 3 * dt * (points[2] - points[0]);
1287 x = points[0];
1288
1289 /* y coordinates. */
1290 ydt2_term = 3 * (points[5] - 2 * points[3] + points[1]);
1291 ydt3_term = points[7] + 3 * (-points[5] + points[3]) - points[1];
1292
1293 ydt2_term = dt2 * ydt2_term;
1294 ydt3_term = dt3 * ydt3_term;
1295
1296 dddy = 6 * ydt3_term;
1297 ddy = -6 * ydt3_term + 2 * ydt2_term;
1298 dy = ydt3_term - ydt2_term + dt * 3 * (points[3] - points[1]);
1299 y = points[1];
1300
1301 cache_point_buffer[2 * ii] = x;
1302 cache_point_buffer[2 * ii + 1] = y;
1303
1304 for (ii = 1; ii < num_segments; ii++) {
1305 ddx += dddx;
1306 dx += ddx;
1307 x += dx;
1308
1309 ddy += dddy;
1310 dy += ddy;
1311 y += dy;
1312
1313 cache_point_buffer[2 * ii] = x;
1314 cache_point_buffer[2 * ii + 1] = y;
1315 }
1316 calculate_ribbon(dest, tex_dest, stride, cache_point_buffer, 2 * sizeof(float), thickness, num_segments);
1317
1318 if (cache_point_buffer != cache_point_buffer_storage) {
1319 al_free(cache_point_buffer);
1320 }
1321 }
1322
1323 /* Function: al_calculate_spline
1324 */
1325 void al_calculate_spline(float* dest, int stride, const float points[8],
1326 float thickness, int num_segments)
1327 {
1328 calculate_spline(dest, NULL, stride, points, thickness, num_segments);
1329 }
1330
1331 /* Function: al_draw_spline
1332 */
1333 void al_draw_spline(const float points[8], ALLEGRO_COLOR color, float thickness)
1334 {
1335 int ii;
1336 float scale = get_scale();
1337 int num_segments = (int)(sqrtf(hypotf(points[2] - points[0], points[3] - points[1]) +
1338 hypotf(points[4] - points[2], points[5] - points[3]) +
1339 hypotf(points[6] - points[4], points[7] - points[5])) *
1340 1.2 * ALLEGRO_PRIM_QUALITY * scale / 10);
1341 LOCAL_VERTEX_CACHE;
1342
1343 if(num_segments < 2)
1344 num_segments = 2;
1345
1346 if (thickness > 0) {
1347 if (2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
1348 num_segments = (ALLEGRO_VERTEX_CACHE_SIZE - 1) / 2;
1349 }
1350
1351 calculate_spline(&vertex_cache[0].x, &vertex_cache[0].u, sizeof(ALLEGRO_VERTEX), points, thickness, num_segments);
1352
1353 for (ii = 0; ii < 2 * num_segments; ii++) {
1354 vertex_cache[ii].color = color;
1355 vertex_cache[ii].z = 0;
1356 }
1357
1358 al_draw_prim(vertex_cache, 0, 0, 0, 2 * num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP);
1359 } else {
1360 if (num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) {
1361 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1;
1362 }
1363
1364 calculate_spline(&vertex_cache[0].x, &vertex_cache[0].u, sizeof(ALLEGRO_VERTEX), points, thickness, num_segments);
1365
1366 for (ii = 0; ii < num_segments; ii++) {
1367 vertex_cache[ii].color = color;
1368 vertex_cache[ii].z = 0;
1369 }
1370
1371 al_draw_prim(vertex_cache, 0, 0, 0, num_segments, ALLEGRO_PRIM_LINE_STRIP);
1372 }
1373 }
1374
1375 /* Function: al_draw_ribbon
1376 */
1377 void al_draw_ribbon(const float *points, int points_stride, ALLEGRO_COLOR color,
1378 float thickness, int num_segments)
1379 {
1380 LOCAL_VERTEX_CACHE;
1381 int ii;
1382
1383 if (num_segments * (thickness > 0 ? 2 : 1) > ALLEGRO_VERTEX_CACHE_SIZE) {
1384 ALLEGRO_ERROR("Ribbon has too many segments.\n");
1385 return;
1386 }
1387
1388 calculate_ribbon(&vertex_cache[0].x, &vertex_cache[0].u, sizeof(ALLEGRO_VERTEX), points, points_stride, thickness, num_segments);
1389
1390 if (thickness > 0) {
1391 for (ii = 0; ii < 2 * num_segments; ii++) {
1392 vertex_cache[ii].color = color;
1393 vertex_cache[ii].z = 0;
1394 }
1395
1396 al_draw_prim(vertex_cache, 0, 0, 0, 2 * num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP);
1397 } else {
1398 for (ii = 0; ii < num_segments; ii++) {
1399 vertex_cache[ii].color = color;
1400 vertex_cache[ii].z = 0;
1401 }
1402
1403 al_draw_prim(vertex_cache, 0, 0, 0, num_segments, ALLEGRO_PRIM_LINE_STRIP);
1404 }
1405 }
1406
1407 /* vim: set sts=3 sw=3 et: */