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