Skip to content

Commit

Permalink
Redo removal Sky_ClipPoly: MAX_CLIP_VERTS hardcoded limit from scratch
Browse files Browse the repository at this point in the history
(precious was bugged and took too much stack space anyway)
  • Loading branch information
vsonnier committed Jul 20, 2024
1 parent 23e6c80 commit 0314204
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions Quake/gl_sky.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ void Sky_ProjectPoly (int nump, vec3_t vecs)
Sky_ClipPoly
=================
*/
void Sky_ClipPoly (int nump, vec3_t vecs, int stage)
static void Sky_ClipPoly (int nump, vec3_t vecs, int stage)
{
float *v;
qboolean front, back;
Expand All @@ -572,13 +572,6 @@ void Sky_ClipPoly (int nump, vec3_t vecs, int stage)

const size_t MAX_CLIP_VERTS = nump + 2;

TEMP_ALLOC (float, dists, MAX_CLIP_VERTS);
TEMP_ALLOC (int, sides, MAX_CLIP_VERTS);

// 2-dim vec3_t newv[2][MAX_CLIP_VERTS]; as 2 arrays
TEMP_ALLOC (vec3_t, newv_0, MAX_CLIP_VERTS);
TEMP_ALLOC (vec3_t, newv_1, MAX_CLIP_VERTS);

if (stage == 6) // fully clipped
{
Sky_ProjectPoly (nump, vecs);
Expand All @@ -587,6 +580,9 @@ void Sky_ClipPoly (int nump, vec3_t vecs, int stage)

front = back = false;

TEMP_ALLOC (int, sides, MAX_CLIP_VERTS);
TEMP_ALLOC (float, dists, MAX_CLIP_VERTS);

for (i = 0, v = vecs; i < nump; i++, v += 3)
{
d = DotProduct (v, skyclip[stage]);
Expand All @@ -607,7 +603,10 @@ void Sky_ClipPoly (int nump, vec3_t vecs, int stage)
}

if (!front || !back)
{ // not clipped
{
// not clipped
TEMP_FREE (dists);
TEMP_FREE (sides);
Sky_ClipPoly (nump, vecs, stage + 1);
return;
}
Expand All @@ -618,6 +617,10 @@ void Sky_ClipPoly (int nump, vec3_t vecs, int stage)
VectorCopy (vecs, (vecs + (i * 3)));
newc[0] = newc[1] = 0;

// 2-dim vec3_t newv[2][MAX_CLIP_VERTS]; as 2 arrays
TEMP_ALLOC (vec3_t, newv_0, MAX_CLIP_VERTS);
TEMP_ALLOC (vec3_t, newv_1, MAX_CLIP_VERTS);

for (i = 0, v = vecs; i < nump; i++, v += 3)
{
switch (sides[i])
Expand Down Expand Up @@ -652,14 +655,17 @@ void Sky_ClipPoly (int nump, vec3_t vecs, int stage)
newc[1]++;
}

// continue
Sky_ClipPoly (newc[0], newv_0[0], stage + 1);
Sky_ClipPoly (newc[1], newv_1[0], stage + 1);
vec3_t tmp_newv_0 = {*(newv_0[0])};
vec3_t tmp_newv_1 = {*(newv_1[0])};

TEMP_FREE (dists);
TEMP_FREE (sides);
TEMP_FREE (newv_0);
TEMP_FREE (newv_1);

// continue
Sky_ClipPoly (newc[0], tmp_newv_0, stage + 1);
Sky_ClipPoly (newc[1], tmp_newv_1, stage + 1);
}

/*
Expand All @@ -686,9 +692,12 @@ void Sky_ProcessPoly (cb_context_t *cbx, glpoly_t *p, float color[3])
poly_vert = &p->verts[0][0] + (i * VERTEXSIZE);
VectorSubtract (poly_vert, r_origin, verts[i]);
}
Sky_ClipPoly (MAX_CLIP_VERTS, verts[0], 0);

vec3_t verts_0 = {*(verts[0])};

TEMP_FREE (verts);

Sky_ClipPoly (MAX_CLIP_VERTS, verts_0, 0);
}
}

Expand Down

0 comments on commit 0314204

Please sign in to comment.