Skip to content

Commit

Permalink
Migrate other model rendering to use VBOs
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Aug 25, 2023
1 parent a8368cb commit c81b3d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 82 deletions.
51 changes: 9 additions & 42 deletions source_files/edge/r_md2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,19 @@ class md2_model_c

int verts_per_frame;

GLuint vbo; // One VBO updated with lerping info
GLuint vbo;

local_gl_vert_t *gl_verts;

public:
md2_model_c(int _nframe, int _npoint, int _ntris) :
num_frames(_nframe), num_points(_npoint),
num_tris(_ntris), verts_per_frame(0), vbo(0)
num_tris(_ntris), verts_per_frame(0), vbo(0), gl_verts(nullptr)
{
frames = new md2_frame_c[num_frames];
points = new md2_point_c[num_points];
tris = new md2_triangle_c[num_tris];
gl_verts = new local_gl_vert_t[num_tris * 3];
}

~md2_model_c()
Expand Down Expand Up @@ -501,13 +504,11 @@ md2_model_c *MD2_LoadModel(epi::file_c *f)

delete[] raw_verts;

#ifdef EDGE_GL_ES2
glGenBuffers(1, &md->vbo);
if (md->vbo == 0)
I_Error("MD2_LoadModel: Failed to bind VBO!\n");
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
glBufferData(GL_ARRAY_BUFFER, md->num_tris * 3 * sizeof(local_gl_vert_t), NULL, GL_STREAM_DRAW);
#endif

return md;
}
Expand Down Expand Up @@ -757,13 +758,11 @@ md2_model_c *MD3_LoadModel(epi::file_c *f)

// TODO: load in bbox (for visibility checking)
}
#ifdef EDGE_GL_ES2
glGenBuffers(1, &md->vbo);
if (md->vbo == 0)
I_Error("MD3_LoadModel: Failed to create VBO!\n");
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
glBufferData(GL_ARRAY_BUFFER, md->num_tris * 3 * sizeof(local_gl_vert_t), NULL, GL_STREAM_DRAW);
#endif
return md;
}

Expand Down Expand Up @@ -1167,8 +1166,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
}
}

#ifdef EDGE_GL_ES2
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
if (!r_culling.d && fc_to_use != RGB_NO_VALUE)
{
GLfloat fc[4];
Expand Down Expand Up @@ -1232,17 +1229,14 @@ I_Debugf("Render model: bad frame %d\n", frame1);
}
else
glDisable(GL_FOG);
#endif

for (int pass = 0; pass < num_pass; pass++)
{
if (pass == 1)
{
blending &= ~BL_Alpha;
blending |= BL_Add;
#ifdef EDGE_GL_ES2
glDisable(GL_FOG);
#endif
}

data.is_additive = (pass > 0 && pass == num_pass-1);
Expand All @@ -1259,7 +1253,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
continue;
}

#ifdef EDGE_GL_ES2
GLuint model_env = data.is_additive ? ENV_SKIP_RGB : GL_MODULATE;

glPolygonOffset(0, -pass);
Expand Down Expand Up @@ -1335,7 +1328,7 @@ I_Debugf("Render model: bad frame %d\n", frame1);
r_dumbclamp.d ? GL_CLAMP : GL_CLAMP_TO_EDGE);
}

local_gl_vert_t *start = (local_gl_vert_t *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
local_gl_vert_t *start = md->gl_verts;

for (int i = 0; i < md->num_tris; i++)
{
Expand All @@ -1352,9 +1345,9 @@ I_Debugf("Render model: bad frame %d\n", frame1);
}
}

glUnmapBuffer(GL_ARRAY_BUFFER);

// setup pointers to client state
// setup client state
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
glBufferData(GL_ARRAY_BUFFER, md->num_tris * 3 * sizeof(local_gl_vert_t), md->gl_verts, GL_STREAM_DRAW);
glVertexPointer(3, GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, pos.x)));
glColorPointer (4, GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, rgba)));
glNormalPointer(GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, normal.x)));
Expand All @@ -1370,32 +1363,7 @@ I_Debugf("Render model: bad frame %d\n", frame1);
// restore the clamping mode
if (old_clamp != 789)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, old_clamp);
#else
local_gl_vert_t * glvert = RGL_BeginUnit(
GL_TRIANGLES, md->num_tris * 3,
data.is_additive ? ENV_SKIP_RGB : GL_MODULATE, skin_tex,
ENV_NONE, 0, pass, blending, pass > 0 ? RGB_NO_VALUE : fc_to_use,
fd_to_use);

for (int i = 0; i < md->num_tris; i++)
{
data.tri = & md->tris[i];

for (int v_idx=0; v_idx < 3; v_idx++)
{
local_gl_vert_t *dest = glvert + (i*3) + v_idx;

ModelCoordFunc(&data, v_idx, &dest->pos, dest->rgba,
&dest->texc[0], &dest->normal);

dest->rgba[3] = trans;
}
}

RGL_EndUnit(md->num_tris * 3);
#endif
}
#ifdef EDGE_GL_ES2
glPolygonOffset(0, 0);

glDisable(GL_TEXTURE_2D);
Expand All @@ -1409,7 +1377,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
#endif
}


