[gl] Use a struct for glpoly_t's vertices

I always hated the float array for the different attributes.
This commit is contained in:
Bill Currie 2024-01-24 00:27:45 +09:00
parent bc9e85e429
commit 2203e2b4fd
6 changed files with 89 additions and 109 deletions

View file

@ -117,13 +117,17 @@ typedef struct {
texture_t *texture; texture_t *texture;
} mtexinfo_t; } mtexinfo_t;
#define VERTEXSIZE 7 typedef struct glvert_s {
float pos[3];
float tex_uv[2];
float lm_uv[2];
} glvert_t;
typedef struct glpoly_s { typedef struct glpoly_s {
struct glpoly_s *next; struct glpoly_s *next;
int numverts; int numverts;
int flags; // for SURF_UNDERWATER int flags; // for SURF_UNDERWATER
float verts[4][VERTEXSIZE]; // variable sized (xyz s1t1 s2t2) glvert_t verts[4]; // variable
} glpoly_t; } glpoly_t;
#define MAX_DLIGHTS 128 #define MAX_DLIGHTS 128

View file

@ -280,17 +280,16 @@ SubdividePolygon (int numverts, float *verts)
return; return;
} }
poly = Hunk_Alloc (0, sizeof (glpoly_t) + (numverts - 4) * VERTEXSIZE * poly = Hunk_Alloc (0, field_offset (glpoly_t, verts[numverts]));
sizeof (float));
poly->next = warpface->polys; poly->next = warpface->polys;
warpface->polys = poly; warpface->polys = poly;
poly->numverts = numverts; poly->numverts = numverts;
for (i = 0; i < numverts; i++, verts += 3) { for (i = 0; i < numverts; i++, verts += 3) {
VectorCopy (verts, poly->verts[i]); VectorCopy (verts, poly->verts[i].pos);
s = DotProduct (verts, warpface->texinfo->vecs[0]); s = DotProduct (verts, warpface->texinfo->vecs[0]);
t = DotProduct (verts, warpface->texinfo->vecs[1]); t = DotProduct (verts, warpface->texinfo->vecs[1]);
poly->verts[i][3] = s; poly->verts[i].tex_uv[0] = s;
poly->verts[i][4] = t; poly->verts[i].tex_uv[1] = t;
} }
} }

View file

