File size: 56,465 Bytes
2c55b92 | 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 | // Copyright 2021 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "render/render_context.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include "render/render_util.h"
#include "render/glad/glad.h"
// bitmap font definitions
#include "render/font/normal50.inc"
#include "render/font/normal100.inc"
#include "render/font/normal150.inc"
#include "render/font/normal200.inc"
#include "render/font/normal250.inc"
#include "render/font/normal300.inc"
#include "render/font/back50.inc"
#include "render/font/back100.inc"
#include "render/font/back150.inc"
#include "render/font/back200.inc"
#include "render/font/back250.inc"
#include "render/font/back300.inc"
#include "render/font/big50.inc"
#include "render/font/big100.inc"
#include "render/font/big150.inc"
#include "render/font/big200.inc"
#include "render/font/big250.inc"
#include "render/font/big300.inc"
//----------------------------- custom OpenGL context ----------------------------------------------
// set default mjrContext
void mjr_defaultContext(mjrContext* con) {
memset(con, 0, sizeof(mjrContext));
}
// allocate lists
static void listAllocate(GLuint* base, GLsizei* range, GLsizei newrange) {
// allocate lists
*range = newrange;
if (newrange) {
*base = glGenLists(*range);
if (*base <= 0) {
mju_error("Could not allocate display lists");
}
}
}
// model planes: with grid
static void makePlane(const mjModel* m, mjrContext* con) {
mjtNum zfar = m->vis.map.zfar * m->stat.extent;
mjtNum pad, left, right, sz[2], sz2;
double grid[2][mjMAXPLANEGRID+1];
int nn[2], nplane;
// count planes
nplane = 0;
for (int i=0; i < m->ngeom; i++) {
if (m->geom_type[i] == mjGEOM_PLANE) {
nplane++;
}
}
// allocate list
listAllocate(&con->basePlane, &con->rangePlane, nplane+1);
// 0: box-division, for rendering plane geoms without dataid
glNewList(con->basePlane, GL_COMPILE);
glBegin(GL_QUADS);
glNormal3d(0, 0, 1);
double d = 2.0/m->vis.quality.numquads;
for (int x=0; x < m->vis.quality.numquads; x++) {
for (int y=0; y < m->vis.quality.numquads; y++) {
glVertex3d(d*(x+0)-1, d*(y+0)-1, 0);
glVertex3d(d*(x+1)-1, d*(y+0)-1, 0);
glVertex3d(d*(x+1)-1, d*(y+1)-1, 0);
glVertex3d(d*(x+0)-1, d*(y+1)-1, 0);
}
}
glEnd();
glEndList();
// construct planes, offset by 1
nplane = 0;
for (int i=0; i < m->ngeom; i++) {
if (m->geom_type[i] == mjGEOM_PLANE) {
// get sizes
sz[0] = m->geom_size[3*i];
sz[1] = m->geom_size[3*i+1];
// loop over (x, y)
for (int k=0; k < 2; k++) {
// regular dimension
if (sz[k] > 0) {
// limit number of grid lines, leave room for padding
sz2 = mju_max(m->geom_size[3*i+2], sz[k]/(mjMAXPLANEGRID-2));
// compute grid size, force even
nn[k] = (int)mju_floor(sz[k]/mju_max(mjMINVAL, sz2));
nn[k] = nn[k] - (nn[k]%2);
// add padding
pad = sz[k] - nn[k]*sz2;
nn[k] += 2;
// make grid
for (int x=0; x < nn[k]; x++) {
// compute left, right (which is bottom, top for y)
if (x == 0) {
left = -sz[k];
right = left + pad;
} else {
left = -sz[k] + pad + (x-1)*sz2*2;
right = left + (x == nn[k]-1 ? pad : sz2*2);
}
// record
grid[k][x] = left;
grid[k][x+1] = mjMAX(left, right); // just in case
}
}
// infinite dimension
else {
// get size increment
mjtNum sX;
int matid = m->geom_matid[i];
if (matid >= 0 && m->mat_texrepeat[2*matid+k] > 0) {
sX = 2/m->mat_texrepeat[2*matid+k];
} else {
sX = 2.1*zfar/(mjMAXPLANEGRID-2);
}
// create grid, larger than skybox
d = (2.1*zfar + 2*sX)/mjMAXPLANEGRID;
for (int x=0; x <= mjMAXPLANEGRID; x++) {
grid[k][x] = d*x - d*(mjMAXPLANEGRID/2);
}
// save number
nn[k] = mjMAXPLANEGRID;
}
}
// init list
glNewList(con->basePlane + nplane+1, GL_COMPILE);
glBegin(GL_QUADS);
glNormal3d(0, 0, 1);
// make grid
for (int x=0; x < nn[0]; x++) {
for (int y=0; y < nn[1]; y++) {
glVertex3d(grid[0][x+0], grid[1][y+0], 0);
glVertex3d(grid[0][x+1], grid[1][y+0], 0);
glVertex3d(grid[0][x+1], grid[1][y+1], 0);
glVertex3d(grid[0][x+0], grid[1][y+1], 0);
}
}
// end list
glEnd();
glEndList();
// advance counter
nplane++;
}
}
}
// model-specific meshes
static void makeMesh(const mjModel* m, mjrContext* con) {
// allocate list
listAllocate(&con->baseMesh, &con->rangeMesh, 2*m->nmesh);
// process meshes
for (int i=0; i < m->nmesh; i++) {
mjr_uploadMesh(m, con, i);
}
}
// (re) upload mesh to GPU
void mjr_uploadMesh(const mjModel* m, const mjrContext* con, int meshid) {
int vertadr, numvert, normaladr, numface, texcoordadr;
float normal[3], *v1, *v2, *v3, *n1, *n2, *n3, *t1, *t2, *t3;
// check index
if (meshid < 0 || meshid >= m->nmesh) {
mju_error("Invalid mesh index %d", meshid);
}
// delete old lists (mesh and convex hull)
glDeleteLists(con->baseMesh + 2*meshid, 2);
// get vertex and texcoord address for this mesh
vertadr = m->mesh_vertadr[meshid];
normaladr = m->mesh_normaladr[meshid];
texcoordadr = m->mesh_texcoordadr[meshid];
// render original mesh
glNewList(con->baseMesh + 2*meshid, GL_COMPILE);
glBegin(GL_TRIANGLES);
for (int face = m->mesh_faceadr[meshid];
face < m->mesh_faceadr[meshid] + m->mesh_facenum[meshid];
face++) {
// compute vertex addresses
v1 = m->mesh_vert + 3*(m->mesh_face[3*face] + vertadr);
v2 = m->mesh_vert + 3*(m->mesh_face[3*face+1] + vertadr);
v3 = m->mesh_vert + 3*(m->mesh_face[3*face+2] + vertadr);
// compute normal addresses
n1 = m->mesh_normal + 3*(m->mesh_facenormal[3*face] + normaladr);
n2 = m->mesh_normal + 3*(m->mesh_facenormal[3*face+1] + normaladr);
n3 = m->mesh_normal + 3*(m->mesh_facenormal[3*face+2] + normaladr);
// compute texcoord addresses
if (texcoordadr >= 0) {
t1 = m->mesh_texcoord + 2*(m->mesh_facetexcoord[3*face] + texcoordadr);
t2 = m->mesh_texcoord + 2*(m->mesh_facetexcoord[3*face+1] + texcoordadr);
t3 = m->mesh_texcoord + 2*(m->mesh_facetexcoord[3*face+2] + texcoordadr);
} else {
t1 = t2 = t3 = NULL;
}
// compute face normal
mjr_makeNormal(normal, v1, v2, v3);
// make OpenGL triangle
// vertex1
if (t1) {
glTexCoord2fv(t1);
glNormal3fv(n1);
} else {
if (n1[0]*normal[0]+n1[1]*normal[1]+n1[2]*normal[2] < 0.8) {
glNormal3fv(normal);
} else {
glNormal3fv(n1);
}
}
glVertex3fv(v1);
// vertex2
if (t2) {
glTexCoord2fv(t2);
glNormal3fv(n2);
} else {
if (n2[0]*normal[0]+n2[1]*normal[1]+n2[2]*normal[2] < 0.8) {
glNormal3fv(normal);
} else {
glNormal3fv(n2);
}
}
glVertex3fv(v2);
// vertex3
if (t3) {
glTexCoord2fv(t3);
glNormal3fv(n3);
} else {
if (n3[0]*normal[0]+n3[1]*normal[1]+n3[2]*normal[2] < 0.8) {
glNormal3fv(normal);
} else {
glNormal3fv(n3);
}
}
glVertex3fv(v3);
}
glEnd();
glEndList();
// render convex hull if present
if (m->mesh_graphadr[meshid] >= 0) {
// get sizes of convex hull
numvert = m->mesh_graph[m->mesh_graphadr[meshid]];
numface = m->mesh_graph[m->mesh_graphadr[meshid]+1];
glNewList(con->baseMesh + 2*meshid+1, GL_COMPILE);
glBegin(GL_TRIANGLES);
for (int face=0; face < numface; face++) {
// face address in graph
int j = m->mesh_graphadr[meshid] + 2 + 3*numvert + 3*numface + 3*face;
// compute vertex addresses
v1 = m->mesh_vert + 3*(m->mesh_graph[j] + vertadr);
v2 = m->mesh_vert + 3*(m->mesh_graph[j+1] + vertadr);
v3 = m->mesh_vert + 3*(m->mesh_graph[j+2] + vertadr);
// compute texcoord addresses
if (texcoordadr >= 0) {
t1 = m->mesh_texcoord + 2*(m->mesh_graph[j] + texcoordadr);
t2 = m->mesh_texcoord + 2*(m->mesh_graph[j+1] + texcoordadr);
t3 = m->mesh_texcoord + 2*(m->mesh_graph[j+2] + texcoordadr);
} else {
t1 = t2 = t3 = NULL;
}
// make OpenGL triangle
mjr_makeNormal(normal, v1, v2, v3);
glNormal3fv(normal);
// vertex 1
if (t1) glTexCoord2fv(t1);
glVertex3fv(v1);
// vertex 2
if (t2) glTexCoord2fv(t2);
glVertex3fv(v2);
// vertex 3
if (t3) glTexCoord2fv(t3);
glVertex3fv(v3);
}
glEnd();
glEndList();
}
}
// helper structure for adding vertices to height field
struct vertbuf {
int nvert;
float vert1[3], vert2[3], vert3[3];
};
// helper function for adding vertices to height field
static void addVert(float x, float y, float z, float sclz, struct vertbuf* buf) {
float normal[3];
// update buffer
memcpy(buf->vert1, buf->vert2, 3*sizeof(float));
memcpy(buf->vert2, buf->vert3, 3*sizeof(float));
buf->vert3[0] = x;
buf->vert3[1] = y;
buf->vert3[2] = z*sclz;
buf->nvert++;
// triangle ready
if (buf->nvert >= 3) {
// compensate for alternating orientation
if (buf->nvert%2) {
mjr_makeNormal(normal, buf->vert1, buf->vert2, buf->vert3);
glNormal3fv(normal);
glVertex3fv(buf->vert1);
glVertex3fv(buf->vert2);
glVertex3fv(buf->vert3);
} else {
mjr_makeNormal(normal, buf->vert1, buf->vert3, buf->vert2);
glNormal3fv(normal);
glVertex3fv(buf->vert1);
glVertex3fv(buf->vert3);
glVertex3fv(buf->vert2);
}
}
}
// model-specific height fields
static void makeHField(const mjModel* m, mjrContext* con) {
// allocate list
listAllocate(&con->baseHField, &con->rangeHField, m->nhfield);
// uploaed all heightfields
for (int i=0; i < m->nhfield; i++) {
mjr_uploadHField(m, con, i);
}
}
// (re) upload height field to GPU
void mjr_uploadHField(const mjModel* m, const mjrContext* con, int hfieldid) {
int d1 = 1, d2 = 0;
float width, height, sz[4];
struct vertbuf buf;
float* data;
// check index
if (hfieldid < 0 || hfieldid >= m->nhfield) {
mju_error("Invalid height field index %d", hfieldid);
}
// delete old list
glDeleteLists(con->baseHField + hfieldid, 1);
// init list, get elevation data address
glNewList(con->baseHField + hfieldid, GL_COMPILE);
data = m->hfield_data + m->hfield_adr[hfieldid];
// (half) width and height of integer grid
width = 0.5f * (m->hfield_ncol[hfieldid]-1);
height = 0.5f * (m->hfield_nrow[hfieldid]-1);
// convert size to float
for (int r=0; r < 4; r++) {
sz[r] = (float)m->hfield_size[4*hfieldid+r];
}
// render height field as triangles
glBegin(GL_TRIANGLES);
int nr = m->hfield_nrow[hfieldid];
int nc = m->hfield_ncol[hfieldid];
for (int r=0; r < nr-1; r++) {
buf.nvert = 0;
for (int c=0; c < nc; c++) {
addVert(sz[0]*(c/width-1.0f), sz[1]*((r+d1)/height-1.0f), data[(r+d1)*nc+c], sz[2], &buf);
addVert(sz[0]*(c/width-1.0f), sz[1]*((r+d2)/height-1.0f), data[(r+d2)*nc+c], sz[2], &buf);
}
}
glEnd();
// render sides as quads
glBegin(GL_QUADS);
for (int r=0; r < nr-1; r++) {
// left
glNormal3f(-1, 0, 0);
glVertex3f(-sz[0], sz[1]*((r+1)/height-1.0f), -sz[3]);
glVertex3f(-sz[0], sz[1]*((r+0)/height-1.0f), -sz[3]);
glVertex3f(-sz[0], sz[1]*((r+0)/height-1.0f), data[(r+0)*nc]*sz[2]);
glVertex3f(-sz[0], sz[1]*((r+1)/height-1.0f), data[(r+1)*nc]*sz[2]);
// right
glNormal3f(+1, 0, 0);
glVertex3f(+sz[0], sz[1]*((r+0)/height-1.0f), -sz[3]);
glVertex3f(+sz[0], sz[1]*((r+1)/height-1.0f), -sz[3]);
glVertex3f(+sz[0], sz[1]*((r+1)/height-1.0f), data[(r+1)*nc + nc-1]*sz[2]);
glVertex3f(+sz[0], sz[1]*((r+0)/height-1.0f), data[(r+0)*nc + nc-1]*sz[2]);
}
for (int c=0; c < nc-1; c++) {
// front
glNormal3f(0, -1, 0);
glVertex3f(sz[0]*((c+0)/width-1.0f), -sz[1], -sz[3]);
glVertex3f(sz[0]*((c+1)/width-1.0f), -sz[1], -sz[3]);
glVertex3f(sz[0]*((c+1)/width-1.0f), -sz[1], data[c+1]*sz[2]);
glVertex3f(sz[0]*((c+0)/width-1.0f), -sz[1], data[c]*sz[2]);
// back
glNormal3f(0, +1, 0);
glVertex3f(sz[0]*((c+1)/width-1.0f), +sz[1], -sz[3]);
glVertex3f(sz[0]*((c+0)/width-1.0f), +sz[1], -sz[3]);
glVertex3f(sz[0]*((c+0)/width-1.0f), +sz[1], data[(nr-1)*nc + c]*sz[2]);
glVertex3f(sz[0]*((c+1)/width-1.0f), +sz[1], data[(nr-1)*nc + c+1]*sz[2]);
}
// recompute for bottom drawing: different number of grid points
width = 0.5f * m->vis.quality.numquads;
height = 0.5f * m->vis.quality.numquads;
// bottom
glNormal3f(0, 0, -1);
for (int r=0; r < m->vis.quality.numquads; r++) {
for (int c=0; c < m->vis.quality.numquads; c++) {
glVertex3f(sz[0]*((c+0)/width-1.0f), sz[1]*((r+0)/height-1.0f), -sz[3]);
glVertex3f(sz[0]*((c+0)/width-1.0f), sz[1]*((r+1)/height-1.0f), -sz[3]);
glVertex3f(sz[0]*((c+1)/width-1.0f), sz[1]*((r+1)/height-1.0f), -sz[3]);
glVertex3f(sz[0]*((c+1)/width-1.0f), sz[1]*((r+0)/height-1.0f), -sz[3]);
}
}
glEnd();
// end list
glEndList();
}
// set one vertex and normal on sphere, given az, el and sign(top/bottom)
static void setVertexSphere(float* v, float* n, float az, float el, int sign) {
v[0] = cosf(az) * cosf(el);
v[1] = sinf(az) * cosf(el);
v[2] = sign + sinf(el);
n[0] = v[0];
n[1] = v[1];
n[2] = v[2] - sign;
}
// make half a unit sphere: +1: top, -1: bottom
static void halfSphere(int sign, int nSlice, int nStack) {
float az1, az2, el1, el2;
float v1[3], v2[3], v3[3], v4[3];
float n1[3], n2[3], n3[3], n4[3];
// pole: use triangles
glBegin(GL_TRIANGLES);
el1 = (mjPI/2.0f * sign * (nStack-1)) / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0.0f)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1.0f)) / (float)nSlice;
// compute triangle vertices
setVertexSphere(v1, n1, az1, el1, sign);
setVertexSphere(v2, n2, az2, el1, sign);
v3[0] = v3[1] = 0;
v3[2] = 2*sign;
n3[0] = n3[1] = 0;
n3[2] = sign;
// make triangle
if (sign > 0) {
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
} else {
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n1);
glVertex3fv(v1);
}
}
glEnd();
// the rest: use quads
glBegin(GL_QUADS);
for (int i=0; i < nStack-1; i++) {
el1 = (mjPI/2.0f * sign * (i+0)) / (float)nStack;
el2 = (mjPI/2.0f * sign * (i+1)) / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
setVertexSphere(v1, n1, az1, el1, sign);
setVertexSphere(v2, n2, az2, el1, sign);
setVertexSphere(v3, n3, az2, el2, sign);
setVertexSphere(v4, n4, az1, el2, sign);
// make quad
if (sign > 0) {
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n4);
glVertex3fv(v4);
} else {
glNormal3fv(n4);
glVertex3fv(v4);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n1);
glVertex3fv(v1);
}
}
}
glEnd();
}
// make unit sphere
static void sphere(int nSlice, int nStack) {
float az1, az2, el1, el2;
float v1[3], v2[3], v3[3], v4[3];
float n1[3], n2[3], n3[3], n4[3];
// poles: use triangles
glBegin(GL_TRIANGLES);
for (int sign=-1; sign <= 1; sign+=2) {
el1 = (0.5*mjPI * sign * (nStack/2-1)) / (float)(nStack/2);
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0.0f)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1.0f)) / (float)nSlice;
// compute triangle vertices
setVertexSphere(v1, n1, az1, el1, 0);
setVertexSphere(v2, n2, az2, el1, 0);
v3[0] = v3[1] = 0;
v3[2] = sign;
n3[0] = n3[1] = 0;
n3[2] = sign;
// make triangle
if (sign > 0) {
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
} else {
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n1);
glVertex3fv(v1);
}
}
}
glEnd();
// the rest: use quads
glBegin(GL_QUADS);
for (int sign=-1; sign <= 1; sign+=2) {
for (int i=0; i < nStack/2-1; i++) {
el1 = (0.5*mjPI * sign * (i+0)) / (float)(nStack/2);
el2 = (0.5*mjPI * sign * (i+1)) / (float)(nStack/2);
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
setVertexSphere(v1, n1, az1, el1, 0);
setVertexSphere(v2, n2, az2, el1, 0);
setVertexSphere(v3, n3, az2, el2, 0);
setVertexSphere(v4, n4, az1, el2, 0);
// make quad
if (sign > 0) {
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n4);
glVertex3fv(v4);
} else {
glNormal3fv(n4);
glVertex3fv(v4);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n1);
glVertex3fv(v1);
}
}
}
}
glEnd();
}
// set one vertex on disk, given az, r and sign(top/bottom)
static void setVertexDisk(float* v, float az, float r, int sign) {
v[0] = cosf(az) * r;
v[1] = sinf(az) * r;
v[2] = sign;
}
// make disk: +1: top, -1: bottom, 0: zero facing down
static void disk(int sign, int nSlice, int nStack) {
float az1, az2, r1, r2;
float v1[3], v2[3], v3[3], v4[3];
float normal[3] = {0, 0, (sign == 0 ? -1 : sign)};
// pole: use triangles
glBegin(GL_TRIANGLES);
glNormal3fv(normal);
r1 = 1.0f / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute triangle vertices
setVertexDisk(v1, az1, r1, sign);
setVertexDisk(v2, az2, r1, sign);
v3[0] = v3[1] = 0;
v3[2] = sign;
// make triangle
if (sign > 0) {
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
} else {
glVertex3fv(v3);
glVertex3fv(v2);
glVertex3fv(v1);
}
}
glEnd();
// the rest: use quads
glBegin(GL_QUADS);
glNormal3fv(normal);
for (int i=0; i < nStack-1; i++) {
r1 = (i+1) / (float)nStack;
r2 = (i+2) / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
setVertexDisk(v1, az1, r2, sign);
setVertexDisk(v2, az2, r2, sign);
setVertexDisk(v3, az2, r1, sign);
setVertexDisk(v4, az1, r1, sign);
// make quad
if (sign > 0) {
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
glVertex3fv(v4);
} else {
glVertex3fv(v4);
glVertex3fv(v3);
glVertex3fv(v2);
glVertex3fv(v1);
}
}
}
glEnd();
}
// set vertex and normal on cone, given az, r
static void setVertexCone(float* v, float* n, float az, float r) {
const float scale = 1.0f/sqrtf(2.0f);
// vertex
v[0] = cosf(az) * r;
v[1] = sinf(az) * r;
v[2] = 1 - r;
// normal
n[0] = cosf(az) * scale;
n[1] = sinf(az) * scale;
n[2] = scale;
}
// make open cone
static void cone(int nSlice, int nStack) {
float az1, az2, r1, r2;
float v1[3], v2[3], v3[3], v4[3];
float n1[3], n2[3], n3[3], n4[3];
// pole: use triangles
glBegin(GL_TRIANGLES);
r1 = 1.0f / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute triangle vertices
setVertexCone(v1, n1, az1, r1);
setVertexCone(v2, n2, az2, r1);
v3[0] = v3[1] = 0;
v3[2] = 1;
n3[0] = n1[0]+n2[0];
n3[1] = n1[1]+n2[1];
n3[2] = n1[2]+n2[2];
mjr_normalizeVec(n3);
// make triangle
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
}
glEnd();
// the rest: use quads
glBegin(GL_QUADS);
for (int i=0; i < nStack-1; i++) {
r1 = (i+1) / (float)nStack;
r2 = (i+2) / (float)nStack;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
setVertexCone(v1, n1, az1, r2);
setVertexCone(v2, n2, az2, r2);
setVertexCone(v3, n3, az2, r1);
setVertexCone(v4, n4, az1, r1);
// make quad
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n4);
glVertex3fv(v4);
}
}
glEnd();
}
// set one vertex and normal on cylinder, given az and h
static void setVertexCylinder(float* v, float* n, float az, float h) {
v[0] = cosf(az);
v[1] = sinf(az);
v[2] = h;
n[0] = v[0]/sqrtf(v[0]*v[0]+v[1]*v[1]);
n[1] = v[1]/sqrtf(v[0]*v[0]+v[1]*v[1]);
n[2] = 0;
}
// open cylinder from -1 to +1 in z, radius 1
static void cylinder(int nSlice, int nStack) {
float az1, az2, h1, h2;
float v1[3], v2[3], v3[3], v4[3];
float n1[3], n2[3], n3[3], n4[3];
// use quads everywhere
glBegin(GL_QUADS);
for (int i=0; i < nStack; i++) {
h1 = 2*(i+0)/(float)nStack - 1;
h2 = 2*(i+1)/(float)nStack - 1;
for (int j=0; j < nSlice; j++) {
az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
setVertexCylinder(v1, n1, az1, h1);
setVertexCylinder(v2, n2, az2, h1);
setVertexCylinder(v3, n3, az2, h2);
setVertexCylinder(v4, n4, az1, h2);
// make quad
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glNormal3fv(n4);
glVertex3fv(v4);
}
}
glEnd();
}
// set one vertex in haze
static void setVertexHaze(float* v, float az, float h, float r) {
v[0] = cosf(az) * (1 - r*(1-h));
v[1] = sinf(az) * (1 - r*(1-h));
v[2] = h;
}
// truncated cone for haze rendering
static void haze(int nSlice, float r, const float* rgba) {
// compute elevation h for transparency transition point
float alpha = atan2f(1, r);
float beta = (float)(0.75*mjPI) - alpha;
float h = sqrtf(0.5f) * r * sinf(alpha) / sinf(beta);
// use quads everywhere
glBegin(GL_QUADS);
// normal not needed (always rendered with lighting off)
glNormal3f(0, 0, 1);
// stacks = 2
for (int i=0; i < 2; i++) {
float h1 = (i == 0 ? 0 : h);
float h2 = (i == 0 ? h : 1);
for (int j=0; j < nSlice; j++) {
float az1 = (2.0f*mjPI * (j+0)) / (float)nSlice;
float az2 = (2.0f*mjPI * (j+1)) / (float)nSlice;
// compute quad vertices
float v1[3], v2[3], v3[3], v4[3];
setVertexHaze(v1, az1, h1, r);
setVertexHaze(v2, az2, h1, r);
setVertexHaze(v3, az2, h2, r);
setVertexHaze(v4, az1, h2, r);
// colors at elevation h1 and h2
float c1 = (i == 1);
float c2 = (i == 0);
// make quad, with colors
glColor4f(rgba[0], rgba[1], rgba[2], c1);
glVertex3fv(v1);
glVertex3fv(v2);
glColor4f(rgba[0], rgba[1], rgba[2], c2);
glVertex3fv(v3);
glVertex3fv(v4);
}
}
glEnd();
}
// builtin geoms types
static void makeBuiltin(const mjModel* m, mjrContext* con) {
int numstacks = m->vis.quality.numstacks;
int numslices = m->vis.quality.numslices;
int numquads = m->vis.quality.numquads;
float d = 2.0f/numquads;
// allocate list
listAllocate(&con->baseBuiltin, &con->rangeBuiltin, mjrNUM);
// sphere
glNewList(con->baseBuiltin + mjrSPHERE, GL_COMPILE);
sphere(numslices, numstacks);
glEndList();
// sphere top
glNewList(con->baseBuiltin + mjrSPHERETOP, GL_COMPILE);
halfSphere(+1, numslices, numstacks/2);
glEndList();
// sphere bottom
glNewList(con->baseBuiltin + mjrSPHEREBOTTOM, GL_COMPILE);
halfSphere(-1, numslices, numstacks/2);
glEndList();
// closed cylinder, center at z=0
glNewList(con->baseBuiltin + mjrCYLINDER, GL_COMPILE);
disk(-1, numslices, numstacks/2);
cylinder(numslices, numstacks);
disk(+1, numslices, numstacks/2);
glEndList();
// open cylinder, center at z=0
glNewList(con->baseBuiltin + mjrCYLINDEROPEN, GL_COMPILE);
cylinder(numslices, numstacks);
glEndList();
// haze truncated cone
glNewList(con->baseBuiltin + mjrHAZE, GL_COMPILE);
haze(numslices, m->vis.map.haze, m->vis.rgba.haze);
glEndList();
// box
glNewList(con->baseBuiltin + mjrBOX, GL_COMPILE);
glBegin(GL_QUADS);
for (int x=0; x < numquads; x++) {
for (int y=0; y < numquads; y++) {
glNormal3f(0, 0, 1); // top
glVertex3f(d*(x+0)-1, d*(y+0)-1, 1);
glVertex3f(d*(x+1)-1, d*(y+0)-1, 1);
glVertex3f(d*(x+1)-1, d*(y+1)-1, 1);
glVertex3f(d*(x+0)-1, d*(y+1)-1, 1);
glNormal3f(0, 0, -1); // bottom
glVertex3f(d*(x+0)-1, d*(y+1)-1, -1);
glVertex3f(d*(x+1)-1, d*(y+1)-1, -1);
glVertex3f(d*(x+1)-1, d*(y+0)-1, -1);
glVertex3f(d*(x+0)-1, d*(y+0)-1, -1);
glNormal3f(1, 0, 0); // right
glVertex3f(1, d*(x+0)-1, d*(y+0)-1);
glVertex3f(1, d*(x+1)-1, d*(y+0)-1);
glVertex3f(1, d*(x+1)-1, d*(y+1)-1);
glVertex3f(1, d*(x+0)-1, d*(y+1)-1);
glNormal3f(-1, 0, 0); // left
glVertex3f(-1, d*(x+0)-1, d*(y+1)-1);
glVertex3f(-1, d*(x+1)-1, d*(y+1)-1);
glVertex3f(-1, d*(x+1)-1, d*(y+0)-1);
glVertex3f(-1, d*(x+0)-1, d*(y+0)-1);
glNormal3f(0, -1, 0); // front
glVertex3f(d*(x+0)-1, -1, d*(y+0)-1);
glVertex3f(d*(x+1)-1, -1, d*(y+0)-1);
glVertex3f(d*(x+1)-1, -1, d*(y+1)-1);
glVertex3f(d*(x+0)-1, -1, d*(y+1)-1);
glNormal3f(0, 1, 0); // back
glVertex3f(d*(x+0)-1, 1, d*(y+1)-1);
glVertex3f(d*(x+1)-1, 1, d*(y+1)-1);
glVertex3f(d*(x+1)-1, 1, d*(y+0)-1);
glVertex3f(d*(x+0)-1, 1, d*(y+0)-1);
}
}
glEnd();
glEndList();
// cone, bottom at z=0
glNewList(con->baseBuiltin + mjrCONE, GL_COMPILE);
cone(numslices, numstacks);
disk(0, numslices, numstacks);
glEndList();
}
// make depth texture and FBO for shadow mapping
static void makeShadow(const mjModel* m, mjrContext* con) {
// return if size is 0
if (!con->shadowSize) {
return;
}
// create FBO
glGenFramebuffers(1, &con->shadowFBO);
if (!con->shadowFBO) {
mju_error("Could not allocate shadow framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, con->shadowFBO);
// Create a shadow depth texture in TEXTURE1 and explicitly select an int24
// depth buffer. A depth stencil format is used because that appears to be
// more widely supported (MacOS does not support GL_DEPTH_COMPONENT24). Using
// a fixed format makes it easier to choose glPolygonOffset parameters that
// result in reasonably consistent and artifact free shadows across platforms.
glGenTextures(1, &con->shadowTex);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, con->shadowTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
con->shadowSize, con->shadowSize, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
// attach to FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, con->shadowTex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
// check FBO status
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err != GL_FRAMEBUFFER_COMPLETE) {
mju_error("Shadow framebuffer is not complete, error 0x%x", err);
}
glDisable(GL_TEXTURE_2D);
}
// make render buffers and FBO for offscreen rendering
static void makeOff(mjrContext* con) {
// return if size is 0
if (!con->offWidth || !con->offHeight) {
return;
}
// create FBO
glGenFramebuffers(1, &con->offFBO);
if (!con->offFBO) {
mju_error("Could not allocate offscreen framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, con->offFBO);
// clamp samples request
int sMax = 0;
glGetIntegerv(GL_MAX_SAMPLES, &sMax);
if (con->offSamples > sMax) {
con->offSamples = sMax;
}
// create color buffer
glGenRenderbuffers(1, &con->offColor);
if (!con->offColor) {
mju_error("Could not allocate offscreen color buffer");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offColor);
if (con->offSamples) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_RGBA8,
con->offWidth, con->offHeight);
} else {
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, con->offWidth, con->offHeight);
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, con->offColor);
// create depth and stencil buffer
glGenRenderbuffers(1, &con->offDepthStencil);
if (!con->offDepthStencil) {
mju_error("Could not allocate offscreen depth and stencil buffer");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil);
GLenum depth_buffer_format =
mjGLAD_GL_ARB_depth_buffer_float ? GL_DEPTH32F_STENCIL8 : GL_DEPTH24_STENCIL8;
if (con->offSamples) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, depth_buffer_format,
con->offWidth, con->offHeight);
} else {
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, con->offDepthStencil);
// check FBO status
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err != GL_FRAMEBUFFER_COMPLETE) {
mju_error("Offscreen framebuffer is not complete, error 0x%x", err);
}
// get actual number of samples
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &con->offSamples);
// create FBO for resolving multisamples
if (con->offSamples) {
// create FBO
glGenFramebuffers(1, &con->offFBO_r);
if (!con->offFBO_r) {
mju_error("Could not allocate offscreen framebuffer_r");
}
glBindFramebuffer(GL_FRAMEBUFFER, con->offFBO_r);
// create color buffer
glGenRenderbuffers(1, &con->offColor_r);
if (!con->offColor_r) {
mju_error("Could not allocate offscreen color buffer_r");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offColor_r);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, con->offWidth, con->offHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, con->offColor_r);
// create depth and stencil buffer
glGenRenderbuffers(1, &con->offDepthStencil_r);
if (!con->offDepthStencil_r) {
mju_error("Could not allocate offscreen depth and stencil buffer_r");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil_r);
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, con->offDepthStencil_r);
// check FBO status
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err != GL_FRAMEBUFFER_COMPLETE) {
mju_error("Offscreen framebuffer_r is not complete, error 0x%x", err);
}
}
}
// make fonts
static void makeFont(mjrContext* con, int fontscale) {
// data addresses
int adr = 0, adr_big = 0;
// font data pointers for given scaling
const unsigned char* font_normal = NULL;
const unsigned char* font_back = NULL;
const unsigned char* font_big = NULL;
switch (fontscale) {
case mjFONTSCALE_50:
font_normal = font_normal50;
font_back = font_back50;
font_big = font_big50;
break;
case mjFONTSCALE_100:
font_normal = font_normal100;
font_back = font_back100;
font_big = font_big100;
break;
case mjFONTSCALE_150:
font_normal = font_normal150;
font_back = font_back150;
font_big = font_big150;
break;
case mjFONTSCALE_200:
font_normal = font_normal200;
font_back = font_back200;
font_big = font_big200;
break;
case mjFONTSCALE_250:
font_normal = font_normal250;
font_back = font_back250;
font_big = font_big250;
break;
case mjFONTSCALE_300:
font_normal = font_normal300;
font_back = font_back300;
font_big = font_big300;
break;
default:
mju_error("Invalid fontscale");
}
// save fontScale
con->fontScale = fontscale;
// init lists
con->rangeFont = 128;
con->baseFontNormal = glGenLists(con->rangeFont);
con->baseFontShadow = glGenLists(con->rangeFont);
con->baseFontBig = glGenLists(con->rangeFont);
if (con->baseFontNormal == 0 || con->baseFontShadow == 0 || con->baseFontBig == 0) {
mju_error("Could not allocate font lists");
}
// loop over printable characters (32-126)
for (unsigned char i=32; i <= 126; i++) {
// assert character code; SHOULD NOT OCCUR
if (font_normal[adr] != i || font_back[adr] != i || font_big[adr_big] != i) {
mju_error("Invalid font data index");
}
// save character sizes
con->charWidth[i] = font_normal[adr+1];
con->charWidthBig[i] = font_big[adr_big+1];
con->charHeight = font_normal[adr+2];
con->charHeightBig = font_big[adr_big+2];
// make bitmap display list: normal
glNewList(con->baseFontNormal+i, GL_COMPILE);
glBitmap(con->charWidth[i], con->charHeight, 0, 0, con->charWidth[i], 0,
font_normal + adr + 3);
glEndList();
// make bitmap display list: back
glNewList(con->baseFontShadow+i, GL_COMPILE);
glBitmap(con->charWidth[i], con->charHeight, 0, 0, con->charWidth[i], 0,
font_back + adr + 3);
glEndList();
// make bitmap display list: big
glNewList(con->baseFontBig+i, GL_COMPILE);
glBitmap(con->charWidthBig[i], con->charHeightBig, 0, 0, con->charWidthBig[i], 0,
font_big + adr_big + 3);
glEndList();
// compute width in bytes
int width = (con->charWidth[i]-1)/8 + 1;
int widthBig = (con->charWidthBig[i]-1)/8 + 1;
// advance addresses
adr += 3 + width*con->charHeight;
adr_big += 3 + widthBig*con->charHeightBig;
}
// assert 123 termination token; SHOULD NOT OCCUR
if (font_normal[adr] != 123 || font_back[adr] != 123 || font_big[adr_big] != 123) {
mju_error("Invalid font data termination");
}
}
// make materials, just for those that have textures
static void makeMaterial(const mjModel* m, mjrContext* con) {
memset(con->mat_texid, -1, sizeof(con->mat_texid));
memset(con->mat_texuniform, 0, sizeof(con->mat_texuniform));
memset(con->mat_texrepeat, 0, sizeof(con->mat_texrepeat));
if (!m->nmat || !m->ntex) {
return;
}
if (m->nmat >= mjMAXMATERIAL-1) {
mju_error("Maximum number of materials is %d, got %d", mjMAXMATERIAL, m->nmat);
}
for (int i=0; i < m->nmat; i++) {
if (m->mat_texid[i*mjNTEXROLE + mjTEXROLE_RGB] >= 0) {
for (int j=0; j < mjNTEXROLE; j++) {
con->mat_texid[i*mjNTEXROLE + j] = m->mat_texid[i*mjNTEXROLE + j];
}
con->mat_texuniform[i] = m->mat_texuniform[i];
con->mat_texrepeat[2*i] = m->mat_texrepeat[2*i];
con->mat_texrepeat[2*i+1] = m->mat_texrepeat[2*i+1];
}
}
// find skybox texture
for (int i=0; i < m->ntex; i++) {
if (m->tex_type[i] == mjTEXTURE_SKYBOX) {
if (m->nmat >= mjMAXMATERIAL-2) {
mju_error("With skybox, maximum number of materials is %d, got %d",
mjMAXMATERIAL-1, m->nmat);
}
for (int j=0; j < mjNTEXROLE; j++) {
con->mat_texid[mjNTEXROLE * (mjMAXMATERIAL-1) + j] = -1;
}
con->mat_texid[mjNTEXROLE * (mjMAXMATERIAL-1) + mjTEXROLE_RGB] = i;
break;
}
}
}
// make textures
static void makeTexture(const mjModel* m, mjrContext* con) {
// checks size
if (m->ntex > mjMAXTEXTURE) {
mju_error("Maximum number of textures is %d", mjMAXTEXTURE);
}
// save new size
con->ntexture = m->ntex;
if (!m->ntex) {
return;
}
// allocate and upload
glGenTextures(con->ntexture, con->texture);
for (int i=0; i < m->ntex; i++) {
con->textureType[i] = m->tex_type[i];
mjr_uploadTexture(m, con, i);
}
}
// (re) upload texture to GPU
void mjr_uploadTexture(const mjModel* m, const mjrContext* con, int texid) {
int w = m->tex_width[texid];
float plane[4];
// 2D texture
if (m->tex_type[texid] == mjTEXTURE_2D) {
// OpenGL settings
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, con->texture[texid]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
// set mapping
mjr_setf4(plane, 1, 0, 0, 0);
glTexGenfv(GL_S, GL_OBJECT_PLANE, plane);
mjr_setf4(plane, 0, 1, 0, 0);
glTexGenfv(GL_T, GL_OBJECT_PLANE, plane);
// assign data
int type = 0;
int internaltype = 0;
if (m->tex_nchannel[texid] == 3) {
type = GL_RGB;
internaltype = (m->tex_colorspace[texid] == mjCOLORSPACE_SRGB) ? GL_SRGB8_EXT : GL_RGB;
} else if (m->tex_nchannel[texid] == 4) {
type = GL_RGBA;
internaltype = (m->tex_colorspace[texid] == mjCOLORSPACE_SRGB) ? GL_SRGB8_ALPHA8_EXT : GL_RGBA;
} else {
mju_error("Number of channels not supported: %d", m->tex_nchannel[texid]);
}
glTexImage2D(GL_TEXTURE_2D, 0, internaltype, m->tex_width[texid],
m->tex_height[texid], 0, type, GL_UNSIGNED_BYTE,
m->tex_data + m->tex_adr[texid]);
// generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_2D);
}
// cube or skybox texture
else {
// OpenGL settings
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, con->texture[texid]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
// set mapping
mjr_setf4(plane, 1, 0, 0, 0);
glTexGenfv(GL_S, GL_OBJECT_PLANE, plane);
mjr_setf4(plane, 0, 1, 0, 0);
glTexGenfv(GL_T, GL_OBJECT_PLANE, plane);
mjr_setf4(plane, 0, 0, 1, 0);
glTexGenfv(GL_R, GL_OBJECT_PLANE, plane);
// assign data: repeated
if (m->tex_width[texid] == m->tex_height[texid]) {
for (int i=0; i < 6; i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0, GL_RGB, w, w, 0,
GL_RGB, GL_UNSIGNED_BYTE, m->tex_data + m->tex_adr[texid]);
}
}
// assign data: separate faces
else {
for (int i=0; i < 6; i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0, GL_RGB, w, w, 0,
GL_RGB, GL_UNSIGNED_BYTE, m->tex_data + m->tex_adr[texid] + i*3*w*w);
}
}
// generate mipmaps
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glDisable(GL_TEXTURE_CUBE_MAP);
}
}
// allocate buffers for skins, copy constants
static void makeSkin(const mjModel* m, mjrContext* con) {
int nskin = m->nskin;
// save number of skins (so we can delete context without model)
con->nskin = m->nskin;
// allocate buffers
if (nskin) {
// allocate VBO names
con->skinvertVBO = (unsigned int*) mju_malloc(nskin*sizeof(int));
con->skinnormalVBO = (unsigned int*) mju_malloc(nskin*sizeof(int));
con->skintexcoordVBO = (unsigned int*) mju_malloc(nskin*sizeof(int));
con->skinfaceVBO = (unsigned int*) mju_malloc(nskin*sizeof(int));
// generage VBOs
glGenBuffers(nskin, con->skinvertVBO);
glGenBuffers(nskin, con->skinnormalVBO);
glGenBuffers(nskin, con->skintexcoordVBO);
glGenBuffers(nskin, con->skinfaceVBO);
// process skins
for (int i=0; i < nskin; i++) {
// texture coordinates
if (m->skin_texcoordadr[i] >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, con->skintexcoordVBO[i]);
glBufferData(GL_ARRAY_BUFFER,
2*m->skin_vertnum[i]*sizeof(float),
m->skin_texcoord + 2*m->skin_texcoordadr[i],
GL_STATIC_DRAW);
} else {
glDeleteBuffers(1, con->skintexcoordVBO+i);
con->skintexcoordVBO[i] = 0;
}
// face indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, con->skinfaceVBO[i]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
3*m->skin_facenum[i]*sizeof(int),
m->skin_face + 3*m->skin_faceadr[i],
GL_STATIC_DRAW);
}
}
}
// callback to print out GL_DEBUG output (when enabled for internal testing)
void GLAPIENTRY debugCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam) {
printf("GL DEBUG: source = 0x%x, type = 0x%x, severity = 0x%x, id = 0x%x\nmessage = %s\n\n",
source, type, severity, id, message);
}
// returns 1 if MUJOCO_GL_DEBUG environment variable is set to 1
static int glDebugEnabled(void) {
char* debug = getenv("MUJOCO_GL_DEBUG");
return debug && strcmp(debug, "1") == 0;
}
void mjr_makeContext_offSize(const mjModel* m, mjrContext* con, int fontscale,
int default_offwidth, int default_offheight) {
// fix fontscale
fontscale = 50 * mju_round(((mjtNum)fontscale)/50.0);
if (fontscale < 100) {
fontscale = 100;
} else if (fontscale > 300) {
fontscale = 300;
}
// initialize GLAD, determine window and FBO availability
if (!con->glInitialized) {
if (!mjGladLoadGL()) {
mju_error("gladLoadGL error");
}
if (!mjGLAD_GL_VERSION_1_5) {
mju_error("OpenGL version 1.5 or higher required");
}
if (!mjGLAD_GL_ARB_framebuffer_object) {
mju_error("OpenGL ARB_framebuffer_object required");
}
if (!mjGLAD_GL_ARB_vertex_buffer_object) {
mju_error("OpenGL ARB_vertex_buffer_object required");
}
con->glInitialized = 1;
// determine window availability (could be EGL-headless)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
unsigned int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status == GL_FRAMEBUFFER_COMPLETE) {
con->windowAvailable = 1;
} else if (status == GL_FRAMEBUFFER_UNDEFINED) {
con->windowAvailable = 0;
} else {
mju_error("Default framebuffer is not complete, error 0x%x", status);
}
}
// OpenGL debug output
if (glDebugEnabled() && mjGLAD_GL_KHR_debug) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(debugCallback, 0);
}
// determine samples, stereo and doublebuffer if window available
if (con->windowAvailable) {
// get stereo
GLboolean b;
glGetBooleanv(GL_STEREO, &b);
con->windowStereo = (int)b;
// get doublebuffer
glGetBooleanv(GL_DOUBLEBUFFER, &b);
con->windowDoublebuffer = (int)b;
// get samples
GLint n;
glGetIntegerv(GL_SAMPLE_BUFFERS, &n);
if (n) {
glGetIntegerv(GL_SAMPLES, &n);
con->windowSamples = (int)n;
} else {
con->windowSamples = 0;
}
}
// set pixel (un)packing
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
mjr_freeContext(con);
// no model: offscreen and font only
if (!m) {
// default offscreen
con->offWidth = default_offwidth;
con->offHeight = default_offheight;
con->offSamples = 0;
makeOff(con);
// font
makeFont(con, fontscale);
// try to bind window (bind offscreen if no window)
mjr_setBuffer(mjFB_WINDOW, con);
return;
}
// map shadow clip and scale to absolute units
con->shadowClip = m->stat.extent * m->vis.map.shadowclip;
con->shadowScale = m->vis.map.shadowscale;
// copy parameters
con->offWidth = m->vis.global.offwidth;
con->offHeight = m->vis.global.offheight;
con->offSamples = m->vis.quality.offsamples;
con->fogStart = (float)(m->stat.extent * m->vis.map.fogstart);
con->fogEnd = (float)(m->stat.extent * m->vis.map.fogend);
con->fogRGBA[0] = m->vis.rgba.fog[0];
con->fogRGBA[1] = m->vis.rgba.fog[1];
con->fogRGBA[2] = m->vis.rgba.fog[2];
con->fogRGBA[3] = m->vis.rgba.fog[3];
con->lineWidth = m->vis.global.linewidth;
con->shadowSize = m->vis.quality.shadowsize;
// set fog parameters (model-dependent)
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, con->fogStart);
glFogf(GL_FOG_END, con->fogEnd);
glFogfv(GL_FOG_COLOR, con->fogRGBA);
glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
glHint(GL_FOG_HINT, GL_NICEST);
// make everything
makeOff(con);
makeShadow(m, con);
makeMaterial(m, con);
makeTexture(m, con);
makePlane(m, con);
makeMesh(m, con);
makeHField(m, con);
makeBuiltin(m, con);
makeSkin(m, con);
makeFont(con, fontscale);
// enable seamless cube maps if supported
if (mjGLAD_GL_ARB_seamless_cube_map) {
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
}
// try to bind window (bind offscreen if no window)
mjr_setBuffer(mjFB_WINDOW, con);
// issue warnings for any OpenGL errors
GLenum err;
while ((err = glGetError())) {
mju_warning("OpenGL error 0x%x in or before mjr_makeContext", err);
}
// set default color pixel format for mjr_readPixels
con->readPixelFormat = GL_RGB;
// set default depth mapping for mjr_readPixels
con->readDepthMap = mjDEPTH_ZERONEAR;
}
// allocate resources in custom OpenGL context
void mjr_makeContext(const mjModel* m, mjrContext* con, int fontscale) {
mjr_makeContext_offSize(m, con, fontscale, 800, 600);
}
// Change font of existing context.
void mjr_changeFont(int fontscale, mjrContext* con) {
// free existing font
if (con->rangeFont) {
glDeleteLists(con->baseFontNormal, con->rangeFont);
glDeleteLists(con->baseFontShadow, con->rangeFont);
glDeleteLists(con->baseFontBig, con->rangeFont);
}
con->baseFontNormal = 0;
con->baseFontShadow = 0;
con->baseFontBig = 0;
con->rangeFont = 0;
// make new font
makeFont(con, fontscale);
}
// Add Aux buffer to context; free previous Aux buffer.
void mjr_addAux(int index, int width, int height, int samples, mjrContext* con) {
// check index
if (index < 0 || index >= mjNAUX) {
mju_error("Invalid aux buffer index");
}
// free previous
if (con->auxColor[index]) {
glDeleteRenderbuffers(1, con->auxColor + index);
}
if (con->auxColor_r[index]) {
glDeleteRenderbuffers(1, con->auxColor_r + index);
}
if (con->auxFBO[index]) {
glDeleteFramebuffers(1, con->auxFBO + index);
}
if (con->auxFBO_r[index]) {
glDeleteFramebuffers(1, con->auxFBO_r + index);
}
con->auxColor[index] = 0;
con->auxColor_r[index] = 0;
con->auxFBO[index] = 0;
con->auxFBO_r[index] = 0;
// return if size is not positive
if (width < 1 || height < 1) {
return;
}
// check max size
int maxSize = 0;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxSize);
if (width > maxSize) {
mju_error(
"Auxiliary buffer width exceeds maximum allowed by OpenGL "
"implementation: %d > %d",
width, maxSize);
}
if (height > maxSize) {
mju_error(
"Auxiliary buffer height exceeds maximum allowed by OpenGL "
"implementation: %d > %d",
height, maxSize);
}
// clamp samples request
int maxSample = 0;
glGetIntegerv(GL_MAX_SAMPLES, &maxSample);
if (samples > maxSample) {
samples = maxSample;
}
// assign sizes
con->auxWidth[index] = width;
con->auxHeight[index] = height;
con->auxSamples[index] = samples;
// create FBO
glGenFramebuffers(1, con->auxFBO + index);
if (!con->auxFBO[index]) {
mju_error("Could not allocate auxiliary framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, con->auxFBO[index]);
// create color buffer with multisamples
glGenRenderbuffers(1, con->auxColor + index);
if (!con->auxColor[index]) {
mju_error("Could not allocate auxiliary color buffer");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->auxColor[index]);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->auxSamples[index], GL_RGBA8,
con->auxWidth[index], con->auxHeight[index]);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, con->auxColor[index]);
// check FBO status
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err != GL_FRAMEBUFFER_COMPLETE) {
mju_error("Auxiliary framebuffer is not complete, error 0x%x", err);
}
// create FBO for resolving
glGenFramebuffers(1, con->auxFBO_r + index);
if (!con->auxFBO_r[index]) {
mju_error("Could not allocate auxiliary resolve framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, con->auxFBO_r[index]);
// create color buffer for resolving multisamples
glGenRenderbuffers(1, con->auxColor_r + index);
if (!con->auxColor_r[index]) {
mju_error("Could not allocate auxiliary color resolve buffer");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->auxColor_r[index]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
con->auxWidth[index], con->auxHeight[index]);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, con->auxColor_r[index]);
// check FBO status
err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err != GL_FRAMEBUFFER_COMPLETE) {
mju_error("Auxiliary framebuffer resolve is not complete, error 0x%x", err);
}
// restore
mjr_restoreBuffer(con);
}
// free resources in custom OpenGL context
void mjr_freeContext(mjrContext* con) {
// save flags
int glInitialized = con->glInitialized;
int windowAvailable = con->windowAvailable;
int windowSamples = con->windowSamples;
int windowStereo = con->windowStereo;
int windowDoublebuffer = con->windowDoublebuffer;
// free GPU resources
if (con->ntexture) glDeleteTextures(con->ntexture, con->texture);
if (con->offColor) glDeleteRenderbuffers(1, &con->offColor);
if (con->offColor_r) glDeleteRenderbuffers(1, &con->offColor_r);
if (con->offDepthStencil) glDeleteRenderbuffers(1, &con->offDepthStencil);
if (con->offDepthStencil_r) glDeleteRenderbuffers(1, &con->offDepthStencil_r);
if (con->offFBO) glDeleteFramebuffers(1, &con->offFBO);
if (con->offFBO_r) glDeleteFramebuffers(1, &con->offFBO_r);
if (con->shadowTex) glDeleteTextures(1, &con->shadowTex);
if (con->shadowFBO) glDeleteFramebuffers(1, &con->shadowFBO);
for (int i=0; i < mjNAUX; i++) {
if (con->auxColor[i]) glDeleteRenderbuffers(1, con->auxColor + i);
if (con->auxColor_r[i]) glDeleteRenderbuffers(1, con->auxColor_r + i);
if (con->auxFBO[i]) glDeleteFramebuffers(1, con->auxFBO + i);
if (con->auxFBO_r[i]) glDeleteFramebuffers(1, con->auxFBO_r + i);
}
if (con->rangePlane) glDeleteLists(con->basePlane, con->rangePlane);
if (con->rangeMesh) glDeleteLists(con->baseMesh, con->rangeMesh);
if (con->rangeHField) glDeleteLists(con->baseHField, con->rangeHField);
if (con->rangeBuiltin) glDeleteLists(con->baseBuiltin, con->rangeBuiltin);
if (con->rangeFont) {
glDeleteLists(con->baseFontNormal, con->rangeFont);
glDeleteLists(con->baseFontShadow, con->rangeFont);
glDeleteLists(con->baseFontBig, con->rangeFont);
}
// delete skin
if (con->nskin) {
// delete VBOs
glDeleteBuffers(con->nskin, con->skinvertVBO);
glDeleteBuffers(con->nskin, con->skinnormalVBO);
glDeleteBuffers(con->nskin, con->skintexcoordVBO);
glDeleteBuffers(con->nskin, con->skinfaceVBO);
mju_free(con->skinvertVBO);
mju_free(con->skinnormalVBO);
mju_free(con->skintexcoordVBO);
mju_free(con->skinfaceVBO);
}
// clear fields
mjr_defaultContext(con);
// restore flags
con->glInitialized = glInitialized;
con->windowAvailable = windowAvailable;
con->windowSamples = windowSamples;
con->windowStereo = windowStereo;
con->windowDoublebuffer = windowDoublebuffer;
}
// resize offscreen buffers
void mjr_resizeOffscreen(int width, int height, mjrContext* con) {
if (con->offWidth == width && con->offHeight == height) {
return;
}
con->offWidth = width;
con->offHeight = height;
if (!width || !height) {
return;
}
if (!con->offFBO) {
makeOff(con);
return;
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offColor);
if (con->offSamples) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_RGBA8,
con->offWidth, con->offHeight);
} else {
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, con->offWidth, con->offHeight);
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil);
if (con->offSamples) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_DEPTH32F_STENCIL8,
con->offWidth, con->offHeight);
} else {
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
}
if (con->offSamples) {
glBindRenderbuffer(GL_RENDERBUFFER, con->offColor_r);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, con->offWidth, con->offHeight);
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil_r);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
}
}
|