Expand Down
49 changes: 9 additions & 40 deletions source_files/edge/r_mdl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,20 @@ class mdl_model_c

std::vector<u32_t> skin_ids;

GLuint vbo; // One VBO updated with lerping info
GLuint vbo;

local_gl_vert_t *gl_verts;

public:
mdl_model_c(int _nframe, int _npoint, int _ntris, int _swidth, int _sheight) :
num_frames(_nframe), num_points(_npoint),
num_tris(_ntris), skin_width(_swidth),
skin_height(_sheight), verts_per_frame(0), vbo(0)
skin_height(_sheight), verts_per_frame(0), vbo(0), gl_verts(nullptr)
{
frames = new mdl_frame_c[num_frames];
points = new mdl_point_c[num_points];
tris = new mdl_triangle_c[num_tris];
gl_verts = new local_gl_vert_t[num_tris * 3];
}

~mdl_model_c()
Expand Down Expand Up @@ -435,13 +438,11 @@ mdl_model_c *MDL_LoadModel(epi::file_c *f)
delete[] texcoords;
delete[] tris;
delete[] frames;
#ifdef EDGE_GL_ES2
glGenBuffers(1, &md->vbo);
if (md->vbo == 0)
I_Error("MDL_LoadModel: Failed to bind VBO!\n");
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
glBufferData(GL_ARRAY_BUFFER, md->num_tris * 3 * sizeof(local_gl_vert_t), NULL, GL_STREAM_DRAW);
#endif
return md;
}

Expand Down Expand Up @@ -853,8 +854,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
fd_to_use = 0.01f * currmap->indoor_fog_density;
}
}
#ifdef EDGE_GL_ES2
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
if (!r_culling.d && fc_to_use != RGB_NO_VALUE)
{
GLfloat fc[4];
Expand Down Expand Up @@ -918,17 +917,14 @@ I_Debugf("Render model: bad frame %d\n", frame1);
}
else
glDisable(GL_FOG);
#endif

for (int pass = 0; pass < num_pass; pass++)
{
if (pass == 1)
{
blending &= ~BL_Alpha;
blending |= BL_Add;
#ifdef EDGE_GL_ES2
glDisable(GL_FOG);
#endif
}

data.is_additive = (pass > 0 && pass == num_pass-1);
Expand All @@ -944,7 +940,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
if (MDL_MulticolMaxRGB(&data, true) <= 0)
continue;
}
#ifdef EDGE_GL_ES2
GLuint model_env = data.is_additive ? ENV_SKIP_RGB : GL_MODULATE;

glPolygonOffset(0, -pass);
Expand Down Expand Up @@ -1020,7 +1015,7 @@ I_Debugf("Render model: bad frame %d\n", frame1);
r_dumbclamp.d ? GL_CLAMP : GL_CLAMP_TO_EDGE);
}

local_gl_vert_t *start = (local_gl_vert_t *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
local_gl_vert_t *start = md->gl_verts;

for (int i = 0; i < md->num_tris; i++)
{
Expand All @@ -1037,9 +1032,9 @@ I_Debugf("Render model: bad frame %d\n", frame1);
}
}

glUnmapBuffer(GL_ARRAY_BUFFER);

// setup pointers to client state
// setup client state
glBindBuffer(GL_ARRAY_BUFFER, md->vbo);
glBufferData(GL_ARRAY_BUFFER, md->num_tris * 3 * sizeof(local_gl_vert_t), md->gl_verts, GL_STREAM_DRAW);
glVertexPointer(3, GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, pos.x)));
glColorPointer (4, GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, rgba)));
glNormalPointer(GL_FLOAT, sizeof(local_gl_vert_t), BUFFER_OFFSET(offsetof(local_gl_vert_t, normal.x)));
Expand All @@ -1055,32 +1050,7 @@ I_Debugf("Render model: bad frame %d\n", frame1);
// restore the clamping mode
if (old_clamp != 789)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, old_clamp);
#else
local_gl_vert_t * glvert = RGL_BeginUnit(
GL_TRIANGLES, md->num_tris * 3,
data.is_additive ? ENV_SKIP_RGB : GL_MODULATE, skin_tex,
ENV_NONE, 0, pass, blending, pass > 0 ? RGB_NO_VALUE : fc_to_use,
fd_to_use);

for (int i = 0; i < md->num_tris; i++)
{
data.strip = & md->tris[i];

for (int v_idx=0; v_idx < 3; v_idx++)
{
local_gl_vert_t *dest = glvert + (i*3) + v_idx;

ModelCoordFunc(&data, v_idx, &dest->pos, dest->rgba,
&dest->texc[0], &dest->normal);

dest->rgba[3] = trans;
}
}

RGL_EndUnit(md->num_tris * 3);
#endif
}
#ifdef EDGE_GL_ES2
glPolygonOffset(0, 0);

glDisable(GL_TEXTURE_2D);
Expand All @@ -1094,7 +1064,6 @@ I_Debugf("Render model: bad frame %d\n", frame1);
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
#endif
}


Expand Down

0 comments on commit c81b3d0

Please sign in to comment.