Skip to content

Commit

Permalink
Quicky hack at DECOUPLED_LM support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpoike committed Nov 2, 2023
1 parent ecf20de commit 528824e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 86 deletions.
7 changes: 7 additions & 0 deletions Quake/bspfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ typedef struct
int lightofs; // start of [numstyles*surfsize] samples
} dlface_t;

struct decoupled_lm_info_s
{
unsigned short lmsize[2]; //made explicit. beware MAX_
unsigned int lmoffset; //replacement offset for vanilla compat.
vec4_t lmvecs[2]; //lmcoord[] = dotproduct3(vertexcoord, lmvecs[])+lmvecs[][3]
};

#define AMBIENT_WATER 0
#define AMBIENT_SKY 1
#define AMBIENT_SLIME 2
Expand Down
102 changes: 73 additions & 29 deletions Quake/gl_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static void Mod_Print (void);
static cvar_t external_ents = {"external_ents", "1", CVAR_ARCHIVE};
cvar_t gl_load24bit = {"gl_load24bit", "1", CVAR_ARCHIVE};
static cvar_t mod_ignorelmscale = {"mod_ignorelmscale", "0"};
static cvar_t mod_lightscale_broken = {"mod_lightscale_broken", "1"}; //match vanilla's brokenness bug with dlights and scaled textures. decoupled_lm bypasses this obviously buggy setting because zomgletmefixstuffffs
cvar_t r_replacemodels = {"r_replacemodels", "", CVAR_ARCHIVE};
static cvar_t external_vis = {"external_vis", "1", CVAR_ARCHIVE};

Expand Down Expand Up @@ -72,6 +73,7 @@ void Mod_Init (void)
Cvar_RegisterVariable (&gl_load24bit);
Cvar_RegisterVariable (&r_replacemodels);
Cvar_RegisterVariable (&mod_ignorelmscale);
Cvar_RegisterVariable (&mod_lightscale_broken);

Cmd_AddCommand ("mcache", Mod_Print);

Expand Down Expand Up @@ -1433,14 +1435,14 @@ CalcSurfaceExtents
Fills in s->texturemins[] and s->extents[]
================
*/
static void CalcSurfaceExtents (msurface_t *s)
static void CalcSurfaceExtents (msurface_t *s, int lmshift)
{
float mins[2], maxs[2], val;
int i,j, e;
mvertex_t *v;
mtexinfo_t *tex;
int bmins[2], bmaxs[2];
int maxextent, lmscale;
int lmscale;

mins[0] = mins[1] = FLT_MAX;
maxs[0] = maxs[1] = -FLT_MAX;
Expand Down Expand Up @@ -1484,18 +1486,24 @@ static void CalcSurfaceExtents (msurface_t *s)
}
}

lmscale = 1<<s->lmshift;
maxextent = q_max(LMBLOCK_WIDTH,LMBLOCK_HEIGHT)*lmscale;
lmscale = 1<<lmshift;

