Skip to content

Commit

Permalink
Refactor voxel vbo code per pogokeen's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeykt committed Mar 17, 2019
1 parent bb70406 commit 209f2c7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 55 deletions.
21 changes: 16 additions & 5 deletions source/blood/src/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "blood.h"
#include "config.h"
#include "resource.h"
#include "tile.h"

void qloadvoxel(int32_t nVoxel)
{
Expand Down Expand Up @@ -110,20 +111,30 @@ int tileInit(char a1, const char *a2)
SetBitString((char*)voxreserve, voxelIndex[i]);
}

artLoaded = 1;

PolymostProcessVoxels_Callback = tileProcessGLVoxels;

return 1;
}

#ifdef USE_OPENGL
void tileProcessGLVoxels(void)
{
static bool voxInit = false;
if (voxInit)
return;
voxInit = true;
for (int i = 0; i < kMaxVoxels; i++)
{
DICTNODE *hVox = gSysRes.Lookup(i, "KVX");
if (!hVox)
continue;
char *pVox = (char*)gSysRes.Load(hVox);
#ifdef USE_OPENGL
voxmodels[i] = loadkvxfrombuf(pVox, hVox->size);
#endif
}
artLoaded = 1;

return 1;
}
#endif

char * tileLoadTile(int nTile)
{
Expand Down
3 changes: 3 additions & 0 deletions source/blood/src/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ extern short voxelIndex[kMaxTiles];
void qloadvoxel(int32_t nVoxel);
void CalcPicsiz(int a1, int a2, int a3);
int tileInit(char a1, const char *a2);
#ifdef USE_OPENGL
void tileProcessGLVoxels(void);
#endif
char * tileLoadTile(int nTile);
char * tileAllocTile(int nTile, int x, int y, int ox, int oy);
void tilePreloadTile(int nTile);
Expand Down
3 changes: 3 additions & 0 deletions source/build/include/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,9 @@ extern int32_t(*deletesprite_replace)(int16_t spritenum);
extern int32_t(*changespritesect_replace)(int16_t spritenum, int16_t newsectnum);
extern int32_t(*changespritestat_replace)(int16_t spritenum, int16_t newstatnum);
extern void(*loadvoxel_replace)(int32_t voxel);
#ifdef USE_OPENGL
extern void(*PolymostProcessVoxels_Callback)(void);
#endif

extern int32_t automapping;
extern int32_t bloodhack;
Expand Down
1 change: 0 additions & 1 deletion source/build/include/mdsprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ typedef struct
vec3_t siz;
vec3f_t piv;
int32_t is8bit;
int32_t vboalloc;
GLuint vbo, vboindex;
} voxmodel_t;

Expand Down
3 changes: 3 additions & 0 deletions source/build/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9971,8 +9971,11 @@ static void videoAllocateBuffers(void)
}

#ifdef USE_OPENGL
void (*PolymostProcessVoxels_Callback)(void) = NULL;
static void PolymostProcessVoxels(void)
{
if (PolymostProcessVoxels_Callback)
PolymostProcessVoxels_Callback();
if (!g_haveVoxels)
return;

Expand Down
102 changes: 53 additions & 49 deletions source/build/src/voxmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,52 @@ static FORCE_INLINE int isair(int32_t i)
return !(vbit[i>>5] & (1<<SHIFTMOD32(i)));
}

#ifdef USE_GLEXT
static void voxvboalloc(voxmodel_t *vm)
{
const float phack[2] = { 0, 1.f / 256.f };
glGenBuffers(1, &vm->vbo);
glGenBuffers(1, &vm->vboindex);
GLfloat *vertex = (GLfloat *)Xmalloc(sizeof(GLfloat) * 5 * 4 * vm->qcnt);
GLuint *index = (GLuint *)Xmalloc(sizeof(GLuint) * 3 * 2 * vm->qcnt);
const float ru = 1.f / ((float)vm->mytexx);
const float rv = 1.f / ((float)vm->mytexy);
for (bssize_t i = 0; i < vm->qcnt; i++)
{
const vert_t *const vptr = &vm->quad[i].v[0];

const int32_t xx = vptr[0].x + vptr[2].x;
const int32_t yy = vptr[0].y + vptr[2].y;
const int32_t zz = vptr[0].z + vptr[2].z;

for (bssize_t j=0; j<4; j++)
{
vertex[(i*4+j)*5+0] = ((float)vptr[j].x) - phack[xx>vptr[j].x*2] + phack[xx<vptr[j].x*2];
vertex[(i*4+j)*5+1] = ((float)vptr[j].y) - phack[yy>vptr[j].y*2] + phack[yy<vptr[j].y*2];
vertex[(i*4+j)*5+2] = ((float)vptr[j].z) - phack[zz>vptr[j].z*2] + phack[zz<vptr[j].z*2];

vertex[(i*4+j)*5+3] = ((float)vptr[j].u)*ru;
vertex[(i*4+j)*5+4] = ((float)vptr[j].v)*rv;
}
index[(i*2+0)*3+0] = i*4+0;
index[(i*2+0)*3+1] = i*4+1;
index[(i*2+0)*3+2] = i*4+2;

index[(i*2+1)*3+0] = i*4+0;
index[(i*2+1)*3+1] = i*4+2;
index[(i*2+1)*3+2] = i*4+3;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->vboindex);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * 3 * 2 * vm->qcnt, index, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vm->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 5 * 4 * vm->qcnt, vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Bfree(vertex);
Bfree(index);
}
#endif

