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 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
|
/*
* PyMOL COLLADA export
*
* (c) 2014 Jared Sampson
* (c) 2014 Schrodinger, Inc.
*/
#include "os_predef.h"
#include "os_std.h"
#include "Feedback.h"
#include "Ray.h"
#ifdef _HAVE_LIBXML
#include <libxml/xmlwriter.h>
#include <libxml/encoding.h>
#include <time.h>
#include "Setting.h"
#include "Ortho.h"
#include "Color.h"
#include "Util.h"
#include "Version.h"
#include "MemoryDebug.h"
#include "Sphere.h"
#include "Scene.h"
#include "CGO.h"
#define XML_VERSION "1.0"
#define XML_ENCODING "UTF-8"
#define PRECISION 0.001f
#define TRANS_PRECISION 0.01f
/*
* Size of individual text nodes must be less than the maximum size for
* the LIBXML Parser node: 10MB. Text nodes longer than this will be split.
*/
#define XML_NODE_SIZE_LIMIT 1000000
/* For debugging output. */
#define COLLADA_DEBUG 0
enum {
COLLADA_GEOM_MODE_DEFAULT = 0,
COLLADA_GEOM_MODE_BLENDER = 1,
/* Total number of `collada_geometry_mode` settings implemented. */
COLLADA_GEOM_MODE_COUNT
};
/* Returns a standard XML-formatted timestamp */
static char *GetTimestamp(){
time_t now;
struct tm *local;
char buffer[20];
time(&now);
local = localtime(&now);
strftime(buffer, 20, "%Y-%m-%dT%H:%M:%S", local);
return strdup(buffer);
}
/*
* Returns index of the first float in an array matching the given value to
* the given precision.
*/
static int GetFloatPositionInArray(float val, float *arr, int len,
float precision)
{
int i;
for (i = 0; i < len; i++) {
if (precision > fabsf(val - arr[i])){
return i;
}
}
return -1; // not found
}
/* Returns 1 if any of the 4 VLA strings has gotten too big, else 0. */
static int ReachedXmlNodeSizeLimit(int pos_str_cc, int norm_str_cc,
int col_str_cc, int p_str_cc)
{
int r = 0;
if(pos_str_cc >= XML_NODE_SIZE_LIMIT)
r = 1;
if(norm_str_cc >= XML_NODE_SIZE_LIMIT)
r = 1;
if(col_str_cc >= XML_NODE_SIZE_LIMIT)
r = 1;
if(p_str_cc >= XML_NODE_SIZE_LIMIT)
r = 1;
return r;
}
/* Writes global <asset> element with file meta information. */
static void ColladaWriteAssetElement(xmlTextWriterPtr w, int geom_mode)
{
xmlTextWriterStartElement(w, BAD_CAST "asset");
xmlTextWriterStartElement(w, BAD_CAST "contributor");
xmlTextWriterWriteElement(w, BAD_CAST "author",
BAD_CAST "PyMOL User");
#ifdef _PyMOL_VERSION
char *vers = (char *)"PyMOL " _PyMOL_VERSION;
#else
char *vers = (char *)"PyMOL";
#endif
xmlTextWriterWriteElement(w, BAD_CAST "authoring_tool",
BAD_CAST vers);
xmlTextWriterEndElement(w); // contributor
char *ts = GetTimestamp();
xmlTextWriterWriteElement(w, BAD_CAST "created", BAD_CAST ts);
xmlTextWriterWriteElement(w, BAD_CAST "modified", BAD_CAST ts);
free(ts);
if (geom_mode != COLLADA_GEOM_MODE_BLENDER) {
xmlTextWriterStartElement(w, BAD_CAST "unit");
xmlTextWriterWriteAttribute(w, BAD_CAST "meter", BAD_CAST "1e-10");
xmlTextWriterWriteAttribute(w, BAD_CAST "name", BAD_CAST "Angstrom");
xmlTextWriterEndElement(w); // unit
}
xmlTextWriterWriteElement(w, BAD_CAST "up_axis", BAD_CAST "Y_UP");
xmlTextWriterEndElement(w); // asset
}
/* Writes the <library_cameras> element, including PyMOL's viewport camera. */
static void ColladaWriteLibraryCameras(xmlTextWriterPtr w, PyMOLGlobals *G, int width,
int height, float fov, float front, float back)
{
SceneViewType view;
SceneGetView(G, view);
float aspect_ratio = (float)width / (float)height;
int ortho = SettingGetGlobal_i(G, cSetting_ortho);
int ray_ortho = SettingGetGlobal_i(G, cSetting_ray_orthoscopic);
if (ray_ortho == -1) {
ray_ortho = ortho;
}
xmlTextWriterStartElement(w, BAD_CAST "library_cameras");
xmlTextWriterStartElement(w, BAD_CAST "camera");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "camera");
xmlTextWriterStartElement(w, BAD_CAST "optics");
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
if (ray_ortho) {
float ymag = SettingGetGlobal_i(G, cSetting_field_of_view) / 2;
float ymag_scale = -view[18] / 50; // scale fov based on camera distance
float ortho_adjust = 0.88f; // prevent apparent size change
ymag = ymag * ymag_scale * ortho_adjust;
xmlTextWriterStartElement(w, BAD_CAST "orthographic");
xmlTextWriterWriteFormatElement(w, BAD_CAST "ymag", "%6.4f", ymag);
} else {
xmlTextWriterStartElement(w, BAD_CAST "perspective");
xmlTextWriterWriteFormatElement(w, BAD_CAST "yfov", "%6.4f", fov);
}
xmlTextWriterWriteFormatElement(w, BAD_CAST "aspect_ratio",
"%6.4f", aspect_ratio);
xmlTextWriterWriteFormatElement(w, BAD_CAST "znear", "%6.4f", front);
xmlTextWriterWriteFormatElement(w, BAD_CAST "zfar", "%6.4f", back);
xmlTextWriterEndElement(w); // perspective or orthographic
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // optics
xmlTextWriterEndElement(w); // camera
xmlTextWriterEndElement(w); // library_cameras
}
/* Writes the <library_lights> element, including ambient and directional
* PyMOL lights. */
static void ColladaWriteLibraryLights(xmlTextWriterPtr w, CRay *I, PyMOLGlobals *G)
{
xmlTextWriterStartElement(w, BAD_CAST "library_lights");
/* Ambient light */
float ambient = SettingGetGlobal_f(I->G, cSetting_ambient);
if(ambient > 0.5){
ambient = 0.5;
}
/* RGB = "0.5 0.5 0.5" seems to work well; `ambient` setting is handled in
* default effect */
xmlTextWriterStartElement(w, BAD_CAST "light");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "ambient-light");
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
xmlTextWriterStartElement(w, BAD_CAST "ambient");
xmlTextWriterWriteElement(w, BAD_CAST "color", BAD_CAST "0.5 0.5 0.5");
xmlTextWriterEndElement(w); // directional
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // light
/* All other lights are directional, and their intensities are equal. Use
* instances of this light in the <node> section below. */
xmlTextWriterStartElement(w, BAD_CAST "light");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "pymol-light");
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
xmlTextWriterStartElement(w, BAD_CAST "directional");
/* Intensity decreases with increasing number of lights. */
float intensity = SceneGetSpecularValue(G, 0.6, 10);
char *value = (char *) malloc(sizeof(char) * 30);
sprintf(value, "%5.3f %5.3f %5.3f", intensity, intensity, intensity);
xmlTextWriterWriteElement(w, BAD_CAST "color", BAD_CAST value);
free(value);
xmlTextWriterEndElement(w); // directional
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // light
xmlTextWriterEndElement(w); // library_lights
}
/* Writes a <color>R G B</color> element. */
static void ColladaWriteCommonColorElement(xmlTextWriterPtr w, char *name,
char *sid, char *value)
{
xmlTextWriterStartElement(w, BAD_CAST name);
xmlTextWriterStartElement(w, BAD_CAST "color");
if(sid) {
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST sid);
}
else {
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST name);
}
xmlTextWriterWriteString(w, BAD_CAST value);
xmlTextWriterEndElement(w); // color
xmlTextWriterEndElement(w); // element
}
/* Writes a <float>value</float> element. */
static void ColladaWriteCommonFloatElement(xmlTextWriterPtr w, char *name,
char *sid, char *value)
{
xmlTextWriterStartElement(w, BAD_CAST name);
xmlTextWriterStartElement(w, BAD_CAST "float");
if(sid) {
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST sid);
}
else {
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST name);
}
xmlTextWriterWriteString(w, BAD_CAST value);
xmlTextWriterEndElement(w); // float
xmlTextWriterEndElement(w); // element
}
/* Writes a set of <phong> shader parameter elements. */
static void ColladaWritePhongEffect(xmlTextWriterPtr w, char *id,
float amb, float spec, float shin,
float trans, float iref)
{
char *value = (char *) malloc(100 * sizeof(char));
xmlTextWriterStartElement(w, BAD_CAST "effect");
xmlTextWriterWriteAttribute(w, BAD_CAST "id",
BAD_CAST id);
xmlTextWriterStartElement(w, BAD_CAST "profile_COMMON");
xmlTextWriterStartElement(w, BAD_CAST "technique");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid",
BAD_CAST "common");
xmlTextWriterStartElement(w, BAD_CAST "phong");
if (amb > PRECISION) {
sprintf(value, "0.5 0.5 0.5 %5.3f", amb);
ColladaWriteCommonColorElement(w, (char *)"ambient", NULL, value);
}
if (spec > PRECISION) {
sprintf(value, "0.5 0.5 0.5 %5.3f", spec);
ColladaWriteCommonColorElement(w, (char *)"specular", NULL, value);
}
if(shin > PRECISION) {
sprintf(value, "%5.3f", shin);
ColladaWriteCommonFloatElement(w, (char *)"shininess", NULL, value);
}
if(trans > PRECISION) {
sprintf(value, "%5.3f", trans);
ColladaWriteCommonFloatElement(w, (char *)"transparency", NULL, value);
}
if(iref > PRECISION) {
sprintf(value, "%5.3f", iref);
ColladaWriteCommonFloatElement(w, (char *)"index_of_refraction", NULL, value);
}
xmlTextWriterEndElement(w); // phong
xmlTextWriterEndElement(w); // technique
xmlTextWriterEndElement(w); // profile_COMMON
xmlTextWriterEndElement(w); // effect
free(value);
}
/* Writes a <material> element as an instance of a specific <effect> by URL. */
static void ColladaWriteInstanceEffectMaterial(xmlTextWriterPtr w,
char *id, char *url)
{
xmlTextWriterStartElement(w, BAD_CAST "material");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST id);
xmlTextWriterStartElement(w, BAD_CAST "instance_effect");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST url);
xmlTextWriterEndElement(w); // instance_effect
xmlTextWriterEndElement(w); // material
}
/* Opens a <geometry id="..."><mesh> element. */
static void ColladaBeginGeometryMesh(xmlTextWriterPtr w, int geom){
xmlTextWriterStartElement(w, BAD_CAST "geometry");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id", "geom%i", geom);
xmlTextWriterStartElement(w, BAD_CAST "mesh");
}
/* Closes a </mesh></geometry> element. */
static void ColladaEndGeometryMesh(xmlTextWriterPtr w){
xmlTextWriterEndElement(w); // mesh
xmlTextWriterEndElement(w); // geometry
}
/* Writes the <library_effects> element, including transparency values. */
static void ColladaWriteLibraryEffects(xmlTextWriterPtr w, PyMOLGlobals *G,
int trans_len, float *trans)
{
xmlTextWriterStartElement(w, BAD_CAST "library_effects");
float amb = SettingGetGlobal_f(G, cSetting_ambient);
if(amb > 0.5){
amb = 0.5;
}
float spec = SettingGetGlobal_f(G, cSetting_specular_intensity);
float shin_factor = 5.0f;
float shin = SettingGetGlobal_f(G, cSetting_shininess) / shin_factor;
/* Default effect */
ColladaWritePhongEffect(w, (char *)"default-effect", amb, spec, shin, 1, 1);
/* Background effect */
ColladaWritePhongEffect(w, (char *)"bg-effect", 0.5f, 0, 0, 0, 0);
/* Transparency effects */
int i;
char *name = (char *) malloc(100 * sizeof(char));
for (i = 0; i < trans_len; i++) {
sprintf(name, "transparency-%1.2f-effect", trans[i]);
ColladaWritePhongEffect(w, name, amb, spec, shin, 1 - trans[i], 1);
#if COLLADA_DEBUG
printf("Wrote Phong effect: %s\n", name);
#endif
}
xmlTextWriterEndElement(w); // library_effects
free(name);
}
/* Writes the <library_materials> element, including transparent materials. */
static void ColladaWriteLibraryMaterials(xmlTextWriterPtr w, int trans_len, float *trans)
{
xmlTextWriterStartElement(w, BAD_CAST "library_materials");
/* Default material */
ColladaWriteInstanceEffectMaterial(w, (char *)"default-material",
(char *)"#default-effect");
ColladaWriteInstanceEffectMaterial(w, (char *)"bg-material",
(char *)"#bg-effect");
/* Transparent materials */
int i;
char *name = (char *) malloc(100 * sizeof(char));
char *url = (char *) malloc(100 * sizeof(char));
for (i = 0; i < trans_len; i++) {
sprintf(name, "transparency-%1.2f-material", trans[i]);
sprintf(url, "#transparency-%1.2f-effect", trans[i]);
ColladaWriteInstanceEffectMaterial(w, name, url);
#if COLLADA_DEBUG
printf("Wrote material: %s for effect: %s\n", name, url);
#endif
}
xmlTextWriterEndElement(w); // library_materials
free(name);
free(url);
}
/*
* Writes a 3-dimensional <source> element, e.g. XYZ or RGB, to define
* position, normal, or color source data.
*
* Note: "count" should be the number of accessible elements (e.g. vertices,
* normals; NOT individual float values). The length of the float array will
* be 3x this value.
*/
static void ColladaWrite3DSource(xmlTextWriterPtr w, int geom, char *name, int count,
char *data_str, char *dim)
{
xmlTextWriterStartElement(w, BAD_CAST "source");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id",
"geom%i-mesh-%s", geom, name);
xmlTextWriterStartElement(w, BAD_CAST "float_array");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id",
"geom%i-mesh-%s-array", geom, name);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count", "%i", count * 3);
xmlTextWriterWriteString(w, BAD_CAST data_str);
xmlTextWriterEndElement(w); // float_array
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
xmlTextWriterStartElement(w, BAD_CAST "accessor");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "source",
"#geom%i-mesh-%s-array", geom, name);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count", "%i", count);
xmlTextWriterWriteAttribute(w, BAD_CAST "stride", BAD_CAST "3");
char n[2] = "\0";
sprintf(n, "%c", dim[0]);
xmlTextWriterStartElement(w, BAD_CAST "param");
xmlTextWriterWriteAttribute(w, BAD_CAST "name", BAD_CAST n);
xmlTextWriterWriteAttribute(w, BAD_CAST "type", BAD_CAST "float");
xmlTextWriterEndElement(w);
sprintf(n, "%c", dim[1]);
xmlTextWriterStartElement(w, BAD_CAST "param");
xmlTextWriterWriteAttribute(w, BAD_CAST "name", BAD_CAST n);
xmlTextWriterWriteAttribute(w, BAD_CAST "type", BAD_CAST "float");
xmlTextWriterEndElement(w);
sprintf(n, "%c", dim[2]);
xmlTextWriterStartElement(w, BAD_CAST "param");
xmlTextWriterWriteAttribute(w, BAD_CAST "name", BAD_CAST n);
xmlTextWriterWriteAttribute(w, BAD_CAST "type", BAD_CAST "float");
xmlTextWriterEndElement(w);
xmlTextWriterEndElement(w); // accessor
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // source
}
/* Write a <vertices> element using the current geometry's positions source. */
static void ColladaWriteVertices(xmlTextWriterPtr w, int geom)
{
xmlTextWriterStartElement(w, BAD_CAST "vertices");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id",
"geom%i-mesh-vertices", geom);
xmlTextWriterStartElement(w, BAD_CAST "input");
xmlTextWriterWriteAttribute(w, BAD_CAST "semantic",
BAD_CAST "POSITION");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "source",
"#geom%i-mesh-positions", geom);
xmlTextWriterEndElement(w); // input
xmlTextWriterEndElement(w); // vertices
}
/* Writes vertex, normal, and color <input> elements for the given geometry. */
static void ColladaWriteVNCInputs(xmlTextWriterPtr w, int geom)
{
xmlTextWriterStartElement(w, BAD_CAST "input");
xmlTextWriterWriteAttribute(w, BAD_CAST "offset", BAD_CAST "0");
xmlTextWriterWriteAttribute(w, BAD_CAST "semantic",
BAD_CAST "VERTEX");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "source",
"#geom%i-mesh-vertices", geom);
xmlTextWriterEndElement(w); // input
xmlTextWriterStartElement(w, BAD_CAST "input");
xmlTextWriterWriteAttribute(w, BAD_CAST "offset", BAD_CAST "1");
xmlTextWriterWriteAttribute(w, BAD_CAST "semantic",
BAD_CAST "NORMAL");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "source",
"#geom%i-mesh-normals", geom);
xmlTextWriterEndElement(w); // input
xmlTextWriterStartElement(w, BAD_CAST "input");
xmlTextWriterWriteAttribute(w, BAD_CAST "offset", BAD_CAST "2");
xmlTextWriterWriteAttribute(w, BAD_CAST "semantic",
BAD_CAST "COLOR");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "source",
"#geom%i-mesh-colors", geom);
xmlTextWriterEndElement(w); // input
}
/* Writes a <vcount> element with the given string as its value. */
static void ColladaWriteVCountElement(xmlTextWriterPtr w, char *vcount_str)
{
xmlTextWriterStartElement(w, BAD_CAST "vcount");
xmlTextWriterWriteFormatString(w, "%s", vcount_str);
xmlTextWriterEndElement(w);
}
/* Writes a <vcount> element for the given number of triangles. */
static void ColladaWriteTrianglesVCountElement(xmlTextWriterPtr w, int tri)
{
int i;
char *vc_str = VLACalloc(char, 1000);
ov_size cc = 0;
char *next = (char *) malloc(10 * sizeof(char));
for(i = 0; i < tri; i++){
sprintf(next, "3 "); // all triangles
UtilConcatVLA(&vc_str, &cc, next);
}
ColladaWriteVCountElement(w, vc_str);
VLAFree(vc_str);
free(next);
}
/* Writes a <p> element with the given string as its value. */
static void ColladaWritePrimitiveElement(xmlTextWriterPtr w, char *p_str)
{
xmlTextWriterStartElement(w, BAD_CAST "p");
xmlTextWriterWriteFormatString(w, "%s", p_str);
xmlTextWriterEndElement(w); // p
}
/* Writes a <triangles> element with the current primitive (<p>) string. */
static void ColladaWriteTrianglesElement(xmlTextWriterPtr w, int geom, int tri,
char *p_str, int mode = COLLADA_GEOM_MODE_DEFAULT)
{
xmlTextWriterStartElement(w,
BAD_CAST(mode == COLLADA_GEOM_MODE_BLENDER ? "polylist" : "triangles"));
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count", "%i", tri);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "material",
"geom%i-material", geom);
ColladaWriteVNCInputs(w, geom);
if (mode == COLLADA_GEOM_MODE_BLENDER) {
ColladaWriteTrianglesVCountElement(w, tri);
}
ColladaWritePrimitiveElement(w, p_str);
xmlTextWriterEndElement(w); // triangles
}
/* Writes a <polylist> element consisting entirely of triangles. */
static void ColladaWriteTrianglesPolylistElement(xmlTextWriterPtr w, int geom, int tri,
char *p_str)
{
ColladaWriteTrianglesElement(w, geom, tri, p_str, COLLADA_GEOM_MODE_BLENDER);
}
/*
* Opens a <polylist> element, including <input>s. Must be followed by at
* least one <p> element and then closed.
*/
static void ColladaBeginPolylistElement(xmlTextWriterPtr w, int geom, int count)
{
xmlTextWriterStartElement(w, BAD_CAST "polylist");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count", "%i", count);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "material",
"geom%i-material", geom);
ColladaWriteVNCInputs(w, geom);
}
/* Closes a <polylist> element. */
static void ColladaEndPolylistElement(xmlTextWriterPtr w)
{
xmlTextWriterEndElement(w);
}
/*
* Opens a <tristrips> element, including inputs. Must be followed by at
* least one <p> element. Multiple <p> elements can be used to have
* multiple triangle strips using the same vertices.
*/
static void ColladaBeginTristripsElement(xmlTextWriterPtr w, int geom, int num_strips)
{
xmlTextWriterStartElement(w, BAD_CAST "tristrips");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count",
"%i", num_strips);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "material",
"geom%i-material", geom);
ColladaWriteVNCInputs(w, geom);
}
/* Closes a <tristrips> element. */
static void ColladaEndTristripsElement(xmlTextWriterPtr w)
{
xmlTextWriterEndElement(w);
}
#if 0
/*
* Opens a <trifans> element, including inputs. Must be followed by at
* least one <p> element.
*/
static void ColladaBeginTrifansElement(xmlTextWriterPtr w, int geom, int num_fans)
{
xmlTextWriterStartElement(w, BAD_CAST "trifans");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "count",
"%i", num_fans);
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "material",
"geom%i-material", geom);
ColladaWriteVNCInputs(w, geom);
}
/* Closes a <trifans> element. */
static void ColladaEndTrifansElement(xmlTextWriterPtr w)
{
xmlTextWriterEndElement(w);
}
#endif
/* Writes a complete <geometry> element for a triangle mesh. */
static void ColladaWriteMeshGeometry(xmlTextWriterPtr w, int geom,
int pos, char* positions_str,
int norm, char* normals_str,
int col, char* colors_str,
int tri, char* p_str, int mode)
{
ColladaBeginGeometryMesh(w, geom);
ColladaWrite3DSource(w, geom, (char *)"positions", pos, positions_str,
(char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"normals", norm, normals_str,
(char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"colors", col, colors_str,
(char *)"RGB");
ColladaWriteVertices(w, geom);
ColladaWriteTrianglesElement(w, geom, tri, p_str, mode);
ColladaEndGeometryMesh(w);
}
#endif // _HAVE_LIBXML
/* Generates COLLADA output and appends it to `vla_ptr`. */
void RayRenderCOLLADA(CRay * I, int width, int height,
char **vla_ptr, float front, float back,
float fov)
{
#ifndef _HAVE_LIBXML
PRINTFB(I->G, FB_Ray, FB_Errors)
" ColladaRender-Error: No libxml2 support, can't output COLLADA file.\n"
ENDFB(I->G);
#elif !(defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED))
PRINTFB(I->G, FB_Ray, FB_Errors)
" ColladaRender-Error: libxml2 library not properly configured " \
"with writer and output support, can't output COLLADA file.\n"
ENDFB(I->G);
#else
char *vla = *vla_ptr;
ov_size cc = 0; /* character count */
PyMOLGlobals *G = I->G;
/*
* Setting: geometry_export_mode
* -----------------------------
* 0 = Output everything. (default)
* 1 = Output geometry and materials only; exclude lighting and camera
* information from scene.
*/
int identity = (SettingGetGlobal_i(I->G, cSetting_geometry_export_mode) == 1);
/*
* Setting: collada_background_box
* -------------------------------
* 0 = Do not include a background box. (default)
* 1 = Include the background box for more accurate reproduction of the scene.
* NB: This setting is overridden by geometry_export_mode == 1, in which case
* no background box will be generated.
*/
int bgbox = SettingGetGlobal_i(I->G, cSetting_collada_background_box);
/*
* Setting: collada_geometry_mode
* ------------------------------
* 0 = Valid COLLADA 1.4.1. (default)
* 1 = Blender-compatible (only <polylist> geometries are used).
*/
int geom_mode = SettingGetGlobal_i(I->G, cSetting_collada_geometry_mode);
if (geom_mode >= COLLADA_GEOM_MODE_COUNT || geom_mode < 0) {
geom_mode = COLLADA_GEOM_MODE_DEFAULT;
}
/*
* Setting: collada_export_lighting
* --------------------------------
* 0 = No lighting information included in output. Best for
* interactive scenes. (default)
* 1 = Lighting information (<library_lights> and light <node>s) included
* in output.
*/
int lighting = (SettingGetGlobal_i(I->G, cSetting_collada_export_lighting) == 1);
int lc = SettingGetGlobal_i(I->G, cSetting_light_count);
/* Ray trace */
RayExpandPrimitives(I);
RayTransformFirst(I, 0, identity);
RayComputeBox(I);
/* initialize for XML writing */
int rc; // return codes for error handling
xmlTextWriterPtr w;
xmlDocPtr doc;
xmlChar *tmp;
int buffersize;
/* Create a new XML DOM tree, to which the COLLADA document will be
* written */
doc = xmlNewDoc(BAD_CAST XML_VERSION);
if (doc == NULL) {
printf("ColladaRender: Error creating the xml document tree (xmlNewDoc).\n");
return;
}
/* Create a new XmlWriter */
w = xmlNewTextWriterTree(doc, NULL, 0);
if (w == NULL) {
printf("ColladaRender: Error creating the xml writer (xmlNewTextWriterTree).\n");
return;
}
/* Start the XML document */
rc = xmlTextWriterStartDocument(w, NULL, XML_ENCODING, NULL);
if (rc < 0) {
printf("ColladaRender: Error at xmlTextWriterStartDocument\n");
return;
}
/* Begin COLLADA */
xmlTextWriterStartElement(w, BAD_CAST "COLLADA");
xmlTextWriterWriteAttribute(w, BAD_CAST "xmlns",
BAD_CAST "http://www.collada.org/2005/11/COLLADASchema");
xmlTextWriterWriteAttribute(w, BAD_CAST "version", BAD_CAST "1.4.1");
/* Asset */
ColladaWriteAssetElement(w, geom_mode);
/* Cameras */
if (!identity) {
ColladaWriteLibraryCameras(w, G, width, height, fov, front, back);
}
/* Lights */
if (!identity && lighting) {
ColladaWriteLibraryLights(w, I, G);
}
/* Geometries */
int geom = 0;
/*
* Track transparency levels used on a per-geom basis to be added to
* additional library_effects and library_materials elements.
*/
float *trans = (float *) malloc(sizeof(float)); // store transparency values
int trans_len = 0;
/* Associate geometries with transparency values. */
int *geom_trans = (int *) malloc(sizeof(int));
int geom_trans_len = 1;
{
xmlTextWriterStartElement(w, BAD_CAST "library_geometries");
int a, tri, pos, norm, col;
CPrimitive *prim;
int mesh_obj = false;
int largest_dim = 10;
float cur_trans = 0.0f;
/* Initialize data string VLAs and character counts. */
char *positions_str = VLACalloc(char, 1000);
char *normals_str = VLACalloc(char, 1000);
char *colors_str = VLACalloc(char, 1000);
char *p_str = VLACalloc(char, 1000);
ov_size pos_str_cc = 0;
ov_size norm_str_cc = 0;
ov_size col_str_cc = 0;
ov_size p_str_cc = 0;
/*
* Loop through primitives, plus one extra time to finish writing a
* triangle mesh if necessary.
*/
for (a = 0; a <= I->NPrimitive; a++) {
prim = I->Primitive + a;
/* Handle transitions between/after triangle meshes. */
if (mesh_obj) {
if (a == I->NPrimitive ||
prim->type != cPrimTriangle ||
TRANS_PRECISION <= fabsf(prim->trans - cur_trans))
{
/*
* Not a triangle primitive, but the previous mesh is still
* active; OR, transparency level is different; OR the final
* primitive was part of a triangle mesh that needs to be
* closed.
*
* Write the previous triangle mesh from its data strings
* and counters, reset mesh tracking variables.
*/
ColladaWriteMeshGeometry(w, geom, pos, positions_str,
norm, normals_str, col, colors_str, tri, p_str, geom_mode);
geom += 1;
mesh_obj = false;
}
}
if(!mesh_obj) {
/* First triangle primitive or any other primititve. */
/* Allocate data strings */
pos_str_cc = 0;
norm_str_cc = 0;
col_str_cc = 0;
p_str_cc = 0;
if(a < I->NPrimitive){
/* Reset counters */
tri = 0;
pos = 0;
norm = 0;
col = 0;
cur_trans = prim->trans;
/* Increase geom_trans with geom, realloc exponentially. */
while (geom >= geom_trans_len) {
geom_trans_len *= 2;
geom_trans = (int *) realloc(geom_trans,
(geom_trans_len) * sizeof(int));
#if COLLADA_DEBUG
printf(" increased geom_trans_len to %i\n", geom_trans_len);
#endif
}
/* Record transparency for each geometry. */
int p = GetFloatPositionInArray(cur_trans, trans,
trans_len, TRANS_PRECISION);
if (0 > p) {
/* Not found, add new transparency level. */
trans = (float *) realloc(trans, (trans_len + 1) * sizeof(float));
trans[trans_len] = cur_trans;
#if COLLADA_DEBUG
printf(" Added new trans[%i] = %1.2f\n", trans_len,
trans[trans_len]);
#endif
geom_trans[geom] = trans_len;
trans_len++;
} else {
/* Reference existing transparency level. */
geom_trans[geom] = p;
}
#if COLLADA_DEBUG
printf("geom_trans[%i] = %i\n", geom, geom_trans[geom]);
#endif
if(prim->type == cPrimTriangle){
/* Track the open triangle mesh. */
mesh_obj = true;
}
}
else {
/*
* No active mesh object, and the last primitive has already been
* processed.
*/
/* Generate bounding box geometry for background. */
/* Only if collada_background_box == 1 and geometry_export_mode == 0 */
if (!identity && bgbox) {
/* Ensure camera is inside bounding box */
int i;
for (i = 0; i < 3; i++) {
if (fabsf(I->Pos[i]) > largest_dim) {
largest_dim = fabsf(I->Pos[i]);
}
}
/* Leave plenty of room */
largest_dim *= 100;
/* Allocate data strings */
pos_str_cc = 0;
norm_str_cc = 0;
col_str_cc = 0;
p_str_cc = 0;
int cube_coords[24] = {
1, 1, 1,
1, -1, 1,
-1, -1, 1,
-1, 1, 1,
-1, 1, -1,
-1, -1, -1,
1, -1, -1,
1, 1, -1 };
int cube_face_verts[24] = {
0, 1, 2, 3,
3, 2, 5, 4,
4, 5, 6, 7,
7, 6, 1, 0,
0, 3, 4, 7,
1, 6, 5, 2 };
char *next = (char *) malloc(200 * sizeof(int));
/* positions */
for(i = 0; i < 24; i++){
sprintf(next, "%i ", cube_coords[i] * largest_dim);
UtilConcatVLA(&positions_str, &pos_str_cc, next);
}
/* normals */
for(i = 0; i < 24; i++){
sprintf(next, "%i ", -cube_coords[i]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
}
/* color */
const float *bg_color;
bg_color = ColorGet(G, SettingGet_color(G, NULL, NULL, cSetting_bg_rgb));
sprintf(next, "%6.4f %6.4f %6.4f", bg_color[0], bg_color[1], bg_color[2]);
UtilConcatVLA(&colors_str, &col_str_cc, next);
/* p */
for(i = 0; i < 6; i++){
sprintf(next, "%i %i 0 %i %i 0 %i %i 0 %i %i 0 ",
cube_face_verts[4 * i], cube_face_verts[4 * i],
cube_face_verts[4 * i + 1], cube_face_verts[4 * i + 1],
cube_face_verts[4 * i + 2], cube_face_verts[4 * i + 2],
cube_face_verts[4 * i + 3], cube_face_verts[4 * i + 3]);
UtilConcatVLA(&p_str, &p_str_cc, next);
}
xmlTextWriterStartElement(w, BAD_CAST "geometry");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "geom-bg");
xmlTextWriterStartElement(w, BAD_CAST "mesh");
ColladaWrite3DSource(w, geom, (char *)"positions", 8,
positions_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"normals", 8,
normals_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"colors", 1,
colors_str, (char *)"RGB");
ColladaWriteVertices(w, geom);
xmlTextWriterStartElement(w, BAD_CAST "polylist");
xmlTextWriterWriteAttribute(w, BAD_CAST "count", BAD_CAST "6");
xmlTextWriterWriteAttribute(w, BAD_CAST "material", BAD_CAST "geom-bg-material");
ColladaWriteVNCInputs(w, geom);
xmlTextWriterWriteElement(w, BAD_CAST "vcount", BAD_CAST "4 4 4 4 4 4");
ColladaWritePrimitiveElement(w, p_str);
xmlTextWriterEndElement(w); // polylist
xmlTextWriterEndElement(w); // mesh
xmlTextWriterEndElement(w); // geometry
free(next);
}
/* Finished with geometries. */
break;
}
}
/* Update largest dimension - assumes distance from origin for most
* objects will be much greater than e.g. sphere/cylinder/cone radius
* and camera won't be "too far" away. */
{
int i;
for(i = 0; i < 3; i++) {
switch (prim->type) {
/* 3 vertices defined */
case cPrimTriangle:
if (largest_dim < prim->v3[i]) {
largest_dim = prim->v3[i];
}
/* 2 vertices defined */
case cPrimCone:
case cPrimCylinder:
if (largest_dim < prim->v2[i]) {
largest_dim = prim->v2[i];
}
/* only 1 vertex defined */
case cPrimSphere:
default:
if (largest_dim < prim->v1[i]) {
largest_dim = prim->v1[i];
}
break;
}
}
}
/* Process the primitive according to its type. */
switch(prim->type){
case cPrimSphere: // 1
{
#if COLLADA_DEBUG > 1
printf("Primitive %i: cPrimSphere\n", a);
#endif
/* sphere_quality range: 0 to (NUMBER_OF_SPHERE_LEVELS - 1). */
int sq = (SettingGetGlobal_i(I->G, cSetting_sphere_quality));
SphereRec *sp;
int i, j;
int *q, *s;
char *next = (char *) malloc(200 * sizeof(char)); // enough for 9 %6.4 floats
sp = I->G->Sphere->Sphere[sq];
q = sp->Sequence;
s = sp->StripLen;
/* Generate <source> elements */
/* For each tristrip, add positions and normals to their respective
* data strings. */
for(i = 0; i < sp->NStrip; i++) {
#if COLLADA_DEBUG > 1
printf("\ni = %i, StripLen = %i\n", i, *s);
#endif
for(j = 0; j < (*s); j++) {
/* positions */
sprintf(next, "%6.4f %6.4f %6.4f ",
prim->v1[0] + (prim->r1 * sp->dot[*q][0]),
prim->v1[1] + (prim->r1 * sp->dot[*q][1]),
prim->v1[2] + (prim->r1 * sp->dot[*q][2]));
#if COLLADA_DEBUG > 1
printf("positions j = %02i: %s\n", j, next);
#endif
UtilConcatVLA(&positions_str, &pos_str_cc, next);
/* normals */
sprintf(next, "%6.4f %6.4f %6.4f ",
sp->dot[*q][0], sp->dot[*q][1], sp->dot[*q][2]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
q++; // next position in sequence
}
s++; // next strip
}
/* Colors: only one color per sphere. */
sprintf(next, "%6.4f %6.4f %6.4f",
prim->c1[0], prim->c1[1], prim->c1[2]);
UtilConcatVLA(&colors_str, &col_str_cc, next);
ColladaBeginGeometryMesh(w, geom);
ColladaWrite3DSource(w, geom, (char *)"positions", sp->NVertTot,
positions_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"normals", sp->NVertTot,
normals_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"colors", 1,
colors_str, (char *)"RGB");
ColladaWriteVertices(w, geom);
/* Reset to the beginning of the list of strips. */
q = sp->Sequence;
s = sp->StripLen;
int vert_ct;
vert_ct = 0;
if (geom_mode == COLLADA_GEOM_MODE_BLENDER) {
/* polylists */
int tri = 0;
for (i = 0; i < sp->NStrip; i++) {
/* Each triangle has the last 2 vertices of previous one as its
* first 2 vertices. Color is the same for all vertices. */
for(j = 2; j < (*s); j++) {
if (j % 2 == 0) {
/* even, normals correct as-is */
sprintf(next, "%i %i 0 %i %i 0 %i %i 0 ",
vert_ct + j - 2, vert_ct + j - 2,
vert_ct + j - 1, vert_ct + j - 1,
vert_ct + j, vert_ct + j);
} else {
/* odd, switch order to maintain correct normal */
sprintf(next, "%i %i 0 %i %i 0 %i %i 0 ",
vert_ct + j - 1, vert_ct + j - 1,
vert_ct + j - 2, vert_ct + j - 2,
vert_ct + j, vert_ct + j);
}
UtilConcatVLA(&p_str, &p_str_cc, next);
q++;
tri++;
}
ColladaWriteTrianglesPolylistElement(w, geom, tri, p_str);
vert_ct += (*s);
s++;
/* Get a fresh p_str for the next strip. */
VLAFree(p_str);
p_str = VLACalloc(char, 1000);
p_str_cc = 0;
tri = 0;
}
} else {
/* tristrips */
ColladaBeginTristripsElement(w, geom, sp->NStrip);
/* For each strip, write a <p> element. */
for(i = 0; i < sp->NStrip; i++) {
/* Write the 2 initial vertices. */
sprintf(next, "%i %i 0 %i %i 0 ",
vert_ct, vert_ct, vert_ct + 1, vert_ct + 1);
UtilConcatVLA(&p_str, &p_str_cc, next);
/* After the first 2, each new vertex adds a new triangle.
* Color is the same for all vertices. */
for(j = 2; j < (*s); j++) {
sprintf(next, "%i %i 0 ", vert_ct + j, vert_ct + j);
UtilConcatVLA(&p_str, &p_str_cc, next);
q++;
}
ColladaWritePrimitiveElement(w, p_str);
/* Start the next strip at the next vertex. */
vert_ct += *s;
s++;
/* Get a fresh p_str for the next strip. */
VLAFree(p_str);
p_str = VLACalloc(char, 1000);
p_str_cc = 0;
}
ColladaEndTristripsElement(w);
}
free(next);
ColladaEndGeometryMesh(w);
geom += 1;
break;
} // cPrimSphere
case cPrimCylinder: // 2
case cPrimSausage: // 4
case cPrimCone: // 7
{
#if COLLADA_DEBUG > 1
if(prim->type == cPrimCylinder) {
printf("Primitive %i: cPrimCylinder\n", a);
} else if(prim->type == cPrimSausage) {
printf("Primitive %i: cPrimSausage\n", a);
} else {
printf("Primitive %i: cPrimCone\n", a);
}
#endif
#define DAE_MAX_EDGE 50
/* Local vars */
float d[3], t[3], p0[3], p1[3], p2[3];
float v_buf[9], *v, vv1[3], vv2[3], vvv1[3], vvv2[3];
float x[DAE_MAX_EDGE + 1], y[DAE_MAX_EDGE + 1];
float overlap, overlap2, nub, nub2, r2 = 0.F;
int nEdge, c; //colorFlag;
int i;
char *next = (char *) malloc(200 * sizeof(char));
char *cap1_p_str = VLACalloc(char, 1000);
ov_size cap1_cc = 0;
bool stick_round_nub = SettingGetGlobal_i(I->G, cSetting_stick_round_nub);
const int j_arr[] = {2, 3, 2};
int nCapTri = 0;
cCylCap captype[2] = {prim->cap1, prim->cap2};
CGO *cgocap[2] = {NULL, NULL};
if(prim->type == cPrimSausage) {
captype[0] = captype[1] = cCylCapRound;
}
v = v_buf;
nEdge = SettingGetGlobal_i(I->G, cSetting_stick_quality);
overlap = prim->r1 * SettingGetGlobal_f(I->G, cSetting_stick_overlap);
nub = prim->r1 * SettingGetGlobal_f(I->G, cSetting_stick_nub);
if(prim->type == cPrimCone) {
r2 = prim->r2;
overlap2 = prim->r2 * SettingGetGlobal_f(I->G, cSetting_stick_overlap);
nub2 = prim->r2 * SettingGetGlobal_f(I->G, cSetting_stick_nub);
}
else {
r2 = prim->r1;
overlap2 = overlap;
nub2 = nub;
}
if(nEdge > DAE_MAX_EDGE)
nEdge = DAE_MAX_EDGE;
subdivide(nEdge, x, y);
//colorFlag = (prim->c1 != prim->c2) && prim->c2;
/* primary axis vector p0 */
p0[0] = (prim->v2[0] - prim->v1[0]);
p0[1] = (prim->v2[1] - prim->v1[1]);
p0[2] = (prim->v2[2] - prim->v1[2]);
normalize3f(p0);
/* cap1 */
copy3f(prim->v1, vv1);
copy3f(vv1, vvv1);
/* cap2 */
copy3f(prim->v2, vv2);
copy3f(vv2, vvv2);
d[0] = (vv2[0] - vv1[0]);
d[1] = (vv2[1] - vv1[1]);
d[2] = (vv2[2] - vv1[2]);
get_divergent3f(d, t);
/* orthogonal vectors p1, p2 */
cross_product3f(d, t, p1);
normalize3f(p1);
cross_product3f(d, p1, p2);
normalize3f(p2);
/* now we have a coordinate system */
/* cap1 */
if(captype[0] == cCylCapRound) {
if (stick_round_nub) {
cgocap[0] = CGONew(I->G);
CGORoundNub(cgocap[0], prim->v1, p0, p1, p2, -1, nEdge, prim->r1);
} else {
for(i = 0; i < 3; i++) {
vv1[i] -= p0[i] * overlap;
vvv1[i] = vv1[i] - p0[i] * nub;
}
}
}
/* cap2 */
if(captype[1] == cCylCapRound) {
if (stick_round_nub) {
cgocap[1] = CGONew(I->G);
CGORoundNub(cgocap[1], prim->v2, p0, p1, p2, 1, nEdge, prim->r1);
} else {
for(i = 0; i < 3; i++) {
vv2[i] += p0[i] * overlap2;
vvv2[i] = vv2[i] + p0[i] * nub2;
}
}
}
/* colors */
sprintf(next, "%6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ",
prim->c1[0], prim->c1[1], prim->c1[2],
prim->c2[0], prim->c2[1], prim->c2[2]);
UtilConcatVLA(&colors_str, &col_str_cc, next);
/* Generate the data strings */
char *vcount_str = VLACalloc(char, 100);
ov_size vc_cc = 0;
{
/* Write values for each edge. The first edge is also the last
* edge. */
for(c = nEdge; c >= 0; c--) {
/* vector only, not positioned yet */
v[0] = p1[0] * x[c] + p2[0] * y[c];
v[1] = p1[1] * x[c] + p2[1] * y[c];
v[2] = p1[2] * x[c] + p2[2] * y[c];
/* vertices */
v[3] = vv1[0] + v[0] * prim->r1;
v[4] = vv1[1] + v[1] * prim->r1;
v[5] = vv1[2] + v[2] * prim->r1;
v[6] = vv2[0] + v[0] * r2;
v[7] = vv2[1] + v[1] * r2;
v[8] = vv2[2] + v[2] * r2;
/* positions */
sprintf(next, "%6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ",
v[3], v[4], v[5], v[6], v[7], v[8]);
UtilConcatVLA(&positions_str, &pos_str_cc, next);
/* normals: Both vertices have the same normal. */
sprintf(next, "%6.4f %6.4f %6.4f ",
v[0], v[1], v[2]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
if (c > 0) {
/* vcount */
sprintf(next, "4 ");
UtilConcatVLA(&vcount_str, &vc_cc, next);
/* p for polylist */
sprintf(next, "%i %i %i %i %i %i %i %i %i %i %i %i ",
pos, norm, col,
pos + 1, norm, col + 1,
pos + 3, norm + 1, col,
pos + 2, norm + 1, col + 1);
UtilConcatVLA(&p_str, &p_str_cc, next);
}
pos += 2;
norm += 1;
}
/* Caps */
{
/* add another vertex-normal-color set for the center of each
* cap if r > 0 */
#define CONE_MIN_RADIUS 10e-6f
if(prim->r1 > CONE_MIN_RADIUS && !cgocap[0] && captype[0] != cCylCapNone){
/* positions */
sprintf(next, "%6.4f %6.4f %6.4f ",
vvv1[0], vvv1[1], vvv1[2]);
UtilConcatVLA(&positions_str, &pos_str_cc, next);
/* normals */
sprintf(next, "%6.4f %6.4f %6.4f ",
-p0[0], -p0[1], -p0[2]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
/* p */
for(i = 0; i < nEdge; i++) {
sprintf(next, "%i %i %i %i %i %i %i %i %i ",
pos, norm, col,
2*i, i, col,
2*i+2, i+1, col);
UtilConcatVLA(&cap1_p_str, &cap1_cc, next);
++nCapTri;
}
++pos;
++norm;
}
if(r2 > CONE_MIN_RADIUS && !cgocap[1] && captype[1] != cCylCapNone){
/* positions */
sprintf(next, "%6.4f %6.4f %6.4f ",
vvv2[0], vvv2[1], vvv2[2]);
UtilConcatVLA(&positions_str, &pos_str_cc, next);
/* normals */
sprintf(next, "%6.4f %6.4f %6.4f ",
p0[0], p0[1], p0[2]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
/* p */
for(i = 0; i < nEdge; i++) {
/* reverse order for other end */
sprintf(next, "%i %i %i %i %i %i %i %i %i ",
pos, norm, col + 1,
2*i+3, i+1, col + 1,
2*i+1, i, col + 1);
UtilConcatVLA(&cap1_p_str, &cap1_cc, next);
++nCapTri;
}
++pos;
++norm;
}
}
#if COLLADA_DEBUG > 1
printf("positions: %s\n", positions_str);
printf("normals: %s\n", normals_str);
printf("colors: %s\n", colors_str);
printf("p: %s\n", p_str);
#endif
for (i = 0; i < 2; ++i) {
if (!cgocap[i])
continue;
int pos0 = -1;
for (auto it = cgocap[i]->begin(); !it.is_stop(); ++it) {
auto pc = it.data();
int op = it.op_code();
switch (op){
case CGO_BEGIN:
pos0 = pos;
break;
case CGO_NORMAL:
/* normals */
sprintf(next, "%6.4f %6.4f %6.4f ", pc[0], pc[1], pc[2]);
UtilConcatVLA(&normals_str, &norm_str_cc, next);
++norm;
break;
case CGO_VERTEX:
/* positions */
sprintf(next, "%6.4f %6.4f %6.4f ", pc[0], pc[1], pc[2]);
UtilConcatVLA(&positions_str, &pos_str_cc, next);
++pos;
/* unroll the triangle strip */
if (pos > pos0 + 2) {
const int * j = j_arr + ((pos - pos0) % 2);
sprintf(next, "%i %i %i %i %i %i %i %i %i ",
pos - j[0], norm - j[0], col + i,
pos - j[1], norm - j[1], col + i,
pos - 1, norm - 1, col + i);
UtilConcatVLA(&cap1_p_str, &cap1_cc, next);
++nCapTri;
}
break;
}
}
CGOFree(cgocap[i]);
}
}
col += 2;
ColladaBeginGeometryMesh(w, geom);
ColladaWrite3DSource(w, geom, (char *)"positions", pos,
positions_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"normals", norm,
normals_str, (char *)"XYZ");
ColladaWrite3DSource(w, geom, (char *)"colors", col,
colors_str, (char *)"RGB");
ColladaWriteVertices(w, geom);
/* polylist for cylinder shaft */
ColladaBeginPolylistElement(w, geom, nEdge);
ColladaWriteVCountElement(w, vcount_str);
ColladaWritePrimitiveElement(w, p_str);
ColladaEndPolylistElement(w);
if (nCapTri) {
ColladaWriteTrianglesElement(w, geom, nCapTri, cap1_p_str, geom_mode);
}
ColladaEndGeometryMesh(w);
geom += 1;
VLAFree(vcount_str);
VLAFree(cap1_p_str);
free(next);
break;
} // cPrimCylinder
case cPrimTriangle: // 3
{
#if COLLADA_DEBUG > 1
printf("Primitive %i: cPrimTriangle\n", a);
#endif
char *next = (char *) malloc(200 * sizeof(char)); // enough for 9 color floats
/*** Positions ***/
sprintf(next, "%6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ",
prim->v1[0], prim->v1[1], prim->v1[2],
prim->v2[0], prim->v2[1], prim->v2[2],
prim->v3[0], prim->v3[1], prim->v3[2]);
UtilConcatVLA(&positions_str, &pos_str_cc, (char *)next);
/*** Normals ***/
/* prim->n0 is a face normal; prim->n1/2/3 are vertex normals. */
sprintf(next, "%6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ",
prim->n1[0], prim->n1[1], prim->n1[2],
prim->n2[0], prim->n2[1], prim->n2[2],
prim->n3[0], prim->n3[1], prim->n3[2]);
UtilConcatVLA(&normals_str, &norm_str_cc, (char *)next);
/* Colors */
/* R, G, B per vertex */
sprintf(next, "%6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ",
prim->c1[0], prim->c1[1], prim->c1[2], // vertex 1
prim->c2[0], prim->c2[1], prim->c2[2], // vertex 2
prim->c3[0], prim->c3[1], prim->c3[2]); // vertex 3
UtilConcatVLA(&colors_str, &col_str_cc, next);
/* <p> indices */
if (TriangleReverse(prim)) {
sprintf(next, "%i %i %i %i %i %i %i %i %i ",
pos, norm, col,
pos + 2, norm + 2, col + 2,
pos + 1, norm + 1, col + 1);
} else {
sprintf(next, "%i %i %i %i %i %i %i %i %i ",
pos, norm, col,
pos + 1, norm + 1, col + 1,
pos + 2, norm + 2, col + 2);
}
UtilConcatVLA(&p_str, &p_str_cc, next);
pos += 3;
norm += 3;
col += 3;
tri += 1;
free(next);
if (ReachedXmlNodeSizeLimit(pos_str_cc, norm_str_cc,
col_str_cc, p_str_cc)) {
/* xmlTextWriter uses the LibXML parser module, which enforces a node size
* limit (10MB as of June 2014). Split oversize nodes into multiple nodes. */
#if COLLADA_DEBUG
printf("Reached XML_NODE_SIZE_LIMIT (%i) at a=%i, geom=%i\n",
XML_NODE_SIZE_LIMIT, a, geom);
#endif
/* Write the current mesh object and clean up */
ColladaWriteMeshGeometry(w, geom, pos, positions_str,
norm, normals_str, col, colors_str, tri, p_str, geom_mode);
geom += 1;
mesh_obj = false;
}
break;
} // cPrimTriangle
/* Character and Ellipsoid not implemented */
case cPrimCharacter: // 5
{
#if COLLADA_DEBUG > 1
printf("Primitive %i: cPrimCharacter\n", a);
#endif
break;
} // cPrimCharacter
case cPrimEllipsoid: // 6
{
#if COLLADA_DEBUG > 1
printf("Primitive %i: cPrimEllipsoid\n", a);
#endif
break;
} // cPrimEllipsoid
} // switch
} // for
xmlTextWriterEndElement(w); // library_geometries
VLAFree(positions_str);
VLAFree(normals_str);
VLAFree(colors_str);
VLAFree(p_str);
}
/* Effects */
ColladaWriteLibraryEffects(w, G, trans_len, trans);
/* Materials */
ColladaWriteLibraryMaterials(w, trans_len, trans);
/* Visual Scenes */
{
xmlTextWriterStartElement(w, BAD_CAST "library_visual_scenes");
// TODO: support for multiple scenes (e.g. stored scenes)
xmlTextWriterStartElement(w, BAD_CAST "visual_scene");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "scene");
xmlTextWriterWriteAttribute(w, BAD_CAST "name", BAD_CAST "scene");
/* One node per geometry element. */
int i, j;
char *mat = (char *) malloc(100 * sizeof(char));
for(i = 0; i < geom; i++){
xmlTextWriterStartElement(w, BAD_CAST "node");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id", "node-geom%i", i);
/* set center screen pixel as origin */
float offset[3];
SceneOriginGet(G, offset);
invert3f(offset);
for (j = 0; j < 2; j++) {
/* camera XY offset (Z handled in camera node) */
offset[j] += I->Pos[j];
}
char *tmp = (char *) malloc(50 * sizeof(char));
sprintf(tmp, "%5.3f %5.3f %5.3f", offset[0], offset[1], offset[2]);
xmlTextWriterStartElement(w, BAD_CAST "translate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "location");
xmlTextWriterWriteString(w, BAD_CAST tmp);
xmlTextWriterEndElement(w); // translate
free(tmp);
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationZ");
xmlTextWriterWriteString(w, BAD_CAST "0 0 1 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationY");
xmlTextWriterWriteString(w, BAD_CAST "0 1 0 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationX");
xmlTextWriterWriteString(w, BAD_CAST "1 0 0 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "scale");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "scale");
xmlTextWriterWriteString(w, BAD_CAST "1 1 1");
xmlTextWriterEndElement(w); // scale
xmlTextWriterStartElement(w, BAD_CAST "instance_geometry");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "url", "#geom%i", i);
xmlTextWriterStartElement(w, BAD_CAST "bind_material");
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
xmlTextWriterStartElement(w, BAD_CAST "instance_material");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "symbol",
"geom%i-material", i);
if (TRANS_PRECISION > geom_trans[i]) {
sprintf(mat, "#default-material");
}
else {
sprintf(mat, "#transparency-%1.2f-material", trans[geom_trans[i]]);
}
xmlTextWriterWriteAttribute(w, BAD_CAST "target", BAD_CAST mat);
xmlTextWriterEndElement(w); // instance_material
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // bind_material
xmlTextWriterEndElement(w); // instance_geometry
xmlTextWriterEndElement(w); // node
}
free(mat);
/* background geometry */
if (!identity && bgbox) {
xmlTextWriterStartElement(w, BAD_CAST "node");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "node-geom-bg");
xmlTextWriterStartElement(w, BAD_CAST "translate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "location");
xmlTextWriterWriteString(w, BAD_CAST "0 0 0");
xmlTextWriterEndElement(w); // translate
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationZ");
xmlTextWriterWriteString(w, BAD_CAST "0 0 1 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationY");
xmlTextWriterWriteString(w, BAD_CAST "0 1 0 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "rotate");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "rotationX");
xmlTextWriterWriteString(w, BAD_CAST "1 0 0 0");
xmlTextWriterEndElement(w); // rotate
xmlTextWriterStartElement(w, BAD_CAST "scale");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "scale");
xmlTextWriterWriteString(w, BAD_CAST "1 1 1");
xmlTextWriterEndElement(w); // scale
xmlTextWriterStartElement(w, BAD_CAST "instance_geometry");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST "#geom-bg");
xmlTextWriterStartElement(w, BAD_CAST "bind_material");
xmlTextWriterStartElement(w, BAD_CAST "technique_common");
xmlTextWriterStartElement(w, BAD_CAST "instance_material");
xmlTextWriterWriteAttribute(w, BAD_CAST "symbol",
BAD_CAST "geom-bg-material");
xmlTextWriterWriteAttribute(w, BAD_CAST "target",
BAD_CAST "#bg-material"); // TODO: custom materials
xmlTextWriterEndElement(w); // instance_material
xmlTextWriterEndElement(w); // technique_common
xmlTextWriterEndElement(w); // bind_material
xmlTextWriterEndElement(w); // instance_geometry
xmlTextWriterEndElement(w); // node
}
/* Camera node */
char *matrix = (char *) malloc(400 * sizeof(char)); // enough for 16 floats
if (!identity) {
float m1[16];
// m1 = RotMatrix * T(0, 0, -Pos[z])
identity44f(m1);
// camera z
m1[11] = -I->Pos[2];
// RotMatrix
left_multiply44f44f(SceneGetMatrix(G), m1);
sprintf(matrix,
"\n%5.3f %5.3f %5.3f %5.3f "
"\n%5.3f %5.3f %5.3f %5.3f "
"\n%5.3f %5.3f %5.3f %5.3f "
"\n%5.3f %5.3f %5.3f %5.3f ",
m1[0], m1[1], m1[2], m1[3],
m1[4], m1[5], m1[6], m1[7],
m1[8], m1[9], m1[10], m1[11],
m1[12], m1[13], m1[14], m1[15]);
xmlTextWriterStartElement(w, BAD_CAST "node");
xmlTextWriterWriteAttribute(w, BAD_CAST "id", BAD_CAST "node-camera");
xmlTextWriterWriteElement(w, BAD_CAST "matrix", BAD_CAST matrix);
xmlTextWriterStartElement(w, BAD_CAST "scale");
xmlTextWriterWriteAttribute(w, BAD_CAST "sid", BAD_CAST "scale");
xmlTextWriterWriteString(w, BAD_CAST "1 1 1");
xmlTextWriterEndElement(w); // scale
xmlTextWriterStartElement(w, BAD_CAST "instance_camera");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST "#camera");
xmlTextWriterEndElement(w); // instance_camera
xmlTextWriterEndElement(w); // node
}
/* Light nodes */
if (!identity && lighting) {
if(lc > 0){
/* First light is the ambient light. For light_count of 2-10,
* we add a directional light, e.g. light, light2, etc. */
int i;
xmlTextWriterStartElement(w, BAD_CAST "node");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id", "node-ambient-light");
xmlTextWriterWriteAttribute(w, BAD_CAST "type", BAD_CAST "NODE");
xmlTextWriterStartElement(w, BAD_CAST "instance_light");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST "#ambient-light");
xmlTextWriterEndElement(w); // instance_light
xmlTextWriterEndElement(w); // node
const float *light_pos_ptr;
float light_pos[3];
for(i = 1; i < lc; i++){
switch(i){
case 1:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light);
break;
case 2:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light2);
break;
case 3:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light3);
break;
case 4:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light4);
break;
case 5:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light5);
break;
case 6:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light6);
break;
case 7:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light7);
break;
case 8:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light8);
break;
default:
light_pos_ptr = SettingGetGlobal_3fv(G, cSetting_light9);
break;
}
copy3f(light_pos_ptr, light_pos);
normalize3f(light_pos);
xmlTextWriterStartElement(w, BAD_CAST "node");
xmlTextWriterWriteFormatAttribute(w, BAD_CAST "id", "node-pymol-light%i", i);
xmlTextWriterWriteAttribute(w, BAD_CAST "type", BAD_CAST "NODE");
char *lookat = (char *) malloc(sizeof(char) * 50);
sprintf(lookat,
"%5.3f %5.3f %5.3f " // position of light on unit sphere
"0 0 0 " // pointed toward origin
"0 1 0", // positive y axis up (arbitrary)
/* negative because PyMOL gives direction of light, not position */
-light_pos[0], -light_pos[1], -light_pos[2]);
xmlTextWriterWriteElement(w, BAD_CAST "lookat", BAD_CAST lookat);
xmlTextWriterWriteElement(w, BAD_CAST "matrix", BAD_CAST matrix);
xmlTextWriterStartElement(w, BAD_CAST "instance_light");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST "#pymol-light");
xmlTextWriterEndElement(w); // instance_light
xmlTextWriterEndElement(w); // node
}
}
}
free(matrix);
xmlTextWriterEndElement(w); // visual_scene
xmlTextWriterEndElement(w); // library_visual_scenes
}
/* Scene */
{
xmlTextWriterStartElement(w, BAD_CAST "scene");
xmlTextWriterStartElement(w, BAD_CAST "instance_visual_scene");
xmlTextWriterWriteAttribute(w, BAD_CAST "url", BAD_CAST "#scene");
xmlTextWriterEndElement(w); // instance_visual_scene
xmlTextWriterEndElement(w); // scene
}
xmlTextWriterEndElement(w); // COLLADA
/* Close the document */
rc = xmlTextWriterEndDocument(w);
if (rc < 0) {
printf("ColladaRender: Error at xmlTextWriterEndDocument.\n");
// return;
}
/*
* Dump the document to buffer
*/
xmlDocDumpFormatMemory(doc, &tmp, &buffersize, 1);
/* Concat buffer to VLA output */
UtilConcatVLA(&vla, &cc, (char *) tmp);
/*
* Free associated memory.
*/
free(trans);
free(geom_trans);
xmlFree(tmp);
xmlFreeTextWriter(w);
xmlFreeDoc(doc);
*vla_ptr = vla;
#endif // _HAVE_LIBXML
}
|