for (i=0 ; i<2 ; i++)
{
bmins[i] = floor(mins[i]/lmscale);
bmaxs[i] = ceil(maxs[i]/lmscale);

s->texturemins[i] = bmins[i] * lmscale;
s->extents[i] = (bmaxs[i] - bmins[i]) * lmscale;
s->lmvecs[i][0] = s->texinfo->vecs[i][0] / lmscale;
s->lmvecs[i][1] = s->texinfo->vecs[i][1] / lmscale;
s->lmvecs[i][2] = s->texinfo->vecs[i][2] / lmscale;
s->lmvecs[i][3] = s->texinfo->vecs[i][3] / lmscale + 0.5/*sigh*/ - bmins[i];
if (mod_lightscale_broken.value)
s->lmvecscale[i] = 16; //luxels->qu... except buggy so dlights have the wrong spread on large surfaces (blame shib7)
else
s->lmvecscale[i] = 1.0f/VectorLength(s->lmvecs[i]); //luxels->qu
s->extents[i] = bmaxs[i] - bmins[i];

if ( !(tex->flags & TEX_SPECIAL) && s->extents[i] > maxextent) //johnfitz -- was 512 in glquake, 256 in winquake
if ( !(tex->flags & TEX_SPECIAL) && s->extents[i] >= (i?LMBLOCK_HEIGHT:LMBLOCK_WIDTH)) //johnfitz -- was 512 in glquake, 256 in winquake
{
s->extents[i] = 1;
// Sys_Error ("Bad surface extents");
Expand Down Expand Up @@ -1560,6 +1568,7 @@ static void Mod_LoadFaces (lump_t *l, qboolean bsp2)
int lumpsize;
char scalebuf[16];
int facestyles;
struct decoupled_lm_info_s *decoupledlm = NULL;

if (bsp2)
{
Expand All @@ -1586,12 +1595,38 @@ static void Mod_LoadFaces (lump_t *l, qboolean bsp2)

if (!mod_ignorelmscale.value)
{
lmshift = Q1BSPX_FindLump("LMSHIFT", &lumpsize);
if (lumpsize != sizeof(*lmshift)*count)
decoupledlm = Q1BSPX_FindLump("DECOUPLED_LM", &lumpsize); //RGB packed data
if (decoupledlm && lumpsize == count*sizeof(*decoupledlm))
{ //basically stomps over the lmshift+lmoffset stuff above. lmstyle/lmstyle16+lit/hdr+lux info is still needed
lmshift = NULL;
lmoffset = Q1BSPX_FindLump("LMOFFSET", &lumpsize);
if (lumpsize != sizeof(*lmoffset)*count)
lmoffset = NULL;
}
else
{
decoupledlm = NULL;

lmshift = Q1BSPX_FindLump("LMSHIFT", &lumpsize);
if (lumpsize != sizeof(*lmshift)*count)
lmshift = NULL;
lmoffset = Q1BSPX_FindLump("LMOFFSET", &lumpsize);
if (lumpsize != sizeof(*lmoffset)*count)
lmoffset = NULL;

if (Mod_ParseWorldspawnKey(loadmodel, "lightmap_scale", scalebuf, sizeof(scalebuf)))
{
char *e;
i = strtol(scalebuf, &e, 10);
if (i < 0 || *e)
Con_Warning("Incorrect value for lightmap_scale field - %s - should be texels-per-luxel (and power-of-two), use 16 (or omit) to match vanilla quake.\n", scalebuf);
else if (i == 0)
; //silently use default when its explicitly set to 0 or empty. a bogus value but oh well.
else
{
for(defaultshift = 0; i > 1; defaultshift++)
i >>= 1;
}
}
}
lmstyle16 = Q1BSPX_FindLump("LMSTYLE16", &lumpsize);
stylesperface = lumpsize/(sizeof(*lmstyle16)*count);
if (lumpsize != sizeof(*lmstyle16)*stylesperface*count)
Expand All @@ -1603,21 +1638,6 @@ static void Mod_LoadFaces (lump_t *l, qboolean bsp2)
if (lumpsize != sizeof(*lmstyle8)*stylesperface*count)
lmstyle8 = NULL;
}

if (Mod_ParseWorldspawnKey(loadmodel, "lightmap_scale", scalebuf, sizeof(scalebuf)))
{
char *e;
i = strtol(scalebuf, &e, 10);
if (i < 0 || *e)
Con_Warning("Incorrect value for lightmap_scale field - %s - should be texels-per-luxel (and power-of-two), use 16 to match vanilla quake.\n", scalebuf);
else if (i == 0)
; //silently use default when its explicitly set to 0 or empty. a bogus value but oh well.
else
{
for(defaultshift = 0; i > 1; defaultshift++)
i >>= 1;
}
}
}

loadmodel->surfaces = out;
Expand Down Expand Up @@ -1675,9 +1695,33 @@ static void Mod_LoadFaces (lump_t *l, qboolean bsp2)

out->plane = loadmodel->planes + planenum;
out->texinfo = loadmodel->texinfo + texinfon;
out->lmshift = shift;

CalcSurfaceExtents (out);
if (decoupledlm)
{
lofs = LittleLong(decoupledlm->lmoffset);
out->extents[0] = (unsigned short)LittleShort(decoupledlm->lmsize[0]) - 1;
out->extents[1] = (unsigned short)LittleShort(decoupledlm->lmsize[1]) - 1;
out->lmvecs[0][0] = LittleFloat(decoupledlm->lmvecs[0][0]);
out->lmvecs[0][1] = LittleFloat(decoupledlm->lmvecs[0][1]);
out->lmvecs[0][2] = LittleFloat(decoupledlm->lmvecs[0][2]);
out->lmvecs[0][3] = LittleFloat(decoupledlm->lmvecs[0][3]) + 0.5f; //sigh
out->lmvecs[1][0] = LittleFloat(decoupledlm->lmvecs[1][0]);
out->lmvecs[1][1] = LittleFloat(decoupledlm->lmvecs[1][1]);
out->lmvecs[1][2] = LittleFloat(decoupledlm->lmvecs[1][2]);
out->lmvecs[1][3] = LittleFloat(decoupledlm->lmvecs[1][3]) + 0.5f; //sigh
out->lmvecscale[0] = 1.0f/VectorLength(out->lmvecs[0]); //luxels->qu
out->lmvecscale[1] = 1.0f/VectorLength(out->lmvecs[1]);
decoupledlm++;

//make sure we don't segfault even if the texture coords get crappified.
if (out->extents[0] >= LMBLOCK_WIDTH || out->extents[1] >= LMBLOCK_HEIGHT)
{
Con_Warning("%s: Bad surface extents (%i*%i, max %i*%u).\n", scalebuf, out->extents[0], out->extents[1], LMBLOCK_WIDTH, LMBLOCK_HEIGHT);
out->extents[0] = out->extents[1] = 1;
}
}
else
CalcSurfaceExtents (out, shift);

Mod_CalcSurfaceBounds (out); //johnfitz -- for per-surface frustum culling

Expand All @@ -1689,7 +1733,7 @@ static void Mod_LoadFaces (lump_t *l, qboolean bsp2)
; //count the styles so we can bound-check properly.
if (lofs == -1)
out->samples = NULL;
else if (lofs+facestyles*((out->extents[0]>>out->lmshift)+1)*((out->extents[1]>>out->lmshift)+1) > loadmodel->lightdatasamples)
else if (lofs+facestyles*((out->extents[0])+1)*((out->extents[1])+1) > loadmodel->lightdatasamples)
out->samples = NULL; //corrupt...
else if (loadmodel->flags & MOD_HDRLIGHTING)
out->samples = loadmodel->lightdata + (lofs * 4); //spike -- hdr lighting data is 4-aligned
Expand Down
5 changes: 3 additions & 2 deletions Quake/gl_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ typedef struct msurface_s
int firstedge; // look up in model->surfedges[], negative numbers
int numedges; // are backwards edges

short texturemins[2];
short extents[2];

vec4_t lmvecs[2];
float lmvecscale[2]; //so dlights spread the correct distance despite texture scaling.

int light_s, light_t; // gl lightmap coordinates
unsigned char lmshift;

glpoly_t *polys; // multiple if warped
struct msurface_s *texturechain;
Expand Down
45 changes: 23 additions & 22 deletions Quake/gl_rlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ void R_MarkLights (dlight_t *light, vec3_t lightorg, int num, mnode_t *node)
for (j=0 ; j<3 ; j++)
impact[j] = lightorg[j] - surf->plane->normal[j]*dist;
// clamp center of light to corner and check brightness
l = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
s = l+0.5;if (s < 0) s = 0;else if (s > surf->extents[0]) s = surf->extents[0];
l = DotProduct (impact, surf->lmvecs[0]) + surf->lmvecs[0][3];
s = l;if (s < 0) s = 0;else if (s > surf->extents[0]) s = surf->extents[0];
s = l - s;
l = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
t = l+0.5;if (t < 0) t = 0;else if (t > surf->extents[1]) t = surf->extents[1];
l = DotProduct (impact, surf->lmvecs[1]) + surf->lmvecs[1][3];
t = l;if (t < 0) t = 0;else if (t > surf->extents[1]) t = surf->extents[1];
t = l - t;
// compare to minimum light
if ((s*s+t*t+dist*dist) < maxdist)
Expand Down Expand Up @@ -359,24 +359,25 @@ int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t rayorg, vec3_t star
{
float sfront, sback, dist;
vec3_t raydelta;
double dsfrac, dtfrac;

if (surf->flags & SURF_DRAWTILED)
continue; // no lightmaps

// ericw -- added double casts to force 64-bit precision.
// Without them the zombie at the start of jam3_ericw.bsp was
// incorrectly being lit up in SSE builds.
ds = (int) ((double) DoublePrecisionDotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
dt = (int) ((double) DoublePrecisionDotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);

if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
dsfrac = DoublePrecisionDotProduct (mid, surf->lmvecs[0]) + surf->lmvecs[0][3];
dtfrac = DoublePrecisionDotProduct (mid, surf->lmvecs[1]) + surf->lmvecs[1][3];
if (dsfrac < 0 || dtfrac < 0)
continue;

ds -= surf->texturemins[0];
dt -= surf->texturemins[1];

if (ds > surf->extents[0] || dt > surf->extents[1])
if (dsfrac > surf->extents[0] || dtfrac > surf->extents[1])
continue;
ds = dsfrac;
dt = dtfrac;
dsfrac -= ds;
dtfrac -= dt;

if (surf->plane->type < 3)
{
Expand Down Expand Up @@ -406,7 +407,7 @@ int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t rayorg, vec3_t star
if (dist < *maxdist)
{
// LordHavoc: enhanced to interpolate lighting
int maps, line3, dsfrac = ds & 15, dtfrac = dt & 15, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
int maps, line3, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
float scale, e;

if (cl.worldmodel->flags & MOD_HDRLIGHTING)
Expand All @@ -418,36 +419,36 @@ int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t rayorg, vec3_t star
1.0/(1<<8), 1.0/(1<<7), 1.0/(1<<6), 1.0/(1<<5), 1.0/(1<<4), 1.0/(1<<3), 1.0/(1<<2), 1.0/(1<<1),
1.0, 1.0*(1<<1), 1.0*(1<<2), 1.0*(1<<3), 1.0*(1<<4), 1.0*(1<<5), 1.0*(1<<6), 1.0*(1<<7),
};
uint32_t *lightmap = (uint32_t*)surf->samples + ((dt>>surf->lmshift) * ((surf->extents[0]>>surf->lmshift)+1) + (ds>>surf->lmshift));
line3 = ((surf->extents[0]>>surf->lmshift)+1);
uint32_t *lightmap = (uint32_t*)surf->samples + (dt * (surf->extents[0]+1) + ds);
line3 = (surf->extents[0]+1);
for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != INVALID_LIGHTSTYLE;maps++)
{
scale = (1<<7) * (float) d_lightstylevalue[surf->styles[maps]] * 1.0f / 256.0f;
e = rgb9e5tab[lightmap[ 0]>>27] * scale;r00 += ((lightmap[ 0]>> 0)&0x1ff) * e;g00 += ((lightmap[ 0]>> 9)&0x1ff) * e;b00 += ((lightmap[ 0]>> 9)&0x1ff) * e;
e = rgb9e5tab[lightmap[ 1]>>27] * scale;r01 += ((lightmap[ 1]>> 0)&0x1ff) * e;g01 += ((lightmap[ 1]>> 9)&0x1ff) * e;b01 += ((lightmap[ 1]>> 9)&0x1ff) * e;
e = rgb9e5tab[lightmap[line3+0]>>27] * scale;r10 += ((lightmap[line3+0]>> 0)&0x1ff) * e;g10 += ((lightmap[line3+0]>> 9)&0x1ff) * e;b10 += ((lightmap[line3+0]>> 9)&0x1ff) * e;
e = rgb9e5tab[lightmap[line3+1]>>27] * scale;r11 += ((lightmap[line3+1]>> 0)&0x1ff) * e;g11 += ((lightmap[line3+1]>> 9)&0x1ff) * e;b11 += ((lightmap[line3+1]>> 9)&0x1ff) * e;
lightmap += ((surf->extents[0]>>surf->lmshift)+1) * ((surf->extents[1]>>surf->lmshift)+1);
lightmap += (surf->extents[0]+1) * (surf->extents[1]+1);
}
}
else
{
byte *lightmap = (byte*)surf->samples + ((dt>>surf->lmshift) * ((surf->extents[0]>>surf->lmshift)+1) + (ds>>surf->lmshift))*3; // LordHavoc: *3 for color
line3 = ((surf->extents[0]>>surf->lmshift)+1)*3;
byte *lightmap = (byte*)surf->samples + (dt * (surf->extents[0]+1) + ds)*3; // LordHavoc: *3 for color
line3 = (surf->extents[0]+1)*3;
for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != INVALID_LIGHTSTYLE;maps++)
{
scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0f / 256.0f;
r00 += (float) lightmap[ 0] * scale;g00 += (float) lightmap[ 1] * scale;b00 += (float) lightmap[2] * scale;
r01 += (float) lightmap[ 3] * scale;g01 += (float) lightmap[ 4] * scale;b01 += (float) lightmap[5] * scale;
r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
lightmap += ((surf->extents[0]>>surf->lmshift)+1) * ((surf->extents[1]>>surf->lmshift)+1)*3; // LordHavoc: *3 for colored lighting
lightmap += (surf->extents[0]+1) * (surf->extents[1]+1)*3; // LordHavoc: *3 for colored lighting
}
}

color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
color[0] += (float) ((int) ((((((r11-r10) * dsfrac) + r10)-(((r01-r00) * dsfrac) + r00)) * dtfrac) + (((r01-r00) * dsfrac) + r00)));
color[1] += (float) ((int) ((((((g11-g10) * dsfrac) + g10)-(((g01-g00) * dsfrac) + g00)) * dtfrac) + (((g01-g00) * dsfrac) + g00)));
color[2] += (float) ((int) ((((((b11-b10) * dsfrac) + b10)-(((b01-b00) * dsfrac) + b00)) * dtfrac) + (((b01-b00) * dsfrac) + b00)));
}
return true; // success
}
Expand Down
4 changes: 2 additions & 2 deletions Quake/pr_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -2547,8 +2547,8 @@ static void PF_getsurfacepointattribute(void)
G_FLOAT(OFS_RETURN+2) = 0;
break;
case SPA_LIGHTMAP0_TEXCOORDS: //lmst coord, not actually very useful
G_FLOAT(OFS_RETURN+0) = (DotProduct(v->position, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3] - fa->texturemins[0] + (fa->light_s+.5)*(1<<fa->lmshift)) / (LMBLOCK_WIDTH*(1<<fa->lmshift));
G_FLOAT(OFS_RETURN+1) = (DotProduct(v->position, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3] - fa->texturemins[1] + (fa->light_t+.5)*(1<<fa->lmshift)) / (LMBLOCK_HEIGHT*(1<<fa->lmshift));
G_FLOAT(OFS_RETURN+0) = (DotProduct(v->position, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3] + fa->light_s) / LMBLOCK_WIDTH;
G_FLOAT(OFS_RETURN+1) = (DotProduct(v->position, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3] + fa->light_t) / LMBLOCK_HEIGHT;
G_FLOAT(OFS_RETURN+2) = 0;
break;
case SPA_LIGHTMAP0_COLOR: //colour
Expand Down
Loading

0 comments on commit 528824e

Please sign in to comment.