static voxmodel_t *vox2poly()
{
int32_t i, j;
Expand Down Expand Up @@ -581,6 +627,11 @@ static voxmodel_t *vox2poly()

Bfree(shp); Bfree(zbit); Bfree(bx0);

#ifdef USE_GLEXT
if (r_vbos)
voxvboalloc(gvox);
#endif

return gvox;
}

Expand Down Expand Up @@ -857,7 +908,7 @@ void voxfree(voxmodel_t *m)
DO_FREE_AND_NULL(m->texid);

#ifdef USE_GLEXT
if (m->vboalloc)
if (r_vbos)
{
glDeleteBuffers(1, &m->vbo);
glDeleteBuffers(1, &m->vboindex);
Expand Down Expand Up @@ -1029,54 +1080,6 @@ int32_t polymost_voxdraw(voxmodel_t *m, const uspritetype *tspr)
if ((tspr->cstat&48)==32)
return 0;

const float phack[2] = { 0, 1.f / 256.f };

#ifdef USE_GLEXT
if (r_vbos && !m->vboalloc)
{
glGenBuffers(1, &m->vbo);
glGenBuffers(1, &m->vboindex);
GLfloat *vertex = (GLfloat *)Xmalloc(sizeof(GLfloat) * 5 * 4 * m->qcnt);
GLuint *index = (GLuint *)Xmalloc(sizeof(GLuint) * 3 * 2 * m->qcnt);
const float ru = 1.f / ((float)m->mytexx);
const float rv = 1.f / ((float)m->mytexy);
for (bssize_t i = 0; i < m->qcnt; i++)
{
const vert_t *const vptr = &m->quad[i].v[0];

const int32_t xx = vptr[0].x + vptr[2].x;
const int32_t yy = vptr[0].y + vptr[2].y;
const int32_t zz = vptr[0].z + vptr[2].z;

for (bssize_t j=0; j<4; j++)
{
vertex[(i*4+j)*5+0] = ((float)vptr[j].x) - phack[xx>vptr[j].x*2] + phack[xx<vptr[j].x*2];
vertex[(i*4+j)*5+1] = ((float)vptr[j].y) - phack[yy>vptr[j].y*2] + phack[yy<vptr[j].y*2];
vertex[(i*4+j)*5+2] = ((float)vptr[j].z) - phack[zz>vptr[j].z*2] + phack[zz<vptr[j].z*2];

vertex[(i*4+j)*5+3] = ((float)vptr[j].u)*ru;
vertex[(i*4+j)*5+4] = ((float)vptr[j].v)*rv;
}
index[(i*2+0)*3+0] = i*4+0;
index[(i*2+0)*3+1] = i*4+1;
index[(i*2+0)*3+2] = i*4+2;

index[(i*2+1)*3+0] = i*4+0;
index[(i*2+1)*3+1] = i*4+2;
index[(i*2+1)*3+2] = i*4+3;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->vboindex);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * 3 * 2 * m->qcnt, index, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 5 * 4 * m->qcnt, vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Bfree(vertex);
Bfree(index);
m->vboalloc = 1;
}
#endif

//updateanimation((md2model *)m,tspr);

vec3f_t m0 = { m->scale, m->scale, m->scale };
Expand Down Expand Up @@ -1179,6 +1182,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, const uspritetype *tspr)
uhack[0] = ru*.125; uhack[1] = -uhack[0];
vhack[0] = rv*.125; vhack[1] = -vhack[0];
#endif
const float phack[2] = { 0, 1.f / 256.f };

if (!m->texid[globalpal])
m->texid[globalpal] = gloadtex(m->mytex, m->mytexx, m->mytexy, m->is8bit, globalpal);
Expand Down

0 comments on commit 209f2c7

Please sign in to comment.