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
|
VARFP(envmapmodels, 0, 1, 1, preloadmodelshaders(true));
VARFP(bumpmodels, 0, 1, 1, preloadmodelshaders(true));
VARP(fullbrightmodels, 0, 0, 200);
struct animmodel : model
{
struct animspec
{
int frame, range;
float speed;
int priority;
};
struct animpos
{
int anim, fr1, fr2;
float t;
void setframes(const animinfo &info)
{
anim = info.anim;
if(info.range<=1)
{
fr1 = 0;
t = 0;
}
else
{
int time = info.anim&ANIM_SETTIME ? info.basetime : lastmillis-info.basetime;
fr1 = (int)(time/info.speed); // round to full frames
t = (time-fr1*info.speed)/info.speed; // progress of the frame, value from 0.0f to 1.0f
}
if(info.anim&ANIM_LOOP)
{
fr1 = fr1%info.range+info.frame;
fr2 = fr1+1;
if(fr2>=info.frame+info.range) fr2 = info.frame;
}
else
{
fr1 = min(fr1, info.range-1)+info.frame;
fr2 = min(fr1+1, info.frame+info.range-1);
}
if(info.anim&ANIM_REVERSE)
{
fr1 = (info.frame+info.range-1)-(fr1-info.frame);
fr2 = (info.frame+info.range-1)-(fr2-info.frame);
}
}
bool operator==(const animpos &a) const { return fr1==a.fr1 && fr2==a.fr2 && (fr1==fr2 || t==a.t); }
bool operator!=(const animpos &a) const { return fr1!=a.fr1 || fr2!=a.fr2 || (fr1!=fr2 && t!=a.t); }
};
struct part;
struct animstate
{
part *owner;
animpos cur, prev;
float interp;
bool operator==(const animstate &a) const { return cur==a.cur && (interp<1 ? interp==a.interp && prev==a.prev : a.interp>=1); }
bool operator!=(const animstate &a) const { return cur!=a.cur || (interp<1 ? interp!=a.interp || prev!=a.prev : a.interp<1); }
};
struct linkedpart;
struct mesh;
struct shaderparams
{
float spec, ambient, glow, glowdelta, glowpulse, specglare, glowglare, fullbright, envmapmin, envmapmax, scrollu, scrollv, alphatest;
shaderparams() : spec(1.0f), ambient(0.3f), glow(3.0f), glowdelta(0), glowpulse(0), specglare(1), glowglare(1), fullbright(0), envmapmin(0), envmapmax(0), scrollu(0), scrollv(0), alphatest(0.9f) {}
};
struct shaderparamskey
{
static hashtable<shaderparams, shaderparamskey> keys;
static int firstversion, lastversion;
int version;
shaderparamskey() : version(-1) {}
bool checkversion()
{
if(version >= firstversion) return true;
version = lastversion;
if(++lastversion <= 0)
{
enumerate(keys, shaderparamskey, key, key.version = -1);
firstversion = 0;
lastversion = 1;
version = 0;
}
return false;
}
static inline void invalidate()
{
firstversion = lastversion;
}
};
struct skin : shaderparams
{
part *owner;
Texture *tex, *masks, *envmap, *normalmap;
Shader *shader;
bool alphablend, cullface;
shaderparamskey *key;
skin() : owner(0), tex(notexture), masks(notexture), envmap(NULL), normalmap(NULL), shader(NULL), alphablend(true), cullface(true), key(NULL) {}
bool masked() const { return masks != notexture; }
bool envmapped() { return envmapmax>0 && envmapmodels; }
bool bumpmapped() { return normalmap && bumpmodels; }
bool tangents() { return bumpmapped(); }
bool alphatested() const { return alphatest > 0 && tex->type&Texture::ALPHA; }
void setkey()
{
key = &shaderparamskey::keys[*this];
}
void setshaderparams(mesh *m, const animstate *as)
{
if(!Shader::lastshader) return;
float mincolor = as->cur.anim&ANIM_FULLBRIGHT ? fullbrightmodels/100.0f : 0.0f;
if(fullbright)
{
gle::colorf(fullbright/2, fullbright/2, fullbright/2, transparent);
}
else
{
gle::color(vec(lightcolor).max(mincolor), transparent);
}
if(key->checkversion() && Shader::lastshader->owner == key) return;
Shader::lastshader->owner = key;
if(alphatested()) LOCALPARAMF(alphatest, alphatest);
if(fullbright)
{
LOCALPARAMF(lightscale, 0, 0, 2);
}
else
{
float bias = max(mincolor-1.0f, 0.2f), scale = 0.5f*max(0.8f-bias, 0.0f),
minshade = scale*max(ambient, mincolor);
LOCALPARAMF(lightscale, scale - minshade, scale, minshade + bias);
}
float curglow = glow;
if(glowpulse > 0)
{
float curpulse = lastmillis*glowpulse;
curpulse -= floor(curpulse);
curglow += glowdelta*2*fabs(curpulse - 0.5f);
}
LOCALPARAMF(maskscale, 0.5f*spec, 0.5f*curglow, 16*specglare, 4*glowglare);
LOCALPARAMF(texscroll, scrollu*lastmillis/1000.0f, scrollv*lastmillis/1000.0f);
if(envmapped()) LOCALPARAMF(envmapscale, envmapmin-envmapmax, envmapmax);
}
Shader *loadshader()
{
#define DOMODELSHADER(name, body) \
do { \
static Shader *name##shader = NULL; \
if(!name##shader) name##shader = useshaderbyname(#name); \
body; \
} while(0)
#define LOADMODELSHADER(name) DOMODELSHADER(name, return name##shader)
#define SETMODELSHADER(m, name) DOMODELSHADER(name, (m)->setshader(name##shader))
if(shader) return shader;
string opts;
int optslen = 0;
if(alphatested()) opts[optslen++] = 'a';
if(owner->tangents()) opts[optslen++] = 'q';
if(bumpmapped()) opts[optslen++] = 'n';
if(envmapped()) opts[optslen++] = 'e';
if(masked()) opts[optslen++] = 'm';
if(!fullbright && (masked() || spec>=0.01f)) opts[optslen++] = 's';
opts[optslen++] = '\0';
defformatstring(name, "model%s", opts);
shader = generateshader(name, "modelshader \"%s\"", opts);
return shader;
}
void cleanup()
{
if(shader && shader->standard) shader = NULL;
}
void preloadBIH()
{
if(tex->type&Texture::ALPHA && !tex->alphamask) loadalphamask(tex);
}
void preloadshader(bool force)
{
if(force) cleanup();
loadshader();
}
void setshader(mesh *m, const animstate *as)
{
m->setshader(loadshader());
}
void bind(mesh *b, const animstate *as)
{
if(!cullface && enablecullface) { glDisable(GL_CULL_FACE); enablecullface = false; }
else if(cullface && !enablecullface) { glEnable(GL_CULL_FACE); enablecullface = true; }
if(as->cur.anim&ANIM_NOSKIN)
{
if(enablealphablend) { glDisable(GL_BLEND); enablealphablend = false; }
if(shadowmapping) SETMODELSHADER(b, shadowmapcaster);
else /*if(as->cur.anim&ANIM_SHADOW)*/ SETMODELSHADER(b, notexturemodel);
return;
}
setshader(b, as);
setshaderparams(b, as);
int activetmu = 0;
if(tex!=lasttex)
{
glBindTexture(GL_TEXTURE_2D, tex->id);
lasttex = tex;
}
if(bumpmapped() && normalmap !=lastnormalmap)
{
glActiveTexture_(GL_TEXTURE3);
activetmu = 3;
glBindTexture(GL_TEXTURE_2D, normalmap->id);
lastnormalmap = normalmap;
}
if(tex->type&Texture::ALPHA)
{
if(alphablend)
{
if(!enablealphablend && !reflecting && !refracting)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
enablealphablend = true;
}
}
else if(enablealphablend) { glDisable(GL_BLEND); enablealphablend = false; }
}
else if(enablealphablend && transparent>=1) { glDisable(GL_BLEND); enablealphablend = false; }
if(masked() && masks!=lastmasks)
{
glActiveTexture_(GL_TEXTURE1);
activetmu = 1;
glBindTexture(GL_TEXTURE_2D, masks->id);
lastmasks = masks;
}
if(envmapped())
{
GLuint emtex = envmap ? envmap->id : closestenvmaptex;
if(lastenvmaptex!=emtex)
{
glActiveTexture_(GL_TEXTURE2);
activetmu = 2;
glBindTexture(GL_TEXTURE_CUBE_MAP, emtex);
lastenvmaptex = emtex;
}
}
if(activetmu != 0) glActiveTexture_(GL_TEXTURE0);
}
};
struct meshgroup;
struct mesh
{
meshgroup *group;
char *name;
bool noclip;
mesh() : group(NULL), name(NULL), noclip(false)
{
}
virtual ~mesh()
{
DELETEA(name);
}
virtual void calcbb(vec &bbmin, vec &bbmax, const matrix4x3 &m) {}
virtual void genBIH(BIH::mesh &m) {}
void genBIH(skin &s, vector<BIH::mesh> &bih, const matrix4x3 &t)
{
BIH::mesh &m = bih.add();
m.xform = t;
m.tex = s.tex;
if(s.tex->type&Texture::ALPHA) m.flags |= BIH::MESH_ALPHA;
if(noclip) m.flags |= BIH::MESH_NOCLIP;
if(s.cullface) m.flags |= BIH::MESH_CULLFACE;
genBIH(m);
while(bih.last().numtris > BIH::mesh::MAXTRIS)
{
BIH::mesh &overflow = bih.dup();
overflow.tris += BIH::mesh::MAXTRIS;
overflow.numtris -= BIH::mesh::MAXTRIS;
bih[bih.length()-2].numtris = BIH::mesh::MAXTRIS;
}
}
virtual void setshader(Shader *s)
{
if(glaring) s->setvariant(0, 1);
else s->set();
}
template<class V, class T> void smoothnorms(V *verts, int numverts, T *tris, int numtris, float limit, bool areaweight)
{
hashtable<vec, int> share;
int *next = new int[numverts];
memset(next, -1, numverts*sizeof(int));
loopi(numverts)
{
V &v = verts[i];
v.norm = vec(0, 0, 0);
int idx = share.access(v.pos, i);
if(idx != i) { next[i] = next[idx]; next[idx] = i; }
}
loopi(numtris)
{
T &t = tris[i];
V &v1 = verts[t.vert[0]], &v2 = verts[t.vert[1]], &v3 = verts[t.vert[2]];
vec norm;
norm.cross(vec(v2.pos).sub(v1.pos), vec(v3.pos).sub(v1.pos));
if(!areaweight) norm.normalize();
v1.norm.add(norm);
v2.norm.add(norm);
v3.norm.add(norm);
}
vec *norms = new vec[numverts];
memclear(norms, numverts);
loopi(numverts)
{
V &v = verts[i];
norms[i].add(v.norm);
if(next[i] >= 0)
{
float vlimit = limit*v.norm.magnitude();
for(int j = next[i]; j >= 0; j = next[j])
{
V &o = verts[j];
if(v.norm.dot(o.norm) >= vlimit*o.norm.magnitude())
{
norms[i].add(o.norm);
norms[j].add(v.norm);
}
}
}
}
loopi(numverts) verts[i].norm = norms[i].normalize();
delete[] next;
delete[] norms;
}
template<class V, class T> void buildnorms(V *verts, int numverts, T *tris, int numtris, bool areaweight)
{
loopi(numverts) verts[i].norm = vec(0, 0, 0);
loopi(numtris)
{
T &t = tris[i];
V &v1 = verts[t.vert[0]], &v2 = verts[t.vert[1]], &v3 = verts[t.vert[2]];
vec norm;
norm.cross(vec(v2.pos).sub(v1.pos), vec(v3.pos).sub(v1.pos));
if(!areaweight) norm.normalize();
v1.norm.add(norm);
v2.norm.add(norm);
v3.norm.add(norm);
}
loopi(numverts) verts[i].norm.normalize();
}
template<class V, class T> void buildnorms(V *verts, int numverts, T *tris, int numtris, bool areaweight, int numframes)
{
if(!numverts) return;
loopi(numframes) buildnorms(&verts[i*numverts], numverts, tris, numtris, areaweight);
}
static inline void fixqtangent(quat &q, float bt)
{
static const float bias = -1.5f/65535, biasscale = sqrtf(1 - bias*bias);
if(bt < 0)
{
if(q.w >= 0) q.neg();
if(q.w > bias) { q.mul3(biasscale); q.w = bias; }
}
else if(q.w < 0) q.neg();
}
template<class V> static inline void calctangent(V &v, const vec &n, const vec &t, float bt)
{
matrix3 m;
m.c = n;
m.a = t;
m.b.cross(m.c, m.a);
quat q(m);
fixqtangent(q, bt);
v.tangent = q;
}
template<class B, class V, class TC, class T> void calctangents(B *bumpverts, V *verts, TC *tcverts, int numverts, T *tris, int numtris, bool areaweight)
{
vec *tangent = new vec[2*numverts], *bitangent = tangent+numverts;
memclear(tangent, 2*numverts);
loopi(numtris)
{
const T &t = tris[i];
const vec &e0 = verts[t.vert[0]].pos;
vec e1 = vec(verts[t.vert[1]].pos).sub(e0), e2 = vec(verts[t.vert[2]].pos).sub(e0);
const vec2 &tc0 = tcverts[t.vert[0]].tc,
&tc1 = tcverts[t.vert[1]].tc,
&tc2 = tcverts[t.vert[2]].tc;
float u1 = tc1.x - tc0.x, v1 = tc1.y - tc0.y,
u2 = tc2.x - tc0.x, v2 = tc2.y - tc0.y;
vec u(e2), v(e2);
u.mul(v1).sub(vec(e1).mul(v2));
v.mul(u1).sub(vec(e1).mul(u2));
if(vec().cross(e2, e1).dot(vec().cross(v, u)) >= 0)
{
u.neg();
v.neg();
}
if(!areaweight)
{
u.normalize();
v.normalize();
}
loopj(3)
{
tangent[t.vert[j]].sub(u);
bitangent[t.vert[j]].add(v);
}
}
loopi(numverts)
{
const vec &n = verts[i].norm,
&t = tangent[i],
&bt = bitangent[i];
B &bv = bumpverts[i];
matrix3 m;
m.c = n;
(m.a = t).project(m.c).normalize();
m.b.cross(m.c, m.a);
quat q(m);
fixqtangent(q, m.b.dot(bt));
bv.tangent = q;
}
delete[] tangent;
}
template<class B, class V, class TC, class T> void calctangents(B *bumpverts, V *verts, TC *tcverts, int numverts, T *tris, int numtris, bool areaweight, int numframes)
{
loopi(numframes) calctangents(&bumpverts[i*numverts], &verts[i*numverts], tcverts, numverts, tris, numtris, areaweight);
}
};
struct meshgroup
{
meshgroup *next;
int shared;
char *name;
vector<mesh *> meshes;
meshgroup() : next(NULL), shared(0), name(NULL)
{
}
virtual ~meshgroup()
{
DELETEA(name);
meshes.deletecontents();
DELETEP(next);
}
virtual int findtag(const char *name) { return -1; }
virtual void concattagtransform(part *p, int i, const matrix4x3 &m, matrix4x3 &n) {}
void calcbb(vec &bbmin, vec &bbmax, const matrix4x3 &m)
{
loopv(meshes) meshes[i]->calcbb(bbmin, bbmax, m);
}
void genBIH(vector<skin> &skins, vector<BIH::mesh> &bih, const matrix4x3 &t)
{
loopv(meshes) meshes[i]->genBIH(skins[i], bih, t);
}
virtual void *animkey() { return this; }
virtual int totalframes() const { return 1; }
bool hasframe(int i) const { return i>=0 && i<totalframes(); }
bool hasframes(int i, int n) const { return i>=0 && i+n<=totalframes(); }
int clipframes(int i, int n) const { return min(n, totalframes() - i); }
virtual void cleanup() {}
virtual void preload(part *p) {}
virtual void render(const animstate *as, float pitch, const vec &axis, const vec &forward, dynent *d, part *p) {}
void bindpos(GLuint ebuf, GLuint vbuf, void *v, int stride)
{
if(lastebuf!=ebuf)
{
gle::bindebo(ebuf);
lastebuf = ebuf;
}
if(lastvbuf!=vbuf)
{
gle::bindvbo(vbuf);
if(!lastvbuf) gle::enablevertex();
gle::vertexpointer(stride, v);
lastvbuf = vbuf;
}
}
void bindtc(void *v, int stride)
{
if(!enabletc)
{
gle::enabletexcoord0();
enabletc = true;
}
if(lasttcbuf!=lastvbuf)
{
gle::texcoord0pointer(stride, v);
lasttcbuf = lastvbuf;
}
}
void bindnormals(void *v, int stride)
{
if(!enablenormals)
{
gle::enablenormal();
enablenormals = true;
}
if(lastnbuf!=lastvbuf)
{
gle::normalpointer(stride, v);
lastnbuf = lastvbuf;
}
}
void bindtangents(void *v, int stride)
{
if(!enabletangents)
{
gle::enabletangent();
enabletangents = true;
}
if(lastxbuf!=lastvbuf)
{
gle::tangentpointer(stride, v, GL_SHORT);
lastxbuf = lastvbuf;
}
}
void bindbones(void *wv, void *bv, int stride)
{
if(!enablebones)
{
gle::enableboneweight();
gle::enableboneindex();
enablebones = true;
}
if(lastbbuf!=lastvbuf)
{
gle::boneweightpointer(stride, wv);
gle::boneindexpointer(stride, bv);
lastbbuf = lastvbuf;
}
}
};
virtual meshgroup *loadmeshes(const char *name, va_list args) { return NULL; }
meshgroup *sharemeshes(const char *name, ...)
{
static hashnameset<meshgroup *> meshgroups;
if(!meshgroups.access(name))
{
va_list args;
va_start(args, name);
meshgroup *group = loadmeshes(name, args);
va_end(args);
if(!group) return NULL;
meshgroups.add(group);
}
return meshgroups[name];
}
struct linkedpart
{
part *p;
int tag, anim, basetime;
vec translate;
vec *pos;
matrix4 matrix;
linkedpart() : p(NULL), tag(-1), anim(-1), basetime(0), translate(0, 0, 0), pos(NULL) {}
};
struct part
{
animmodel *model;
int index;
meshgroup *meshes;
vector<linkedpart> links;
vector<skin> skins;
vector<animspec> *anims[MAXANIMPARTS];
int numanimparts;
float pitchscale, pitchoffset, pitchmin, pitchmax;
vec translate;
part(animmodel *model, int index = 0) : model(model), index(index), meshes(NULL), numanimparts(1), pitchscale(1), pitchoffset(0), pitchmin(0), pitchmax(0), translate(0, 0, 0)
{
loopk(MAXANIMPARTS) anims[k] = NULL;
}
virtual ~part()
{
loopk(MAXANIMPARTS) DELETEA(anims[k]);
}
virtual void cleanup()
{
if(meshes) meshes->cleanup();
loopv(skins) skins[i].cleanup();
}
void calcbb(vec &bbmin, vec &bbmax, const matrix4x3 &m)
{
matrix4x3 t = m;
t.scale(model->scale);
t.translate(translate);
meshes->calcbb(bbmin, bbmax, t);
loopv(links)
{
matrix4x3 n;
meshes->concattagtransform(this, links[i].tag, m, n);
n.translate(links[i].translate, model->scale);
links[i].p->calcbb(bbmin, bbmax, n);
}
}
void genBIH(vector<BIH::mesh> &bih, const matrix4x3 &m)
{
matrix4x3 t = m;
t.scale(model->scale);
t.translate(translate);
meshes->genBIH(skins, bih, t);
loopv(links)
{
matrix4x3 n;
meshes->concattagtransform(this, links[i].tag, m, n);
n.translate(links[i].translate, model->scale);
links[i].p->genBIH(bih, n);
}
}
bool link(part *p, const char *tag, const vec &translate = vec(0, 0, 0), int anim = -1, int basetime = 0, vec *pos = NULL)
{
int i = meshes ? meshes->findtag(tag) : -1;
if(i<0)
{
loopv(links) if(links[i].p && links[i].p->link(p, tag, translate, anim, basetime, pos)) return true;
return false;
}
linkedpart &l = links.add();
l.p = p;
l.tag = i;
l.anim = anim;
l.basetime = basetime;
l.translate = translate;
l.pos = pos;
return true;
}
bool unlink(part *p)
{
loopvrev(links) if(links[i].p==p) { links.remove(i, 1); return true; }
loopv(links) if(links[i].p && links[i].p->unlink(p)) return true;
return false;
}
void initskins(Texture *tex = notexture, Texture *masks = notexture, int limit = 0)
{
if(!limit)
{
if(!meshes) return;
limit = meshes->meshes.length();
}
while(skins.length() < limit)
{
skin &s = skins.add();
s.owner = this;
s.tex = tex;
s.masks = masks;
}
}
bool envmapped()
{
loopv(skins) if(skins[i].envmapped()) return true;
return false;
}
bool tangents()
{
loopv(skins) if(skins[i].tangents()) return true;
return false;
}
void preloadBIH()
{
loopv(skins) skins[i].preloadBIH();
}
void preloadshaders(bool force)
{
loopv(skins) skins[i].preloadshader(force);
}
void preloadmeshes()
{
if(meshes) meshes->preload(this);
}
virtual void getdefaultanim(animinfo &info, int anim, uint varseed, dynent *d)
{
info.frame = 0;
info.range = 1;
}
bool calcanim(int animpart, int anim, int basetime, int basetime2, dynent *d, int interp, animinfo &info, int &aitime)
{
uint varseed = uint((size_t)d);
info.anim = anim;
info.basetime = basetime;
info.varseed = varseed;
info.speed = anim&ANIM_SETSPEED ? basetime2 : 100.0f;
if((anim&ANIM_INDEX)==ANIM_ALL)
{
info.frame = 0;
info.range = meshes->totalframes();
}
else
{
animspec *spec = NULL;
if(anims[animpart])
{
int primaryidx = anim&ANIM_INDEX;
if(primaryidx < NUMANIMS)
{
vector<animspec> &primary = anims[animpart][primaryidx];
if(primary.length()) spec = &primary[uint(varseed + basetime)%primary.length()];
}
if((anim>>ANIM_SECONDARY)&(ANIM_INDEX|ANIM_DIR))
{
int secondaryidx = (anim>>ANIM_SECONDARY)&ANIM_INDEX;
if(secondaryidx < NUMANIMS)
{
vector<animspec> &secondary = anims[animpart][secondaryidx];
if(secondary.length())
{
animspec &spec2 = secondary[uint(varseed + basetime2)%secondary.length()];
if(!spec || spec2.priority > spec->priority)
{
spec = &spec2;
info.anim >>= ANIM_SECONDARY;
info.basetime = basetime2;
}
}
}
}
}
if(spec)
{
info.frame = spec->frame;
info.range = spec->range;
if(spec->speed>0) info.speed = 1000.0f/spec->speed;
}
else getdefaultanim(info, anim, uint(varseed + info.basetime), d);
}
info.anim &= (1<<ANIM_SECONDARY)-1;
info.anim |= anim&ANIM_FLAGS;
if((info.anim&ANIM_CLAMP) != ANIM_CLAMP)
{
if(info.anim&(ANIM_LOOP|ANIM_START|ANIM_END))
{
info.anim &= ~ANIM_SETTIME;
if(!info.basetime) info.basetime = -((int)(size_t)d&0xFFF);
}
if(info.anim&(ANIM_START|ANIM_END))
{
if(info.anim&ANIM_END) info.frame += info.range-1;
info.range = 1;
}
}
if(!meshes->hasframes(info.frame, info.range))
{
if(!meshes->hasframe(info.frame)) return false;
info.range = meshes->clipframes(info.frame, info.range);
}
if(d && interp>=0)
{
animinterpinfo &ai = d->animinterp[interp];
if((info.anim&ANIM_CLAMP)==ANIM_CLAMP) aitime = min(aitime, int(info.range*info.speed*0.5e-3f));
void *ak = meshes->animkey();
if(d->ragdoll && !(anim&ANIM_RAGDOLL))
{
ai.prev.range = ai.cur.range = 0;
ai.lastswitch = -1;
}
else if(ai.lastmodel!=ak || ai.lastswitch<0 || lastmillis-d->lastrendered>aitime)
{
ai.prev = ai.cur = info;
ai.lastswitch = lastmillis-aitime*2;
}
else if(ai.cur!=info)
{
if(lastmillis-ai.lastswitch>aitime/2) ai.prev = ai.cur;
ai.cur = info;
ai.lastswitch = lastmillis;
}
else if(info.anim&ANIM_SETTIME) ai.cur.basetime = info.basetime;
ai.lastmodel = ak;
}
return true;
}
void render(int anim, int basetime, int basetime2, float pitch, const vec &axis, const vec &forward, dynent *d)
{
animstate as[MAXANIMPARTS];
render(anim, basetime, basetime2, pitch, axis, forward, d, as);
}
void render(int anim, int basetime, int basetime2, float pitch, const vec &axis, const vec &forward, dynent *d, animstate *as)
{
if(!(anim&ANIM_REUSE)) loopi(numanimparts)
{
animinfo info;
int interp = d && index+numanimparts<=MAXANIMPARTS ? index+i : -1, aitime = animationinterpolationtime;
if(!calcanim(i, anim, basetime, basetime2, d, interp, info, aitime)) return;
animstate &p = as[i];
p.owner = this;
p.cur.setframes(info);
p.interp = 1;
if(interp>=0 && d->animinterp[interp].prev.range>0)
{
int diff = lastmillis-d->animinterp[interp].lastswitch;
if(diff<aitime)
{
p.prev.setframes(d->animinterp[interp].prev);
p.interp = diff/float(aitime);
}
}
}
vec oaxis, oforward;
matrixstack[matrixpos].transposedtransformnormal(axis, oaxis);
float pitchamount = pitchscale*pitch + pitchoffset;
if((pitchmin || pitchmax) && pitchmin <= pitchmax) pitchamount = clamp(pitchamount, pitchmin, pitchmax);
if(as->cur.anim&ANIM_NOPITCH || (as->interp < 1 && as->prev.anim&ANIM_NOPITCH))
pitchamount *= (as->cur.anim&ANIM_NOPITCH ? 0 : as->interp) + (as->interp < 1 && as->prev.anim&ANIM_NOPITCH ? 0 : 1-as->interp);
if(pitchamount)
{
++matrixpos;
matrixstack[matrixpos] = matrixstack[matrixpos-1];
matrixstack[matrixpos].rotate(pitchamount*RAD, oaxis);
}
matrixstack[matrixpos].transposedtransformnormal(forward, oforward);
if(!(anim&ANIM_NORENDER))
{
matrix4 modelmatrix;
modelmatrix.mul(shadowmapping ? shadowmatrix : camprojmatrix, matrixstack[matrixpos]);
if(model->scale!=1) modelmatrix.scale(model->scale);
if(!translate.iszero()) modelmatrix.translate(translate);
GLOBALPARAM(modelmatrix, modelmatrix);
if(!(anim&ANIM_NOSKIN))
{
if(envmapped()) GLOBALPARAM(modelworld, matrix3(matrixstack[matrixpos]));
vec odir, ocampos;
matrixstack[matrixpos].transposedtransformnormal(lightdir, odir);
GLOBALPARAM(lightdir, odir);
matrixstack[matrixpos].transposedtransform(camera1->o, ocampos);
ocampos.div(model->scale).sub(translate);
GLOBALPARAM(modelcamera, ocampos);
}
}
meshes->render(as, pitch, oaxis, oforward, d, this);
if(!(anim&ANIM_REUSE))
{
loopv(links)
{
linkedpart &link = links[i];
link.matrix.translate(links[i].translate, model->scale);
matrixpos++;
matrixstack[matrixpos].mul(matrixstack[matrixpos-1], link.matrix);
if(link.pos) *link.pos = matrixstack[matrixpos].gettranslation();
if(!link.p)
{
matrixpos--;
continue;
}
int nanim = anim, nbasetime = basetime, nbasetime2 = basetime2;
if(link.anim>=0)
{
nanim = link.anim | (anim&ANIM_FLAGS);
nbasetime = link.basetime;
nbasetime2 = 0;
}
link.p->render(nanim, nbasetime, nbasetime2, pitch, axis, forward, d);
matrixpos--;
}
}
if(pitchamount) matrixpos--;
}
void setanim(int animpart, int num, int frame, int range, float speed, int priority = 0)
{
if(animpart<0 || animpart>=MAXANIMPARTS) return;
if(frame<0 || range<=0 || !meshes || !meshes->hasframes(frame, range))
{
conoutf(CON_ERROR, "invalid frame %d, range %d in model %s", frame, range, model->name);
return;
}
if(!anims[animpart]) anims[animpart] = new vector<animspec>[NUMANIMS];
animspec &spec = anims[animpart][num].add();
spec.frame = frame;
spec.range = range;
spec.speed = speed;
spec.priority = priority;
}
virtual void loaded()
{
meshes->shared++;
loopv(skins) skins[i].setkey();
}
};
enum
{
LINK_TAG = 0,
LINK_COOP,
LINK_REUSE
};
virtual int linktype(animmodel *m) const { return LINK_TAG; }
void render(int anim, int basetime, int basetime2, float pitch, const vec &axis, const vec &forward, dynent *d, modelattach *a)
{
int numtags = 0;
if(a)
{
int index = parts.last()->index + parts.last()->numanimparts;
for(int i = 0; a[i].tag; i++)
{
numtags++;
animmodel *m = (animmodel *)a[i].m;
if(!m)
{
if(a[i].pos) link(NULL, a[i].tag, vec(0, 0, 0), 0, 0, a[i].pos);
continue;
}
part *p = m->parts[0];
switch(linktype(m))
{
case LINK_TAG:
p->index = link(p, a[i].tag, vec(0, 0, 0), a[i].anim, a[i].basetime, a[i].pos) ? index : -1;
break;
case LINK_COOP:
p->index = index;
break;
default:
continue;
}
index += p->numanimparts;
}
}
animstate as[MAXANIMPARTS];
parts[0]->render(anim, basetime, basetime2, pitch, axis, forward, d, as);
if(a) for(int i = numtags-1; i >= 0; i--)
{
animmodel *m = (animmodel *)a[i].m;
if(!m)
{
if(a[i].pos) unlink(NULL);
continue;
}
part *p = m->parts[0];
switch(linktype(m))
{
case LINK_TAG:
if(p->index >= 0) unlink(p);
p->index = 0;
break;
case LINK_COOP:
p->render(anim, basetime, basetime2, pitch, axis, forward, d);
p->index = 0;
break;
case LINK_REUSE:
p->render(anim | ANIM_REUSE, basetime, basetime2, pitch, axis, forward, d, as);
break;
}
}
}
void render(int anim, int basetime, int basetime2, const vec &o, float yaw, float pitch, dynent *d, modelattach *a, const vec &color, const vec &dir, float trans)
{
yaw += spinyaw*lastmillis/1000.0f;
pitch += offsetpitch + spinpitch*lastmillis/1000.0f;
vec axis(0, -1, 0), forward(1, 0, 0);
matrixpos = 0;
matrixstack[0].identity();
if(!d || !d->ragdoll || anim&ANIM_RAGDOLL)
{
matrixstack[0].settranslation(o);
matrixstack[0].rotate_around_z(yaw*RAD);
matrixstack[0].transformnormal(vec(axis), axis);
matrixstack[0].transformnormal(vec(forward), forward);
if(offsetyaw) matrixstack[0].rotate_around_z(offsetyaw*RAD);
}
else pitch = 0;
if(anim&ANIM_NORENDER)
{
render(anim, basetime, basetime2, pitch, axis, forward, d, a);
if(d) d->lastrendered = lastmillis;
return;
}
if(!(anim&ANIM_NOSKIN))
{
transparent = trans;
lightdir = dir;
lightcolor = color;
if(envmapped())
{
setupenvmap:
closestenvmaptex = lookupenvmap(closestenvmap(o));
GLOBALPARAM(lightdirworld, dir);
}
else if(a) for(int i = 0; a[i].tag; i++) if(a[i].m && a[i].m->envmapped()) goto setupenvmap;
}
if(depthoffset && !enabledepthoffset)
{
enablepolygonoffset(GL_POLYGON_OFFSET_FILL);
enabledepthoffset = true;
}
if(transparent<1)
{
if(anim&ANIM_GHOST)
{
glDepthFunc(GL_GREATER);
glDepthMask(GL_FALSE);
}
else if(alphadepth)
{
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
render(anim|ANIM_NOSKIN, basetime, basetime2, pitch, axis, forward, d, a);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, fading ? GL_FALSE : GL_TRUE);
glDepthFunc(GL_LEQUAL);
}
if(!enablealphablend)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
enablealphablend = true;
}
}
render(anim, basetime, basetime2, pitch, axis, forward, d, a);
if(transparent<1 && (alphadepth || anim&ANIM_GHOST))
{
glDepthFunc(GL_LESS);
if(anim&ANIM_GHOST) glDepthMask(GL_TRUE);
}
if(d) d->lastrendered = lastmillis;
}
vector<part *> parts;
animmodel(const char *name) : model(name)
{
}
~animmodel()
{
parts.deletecontents();
}
void cleanup()
{
loopv(parts) parts[i]->cleanup();
}
virtual void flushpart() {}
part &addpart()
{
flushpart();
part *p = new part(this, parts.length());
parts.add(p);
return *p;
}
void initmatrix(matrix4x3 &m)
{
m.identity();
if(offsetyaw) m.rotate_around_z(offsetyaw*RAD);
if(offsetpitch) m.rotate_around_y(-offsetpitch*RAD);
}
void genBIH(vector<BIH::mesh> &bih)
{
if(parts.empty()) return;
matrix4x3 m;
initmatrix(m);
parts[0]->genBIH(bih, m);
}
void preloadBIH()
{
model::preloadBIH();
if(bih) loopv(parts) parts[i]->preloadBIH();
}
BIH *setBIH()
{
if(bih) return bih;
vector<BIH::mesh> meshes;
genBIH(meshes);
bih = new BIH(meshes);
return bih;
}
bool link(part *p, const char *tag, const vec &translate = vec(0, 0, 0), int anim = -1, int basetime = 0, vec *pos = NULL)
{
if(parts.empty()) return false;
return parts[0]->link(p, tag, translate, anim, basetime, pos);
}
bool unlink(part *p)
{
if(parts.empty()) return false;
return parts[0]->unlink(p);
}
bool envmapped()
{
loopv(parts) if(parts[i]->envmapped()) return true;
return false;
}
virtual bool flipy() const { return false; }
virtual bool loadconfig() { return false; }
virtual bool loaddefaultparts() { return false; }
virtual void startload() {}
virtual void endload() {}
bool load()
{
startload();
bool success = loadconfig() && parts.length(); // configured model, will call the model commands below
if(!success)
success = loaddefaultparts(); // model without configuration, try default tris and skin
flushpart();
endload();
if(flipy()) translate.y = -translate.y;
if(!success) return false;
loopv(parts) if(!parts[i]->meshes) return false;
loaded();
return true;
}
void preloadshaders(bool force)
{
loopv(parts) parts[i]->preloadshaders(force);
}
void preloadmeshes()
{
loopv(parts) parts[i]->preloadmeshes();
}
void setshader(Shader *shader)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].shader = shader;
}
void setenvmap(float envmapmin, float envmapmax, Texture *envmap)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins)
{
skin &s = parts[i]->skins[j];
if(envmapmax)
{
s.envmapmin = envmapmin;
s.envmapmax = envmapmax;
}
if(envmap) s.envmap = envmap;
}
}
void setspec(float spec)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].spec = spec;
}
void setambient(float ambient)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].ambient = ambient;
}
void setglow(float glow, float delta, float pulse)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins)
{
skin &s = parts[i]->skins[j];
s.glow = glow;
s.glowdelta = delta;
s.glowpulse = pulse;
}
}
void setglare(float specglare, float glowglare)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins)
{
skin &s = parts[i]->skins[j];
s.specglare = specglare;
s.glowglare = glowglare;
}
}
void setalphatest(float alphatest)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].alphatest = alphatest;
}
void setalphablend(bool alphablend)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].alphablend = alphablend;
}
void setfullbright(float fullbright)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].fullbright = fullbright;
}
void setcullface(bool cullface)
{
if(parts.empty()) loaddefaultparts();
loopv(parts) loopvj(parts[i]->skins) parts[i]->skins[j].cullface = cullface;
}
void calcbb(vec ¢er, vec &radius)
{
if(parts.empty()) return;
vec bbmin(1e16f, 1e16f, 1e16f), bbmax(-1e16f, -1e16f, -1e16f);
matrix4x3 m;
initmatrix(m);
parts[0]->calcbb(bbmin, bbmax, m);
radius = bbmax;
radius.sub(bbmin);
radius.mul(0.5f);
center = bbmin;
center.add(radius);
}
virtual void loaded()
{
scale /= 4;
if(parts.length()) parts[0]->translate = translate;
loopv(parts) parts[i]->loaded();
}
static bool enabletc, enablealphablend, enablecullface, enablenormals, enabletangents, enablebones, enabledepthoffset;
static vec lightdir, lightcolor;
static float transparent, lastalphatest;
static GLuint lastvbuf, lasttcbuf, lastnbuf, lastxbuf, lastbbuf, lastebuf, lastenvmaptex, closestenvmaptex;
static Texture *lasttex, *lastmasks, *lastnormalmap;
static int matrixpos;
static matrix4 matrixstack[64];
void startrender()
{
enabletc = enablealphablend = enablenormals = enabletangents = enablebones = enabledepthoffset = false;
enablecullface = true;
lastalphatest = -1;
lastvbuf = lasttcbuf = lastxbuf = lastnbuf = lastbbuf = lastebuf = lastenvmaptex = closestenvmaptex = 0;
lasttex = lastmasks = lastnormalmap = NULL;
transparent = 1;
shaderparamskey::invalidate();
}
static void disablebones()
{
gle::disableboneweight();
gle::disableboneindex();
enablebones = false;
}
static void disabletangents()
{
gle::disabletangent();
enabletangents = false;
}
static void disabletc()
{
gle::disabletexcoord0();
enabletc = false;
}
static void disablenormals()
{
gle::disablenormal();
enablenormals = false;
}
static void disablevbo()
{
if(lastebuf) gle::clearebo();
if(lastvbuf)
{
gle::clearvbo();
gle::disablevertex();
}
if(enabletc) disabletc();
if(enablenormals) disablenormals();
if(enabletangents) disabletangents();
if(enablebones) disablebones();
lastvbuf = lasttcbuf = lastxbuf = lastnbuf = lastbbuf = lastebuf = 0;
}
void endrender()
{
if(lastvbuf || lastebuf) disablevbo();
if(enablealphablend) glDisable(GL_BLEND);
if(!enablecullface) glEnable(GL_CULL_FACE);
if(enabledepthoffset) disablepolygonoffset(GL_POLYGON_OFFSET_FILL);
}
};
bool animmodel::enabletc = false, animmodel::enablealphablend = false,
animmodel::enablecullface = true,
animmodel::enablenormals = false, animmodel::enabletangents = false, animmodel::enablebones = false, animmodel::enabledepthoffset = false;
vec animmodel::lightdir(0, 0, 1), animmodel::lightcolor(1, 1, 1);
float animmodel::transparent = 1, animmodel::lastalphatest = -1;
GLuint animmodel::lastvbuf = 0, animmodel::lasttcbuf = 0, animmodel::lastnbuf = 0, animmodel::lastxbuf = 0, animmodel::lastbbuf = 0,
animmodel::lastebuf = 0, animmodel::lastenvmaptex = 0, animmodel::closestenvmaptex = 0;
Texture *animmodel::lasttex = NULL, *animmodel::lastmasks = NULL, *animmodel::lastnormalmap = NULL;
int animmodel::matrixpos = 0;
matrix4 animmodel::matrixstack[64];
static inline uint hthash(const animmodel::shaderparams &k)
{
return memhash(&k, sizeof(k));
}
static inline bool htcmp(const animmodel::shaderparams &x, const animmodel::shaderparams &y)
{
return !memcmp(&x, &y, sizeof(animmodel::shaderparams));
}
hashtable<animmodel::shaderparams, animmodel::shaderparamskey> animmodel::shaderparamskey::keys;
int animmodel::shaderparamskey::firstversion = 0, animmodel::shaderparamskey::lastversion = 1;
template<class MDL, class BASE> struct modelloader : BASE
{
static MDL *loading;
static string dir;
modelloader(const char *name) : BASE(name) {}
static bool animated() { return true; }
static bool multiparted() { return true; }
static bool multimeshed() { return true; }
void startload()
{
loading = (MDL *)this;
}
void endload()
{
loading = NULL;
}
bool loadconfig()
{
formatstring(dir, "packages/models/%s", BASE::name);
defformatstring(cfgname, "packages/models/%s/%s.cfg", BASE::name, MDL::formatname());
identflags &= ~IDF_PERSIST;
bool success = execfile(cfgname, false);
identflags |= IDF_PERSIST;
return success;
}
};
template<class MDL, class BASE> MDL *modelloader<MDL, BASE>::loading = NULL;
template<class MDL, class BASE> string modelloader<MDL, BASE>::dir = {'\0'}; // crashes clang if "" is used here
template<class MDL, class MESH> struct modelcommands
{
typedef struct MDL::part part;
typedef struct MDL::skin skin;
static void setdir(char *name)
{
if(!MDL::loading) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; }
formatstring(MDL::dir, "packages/models/%s", name);
}
#define loopmeshes(meshname, m, body) \
if(!MDL::loading || MDL::loading->parts.empty()) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; } \
part &mdl = *MDL::loading->parts.last(); \
if(!mdl.meshes) return; \
loopv(mdl.meshes->meshes) \
{ \
MESH &m = *(MESH *)mdl.meshes->meshes[i]; \
if(!strcmp(meshname, "*") || (m.name && !strcmp(m.name, meshname))) \
{ \
body; \
} \
}
#define loopskins(meshname, s, body) loopmeshes(meshname, m, { skin &s = mdl.skins[i]; body; })
static void setskin(char *meshname, char *tex, char *masks, float *envmapmax, float *envmapmin)
{
loopskins(meshname, s,
s.tex = textureload(makerelpath(MDL::dir, tex), 0, true, false);
if(*masks)
{
s.masks = textureload(makerelpath(MDL::dir, masks), 0, true, false);
s.envmapmax = *envmapmax;
s.envmapmin = *envmapmin;
}
);
}
static void setspec(char *meshname, int *percent)
{
float spec = 1.0f;
if(*percent>0) spec = *percent/100.0f;
else if(*percent<0) spec = 0.0f;
loopskins(meshname, s, s.spec = spec);
}
static void setambient(char *meshname, int *percent)
{
float ambient = 0.3f;
if(*percent>0) ambient = *percent/100.0f;
else if(*percent<0) ambient = 0.0f;
loopskins(meshname, s, s.ambient = ambient);
}
static void setglow(char *meshname, int *percent, int *delta, float *pulse)
{
float glow = 3.0f, glowdelta = *delta/100.0f, glowpulse = *pulse > 0 ? *pulse/1000.0f : 0;
if(*percent>0) glow = *percent/100.0f;
else if(*percent<0) glow = 0.0f;
glowdelta -= glow;
loopskins(meshname, s, { s.glow = glow; s.glowdelta = glowdelta; s.glowpulse = glowpulse; });
}
static void setglare(char *meshname, float *specglare, float *glowglare)
{
loopskins(meshname, s, { s.specglare = *specglare; s.glowglare = *glowglare; });
}
static void setalphatest(char *meshname, float *cutoff)
{
loopskins(meshname, s, s.alphatest = max(0.0f, min(1.0f, *cutoff)));
}
static void setalphablend(char *meshname, int *blend)
{
loopskins(meshname, s, s.alphablend = *blend!=0);
}
static void setcullface(char *meshname, int *cullface)
{
loopskins(meshname, s, s.cullface = *cullface!=0);
}
static void setenvmap(char *meshname, char *envmap)
{
Texture *tex = cubemapload(envmap);
loopskins(meshname, s, s.envmap = tex);
}
static void setbumpmap(char *meshname, char *normalmapfile)
{
Texture *normalmaptex = textureload(makerelpath(MDL::dir, normalmapfile), 0, true, false);
loopskins(meshname, s, s.normalmap = normalmaptex);
}
static void setfullbright(char *meshname, float *fullbright)
{
loopskins(meshname, s, s.fullbright = *fullbright);
}
static void setshader(char *meshname, char *shader)
{
loopskins(meshname, s, s.shader = lookupshaderbyname(shader));
}
static void setscroll(char *meshname, float *scrollu, float *scrollv)
{
loopskins(meshname, s, { s.scrollu = *scrollu; s.scrollv = *scrollv; });
}
static void setnoclip(char *meshname, int *noclip)
{
loopmeshes(meshname, m, m.noclip = *noclip!=0);
}
static void setlink(int *parent, int *child, char *tagname, float *x, float *y, float *z)
{
if(!MDL::loading) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; }
if(!MDL::loading->parts.inrange(*parent) || !MDL::loading->parts.inrange(*child)) { conoutf(CON_ERROR, "no models loaded to link"); return; }
if(!MDL::loading->parts[*parent]->link(MDL::loading->parts[*child], tagname, vec(*x, *y, *z))) conoutf(CON_ERROR, "could not link model %s", MDL::loading->name);
}
template<class F> void modelcommand(F *fun, const char *suffix, const char *args)
{
defformatstring(name, "%s%s", MDL::formatname(), suffix);
addcommand(newstring(name), (void (*)())fun, args);
}
modelcommands()
{
modelcommand(setdir, "dir", "s");
if(MDL::multimeshed())
{
modelcommand(setskin, "skin", "sssff");
modelcommand(setspec, "spec", "si");
modelcommand(setambient, "ambient", "si");
modelcommand(setglow, "glow", "siif");
modelcommand(setglare, "glare", "sff");
modelcommand(setalphatest, "alphatest", "sf");
modelcommand(setalphablend, "alphablend", "si");
modelcommand(setcullface, "cullface", "si");
modelcommand(setenvmap, "envmap", "ss");
modelcommand(setbumpmap, "bumpmap", "ss");
modelcommand(setfullbright, "fullbright", "sf");
modelcommand(setshader, "shader", "ss");
modelcommand(setscroll, "scroll", "sff");
modelcommand(setnoclip, "noclip", "si");
}
if(MDL::multiparted()) modelcommand(setlink, "link", "iisfff");
}
};
|