From a5f4d3f2f8054f0ce3014000244560817961922c Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Sat, 13 Sep 2008 10:15:29 +0000 Subject: [PATCH] shadow blending optimization git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2560 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- packages/models/playermodels/md2.cfg | 2 +- source/src/geom.h | 122 +++++++++++++++++ source/src/md2.h | 12 +- source/src/md3.h | 14 +- source/src/protos.h | 8 +- source/src/rendergl.cpp | 192 +++++++++++++++++++++++---- source/src/renderparticles.cpp | 6 +- source/src/vertmodel.h | 162 ++++++++++++++-------- 8 files changed, 423 insertions(+), 95 deletions(-) diff --git a/packages/models/playermodels/md2.cfg b/packages/models/playermodels/md2.cfg index 072f2bf8..84785810 100644 --- a/packages/models/playermodels/md2.cfg +++ b/packages/models/playermodels/md2.cfg @@ -1,5 +1,5 @@ mdlscale 155 mdltrans 0 0 24 -mdlshadowdist 32 +mdlshadowdist 16 mdlcachelimit 16 diff --git a/source/src/geom.h b/source/src/geom.h index 61cd31bc..65bf4a1b 100644 --- a/source/src/geom.h +++ b/source/src/geom.h @@ -137,3 +137,125 @@ static inline uint hthash(const ivec &k) return k.x^k.y^k.z; } +#ifndef STANDALONE +struct glmatrixf +{ + GLfloat v[16]; + + #define ROTVEC(A, B) \ + { \ + float a = A, b = B; \ + A = a*c + b*s; \ + B = b*c - a*s; \ + } + + void rotate_around_x(float angle) + { + float c = cosf(angle), s = sinf(angle); + ROTVEC(v[4], v[8]); + ROTVEC(v[5], v[9]); + ROTVEC(v[6], v[10]); + } + + void rotate_around_y(float angle) + { + float c = cosf(angle), s = sinf(angle); + ROTVEC(v[8], v[0]); + ROTVEC(v[9], v[1]); + ROTVEC(v[10], v[2]); + } + + void rotate_around_z(float angle) + { + float c = cosf(angle), s = sinf(angle); + ROTVEC(v[0], v[4]); + ROTVEC(v[1], v[5]); + ROTVEC(v[2], v[6]); + } + + #undef ROTVEC + + #define MULMAT(row, col) \ + v[col + row] = x[row]*y[col] + x[row + 4]*y[col + 1] + x[row + 8]*y[col + 2] + x[row + 12]*y[col + 3]; + + template + void mul(const XT x[16], const YT y[16]) + { + MULMAT(0, 0); MULMAT(1, 0); MULMAT(2, 0); MULMAT(3, 0); + MULMAT(0, 4); MULMAT(1, 4); MULMAT(2, 4); MULMAT(3, 4); + MULMAT(0, 8); MULMAT(1, 8); MULMAT(2, 8); MULMAT(3, 8); + MULMAT(0, 12); MULMAT(1, 12); MULMAT(2, 12); MULMAT(3, 12); + } + + #undef MULMAT + + void mul(const glmatrixf &x, const glmatrixf &y) + { + mul(x.v, y.v); + } + + void identity() + { + static const GLfloat m[16] = + { + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + }; + memcpy(v, m, sizeof(v)); + } + + void translate(float x, float y, float z) + { + v[12] += x; + v[13] += y; + v[14] += z; + } + + void translate(const vec &o) + { + translate(o.x, o.y, o.z); + } + + void scale(float x, float y, float z) + { + v[0] *= x; v[1] *= x; v[2] *= x; v[3] *= x; + v[4] *= y; v[5] *= y; v[6] *= y; v[7] *= y; + v[8] *= z; v[9] *= z; v[10] *= z; v[11] *= z; + } + + void invertnormal(vec &dir) const + { + vec n(dir); + dir.x = n.x*v[0] + n.y*v[1] + n.z*v[2]; + dir.y = n.x*v[4] + n.y*v[5] + n.z*v[6]; + dir.z = n.x*v[8] + n.y*v[9] + n.z*v[10]; + } + + void invertvertex(vec &pos) const + { + vec p(pos); + p.x -= v[12]; + p.y -= v[13]; + p.z -= v[14]; + pos.x = p.x*v[0] + p.y*v[1] + p.z*v[2]; + pos.y = p.x*v[4] + p.y*v[5] + p.z*v[6]; + pos.z = p.x*v[8] + p.y*v[9] + p.z*v[10]; + } + + void transform(const vec &in, vec4 &out) const + { + out.x = in.x*v[0] + in.y*v[4] + in.z*v[8] + v[12]; + out.y = in.x*v[1] + in.y*v[5] + in.z*v[9] + v[13]; + out.z = in.x*v[2] + in.y*v[6] + in.z*v[10] + v[14]; + out.w = in.x*v[3] + in.y*v[7] + in.z*v[11] + v[15]; + } + + vec gettranslation() const + { + return vec(v[12], v[13], v[14]); + } +}; +#endif + diff --git a/source/src/md2.h b/source/src/md2.h index c42ddc6b..80f6c85f 100644 --- a/source/src/md2.h +++ b/source/src/md2.h @@ -190,10 +190,13 @@ struct md2 : vertmodel } glPushMatrix(); - glTranslatef(o.x, o.y, o.z); - glRotatef(yaw+180, 0, 0, 1); - glRotatef(pitch, 0, -1, 0); - if(anim&ANIM_MIRROR || scale!=1) glScalef(scale, anim&ANIM_MIRROR ? -scale : scale, scale); + matrixpos = 0; + matrixstack[0].identity(); + matrixstack[0].translate(o); + matrixstack[0].rotate_around_z((yaw+180)*RAD); + matrixstack[0].rotate_around_y(-pitch*RAD); + if(anim&ANIM_MIRROR || scale!=1) matrixstack[0].scale(scale, anim&ANIM_MIRROR ? -scale : scale, scale); + glMultMatrixf(matrixstack[0].v); parts[0]->render(anim, varseed, speed, basetime, d); glPopMatrix(); @@ -253,6 +256,7 @@ struct md2 : vertmodel loopv(parts) parts[i]->scaleverts(scale/16.0f, vec(translate.x, -translate.y, translate.z)); radius = calcradius(); if(shadowdist) calcneighbors(); + calcbbs(); return loaded = true; } }; diff --git a/source/src/md3.h b/source/src/md3.h index 93839128..a38685fe 100644 --- a/source/src/md3.h +++ b/source/src/md3.h @@ -169,13 +169,14 @@ struct md3 : vertmodel } glPushMatrix(); - glTranslatef(o.x, o.y, o.z); - glRotatef(yaw+180, 0, 0, 1); - glRotatef(pitch, 0, -1, 0); - - if(anim&ANIM_MIRROR || scale!=1) glScalef(scale, anim&ANIM_MIRROR ? -scale : scale, scale); + matrixpos = 0; + matrixstack[0].identity(); + matrixstack[0].translate(o); + matrixstack[0].rotate_around_z((yaw+180)*RAD); + matrixstack[0].rotate_around_y(-pitch*RAD); + if(anim&ANIM_MIRROR || scale!=1) matrixstack[0].scale(scale, anim&ANIM_MIRROR ? -scale : scale, scale); + glMultMatrixf(matrixstack[0].v); parts[0]->render(anim, varseed, speed, basetime, d); - glPopMatrix(); if(!cullface) glEnable(GL_CULL_FACE); @@ -231,6 +232,7 @@ struct md3 : vertmodel loopv(parts) parts[i]->scaleverts(scale/16.0f, vec(translate.x, -translate.y, translate.z)); radius = calcradius(); if(shadowdist) calcneighbors(); + calcbbs(); return loaded = true; } }; diff --git a/source/src/protos.h b/source/src/protos.h index b7c4f39a..5caa8d0e 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -221,7 +221,7 @@ extern void renderaboveheadicon(playerent *p); extern void drawcrosshair(playerent *p, bool showteamwarning); extern void drawscope(); extern float dynfov(); - +extern bool addshadowbox(const vec &bbmin, const vec &bbmax, const glmatrixf &mat); // texture struct Texture @@ -476,11 +476,11 @@ enum extern void particleinit(); extern void particlereset(); -extern void particle_flash(int type, float scale, float angle, vec &p); +extern void particle_flash(int type, float scale, float angle, const vec &p); extern void particle_splash(int type, int num, int fade, const vec &p); extern void particle_trail(int type, int fade, const vec &from, const vec &to); -extern void particle_emit(int type, int *args, int basetime, int seed, vec &p); -extern void particle_fireball(int type, vec &o); +extern void particle_emit(int type, int *args, int basetime, int seed, const vec &p); +extern void particle_fireball(int type, const vec &o); extern void addshotline(dynent *d, const vec &from, const vec &to); extern bool addbullethole(dynent *d, const vec &from, const vec &to, float radius = 1, bool noisy = true); extern bool addscorchmark(vec &o, float radius = 7); diff --git a/source/src/rendergl.cpp b/source/src/rendergl.cpp index 9f74aa53..47f03be9 100644 --- a/source/src/rendergl.cpp +++ b/source/src/rendergl.cpp @@ -834,6 +834,135 @@ VARP(stencilshadow, 0, 40, 100); int stenciling = 0; +VAR(shadowcasters, 1, 0, 0); + +#define SHADOWTILES 32 +#define SHADOWTILEMASK (0xFFFFFFFFU>>(32-SHADOWTILES)) +uint shadowtiles[SHADOWTILES+1]; +float shadowx1 = 1, shadowy1 = 1, shadowx2 = -1, shadowy2 = -1; + +bool addshadowtiles(float x1, float y1, float x2, float y2) +{ + if(x1 >= 1 || y1 >= 1 || x2 <= -1 || y2 <= -1) return false; + + shadowcasters++; + + shadowx1 = min(shadowx1, max(x1, -1.0f)); + shadowy1 = min(shadowy1, max(y1, -1.0f)); + shadowx2 = max(shadowx2, min(x2, 1.0f)); + shadowy2 = max(shadowy2, min(y2, 1.0f)); + + int tx1 = clamp(int((x1 + 1)/2 * SHADOWTILES), 0, SHADOWTILES - 1), + ty1 = clamp(int((y1 + 1)/2 * SHADOWTILES), 0, SHADOWTILES - 1), + tx2 = clamp(int((x2 + 1)/2 * SHADOWTILES), 0, SHADOWTILES - 1), + ty2 = clamp(int((y2 + 1)/2 * SHADOWTILES), 0, SHADOWTILES - 1); + + uint mask = (SHADOWTILEMASK>>(SHADOWTILES - (tx2+1))) & (SHADOWTILEMASK<= 0) + { + float x = pv.x / pv.w, y = pv.y / pv.w; + sx1 = min(sx1, x); + sy1 = min(sy1, y); + sx2 = max(sx2, x); + sy2 = max(sy2, y); + } + } + if(sx1 >= sx2 || sy1 >= sy2) return false; + loopi(8) + { + const vec4 &p = v[i]; + if(p.z >= 0) continue; + loopj(3) + { + const vec4 &o = v[i^(1<= shadowx2 || shadowy1 >= shadowy2) return; + + float clipx1 = (shadowx1 + 1) / 2, + clipy1 = (shadowy1 + 1) / 2, + clipx2 = (shadowx2 + 1) / 2, + clipy2 = (shadowy2 + 1) / 2; + if(!shadowclip) + { + clipx1 = clipy1 = 0; + clipx2 = clipy2 = 1; + } + + if(!shadowtile) + { + glBegin(GL_QUADS); + glVertex2f(clipx1, clipy2); + glVertex2f(clipx2, clipy2); + glVertex2f(clipx2, clipy1); + glVertex2f(clipx1, clipy1); + glEnd(); + return; + } + + glBegin(GL_QUADS); + float tsz = 1.0f/SHADOWTILES; + loop(y, SHADOWTILES+1) + { + uint mask = shadowtiles[y]; + int x = 0; + while(mask) + { + while(!(mask&0xFF)) { mask >>= 8; x += 8; } + while(!(mask&1)) { mask >>= 1; x++; } + int xstart = x; + do { mask >>= 1; x++; } while(mask&1); + uint strip = (SHADOWTILEMASK>>(SHADOWTILES - x)) & (SHADOWTILEMASK<=MAXPARTYPES) return; parttype &pt = parttypes[type]; @@ -546,7 +546,7 @@ void particle_emit(int type, int *args, int basetime, int seed, vec &p) else particle_splash(type, args[0], args[1], p); } -void particle_flash(int type, float scale, float angle, vec &p) +void particle_flash(int type, float scale, float angle, const vec &p) { angle *= RAD; newparticle(p, vec(cosf(angle), sinf(angle), scale), 0, type); @@ -585,7 +585,7 @@ void particle_trail(int type, int fade, const vec &s, const vec &e) } } -void particle_fireball(int type, vec &o) +void particle_fireball(int type, const vec &o) { newparticle(o, vec(0, 0, 0), (int)((parttypes[type].sz-1.0f)*100.0f), type); } diff --git a/source/src/vertmodel.h b/source/src/vertmodel.h index 4ca530b3..eaddf93d 100644 --- a/source/src/vertmodel.h +++ b/source/src/vertmodel.h @@ -42,6 +42,37 @@ struct vertmodel : model bool operator!=(const anpos &a) const { return fr1!=a.fr1 || fr2!=a.fr2 || (fr1!=fr2 && t!=a.t); } }; + struct bb + { + vec low, high; + + void addlow(const vec &v) + { + low.x = min(low.x, v.x); + low.y = min(low.y, v.y); + low.z = min(low.z, v.z); + } + + void addhigh(const vec &v) + { + high.x = max(high.x, v.x); + high.y = max(high.y, v.y); + high.z = max(high.z, v.z); + } + + void add(const vec &v) + { + addlow(v); + addhigh(v); + } + + void add(const bb &b) + { + addlow(b.low); + addhigh(b.high); + } + }; + struct tcvert { float u, v; }; struct tri { ushort vert[3]; ushort neighbor[3]; }; @@ -74,6 +105,7 @@ struct vertmodel : model part *owner; vec *verts; tcvert *tcverts; + bb *bbs; ushort *shareverts; tri *tris; int numverts, numtris; @@ -91,7 +123,7 @@ struct vertmodel : model GLuint statlist; int statlen; - mesh() : name(0), owner(0), verts(0), tcverts(0), shareverts(0), tris(0), skin(notexture), tex(0), dynidx(0), dyndraws(0), statlist(0) + mesh() : name(0), owner(0), verts(0), tcverts(0), bbs(0), shareverts(0), tris(0), skin(notexture), tex(0), dynidx(0), dyndraws(0), statlist(0) { } @@ -100,6 +132,7 @@ struct vertmodel : model DELETEA(name); DELETEA(verts); DELETEA(tcverts); + DELETEA(bbs); DELETEA(shareverts); DELETEA(tris); if(statlist) glDeleteLists(statlist, 1); @@ -198,19 +231,15 @@ struct vertmodel : model const tri &t = tris[i]; loopj(3) { - uint e1 = shareverts[t.vert[j]], e2 = shareverts[t.vert[(j+1)%3]]; - uint &edge = edges.access(min(e1, e2) | (max(e1, e2)<<16), ~0U); - if((edge&0xFFFF)==0xFFFF) + uint e1 = shareverts[t.vert[j]], e2 = shareverts[t.vert[(j+1)%3]], shift = 0; + if(e1 > e2) { swap(e1, e2); shift = 16; } + uint &edge = edges.access(e1 | (e2<<16), ~0U); + if(((edge>>shift)&0xFFFF) != 0xFFFF) edge = 0; + else { - edge &= ~0xFFFF; - edge |= i; + edge &= 0xFFFF<<(16-shift); + edge |= i<>16 : edge&0xFFFF; + uint e1 = shareverts[t.vert[j]], e2 = shareverts[t.vert[(j+1)%3]], shift = 0; + if(e1 > e2) { swap(e1, e2); shift = 16; } + uint edge = edges[e1 | (e2<<16)]; + if(!edge || int((edge>>shift)&0xFFFF)!=i) t.neighbor[j] = 0xFFFF; + else t.neighbor[j] = (edge>>(16-shift))&0xFFFF; } } } + void cleanup() { if(statlist) @@ -296,6 +327,18 @@ struct vertmodel : model return d; } + void getcurbb(bb &b, animstate &as, anpos &cur, anpos *prev, float ai_t) + { + b = bbs[cur.fr1]; + b.add(bbs[cur.fr2]); + + if(prev) + { + b.add(bbs[prev->fr1]); + b.add(bbs[prev->fr2]); + } + } + void render(animstate &as, anpos &cur, anpos *prev, float ai_t) { if(!(as.anim&ANIM_NOSKIN)) @@ -329,6 +372,16 @@ struct vertmodel : model } else { + if(stenciling==1) + { + bb curbb; + getcurbb(curbb, as, cur, prev, ai_t); + curbb.add(shadowpos); + glmatrixf mat; + mat.mul(mvpmatrix, matrixstack[matrixpos].v); + if(!addshadowbox(curbb.low, curbb.high, mat)) return; + } + vec *buf = verts; dyncacheentry *d = NULL; if(!isstat) @@ -419,6 +472,20 @@ struct vertmodel : model findneighbors(); } } + + void calcbbs() + { + if(bbs) return; + bbs = new bb[owner->numframes]; + loopi(owner->numframes) + { + bb &b = bbs[i]; + b.low = vec(1e16f, 1e16f, 1e16f); + b.high = vec(-1e16f, -1e16f, -1e16f); + const vec *frame = &verts[numverts*i]; + loopj(numverts) b.add(frame[j]); + } + } }; struct animinfo @@ -666,27 +733,6 @@ struct vertmodel : model return true; } - void calcnormal(GLfloat *m, vec &dir) - { - vec n(dir); - dir.x = n.x*m[0] + n.y*m[1] + n.z*m[2]; - dir.y = n.x*m[4] + n.y*m[5] + n.z*m[6]; - dir.z = n.x*m[8] + n.y*m[9] + n.z*m[10]; - } - - void calcvertex(GLfloat *m, vec &pos) - { - vec p(pos); - - p.x -= m[12]; - p.y -= m[13]; - p.z -= m[14]; - - pos.x = p.x*m[0] + p.y*m[1] + p.z*m[2]; - pos.y = p.x*m[4] + p.y*m[5] + p.z*m[6]; - pos.z = p.x*m[8] + p.y*m[9] + p.z*m[10]; - } - void render(int anim, int varseed, float speed, int basetime, dynent *d) { if(meshes.empty()) return; @@ -710,8 +756,11 @@ struct vertmodel : model loopi(numtags) if(links[i] || (anim&ANIM_PARTICLE && emitters && emitters[i].type>=0)) // render the linked models - interpolate rotation and position of the 'link-tags' { - GLfloat matrix[16]; - gentagmatrix(cur, doai ? &prev : NULL, ai_t, i, matrix); + glmatrixf linkmat; + gentagmatrix(cur, doai ? &prev : NULL, ai_t, i, linkmat.v); + + matrixpos++; + matrixstack[matrixpos].mul(matrixstack[matrixpos-1].v, linkmat.v); part *link = links[i]; if(link) @@ -722,14 +771,12 @@ struct vertmodel : model { oldshadowdir = shadowdir; oldshadowpos = shadowpos; - calcnormal(matrix, shadowdir); - calcvertex(matrix, shadowpos); + linkmat.invertnormal(shadowdir); + linkmat.invertvertex(shadowpos); } - glPushMatrix(); - glMultMatrixf(matrix); - link->render(anim, varseed, speed, basetime, d); - glPopMatrix(); + glLoadMatrixf(matrixstack[matrixpos].v); + link->render(anim, varseed, speed, basetime, d); if(stenciling) { @@ -746,15 +793,10 @@ struct vertmodel : model emitters[i].lastemit = basetime; } - vec tagpos(matrix[12], matrix[13], matrix[14]); - GLdouble mm[16]; - glGetDoublev(GL_MODELVIEW_MATRIX, mm); - vec eyepos; - loopk(3) eyepos[k] = tagpos.x*mm[0+k] + tagpos.y*mm[4+k] + tagpos.z*mm[8+k] + mm[12+k]; - vec worldpos; - loopk(3) worldpos[k] = eyepos.x*invmvmatrix[0+k] + eyepos.y*invmvmatrix[4+k] + eyepos.z*invmvmatrix[8+k] + invmvmatrix[12+k]; - particle_emit(emitters[i].type, emitters[i].args, basetime, emitters[i].seed, worldpos); + particle_emit(emitters[i].type, emitters[i].args, basetime, emitters[i].seed, matrixstack[matrixpos].gettranslation()); } + + matrixpos--; } } @@ -955,6 +997,11 @@ struct vertmodel : model { loopv(meshes) meshes[i]->calcneighbors(); } + + void calcbbs() + { + loopv(meshes) meshes[i]->calcbbs(); + } }; bool loaded; @@ -1013,10 +1060,17 @@ struct vertmodel : model loopv(parts) parts[i]->calcneighbors(); } + void calcbbs() + { + loopv(parts) parts[i]->calcbbs(); + } + static bool enablealphablend, enablealphatest, enabledepthmask; static GLuint lasttex; static float lastalphatest; static void *lastvertexarray, *lasttexcoordarray; + static glmatrixf matrixstack[32]; + static int matrixpos; void startrender() { @@ -1043,6 +1097,8 @@ bool vertmodel::enablealphablend = false, vertmodel::enablealphatest = false, ve GLuint vertmodel::lasttex = 0; float vertmodel::lastalphatest = -1; void *vertmodel::lastvertexarray = NULL, *vertmodel::lasttexcoordarray = NULL; +glmatrixf vertmodel::matrixstack[32]; +int vertmodel::matrixpos = 0; VARF(mdldyncache, 1, 2, 32, vertmodel::dynalloc.resize(mdldyncache<<20)); VARF(mdlstatcache, 1, 1, 32, vertmodel::statalloc.resize(mdlstatcache<<20));