@ -432,7 +432,6 @@ R_BuildLightMap_4 (const vec4f_t *transform, mod_brush_t *brush,
void void
gl_R_BlendLightmaps (void) gl_R_BlendLightmaps (void)
{ {
float *v;
instsurf_t *sc; instsurf_t *sc;
glpoly_t *p; glpoly_t *p;
@ -447,10 +446,10 @@ gl_R_BlendLightmaps (void)
} }
for (p = sc->surface->polys; p; p = p->next) { for (p = sc->surface->polys; p; p = p->next) {
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
v = p->verts[0]; auto v = p->verts;
for (int j = 0; j < p->numverts; j++, v += VERTEXSIZE) { for (int j = 0; j < p->numverts; j++, v++) {
qfglTexCoord2fv (&v[5]); qfglTexCoord2fv (v->lm_uv);
qfglVertex3fv (v); qfglVertex3fv (v->pos);
} }
qfglEnd (); qfglEnd ();
} }

View file

@ -183,13 +183,11 @@ gl_R_ClearTextures (void)
static void static void
R_RenderFullbrights (void) R_RenderFullbrights (void)
{ {
float *v;
int i, j;
glpoly_t *p; glpoly_t *p;
instsurf_t *sc; instsurf_t *sc;
gltex_t *tex; gltex_t *tex;
for (i = 0; i < r_num_texture_chains; i++) { for (int i = 0; i < r_num_texture_chains; i++) {
if (!(tex = r_texture_chains[i]) || !tex->gl_fb_texturenum) if (!(tex = r_texture_chains[i]) || !tex->gl_fb_texturenum)
continue; continue;
qfglBindTexture (GL_TEXTURE_2D, tex->gl_fb_texturenum); qfglBindTexture (GL_TEXTURE_2D, tex->gl_fb_texturenum);
@ -202,11 +200,11 @@ R_RenderFullbrights (void)
qfglColor4fv (sc->color); qfglColor4fv (sc->color);
for (p = sc->surface->polys; p; p = p->next) { for (p = sc->surface->polys; p; p = p->next) {
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
for (j = 0, v = p->verts[0]; j < p->numverts; auto v = p->verts;
j++, v += VERTEXSIZE) for (int j = 0; j < p->numverts; j++, v++)
{ {
qfglTexCoord2fv (&v[3]); qfglTexCoord2fv (v->tex_uv);
qfglVertex3fv (v); qfglVertex3fv (v->pos);
} }
qfglEnd (); qfglEnd ();
} }
@ -221,19 +219,15 @@ R_RenderFullbrights (void)
static inline void static inline void
R_RenderBrushPoly_3 (msurface_t *surf) R_RenderBrushPoly_3 (msurface_t *surf)
{ {
float *v;
int i;
gl_ctx->brush_polys++; gl_ctx->brush_polys++;
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
v = surf->polys->verts[0]; auto v = surf->polys->verts;
for (int i = 0; i < surf->polys->numverts; i++, v++) {
for (i = 0; i < surf->polys->numverts; i++, v += VERTEXSIZE) { qglMultiTexCoord2fv (gl_mtex_enum + 0, v->tex_uv);
qglMultiTexCoord2fv (gl_mtex_enum + 0, &v[3]); qglMultiTexCoord2fv (gl_mtex_enum + 1, v->lm_uv);
qglMultiTexCoord2fv (gl_mtex_enum + 1, &v[5]); qglMultiTexCoord2fv (gl_mtex_enum + 2, v->tex_uv);
qglMultiTexCoord2fv (gl_mtex_enum + 2, &v[3]); qfglVertex3fv (v->pos);
qfglVertex3fv (v);
} }
qfglEnd (); qfglEnd ();
@ -242,18 +236,14 @@ R_RenderBrushPoly_3 (msurface_t *surf)
static inline void static inline void
R_RenderBrushPoly_2 (msurface_t *surf) R_RenderBrushPoly_2 (msurface_t *surf)
{ {
float *v;
int i;
gl_ctx->brush_polys++; gl_ctx->brush_polys++;
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
v = surf->polys->verts[0]; auto v = surf->polys->verts;
for (int i = 0; i < surf->polys->numverts; i++, v++) {
for (i = 0; i < surf->polys->numverts; i++, v += VERTEXSIZE) { qglMultiTexCoord2fv (gl_mtex_enum + 0, v->tex_uv);
qglMultiTexCoord2fv (gl_mtex_enum + 0, &v[3]); qglMultiTexCoord2fv (gl_mtex_enum + 1, v->lm_uv);
qglMultiTexCoord2fv (gl_mtex_enum + 1, &v[5]); qfglVertex3fv (v->pos);
qfglVertex3fv (v);
} }
qfglEnd (); qfglEnd ();
@ -262,17 +252,13 @@ R_RenderBrushPoly_2 (msurface_t *surf)
static inline void static inline void
R_RenderBrushPoly_1 (msurface_t *surf) R_RenderBrushPoly_1 (msurface_t *surf)
{ {
float *v;
int i;
gl_ctx->brush_polys++; gl_ctx->brush_polys++;
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
v = surf->polys->verts[0]; auto v = surf->polys->verts;
for (int i = 0; i < surf->polys->numverts; i++, v++) {
for (i = 0; i < surf->polys->numverts; i++, v += VERTEXSIZE) { qfglTexCoord2fv (v->tex_uv);
qfglTexCoord2fv (&v[3]); qfglVertex3fv (v->pos);
qfglVertex3fv (v);
} }
qfglEnd (); qfglEnd ();
@ -820,8 +806,7 @@ GL_BuildSurfaceDisplayList (mod_brush_t *brush, msurface_t *surf)
lnumverts = surf->numedges; lnumverts = surf->numedges;
// draw texture // draw texture
poly = Hunk_Alloc (0, sizeof (glpoly_t) + (lnumverts - 4) * poly = Hunk_Alloc (0, field_offset (glpoly_t, verts[lnumverts]));
VERTEXSIZE * sizeof (float));
poly->next = surf->polys; poly->next = surf->polys;
poly->flags = surf->flags; poly->flags = surf->flags;
surf->polys = poly; surf->polys = poly;
@ -844,9 +829,9 @@ GL_BuildSurfaceDisplayList (mod_brush_t *brush, msurface_t *surf)
t = DotProduct (vec, texinfo->vecs[1]) + texinfo->vecs[1][3]; t = DotProduct (vec, texinfo->vecs[1]) + texinfo->vecs[1][3];
t /= texinfo->texture->height; t /= texinfo->texture->height;
VectorCopy (vec, poly->verts[i]); VectorCopy (vec, poly->verts[i].pos);
poly->verts[i][3] = s; poly->verts[i].tex_uv[0] = s;
poly->verts[i][4] = t; poly->verts[i].tex_uv[1] = t;
// lightmap texture coordinates // lightmap texture coordinates
s = DotProduct (vec, texinfo->vecs[0]) + texinfo->vecs[0][3]; s = DotProduct (vec, texinfo->vecs[0]) + texinfo->vecs[0][3];
@ -857,23 +842,22 @@ GL_BuildSurfaceDisplayList (mod_brush_t *brush, msurface_t *surf)
t += surf->lightpic->rect->y * 16 + 8; t += surf->lightpic->rect->y * 16 + 8;
s /= 16; s /= 16;
t /= 16; t /= 16;
poly->verts[i][5] = s * surf->lightpic->size; poly->verts[i].lm_uv[0] = s * surf->lightpic->size;
poly->verts[i][6] = t * surf->lightpic->size; poly->verts[i].lm_uv[1] = t * surf->lightpic->size;
} }
// remove co-linear points - Ed // remove co-linear points - Ed
if (!gl_keeptjunctions) { if (!gl_keeptjunctions) {
for (i = 0; i < lnumverts; ++i) { for (i = 0; i < lnumverts; ++i) {
vec3_t v1, v2; vec3_t v1, v2;
float *prev, *this, *next;
prev = poly->verts[(i + lnumverts - 1) % lnumverts]; auto prev = &poly->verts[(i + lnumverts - 1) % lnumverts];
this = poly->verts[i]; auto this = &poly->verts[i];
next = poly->verts[(i + 1) % lnumverts]; auto next = &poly->verts[(i + 1) % lnumverts];
VectorSubtract (this, prev, v1); VectorSubtract (this->pos, prev->pos, v1);
VectorNormalize (v1); VectorNormalize (v1);
VectorSubtract (next, prev, v2); VectorSubtract (next->pos, prev->pos, v2);
VectorNormalize (v2); VectorNormalize (v2);
// skip co-linear points // skip co-linear points
@ -881,13 +865,8 @@ GL_BuildSurfaceDisplayList (mod_brush_t *brush, msurface_t *surf)
if ((fabs (v1[0] - v2[0]) <= COLINEAR_EPSILON) && if ((fabs (v1[0] - v2[0]) <= COLINEAR_EPSILON) &&
(fabs (v1[1] - v2[1]) <= COLINEAR_EPSILON) && (fabs (v1[1] - v2[1]) <= COLINEAR_EPSILON) &&
(fabs (v1[2] - v2[2]) <= COLINEAR_EPSILON)) { (fabs (v1[2] - v2[2]) <= COLINEAR_EPSILON)) {
int j; for (int j = i + 1; j < lnumverts; ++j) {
poly->verts[j - 1] = poly->verts[j];
for (j = i + 1; j < lnumverts; ++j) {
int k;
for (k = 0; k < VERTEXSIZE; ++k)
poly->verts[j - 1][k] = poly->verts[j][k];
} }
--lnumverts; --lnumverts;
// retry next vertex next time, which is now current vertex // retry next vertex next time, which is now current vertex

View file

@ -83,7 +83,7 @@ static const vec_t face_offset[] = { 1024, 1024, 1024, -1024, -1024, -1024 };
struct face_def { struct face_def {
int tex; // texture to bind to int tex; // texture to bind to
glpoly_t poly; // describe the polygon of this face glpoly_t poly; // describe the polygon of this face
float verts[32][VERTEXSIZE]; glvert_t verts[32];
}; };
struct visit_def { struct visit_def {
@ -224,31 +224,32 @@ find_cube_vertex (int face1, int face2, int face3, vec3_t v)
static void static void
set_vertex (struct box_def *box, int face, int ind, const vec3_t v) set_vertex (struct box_def *box, int face, int ind, const vec3_t v)
{ {
VectorAdd (v, r_refdef.frame.position, box->face[face].poly.verts[ind]); auto poly = &box->face[face].poly;
VectorAdd (v, r_refdef.frame.position, poly->verts[ind].pos);
switch (face) { switch (face) {
case 0: case 0:
box->face[face].poly.verts[ind][3] = (1024 - v[1] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 - v[1] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 - v[2] + 4) / BOX_WIDTH;
break; break;
case 1: case 1:
box->face[face].poly.verts[ind][3] = (1024 + v[0] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 + v[0] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 - v[2] + 4) / BOX_WIDTH;
break; break;
case 2: case 2:
box->face[face].poly.verts[ind][3] = (1024 - v[1] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 - v[1] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 + v[0] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 + v[0] + 4) / BOX_WIDTH;
break; break;
case 3: case 3:
box->face[face].poly.verts[ind][3] = (1024 + v[1] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 + v[1] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 - v[2] + 4) / BOX_WIDTH;
break; break;
case 4: case 4:
box->face[face].poly.verts[ind][3] = (1024 - v[0] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 - v[0] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 - v[2] + 4) / BOX_WIDTH;
break; break;
case 5: case 5:
box->face[face].poly.verts[ind][3] = (1024 - v[1] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[0] = (1024 - v[1] + 4) / BOX_WIDTH;
box->face[face].poly.verts[ind][4] = (1024 - v[0] + 4) / BOX_WIDTH; poly->verts[ind].tex_uv[1] = (1024 - v[0] + 4) / BOX_WIDTH;
break; break;
} }
} }
@ -300,7 +301,7 @@ insert_cube_vertices (struct box_def *box, struct visit_def visit, int count,
int c = p->numverts - ind; int c = p->numverts - ind;
const int vert_size = sizeof (p->verts[0]); const int vert_size = sizeof (p->verts[0]);
memmove (p->verts[ind + count], p->verts[ind], c * vert_size); memmove (&p->verts[ind + count], &p->verts[ind], c * vert_size);
p->numverts += count; p->numverts += count;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
set_vertex (box, face, ind + i, *v[i]); set_vertex (box, face, ind + i, *v[i]);
@ -568,8 +569,8 @@ render_box (const struct box_def *box)
qfglBindTexture (GL_TEXTURE_2D, box->face[i].tex); qfglBindTexture (GL_TEXTURE_2D, box->face[i].tex);
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
for (j = 0; j < box->face[i].poly.numverts; j++) { for (j = 0; j < box->face[i].poly.numverts; j++) {
qfglTexCoord2fv (box->face[i].poly.verts[j] + 3); qfglTexCoord2fv (box->face[i].poly.verts[j].tex_uv);
qfglVertex3fv (box->face[i].poly.verts[j]); qfglVertex3fv (box->face[i].poly.verts[j].pos);
} }
qfglEnd (); qfglEnd ();
} }
@ -598,14 +599,15 @@ R_DrawSkyBoxPoly (const glpoly_t *poly)
Sys_Error ("too many verts!"); Sys_Error ("too many verts!");
} }
VectorSubtract (poly->verts[poly->numverts - 1], r_refdef.frame.position, last_v); VectorSubtract (poly->verts[poly->numverts - 1].pos,
r_refdef.frame.position, last_v);
prev_face = determine_face (last_v); prev_face = determine_face (last_v);
box.visited_faces[0].face = prev_face; box.visited_faces[0].face = prev_face;
box.face_count = 1; box.face_count = 1;
for (i = 0; i < poly->numverts; i++) { for (i = 0; i < poly->numverts; i++) {
VectorSubtract (poly->verts[i], r_refdef.frame.position, v); VectorSubtract (poly->verts[i].pos, r_refdef.frame.position, v);
face = determine_face (v); face = determine_face (v);
if (face != prev_face) { if (face != prev_face) {
if ((face_axis[face]) == (face_axis[prev_face])) { if ((face_axis[face]) == (face_axis[prev_face])) {
@ -637,18 +639,16 @@ static void
EmitSkyPolys (float speedscale, const instsurf_t *sc) EmitSkyPolys (float speedscale, const instsurf_t *sc)
{ {
float length, s, t; float length, s, t;
float *v;
int i;
glpoly_t *p;
vec3_t dir; vec3_t dir;
msurface_t *surf = sc->surface; msurface_t *surf = sc->surface;
vec4f_t origin = r_refdef.frame.position; vec4f_t origin = r_refdef.frame.position;
//FIXME transform/color //FIXME transform/color
for (p = surf->polys; p; p = p->next) { for (auto p = surf->polys; p; p = p->next) {
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
for (i = 0, v = p->verts[0]; i < p->numverts; i++, v += VERTEXSIZE) { auto v = p->verts;
VectorSubtract (v, origin, dir); for (int i = 0; i < p->numverts; i++, v++) {
VectorSubtract (v->pos, origin, dir);
dir[2] *= 3.0; // flatten the sphere dir[2] *= 3.0; // flatten the sphere
length = DotProduct (dir, dir); length = DotProduct (dir, dir);
@ -661,7 +661,7 @@ EmitSkyPolys (float speedscale, const instsurf_t *sc)
t = speedscale + dir[1]; t = speedscale + dir[1];
qfglTexCoord2f (s, t); qfglTexCoord2f (s, t);
qfglVertex3fv (v); qfglVertex3fv (v->pos);
} }
qfglEnd (); qfglEnd ();
} }
@ -674,7 +674,7 @@ draw_poly (const glpoly_t *poly)
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
for (i = 0; i < poly->numverts; i++) { for (i = 0; i < poly->numverts; i++) {
qfglVertex3fv (poly->verts[i]); qfglVertex3fv (poly->verts[i].pos);
} }
qfglEnd (); qfglEnd ();
} }
@ -836,7 +836,7 @@ gl_R_DrawSkyChain (const instsurf_t *sky_chain)
qfglBegin (GL_LINE_LOOP); qfglBegin (GL_LINE_LOOP);
for (i = 0; i < p->numverts; i++) { for (i = 0; i < p->numverts; i++) {
qfglVertex3fv (p->verts[i]); qfglVertex3fv (p->verts[i].pos);
} }
qfglEnd (); qfglEnd ();
p = p->next; p = p->next;
@ -862,7 +862,8 @@ gl_R_DrawSkyChain (const instsurf_t *sky_chain)
vec3_t x, c = { 0, 0, 0 }; vec3_t x, c = { 0, 0, 0 };
for (i = 0; i < p->numverts; i++) { for (i = 0; i < p->numverts; i++) {
VectorSubtract (p->verts[i], r_refdef.frame.position, x); VectorSubtract (p->verts[i].pos,
r_refdef.frame.position, x);
VectorAdd (x, c, c); VectorAdd (x, c, c);
} }
VectorScale (c, 1.0 / p->numverts, c); VectorScale (c, 1.0 / p->numverts, c);

View file

@ -55,29 +55,27 @@ void
GL_EmitWaterPolys (msurface_t *surf) GL_EmitWaterPolys (msurface_t *surf)
{ {
float os, ot, s, t, timetemp; float os, ot, s, t, timetemp;
float *v;
int i;
glpoly_t *p;
timetemp = vr_data.realtime * TURBSCALE; timetemp = vr_data.realtime * TURBSCALE;
for (p = surf->polys; p; p = p->next) { for (auto p = surf->polys; p; p = p->next) {
qfglBegin (GL_POLYGON); qfglBegin (GL_POLYGON);
for (i = 0, v = p->verts[0]; i < p->numverts; i++, v += VERTEXSIZE) { auto v = p->verts;
os = turbsin[(int) (v[3] * TURBFRAC + timetemp) & 255]; for (int i = 0; i < p->numverts; i++, v++) {
ot = turbsin[(int) (v[4] * TURBFRAC + timetemp) & 255]; os = turbsin[(int) (v->tex_uv[0] * TURBFRAC + timetemp) & 255];
s = (v[3] + ot) * (1.0 / 64.0); ot = turbsin[(int) (v->tex_uv[1] * TURBFRAC + timetemp) & 255];
t = (v[4] + os) * (1.0 / 64.0); s = (v->tex_uv[0] + ot) * (1.0 / 64.0);
t = (v->tex_uv[1] + os) * (1.0 / 64.0);
qfglTexCoord2f (s, t); qfglTexCoord2f (s, t);
if (r_waterripple != 0) { if (r_waterripple != 0) {
vec3_t nv; vec3_t nv;
VectorCopy (v, nv); VectorCopy (v->pos, nv);
nv[2] += r_waterripple * os * ot * (1.0 / 64.0); nv[2] += r_waterripple * os * ot * (1.0 / 64.0);
qfglVertex3fv (nv); qfglVertex3fv (nv);
} else } else
qfglVertex3fv (v); qfglVertex3fv (v->pos);
} }
qfglEnd (); qfglEnd ();
} }