2019-11-24 20:45:15 -08:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-2001 Id Software, Inc.
|
|
|
|
Copyright (C) 2002-2009 John Fitzgibbons and others
|
|
|
|
Copyright (C) 2010-2014 QuakeSpasm developers
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
// gl_mesh.c: triangle model functions
|
|
|
|
|
2019-12-02 07:07:37 -08:00
|
|
|
#include "q_defs.h"
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================================================================
|
|
|
|
|
|
|
|
ALIAS MODEL DISPLAY LIST GENERATION
|
|
|
|
|
|
|
|
=================================================================
|
|
|
|
*/
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
qmodel_t *aliasmodel;
|
|
|
|
aliashdr_t *paliashdr;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t used[8192]; // bool
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// the command list holds counts and s/t values that are valid for
|
|
|
|
// every frame
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t commands[8192];
|
|
|
|
int32_t numcommands;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// all frames will have their vertexes rearranged and expanded
|
|
|
|
// so they are in the order expected by the command list
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t vertexorder[8192];
|
|
|
|
int32_t numorder;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t allverts, alltris;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t stripverts[128];
|
|
|
|
int32_t striptris[128];
|
|
|
|
int32_t stripcount;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
StripLength
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t StripLength(int32_t starttri, int32_t startv)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t m1, m2;
|
|
|
|
int32_t j;
|
|
|
|
mtriangle_t *last, *check;
|
|
|
|
int32_t k;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
used[starttri] = 2;
|
|
|
|
|
|
|
|
last = &triangles[starttri];
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
stripverts[0] = last->vertindex[(startv) % 3];
|
|
|
|
stripverts[1] = last->vertindex[(startv + 1) % 3];
|
|
|
|
stripverts[2] = last->vertindex[(startv + 2) % 3];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
striptris[0] = starttri;
|
|
|
|
stripcount = 1;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
m1 = last->vertindex[(startv + 2) % 3];
|
|
|
|
m2 = last->vertindex[(startv + 1) % 3];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// look for a matching triangle
|
|
|
|
nexttri:
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = starttri + 1, check = &triangles[starttri + 1] ; j < pheader->numtris ; j++, check++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->facesfront != last->facesfront)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(k = 0 ; k < 3 ; k++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->vertindex[k] != m1)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->vertindex[(k + 1) % 3 ] != m2)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// this is the next part of the fan
|
|
|
|
|
|
|
|
// if we can't use this triangle, this tristrip is done
|
2019-11-25 17:40:18 -08:00
|
|
|
if(used[j])
|
2019-11-24 20:45:15 -08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
// the new edge
|
2019-11-25 17:40:18 -08:00
|
|
|
if(stripcount & 1)
|
|
|
|
m2 = check->vertindex[(k + 2) % 3 ];
|
2019-11-24 20:45:15 -08:00
|
|
|
else
|
2019-11-25 17:40:18 -08:00
|
|
|
m1 = check->vertindex[(k + 2) % 3 ];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
stripverts[stripcount + 2] = check->vertindex[(k + 2) % 3 ];
|
2019-11-24 20:45:15 -08:00
|
|
|
striptris[stripcount] = j;
|
|
|
|
stripcount++;
|
|
|
|
|
|
|
|
used[j] = 2;
|
|
|
|
goto nexttri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
|
|
|
|
// clear the temp used flags
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = starttri + 1 ; j < pheader->numtris ; j++)
|
|
|
|
if(used[j] == 2)
|
2019-11-24 20:45:15 -08:00
|
|
|
used[j] = 0;
|
|
|
|
|
|
|
|
return stripcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FanLength
|
|
|
|
===========
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t FanLength(int32_t starttri, int32_t startv)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t m1, m2;
|
|
|
|
int32_t j;
|
|
|
|
mtriangle_t *last, *check;
|
|
|
|
int32_t k;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
used[starttri] = 2;
|
|
|
|
|
|
|
|
last = &triangles[starttri];
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
stripverts[0] = last->vertindex[(startv) % 3];
|
|
|
|
stripverts[1] = last->vertindex[(startv + 1) % 3];
|
|
|
|
stripverts[2] = last->vertindex[(startv + 2) % 3];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
striptris[0] = starttri;
|
|
|
|
stripcount = 1;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
m1 = last->vertindex[(startv + 0) % 3];
|
|
|
|
m2 = last->vertindex[(startv + 2) % 3];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
// look for a matching triangle
|
|
|
|
nexttri:
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = starttri + 1, check = &triangles[starttri + 1] ; j < pheader->numtris ; j++, check++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->facesfront != last->facesfront)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(k = 0 ; k < 3 ; k++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->vertindex[k] != m1)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
2019-11-25 17:40:18 -08:00
|
|
|
if(check->vertindex[(k + 1) % 3 ] != m2)
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// this is the next part of the fan
|
|
|
|
|
|
|
|
// if we can't use this triangle, this tristrip is done
|
2019-11-25 17:40:18 -08:00
|
|
|
if(used[j])
|
2019-11-24 20:45:15 -08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
// the new edge
|
2019-11-25 17:40:18 -08:00
|
|
|
m2 = check->vertindex[(k + 2) % 3 ];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
stripverts[stripcount + 2] = m2;
|
2019-11-24 20:45:15 -08:00
|
|
|
striptris[stripcount] = j;
|
|
|
|
stripcount++;
|
|
|
|
|
|
|
|
used[j] = 2;
|
|
|
|
goto nexttri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
|
|
|
|
// clear the temp used flags
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = starttri + 1 ; j < pheader->numtris ; j++)
|
|
|
|
if(used[j] == 2)
|
2019-11-24 20:45:15 -08:00
|
|
|
used[j] = 0;
|
|
|
|
|
|
|
|
return stripcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
BuildTris
|
|
|
|
|
|
|
|
Generate a list of trifans or strips
|
|
|
|
for the model, which holds for all frames
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
void BuildTris(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t i, j, k;
|
|
|
|
int32_t startv;
|
|
|
|
float s, t;
|
|
|
|
int32_t len, bestlen, besttype;
|
|
|
|
int32_t bestverts[1024];
|
|
|
|
int32_t besttris[1024];
|
|
|
|
int32_t type;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// build tristrips
|
|
|
|
//
|
|
|
|
numorder = 0;
|
|
|
|
numcommands = 0;
|
2019-11-25 17:40:18 -08:00
|
|
|
memset(used, 0, sizeof(used));
|
|
|
|
for(i = 0; i < pheader->numtris; i++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
// pick an unused triangle and start the trifan
|
2019-11-25 17:40:18 -08:00
|
|
|
if(used[i])
|
2019-11-24 20:45:15 -08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bestlen = 0;
|
|
|
|
besttype = 0;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(type = 0 ; type < 2 ; type++)
|
2019-11-24 20:45:15 -08:00
|
|
|
// type = 1;
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
for(startv = 0; startv < 3; startv++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(type == 1)
|
|
|
|
len = StripLength(i, startv);
|
2019-11-24 20:45:15 -08:00
|
|
|
else
|
2019-11-25 17:40:18 -08:00
|
|
|
len = FanLength(i, startv);
|
|
|
|
if(len > bestlen)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
besttype = type;
|
|
|
|
bestlen = len;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 0; j < bestlen + 2; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
bestverts[j] = stripverts[j];
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 0; j < bestlen; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
besttris[j] = striptris[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark the tris on the best strip as used
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 0; j < bestlen; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
used[besttris[j]] = 1;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(besttype == 1)
|
|
|
|
commands[numcommands++] = (bestlen + 2);
|
2019-11-24 20:45:15 -08:00
|
|
|
else
|
2019-11-25 17:40:18 -08:00
|
|
|
commands[numcommands++] = -(bestlen + 2);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 0; j < bestlen + 2; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t tmp;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// emit a vertex into the reorder buffer
|
|
|
|
k = bestverts[j];
|
|
|
|
vertexorder[numorder++] = k;
|
|
|
|
|
|
|
|
// emit s/t coords into the commands stream
|
|
|
|
s = stverts[k].s;
|
|
|
|
t = stverts[k].t;
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!triangles[besttris[0]].facesfront && stverts[k].onseam)
|
|
|
|
s += pheader->skinwidth / 2; // on back side
|
2019-11-24 20:45:15 -08:00
|
|
|
s = (s + 0.5) / pheader->skinwidth;
|
|
|
|
t = (t + 0.5) / pheader->skinheight;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
// *(float *)&commands[numcommands++] = s;
|
|
|
|
// *(float *)&commands[numcommands++] = t;
|
2019-11-25 16:49:58 -08:00
|
|
|
// NOTE: 4 == sizeof(int32_t)
|
2019-11-25 17:40:18 -08:00
|
|
|
// == sizeof(float)
|
|
|
|
memcpy(&tmp, &s, 4);
|
2019-11-24 20:45:15 -08:00
|
|
|
commands[numcommands++] = tmp;
|
2019-11-25 17:40:18 -08:00
|
|
|
memcpy(&tmp, &t, 4);
|
2019-11-24 20:45:15 -08:00
|
|
|
commands[numcommands++] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
commands[numcommands++] = 0; // end of list marker
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
Con_DPrintf2("%3" PRIi32 " tri %3" PRIi32 " vert %3" PRIi32 " cmd\n", pheader->numtris, numorder, numcommands);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
allverts += numorder;
|
|
|
|
alltris += pheader->numtris;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static void GL_MakeAliasModelDisplayLists_VBO(void);
|
|
|
|
static void GLMesh_LoadVertexBuffer(qmodel_t *m, const aliashdr_t *hdr);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
GL_MakeAliasModelDisplayLists
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
void GL_MakeAliasModelDisplayLists(qmodel_t *m, aliashdr_t *hdr)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t i, j;
|
|
|
|
int32_t *cmds;
|
|
|
|
trivertx_t *verts;
|
|
|
|
float hscale, vscale; //johnfitz -- padded skins
|
|
|
|
int32_t count; //johnfitz -- precompute texcoords for padded skins
|
|
|
|
int32_t *loadcmds; //johnfitz
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//johnfitz -- padded skins
|
2019-11-25 17:40:18 -08:00
|
|
|
hscale = (float)hdr->skinwidth / (float)TexMgr_PadConditional(hdr->skinwidth);
|
|
|
|
vscale = (float)hdr->skinheight / (float)TexMgr_PadConditional(hdr->skinheight);
|
2019-11-24 20:45:15 -08:00
|
|
|
//johnfitz
|
|
|
|
|
|
|
|
aliasmodel = m;
|
2019-11-25 17:40:18 -08:00
|
|
|
paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//johnfitz -- generate meshes
|
2019-11-25 17:40:18 -08:00
|
|
|
Con_DPrintf2("meshing %s...\n", m->name);
|
|
|
|
BuildTris();
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// save the data out
|
|
|
|
|
|
|
|
paliashdr->poseverts = numorder;
|
|
|
|
|
2019-12-04 04:24:35 -08:00
|
|
|
cmds = Hunk_AllocName(numcommands * 4, __func__);
|
2019-11-24 20:45:15 -08:00
|
|
|
paliashdr->commands = (byte *)cmds - (byte *)paliashdr;
|
|
|
|
|
|
|
|
//johnfitz -- precompute texcoords for padded skins
|
|
|
|
loadcmds = commands;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
*cmds++ = count = *loadcmds++;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!count)
|
2019-11-24 20:45:15 -08:00
|
|
|
break;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(count < 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
count = -count;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*(float *)cmds++ = hscale * (*(float *)loadcmds++);
|
|
|
|
*(float *)cmds++ = vscale * (*(float *)loadcmds++);
|
2019-11-25 17:40:18 -08:00
|
|
|
}
|
|
|
|
while(--count);
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
//johnfitz
|
|
|
|
|
2019-12-04 04:24:35 -08:00
|
|
|
verts = Hunk_AllocName(paliashdr->numposes * paliashdr->poseverts * sizeof(trivertx_t), __func__);
|
2019-11-24 20:45:15 -08:00
|
|
|
paliashdr->posedata = (byte *)verts - (byte *)paliashdr;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(i = 0 ; i < paliashdr->numposes ; i++)
|
|
|
|
for(j = 0 ; j < numorder ; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
*verts++ = poseverts[i][vertexorder[j]];
|
|
|
|
|
|
|
|
// ericw
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_MakeAliasModelDisplayLists_VBO();
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
|
2019-11-25 14:05:12 -08:00
|
|
|
uint32_t r_meshindexbuffer = 0;
|
|
|
|
uint32_t r_meshvertexbuffer = 0;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
GL_MakeAliasModelDisplayLists_VBO
|
|
|
|
|
|
|
|
Saves data needed to build the VBO for this model on the hunk. Afterwards this
|
|
|
|
is copied to Mod_Extradata.
|
|
|
|
|
|
|
|
Original code by MH from RMQEngine
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
void GL_MakeAliasModelDisplayLists_VBO(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t i, j;
|
|
|
|
int32_t maxverts_vbo;
|
2019-11-24 20:45:15 -08:00
|
|
|
trivertx_t *verts;
|
2019-11-25 14:09:48 -08:00
|
|
|
uint16_t *indexes;
|
2019-11-24 20:45:15 -08:00
|
|
|
aliasmesh_t *desc;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!gl_glsl_alias_able)
|
2019-11-24 20:45:15 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// first, copy the verts onto the hunk
|
2019-12-04 04:24:35 -08:00
|
|
|
verts = Hunk_AllocName(paliashdr->numposes * paliashdr->numverts * sizeof(trivertx_t), __func__);
|
2019-11-24 20:45:15 -08:00
|
|
|
paliashdr->vertexes = (byte *)verts - (byte *)paliashdr;
|
2019-11-25 17:40:18 -08:00
|
|
|
for(i = 0 ; i < paliashdr->numposes ; i++)
|
|
|
|
for(j = 0 ; j < paliashdr->numverts ; j++)
|
|
|
|
verts[i * paliashdr->numverts + j] = poseverts[i][j];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// there can never be more than this number of verts and we just put them all on the hunk
|
|
|
|
maxverts_vbo = pheader->numtris * 3;
|
2019-12-04 04:24:35 -08:00
|
|
|
desc = Hunk_AllocName(sizeof(aliasmesh_t) * maxverts_vbo, __func__);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// there will always be this number of indexes
|
2019-12-04 04:24:35 -08:00
|
|
|
indexes = Hunk_AllocName(sizeof(uint16_t) * maxverts_vbo, __func__);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
pheader->indexes = (intptr_t) indexes - (intptr_t) pheader;
|
|
|
|
pheader->meshdesc = (intptr_t) desc - (intptr_t) pheader;
|
|
|
|
pheader->numindexes = 0;
|
|
|
|
pheader->numverts_vbo = 0;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
for(i = 0; i < pheader->numtris; i++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 0; j < 3; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t v;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// index into hdr->vertexes
|
2019-11-25 14:09:48 -08:00
|
|
|
uint16_t vertindex = triangles[i].vertindex[j];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// basic s/t coords
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t s = stverts[vertindex].s;
|
|
|
|
int32_t t = stverts[vertindex].t;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// check for back side and adjust texcoord s
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!triangles[i].facesfront && stverts[vertindex].onseam) s += pheader->skinwidth / 2;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// see does this vert already exist
|
2019-11-25 17:40:18 -08:00
|
|
|
for(v = 0; v < pheader->numverts_vbo; v++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
// it could use the same xyz but have different s and t
|
2019-11-25 17:40:18 -08:00
|
|
|
if(desc[v].vertindex == vertindex && (int32_t) desc[v].st[0] == s && (int32_t) desc[v].st[1] == t)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
// exists; emit an index for it
|
|
|
|
indexes[pheader->numindexes++] = v;
|
|
|
|
|
|
|
|
// no need to check any more
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(v == pheader->numverts_vbo)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
// doesn't exist; emit a new vert and index
|
|
|
|
indexes[pheader->numindexes++] = pheader->numverts_vbo;
|
|
|
|
|
|
|
|
desc[pheader->numverts_vbo].vertindex = vertindex;
|
|
|
|
desc[pheader->numverts_vbo].st[0] = s;
|
|
|
|
desc[pheader->numverts_vbo++].st[1] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
// upload immediately
|
2019-11-25 17:40:18 -08:00
|
|
|
GLMesh_LoadVertexBuffer(aliasmodel, pheader);
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
#define NUMVERTEXNORMALS 162
|
|
|
|
extern float r_avertexnormals[NUMVERTEXNORMALS][3];
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
GLMesh_LoadVertexBuffer
|
|
|
|
|
|
|
|
Upload the given alias model's mesh to a VBO
|
|
|
|
|
|
|
|
Original code by MH from RMQEngine
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
static void GLMesh_LoadVertexBuffer(qmodel_t *m, const aliashdr_t *hdr)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t totalvbosize = 0;
|
2019-11-24 20:45:15 -08:00
|
|
|
const aliasmesh_t *desc;
|
2019-11-25 14:15:33 -08:00
|
|
|
const int16_t *indexes;
|
2019-11-24 20:45:15 -08:00
|
|
|
const trivertx_t *trivertexes;
|
|
|
|
byte *vbodata;
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t f;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!gl_glsl_alias_able)
|
2019-11-24 20:45:15 -08:00
|
|
|
return;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
// count the sizes we need
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
// ericw -- RMQEngine stored these vbo*ofs values in aliashdr_t, but we must not
|
|
|
|
// mutate Mod_Extradata since it might be reloaded from disk, so I moved them to qmodel_t
|
|
|
|
// (test case: roman1.bsp from arwop, 64mb heap)
|
|
|
|
m->vboindexofs = 0;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
m->vboxyzofs = 0;
|
2019-11-25 17:40:18 -08:00
|
|
|
totalvbosize += (hdr->numposes * hdr->numverts_vbo * sizeof(meshxyz_t)); // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
m->vbostofs = totalvbosize;
|
2019-11-25 17:40:18 -08:00
|
|
|
totalvbosize += (hdr->numverts_vbo * sizeof(meshst_t));
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!hdr->numindexes) return;
|
|
|
|
if(!totalvbosize) return;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
// grab the pointers to data in the extradata
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
desc = (aliasmesh_t *)((byte *) hdr + hdr->meshdesc);
|
|
|
|
indexes = (int16_t *)((byte *) hdr + hdr->indexes);
|
|
|
|
trivertexes = (trivertx_t *)((byte *)hdr + hdr->vertexes);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// upload indices buffer
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_DeleteBuffersFunc(1, &m->meshindexesvbo);
|
|
|
|
GL_GenBuffersFunc(1, &m->meshindexesvbo);
|
|
|
|
GL_BindBufferFunc(GL_ELEMENT_ARRAY_BUFFER, m->meshindexesvbo);
|
|
|
|
GL_BufferDataFunc(GL_ELEMENT_ARRAY_BUFFER, hdr->numindexes * sizeof(uint16_t), indexes, GL_STATIC_DRAW);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// create the vertex buffer (empty)
|
|
|
|
|
|
|
|
vbodata = (byte *) malloc(totalvbosize);
|
|
|
|
memset(vbodata, 0, totalvbosize);
|
|
|
|
|
|
|
|
// fill in the vertices at the start of the buffer
|
2019-11-25 17:40:18 -08:00
|
|
|
for(f = 0; f < hdr->numposes; f++) // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t v;
|
2019-11-25 17:40:18 -08:00
|
|
|
meshxyz_t *xyz = (meshxyz_t *)(vbodata + (f * hdr->numverts_vbo * sizeof(meshxyz_t)));
|
2019-11-24 20:45:15 -08:00
|
|
|
const trivertx_t *tv = trivertexes + (hdr->numverts * f);
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
for(v = 0; v < hdr->numverts_vbo; v++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
trivertx_t trivert = tv[desc[v].vertindex];
|
|
|
|
|
|
|
|
xyz[v].xyz[0] = trivert.v[0];
|
|
|
|
xyz[v].xyz[1] = trivert.v[1];
|
|
|
|
xyz[v].xyz[2] = trivert.v[2];
|
2019-11-25 17:40:18 -08:00
|
|
|
xyz[v].xyz[3] = 1; // need w 1 for 4 byte vertex compression
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 14:23:46 -08:00
|
|
|
// map the normal coordinates in [-1..1] to [-127..127] and store in an uint8_t.
|
2019-11-24 20:45:15 -08:00
|
|
|
// this introduces some error (less than 0.004), but the normals were very coarse
|
|
|
|
// to begin with
|
|
|
|
xyz[v].normal[0] = 127 * r_avertexnormals[trivert.lightnormalindex][0];
|
|
|
|
xyz[v].normal[1] = 127 * r_avertexnormals[trivert.lightnormalindex][1];
|
|
|
|
xyz[v].normal[2] = 127 * r_avertexnormals[trivert.lightnormalindex][2];
|
2019-11-25 17:40:18 -08:00
|
|
|
xyz[v].normal[3] = 0; // unused; for 4-byte alignment
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in the ST coords at the end of the buffer
|
|
|
|
{
|
|
|
|
meshst_t *st;
|
|
|
|
float hscale, vscale;
|
|
|
|
|
|
|
|
//johnfitz -- padded skins
|
2019-11-25 17:40:18 -08:00
|
|
|
hscale = (float)hdr->skinwidth / (float)TexMgr_PadConditional(hdr->skinwidth);
|
|
|
|
vscale = (float)hdr->skinheight / (float)TexMgr_PadConditional(hdr->skinheight);
|
2019-11-24 20:45:15 -08:00
|
|
|
//johnfitz
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
st = (meshst_t *)(vbodata + m->vbostofs);
|
|
|
|
for(f = 0; f < hdr->numverts_vbo; f++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
st[f].st[0] = hscale * ((float) desc[f].st[0] + 0.5f) / (float) hdr->skinwidth;
|
|
|
|
st[f].st[1] = vscale * ((float) desc[f].st[1] + 0.5f) / (float) hdr->skinheight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// upload vertexes buffer
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_DeleteBuffersFunc(1, &m->meshvbo);
|
|
|
|
GL_GenBuffersFunc(1, &m->meshvbo);
|
|
|
|
GL_BindBufferFunc(GL_ARRAY_BUFFER, m->meshvbo);
|
|
|
|
GL_BufferDataFunc(GL_ARRAY_BUFFER, totalvbosize, vbodata, GL_STATIC_DRAW);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
free(vbodata);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// invalidate the cached bindings
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_ClearBufferBindings();
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
GLMesh_LoadVertexBuffers
|
|
|
|
|
|
|
|
Loop over all precached alias models, and upload each one to a VBO.
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
void GLMesh_LoadVertexBuffers(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t j;
|
2019-11-24 20:45:15 -08:00
|
|
|
qmodel_t *m;
|
|
|
|
const aliashdr_t *hdr;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!gl_glsl_alias_able)
|
2019-11-24 20:45:15 -08:00
|
|
|
return;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 1; j < MAX_MODELS; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!(m = cl.model_precache[j])) break;
|
|
|
|
if(m->type != mod_alias) continue;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
hdr = (const aliashdr_t *) Mod_Extradata(m);
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
GLMesh_LoadVertexBuffer(m, hdr);
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
GLMesh_DeleteVertexBuffers
|
|
|
|
|
|
|
|
Delete VBOs for all loaded alias models
|
|
|
|
================
|
|
|
|
*/
|
2019-11-25 17:40:18 -08:00
|
|
|
void GLMesh_DeleteVertexBuffers(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t j;
|
2019-11-24 20:45:15 -08:00
|
|
|
qmodel_t *m;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!gl_glsl_alias_able)
|
2019-11-24 20:45:15 -08:00
|
|
|
return;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
for(j = 1; j < MAX_MODELS; j++)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!(m = cl.model_precache[j])) break;
|
|
|
|
if(m->type != mod_alias) continue;
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_DeleteBuffersFunc(1, &m->meshvbo);
|
2019-11-24 20:45:15 -08:00
|
|
|
m->meshvbo = 0;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_DeleteBuffersFunc(1, &m->meshindexesvbo);
|
2019-11-24 20:45:15 -08:00
|
|
|
m->meshindexesvbo = 0;
|
|
|
|
}
|
2019-11-25 17:29:41 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
GL_ClearBufferBindings();
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|