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
|
struct vertmodel : animmodel
{
struct vert { vec pos, norm; };
struct vvert { vec pos; vec2 tc; };
struct vvertn : vvert { vec norm; };
struct vvertbump : vvert { squat tangent; };
struct tcvert { vec2 tc; };
struct bumpvert { vec4 tangent; };
struct tri { ushort vert[3]; };
struct vbocacheentry
{
GLuint vbuf;
animstate as;
int millis;
vbocacheentry() : vbuf(0) { as.cur.fr1 = as.prev.fr1 = -1; }
};
struct vertmesh : mesh
{
vert *verts;
tcvert *tcverts;
bumpvert *bumpverts;
tri *tris;
int numverts, numtris;
int voffset, eoffset, elen;
ushort minvert, maxvert;
vertmesh() : verts(0), tcverts(0), bumpverts(0), tris(0)
{
}
virtual ~vertmesh()
{
DELETEA(verts);
DELETEA(tcverts);
DELETEA(bumpverts);
DELETEA(tris);
}
void smoothnorms(float limit = 0, bool areaweight = true)
{
if(((vertmeshgroup *)group)->numframes == 1) mesh::smoothnorms(verts, numverts, tris, numtris, limit, areaweight);
else buildnorms(areaweight);
}
void buildnorms(bool areaweight = true)
{
mesh::buildnorms(verts, numverts, tris, numtris, areaweight, ((vertmeshgroup *)group)->numframes);
}
void calctangents(bool areaweight = true)
{
if(bumpverts) return;
bumpverts = new bumpvert[((vertmeshgroup *)group)->numframes*numverts];
mesh::calctangents(bumpverts, verts, tcverts, numverts, tris, numtris, areaweight, ((vertmeshgroup *)group)->numframes);
}
void calcbb(vec &bbmin, vec &bbmax, const matrix4x3 &m)
{
loopj(numverts)
{
vec v = m.transform(verts[j].pos);
loopi(3)
{
bbmin[i] = min(bbmin[i], v[i]);
bbmax[i] = max(bbmax[i], v[i]);
}
}
}
void genBIH(BIH::mesh &m)
{
m.tris = (const BIH::tri *)tris;
m.numtris = numtris;
m.pos = (const uchar *)&verts->pos;
m.posstride = sizeof(vert);
m.tc = (const uchar *)&tcverts->tc;
m.tcstride = sizeof(tcvert);
}
static inline void assignvert(vvertn &vv, int j, tcvert &tc, vert &v)
{
vv.pos = v.pos;
vv.norm = v.norm;
vv.tc = tc.tc;
}
inline void assignvert(vvertbump &vv, int j, tcvert &tc, vert &v)
{
vv.pos = v.pos;
vv.tc = tc.tc;
vv.tangent = bumpverts[j].tangent;
}
template<class T>
int genvbo(vector<ushort> &idxs, int offset, vector<T> &vverts, int *htdata, int htlen)
{
voffset = offset;
eoffset = idxs.length();
minvert = 0xFFFF;
loopi(numtris)
{
tri &t = tris[i];
loopj(3)
{
int index = t.vert[j];
tcvert &tc = tcverts[index];
vert &v = verts[index];
T vv;
assignvert(vv, index, tc, v);
int htidx = hthash(v.pos)&(htlen-1);
loopk(htlen)
{
int &vidx = htdata[(htidx+k)&(htlen-1)];
if(vidx < 0) { vidx = idxs.add(ushort(vverts.length())); vverts.add(vv); break; }
else if(!memcmp(&vverts[vidx], &vv, sizeof(vv))) { minvert = min(minvert, idxs.add(ushort(vidx))); break; }
}
}
}
minvert = min(minvert, ushort(voffset));
maxvert = max(minvert, ushort(vverts.length()-1));
elen = idxs.length()-eoffset;
return vverts.length()-voffset;
}
int genvbo(vector<ushort> &idxs, int offset)
{
voffset = offset;
eoffset = idxs.length();
loopi(numtris)
{
tri &t = tris[i];
loopj(3) idxs.add(voffset+t.vert[j]);
}
minvert = voffset;
maxvert = voffset + numverts-1;
elen = idxs.length()-eoffset;
return numverts;
}
template<class T>
static inline void fillvert(T &vv, int j, tcvert &tc, vert &v)
{
vv.tc = tc.tc;
}
template<class T>
void fillverts(T *vdata)
{
vdata += voffset;
loopi(numverts) fillvert(vdata[i], i, tcverts[i], verts[i]);
}
void interpverts(const animstate &as, bool tangents, void * RESTRICT vdata, skin &s)
{
const vert * RESTRICT vert1 = &verts[as.cur.fr1 * numverts],
* RESTRICT vert2 = &verts[as.cur.fr2 * numverts],
* RESTRICT pvert1 = as.interp<1 ? &verts[as.prev.fr1 * numverts] : NULL,
* RESTRICT pvert2 = as.interp<1 ? &verts[as.prev.fr2 * numverts] : NULL;
#define ipvert(attrib) v.attrib.lerp(vert1[i].attrib, vert2[i].attrib, as.cur.t)
#define ipbvert(attrib, type) v.attrib.lerp(bvert1[i].attrib, bvert2[i].attrib, as.cur.t)
#define ipvertp(attrib) v.attrib.lerp(pvert1[i].attrib, pvert2[i].attrib, as.prev.t).lerp(vec().lerp(vert1[i].attrib, vert2[i].attrib, as.cur.t), as.interp)
#define ipbvertp(attrib, type) v.attrib.lerp(type().lerp(bpvert1[i].attrib, bpvert2[i].attrib, as.prev.t), type().lerp(bvert1[i].attrib, bvert2[i].attrib, as.cur.t), as.interp)
#define iploop(type, body) \
loopi(numverts) \
{ \
type &v = ((type * RESTRICT)vdata)[i]; \
body; \
}
if(tangents)
{
const bumpvert * RESTRICT bvert1 = &bumpverts[as.cur.fr1 * numverts],
* RESTRICT bvert2 = &bumpverts[as.cur.fr2 * numverts],
* RESTRICT bpvert1 = as.interp<1 ? &bumpverts[as.prev.fr1 * numverts] : NULL,
* RESTRICT bpvert2 = as.interp<1 ? &bumpverts[as.prev.fr2 * numverts] : NULL;
if(as.interp<1) iploop(vvertbump, { ipvertp(pos); ipbvertp(tangent, vec4); })
else iploop(vvertbump, { ipvert(pos); ipbvert(tangent, vec4); })
}
else
{
if(as.interp<1) iploop(vvertn, { ipvertp(pos); ipvertp(norm); })
else iploop(vvertn, { ipvert(pos); ipvert(norm); })
}
#undef iploop
#undef ipvert
#undef ipbvert
#undef ipvertp
#undef ipbvertp
}
void render(const animstate *as, skin &s, vbocacheentry &vc)
{
if(!Shader::lastshader) return;
glDrawRangeElements_(GL_TRIANGLES, minvert, maxvert, elen, GL_UNSIGNED_SHORT, &((vertmeshgroup *)group)->edata[eoffset]);
glde++;
xtravertsva += numverts;
}
};
struct tag
{
char *name;
matrix4x3 transform;
tag() : name(NULL) {}
~tag() { DELETEA(name); }
};
struct vertmeshgroup : meshgroup
{
int numframes;
tag *tags;
int numtags;
static const int MAXVBOCACHE = 16;
vbocacheentry vbocache[MAXVBOCACHE];
ushort *edata;
GLuint ebuf;
bool vtangents;
int vlen, vertsize;
uchar *vdata;
vertmeshgroup() : numframes(0), tags(NULL), numtags(0), edata(NULL), ebuf(0), vtangents(false), vlen(0), vertsize(0), vdata(NULL)
{
}
virtual ~vertmeshgroup()
{
DELETEA(tags);
if(ebuf) glDeleteBuffers_(1, &ebuf);
loopi(MAXVBOCACHE)
{
if(vbocache[i].vbuf) glDeleteBuffers_(1, &vbocache[i].vbuf);
}
DELETEA(vdata);
}
int findtag(const char *name)
{
loopi(numtags) if(!strcmp(tags[i].name, name)) return i;
return -1;
}
int totalframes() const { return numframes; }
void concattagtransform(part *p, int i, const matrix4x3 &m, matrix4x3 &n)
{
n.mul(m, tags[numtags + i].transform);
n.posttranslate(m.transformnormal(p->translate), p->model->scale);
}
void calctagmatrix(part *p, int i, const animstate &as, matrix4 &matrix)
{
const matrix4x3 &tag1 = tags[as.cur.fr1*numtags + i].transform,
&tag2 = tags[as.cur.fr2*numtags + i].transform;
matrix4x3 tag;
tag.lerp(tag1, tag2, as.cur.t);
if(as.interp<1)
{
const matrix4x3 &tag1p = tags[as.prev.fr1*numtags + i].transform,
&tag2p = tags[as.prev.fr2*numtags + i].transform;
matrix4x3 tagp;
tagp.lerp(tag1p, tag2p, as.prev.t);
tag.lerp(tagp, tag, as.interp);
}
tag.d.add(p->translate).mul(p->model->scale);
matrix = matrix4(tag);
}
void genvbo(bool tangents, vbocacheentry &vc)
{
if(!vc.vbuf) glGenBuffers_(1, &vc.vbuf);
if(ebuf) return;
vector<ushort> idxs;
if(tangents) loopv(meshes) ((vertmesh *)meshes[i])->calctangents();
vtangents = tangents;
vertsize = tangents ? sizeof(vvertbump) : sizeof(vvertn);
vlen = 0;
if(numframes>1)
{
loopv(meshes) vlen += ((vertmesh *)meshes[i])->genvbo(idxs, vlen);
DELETEA(vdata);
vdata = new uchar[vlen*vertsize];
#define FILLVDATA(type) do { \
loopv(meshes) ((vertmesh *)meshes[i])->fillverts((type *)vdata); \
} while(0)
if(tangents) FILLVDATA(vvertbump);
else FILLVDATA(vvertn);
#undef FILLVDATA
}
else
{
gle::bindvbo(vc.vbuf);
#define GENVBO(type) do { \
vector<type> vverts; \
loopv(meshes) vlen += ((vertmesh *)meshes[i])->genvbo(idxs, vlen, vverts, htdata, htlen); \
glBufferData_(GL_ARRAY_BUFFER, vverts.length()*sizeof(type), vverts.getbuf(), GL_STATIC_DRAW); \
} while(0)
int numverts = 0, htlen = 128;
loopv(meshes) numverts += ((vertmesh *)meshes[i])->numverts;
while(htlen < numverts) htlen *= 2;
if(numverts*4 > htlen*3) htlen *= 2;
int *htdata = new int[htlen];
memset(htdata, -1, htlen*sizeof(int));
if(tangents) GENVBO(vvertbump);
else GENVBO(vvertn);
delete[] htdata;
#undef GENVBO
gle::clearvbo();
}
glGenBuffers_(1, &ebuf);
gle::bindebo(ebuf);
glBufferData_(GL_ELEMENT_ARRAY_BUFFER, idxs.length()*sizeof(ushort), idxs.getbuf(), GL_STATIC_DRAW);
gle::clearebo();
}
void bindvbo(const animstate *as, vbocacheentry &vc)
{
vvert *vverts = 0;
bindpos(ebuf, vc.vbuf, &vverts->pos, vertsize);
if(as->cur.anim&ANIM_NOSKIN)
{
if(enabletc) disabletc();
if(enablenormals) disablenormals();
if(enabletangents) disabletangents();
}
else
{
if(vtangents)
{
if(enablenormals) disablenormals();
vvertbump *vvertbumps = 0;
bindtangents(&vvertbumps->tangent, vertsize);
}
else
{
if(enabletangents) disabletangents();
vvertn *vvertns = 0;
bindnormals(&vvertns->norm, vertsize);
}
bindtc(&vverts->tc, vertsize);
}
if(enablebones) disablebones();
}
void cleanup()
{
loopi(MAXVBOCACHE)
{
vbocacheentry &c = vbocache[i];
if(c.vbuf) { glDeleteBuffers_(1, &c.vbuf); c.vbuf = 0; }
c.as.cur.fr1 = -1;
}
if(ebuf) { glDeleteBuffers_(1, &ebuf); ebuf = 0; }
}
void preload(part *p)
{
if(numframes > 1) return;
bool tangents = p->tangents();
if(tangents!=vtangents) cleanup();
if(!vbocache->vbuf) genvbo(tangents, *vbocache);
}
void render(const animstate *as, float pitch, const vec &axis, const vec &forward, dynent *d, part *p)
{
if(as->cur.anim&ANIM_NORENDER)
{
loopv(p->links) calctagmatrix(p, p->links[i].tag, *as, p->links[i].matrix);
return;
}
bool tangents = p->tangents();
if(tangents!=vtangents) { cleanup(); disablevbo(); }
vbocacheentry *vc = NULL;
if(numframes<=1) vc = vbocache;
else
{
loopi(MAXVBOCACHE)
{
vbocacheentry &c = vbocache[i];
if(!c.vbuf) continue;
if(c.as==*as) { vc = &c; break; }
}
if(!vc) loopi(MAXVBOCACHE) { vc = &vbocache[i]; if(!vc->vbuf || vc->millis < lastmillis) break; }
}
if(!vc->vbuf) genvbo(tangents, *vc);
if(numframes>1)
{
if(vc->as!=*as)
{
vc->as = *as;
vc->millis = lastmillis;
loopv(meshes)
{
vertmesh &m = *(vertmesh *)meshes[i];
m.interpverts(*as, tangents, vdata + m.voffset*vertsize, p->skins[i]);
}
gle::bindvbo(vc->vbuf);
glBufferData_(GL_ARRAY_BUFFER, vlen*vertsize, vdata, GL_STREAM_DRAW);
}
vc->millis = lastmillis;
}
bindvbo(as, *vc);
loopv(meshes)
{
vertmesh *m = (vertmesh *)meshes[i];
p->skins[i].bind(m, as);
m->render(as, p->skins[i], *vc);
}
loopv(p->links) calctagmatrix(p, p->links[i].tag, *as, p->links[i].matrix);
}
};
vertmodel(const char *name) : animmodel(name)
{
}
};
template<class MDL> struct vertloader : modelloader<MDL, vertmodel>
{
vertloader(const char *name) : modelloader<MDL, vertmodel>(name) {}
};
template<class MDL> struct vertcommands : modelcommands<MDL, struct MDL::vertmesh>
{
typedef struct MDL::part part;
typedef struct MDL::skin skin;
static void loadpart(char *model, float *smooth)
{
if(!MDL::loading) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; }
defformatstring(filename, "%s/%s", MDL::dir, model);
part &mdl = MDL::loading->addpart();
if(mdl.index) mdl.pitchscale = mdl.pitchoffset = mdl.pitchmin = mdl.pitchmax = 0;
mdl.meshes = MDL::loading->sharemeshes(path(filename), double(*smooth > 0 ? cos(clamp(*smooth, 0.0f, 180.0f)*RAD) : 2));
if(!mdl.meshes) conoutf(CON_ERROR, "could not load %s", filename);
else mdl.initskins();
}
static void setpitch(float *pitchscale, float *pitchoffset, float *pitchmin, float *pitchmax)
{
if(!MDL::loading || MDL::loading->parts.empty()) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; }
part &mdl = *MDL::loading->parts.last();
mdl.pitchscale = *pitchscale;
mdl.pitchoffset = *pitchoffset;
if(*pitchmin || *pitchmax)
{
mdl.pitchmin = *pitchmin;
mdl.pitchmax = *pitchmax;
}
else
{
mdl.pitchmin = -360*fabs(mdl.pitchscale) + mdl.pitchoffset;
mdl.pitchmax = 360*fabs(mdl.pitchscale) + mdl.pitchoffset;
}
}
static void setanim(char *anim, int *frame, int *range, float *speed, int *priority)
{
if(!MDL::loading || MDL::loading->parts.empty()) { conoutf(CON_ERROR, "not loading an %s", MDL::formatname()); return; }
vector<int> anims;
findanims(anim, anims);
if(anims.empty()) conoutf(CON_ERROR, "could not find animation %s", anim);
else loopv(anims)
{
MDL::loading->parts.last()->setanim(0, anims[i], *frame, *range, *speed, *priority);
}
}
vertcommands()
{
if(MDL::multiparted()) this->modelcommand(loadpart, "load", "sf");
this->modelcommand(setpitch, "pitch", "ffff");
if(MDL::animated()) this->modelcommand(setanim, "anim", "siiff");
}
};
|