OpenGL2: Preallocate grid surfaces like other BSP surfaces.

This commit is contained in:
SmileTheory 2016-08-14 22:22:20 -07:00
parent 1cf0b21cda
commit fe8ac838e2
3 changed files with 65 additions and 88 deletions

View file

@ -254,14 +254,11 @@ static void MakeMeshTangentVectors(int width, int height, srfVert_t ctrl[MAX_GRI
#endif
static int MakeMeshIndexes(int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE],
glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3])
static int MakeMeshIndexes(int width, int height, glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3])
{
int i, j;
int numIndexes;
int w, h;
srfVert_t *dv;
static srfVert_t ctrl2[MAX_GRID_SIZE * MAX_GRID_SIZE];
h = height - 1;
w = width - 1;
@ -288,16 +285,6 @@ static int MakeMeshIndexes(int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][
}
}
// FIXME: use more elegant way
for(i = 0; i < width; i++)
{
for(j = 0; j < height; j++)
{
dv = &ctrl2[j * width + i];
*dv = ctrl[j][i];
}
}
return numIndexes;
}
@ -375,21 +362,17 @@ static void PutPointsOnCurve( srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE],
R_CreateSurfaceGridMesh
=================
*/
srfBspSurface_t *R_CreateSurfaceGridMesh(int width, int height,
void R_CreateSurfaceGridMesh(srfBspSurface_t *grid, int width, int height,
srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], float errorTable[2][MAX_GRID_SIZE],
int numIndexes, glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3]) {
int i, j, size;
int i, j;
srfVert_t *vert;
vec3_t tmpVec;
srfBspSurface_t *grid;
// copy the results out to a grid
size = (width * height - 1) * sizeof( srfVert_t ) + sizeof( *grid );
Com_Memset(grid, 0, sizeof(*grid));
#ifdef PATCH_STITCHING
grid = /*ri.Hunk_Alloc*/ ri.Malloc( size );
Com_Memset(grid, 0, size);
grid->widthLodError = /*ri.Hunk_Alloc*/ ri.Malloc( width * 4 );
Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
@ -403,9 +386,6 @@ srfBspSurface_t *R_CreateSurfaceGridMesh(int width, int height,
grid->numVerts = (width * height);
grid->verts = ri.Malloc(grid->numVerts * sizeof(srfVert_t));
#else
grid = ri.Hunk_Alloc( size );
Com_Memset(grid, 0, size);
grid->widthLodError = ri.Hunk_Alloc( width * 4 );
Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
@ -441,7 +421,6 @@ srfBspSurface_t *R_CreateSurfaceGridMesh(int width, int height,
VectorCopy( grid->cullOrigin, grid->lodOrigin );
grid->lodRadius = grid->cullRadius;
//
return grid;
}
/*
@ -449,12 +428,11 @@ srfBspSurface_t *R_CreateSurfaceGridMesh(int width, int height,
R_FreeSurfaceGridMesh
=================
*/
void R_FreeSurfaceGridMesh( srfBspSurface_t *grid ) {
void R_FreeSurfaceGridMeshData( srfBspSurface_t *grid ) {
ri.Free(grid->widthLodError);
ri.Free(grid->heightLodError);
ri.Free(grid->indexes);
ri.Free(grid->verts);
ri.Free(grid);
}
/*
@ -462,7 +440,7 @@ void R_FreeSurfaceGridMesh( srfBspSurface_t *grid ) {
R_SubdividePatchToGrid
=================
*/
srfBspSurface_t *R_SubdividePatchToGrid( int width, int height,
void R_SubdividePatchToGrid( srfBspSurface_t *grid, int width, int height,
srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) {
int i, j, k, l;
srfVert_t_cleared( prev );
@ -629,7 +607,7 @@ srfBspSurface_t *R_SubdividePatchToGrid( int width, int height,
#endif
// calculate indexes
numIndexes = MakeMeshIndexes(width, height, ctrl, indexes);
numIndexes = MakeMeshIndexes(width, height, indexes);
// calculate normals
MakeMeshNormals( width, height, ctrl );
@ -637,7 +615,7 @@ srfBspSurface_t *R_SubdividePatchToGrid( int width, int height,
MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
#endif
return R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numIndexes, indexes);
R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
}
/*
@ -645,7 +623,7 @@ srfBspSurface_t *R_SubdividePatchToGrid( int width, int height,
R_GridInsertColumn
===============
*/
srfBspSurface_t *R_GridInsertColumn( srfBspSurface_t *grid, int column, int row, vec3_t point, float loderror ) {
void R_GridInsertColumn( srfBspSurface_t *grid, int column, int row, vec3_t point, float loderror ) {
int i, j;
int width, height, oldwidth;
srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
@ -658,7 +636,7 @@ srfBspSurface_t *R_GridInsertColumn( srfBspSurface_t *grid, int column, int row,
oldwidth = 0;
width = grid->width + 1;
if (width > MAX_GRID_SIZE)
return NULL;
return;
height = grid->height;
for (i = 0; i < width; i++) {
if (i == column) {
@ -684,20 +662,22 @@ srfBspSurface_t *R_GridInsertColumn( srfBspSurface_t *grid, int column, int row,
//PutPointsOnCurve( ctrl, width, height );
// calculate indexes
numIndexes = MakeMeshIndexes(width, height, ctrl, indexes);
numIndexes = MakeMeshIndexes(width, height, indexes);
// calculate normals
MakeMeshNormals( width, height, ctrl );
#ifdef USE_VERT_TANGENT_SPACE
MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
#endif
VectorCopy(grid->lodOrigin, lodOrigin);
lodRadius = grid->lodRadius;
// free the old grid
R_FreeSurfaceGridMesh(grid);
R_FreeSurfaceGridMeshData(grid);
// create a new grid
grid = R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numIndexes, indexes);
R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
grid->lodRadius = lodRadius;
VectorCopy(lodOrigin, grid->lodOrigin);
return grid;
}
/*
@ -705,7 +685,7 @@ srfBspSurface_t *R_GridInsertColumn( srfBspSurface_t *grid, int column, int row,
R_GridInsertRow
===============
*/
srfBspSurface_t *R_GridInsertRow( srfBspSurface_t *grid, int row, int column, vec3_t point, float loderror ) {
void R_GridInsertRow( srfBspSurface_t *grid, int row, int column, vec3_t point, float loderror ) {
int i, j;
int width, height, oldheight;
srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
@ -719,7 +699,7 @@ srfBspSurface_t *R_GridInsertRow( srfBspSurface_t *grid, int row, int column, ve
width = grid->width;
height = grid->height + 1;
if (height > MAX_GRID_SIZE)
return NULL;
return;
for (i = 0; i < height; i++) {
if (i == row) {
//insert new row
@ -744,18 +724,20 @@ srfBspSurface_t *R_GridInsertRow( srfBspSurface_t *grid, int row, int column, ve
//PutPointsOnCurve( ctrl, width, height );
// calculate indexes
numIndexes = MakeMeshIndexes(width, height, ctrl, indexes);
numIndexes = MakeMeshIndexes(width, height, indexes);
// calculate normals
MakeMeshNormals( width, height, ctrl );
#ifdef USE_VERT_TANGENT_SPACE
MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
#endif
VectorCopy(grid->lodOrigin, lodOrigin);
lodRadius = grid->lodRadius;
// free the old grid
R_FreeSurfaceGridMesh(grid);
R_FreeSurfaceGridMeshData(grid);
// create a new grid
grid = R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numIndexes, indexes);
R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
grid->lodRadius = lodRadius;
VectorCopy(lodOrigin, grid->lodOrigin);
return grid;
}