Merge pull request #788 from 0lvin/softlightmap

soft: add r_lightmap implementation
This commit is contained in:
Yamagi 2022-02-12 16:58:33 +01:00 committed by GitHub
commit b68910905b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 150 additions and 88 deletions

View file

@ -664,7 +664,7 @@ cheatvar_t cheatvars[] = {
{"paused", "0"},
{"fixedtime", "0"},
{"sw_draworder", "0"},
{"gl_lightmap", "0"},
{"r_lightmap", "0"},
{"gl_saturatelighting", "0"},
{"cl_kickangles", "1"},
{"g_footsteps", "1"},

View file

@ -303,17 +303,20 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end)
for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255;
maps++)
{
const float *rgb;
int j;
rgb = r_newrefdef.lightstyles[surf->styles[maps]].rgb;
/* Apply light level to models */
for (j = 0; j < 3; j++)
{
scale[j] = r_modulate->value *
r_newrefdef.lightstyles[surf->styles[maps]].rgb[j];
float scale;
scale = rgb[j] * r_modulate->value;
pointcolor[j] += lightmap[j] * scale * (1.0 / 255);
}
pointcolor[0] += lightmap[0] * scale[0] * (1.0 / 255);
pointcolor[1] += lightmap[1] * scale[1] * (1.0 / 255);
pointcolor[2] += lightmap[2] * scale[2] * (1.0 / 255);
lightmap += 3 * ((surf->extents[0] >> 4) + 1) *
((surf->extents[1] >> 4) + 1);
}

View file

@ -1223,7 +1223,7 @@ R_Register(void)
r_modulate = ri.Cvar_Get("r_modulate", "1", CVAR_ARCHIVE);
r_mode = ri.Cvar_Get("r_mode", "4", CVAR_ARCHIVE);
gl_lightmap = ri.Cvar_Get("gl_lightmap", "0", 0);
gl_lightmap = ri.Cvar_Get("r_lightmap", "0", 0);
gl_shadows = ri.Cvar_Get("r_shadows", "0", CVAR_ARCHIVE);
gl1_stencilshadow = ri.Cvar_Get("gl1_stencilshadow", "0", CVAR_ARCHIVE);
gl1_dynamic = ri.Cvar_Get("gl1_dynamic", "1", 0);

View file

@ -231,15 +231,20 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end)
for (maps = 0; maps < MAX_LIGHTMAPS_PER_SURFACE && surf->styles[maps] != 255;
maps++)
{
for (int j = 0; j < 3; j++)
const float *rgb;
int j;
rgb = gl3_newrefdef.lightstyles[surf->styles[maps]].rgb;
/* Apply light level to models */
for (j = 0; j < 3; j++)
{
scale[j] = r_modulate->value *
gl3_newrefdef.lightstyles[surf->styles[maps]].rgb[j];
float scale;
scale = rgb[j] * r_modulate->value;
pointcolor[j] += lightmap[j] * scale * (1.0 / 255);
}
pointcolor[0] += lightmap[0] * scale[0] * (1.0 / 255);
pointcolor[1] += lightmap[1] * scale[1] * (1.0 / 255);
pointcolor[2] += lightmap[2] * scale[2] * (1.0 / 255);
lightmap += 3 * ((surf->extents[0] >> 4) + 1) *
((surf->extents[1] >> 4) + 1);
}

View file

@ -237,7 +237,7 @@ GL3_Register(void)
r_lightlevel = ri.Cvar_Get("r_lightlevel", "0", 0);
gl3_overbrightbits = ri.Cvar_Get("gl3_overbrightbits", "1.3", CVAR_ARCHIVE);
gl_lightmap = ri.Cvar_Get("gl_lightmap", "0", 0);
gl_lightmap = ri.Cvar_Get("r_lightmap", "0", 0);
gl_shadows = ri.Cvar_Get("r_shadows", "0", CVAR_ARCHIVE);
r_modulate = ri.Cvar_Get("r_modulate", "1", CVAR_ARCHIVE);
@ -272,7 +272,7 @@ GL3_Register(void)
//gl_modulate = ri.Cvar_Get("gl_modulate", "1", CVAR_ARCHIVE);
//r_mode = ri.Cvar_Get("r_mode", "4", CVAR_ARCHIVE);
//gl_lightmap = ri.Cvar_Get("gl_lightmap", "0", 0);
//gl_lightmap = ri.Cvar_Get("r_lightmap", "0", 0);
//gl_shadows = ri.Cvar_Get("r_shadows", "0", CVAR_ARCHIVE);
//gl_nobind = ri.Cvar_Get("gl_nobind", "0", 0);
gl_showtris = ri.Cvar_Get("gl_showtris", "0", 0);

View file

@ -211,7 +211,7 @@ typedef struct finalvert_s {
typedef struct
{
void *pskin;
pixel_t *pskin;
int skinwidth;
int skinheight;
} affinetridesc_t;
@ -431,6 +431,7 @@ extern cvar_t *r_fullbright;
extern cvar_t *r_lefthand;
extern cvar_t *r_gunfov;
extern cvar_t *r_farsee;
extern cvar_t *r_lightmap;
extern cvar_t *r_drawworld;
extern cvar_t *r_lerpmodels;
extern cvar_t *r_lightlevel;

View file

@ -578,7 +578,7 @@ R_AliasSetupLighting(entity_t *currententity)
}
}
j = (light[0] + light[1] + light[2])*0.3333*255;
j = (light[0] + light[1] + light[2]) * 0.3333 * 255;
lighting.ambientlight = j;
lighting.shadelight = j;

View file

@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "header/local.h"
#define MAX_RIMAGES 1024
static image_t *r_whitetexture_mip = NULL;
static image_t r_images[MAX_RIMAGES];
static int numr_images;
static int image_max = 0;
@ -210,12 +211,15 @@ R_LoadPic
================
*/
static image_t *
R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int realheight, imagetype_t type)
R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int realheight,
size_t data_size, imagetype_t type)
{
image_t *image;
size_t i, size, full_size;
if (!pic)
size = width * height;
if (!pic || data_size <= 0 || width <= 0 || height <= 0 || size <= 0)
return NULL;
image = R_FindFreeImage();
@ -230,7 +234,6 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
image->asset_height = realheight;
image->type = type;
size = width * height;
full_size = R_GetImageMipsSize(size);
image->pixels[0] = malloc(full_size);
if (!image->pixels[0])
@ -239,21 +242,35 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
// code never returns after ERR_FATAL
return NULL;
}
image->transparent = false;
for (i=0 ; i<size ; i++)
if (type != it_wall)
{
if (pic[i] == 255)
for (i=0 ; i<size ; i++)
{
image->transparent = true;
break;
if (pic[i] == 255)
{
image->transparent = true;
break;
}
}
}
memcpy(image->pixels[0], pic, size);
if (data_size > full_size)
{
data_size = full_size;
}
memcpy(image->pixels[0], pic, data_size);
// restore mips
R_RestoreImagePointers(image, 0);
// restore everything from first image
R_RestoreMips(image, 0);
if (full_size > data_size)
{
// looks short, restore everything from first image
R_RestoreMips(image, 0);
}
return image;
}
@ -268,7 +285,7 @@ R_LoadWal (char *name, imagetype_t type)
miptex_t *mt;
int ofs;
image_t *image;
size_t size, file_size;
size_t file_size, width, height;
file_size = ri.FS_LoadFile (name, (void **)&mt);
if (!mt)
@ -284,72 +301,57 @@ R_LoadWal (char *name, imagetype_t type)
return r_notexture_mip;
}
image = R_FindFreeImage ();
strcpy (image->name, name);
image->width = LittleLong (mt->width);
image->height = LittleLong (mt->height);
image->asset_width = image->width;
image->asset_height = image->height;
image->type = type;
image->registration_sequence = registration_sequence;
width = LittleLong (mt->width);
height = LittleLong (mt->height);
ofs = LittleLong(mt->offsets[0]);
size = R_GetImageMipsSize(image->width * image->height);
if ((ofs <= 0) || (image->width <= 0) || (image->height <= 0) ||
((file_size - ofs) / image->width < image->height))
if ((ofs <= 0) || (width <= 0) || (height <= 0) ||
((file_size - ofs) / width < height))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small body\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return r_notexture_mip;
}
image->pixels[0] = malloc (size);
R_RestoreImagePointers(image, 0);
if (size > (file_size - ofs))
{
memcpy(image->pixels[0], (byte *)mt + ofs, file_size - ofs);
// looks short, restore everything from first image
R_RestoreMips(image, 0);
}
else
{
memcpy(image->pixels[0], (byte *)mt + ofs, size);
}
image = R_LoadPic(name, (byte *)mt + ofs,
width, width,
height, height,
(file_size - ofs), type);
ri.FS_FreeFile((void *)mt);
return image;
}
static unsigned char *d_16to8table; // 16 to 8 bit conversion table
static unsigned char *d_16to8table = NULL; // 16 to 8 bit conversion table
static void
R_Convert32To8bit(const unsigned char* pic_in, unsigned char* pic_out, size_t size)
R_Convert32To8bit(const unsigned char* pic_in, pixel_t* pic_out, size_t size)
{
size_t i;
if (!d_16to8table)
return;
for(i=0; i<size; i++)
for(i=0; i < size; i++)
{
unsigned int r, g, b, c;
r = ( pic_in[i * 4 + 0] >> 3 ) & 31;
g = ( pic_in[i * 4 + 1] >> 2 ) & 63;
b = ( pic_in[i * 4 + 2] >> 3 ) & 31;
r = ( pic_in[0] >> 3 ) & 31;
g = ( pic_in[1] >> 2 ) & 63;
b = ( pic_in[2] >> 3 ) & 31;
c = r | ( g << 5 ) | ( b << 11 );
pic_out[i] = d_16to8table[c & 0xFFFF];
pic_in += 4;
}
}
static void
R_FixPalette(unsigned char* pixels, size_t size, rgb_t* pallette)
R_FixPalette(pixel_t* pixels, size_t size, const rgb_t* pallette)
{
unsigned char* convert = malloc(256);
pixel_t* convert = malloc(256);
size_t i;
@ -554,14 +556,20 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
pic32, uploadwidth, uploadheight))
{
R_Convert32To8bit(pic32, pic8, uploadwidth * uploadheight);
image = R_LoadPic(name, pic8, uploadwidth, realwidth, uploadheight, realheight, type);
image = R_LoadPic(name, pic8,
uploadwidth, realwidth,
uploadheight, realheight,
uploadwidth * uploadheight, type);
}
free(pic32);
}
else
{
R_Convert32To8bit(pic, pic8, width * height);
image = R_LoadPic(name, pic8, width, width, height, height, type);
image = R_LoadPic(name, pic8,
width, width,
height, height,
width * height, type);
}
free(pic8);
}
@ -614,12 +622,18 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
scale2x(pic, scaled, width, height);
width *= 2;
height *= 2;
image = R_LoadPic(name, scaled, width, realwidth, height, realheight, type);
image = R_LoadPic(name, scaled,
width, realwidth,
height, realheight,
width * height, type);
free(scaled);
}
else
{
image = R_LoadPic(name, pic, width, width, height, height, type);
image = R_LoadPic(name, pic,
width, width,
height, height,
width * height, type);
}
if (palette)
@ -662,6 +676,12 @@ R_FindImage(char *name, imagetype_t type)
return NULL;
}
/* just return white image if show lighmap only */
if (type == it_wall && r_lightmap->value)
{
return r_whitetexture_mip;
}
ext = COM_FileExtension(name);
if(!ext[0])
{
@ -762,17 +782,12 @@ R_ImageHasFreeSpace(void)
static struct texture_buffer {
image_t image;
byte buffer[4096];
} r_notexture_buffer;
} r_notexture_buffer, r_whitetexture_buffer;
/*
==================
R_InitTextures
==================
*/
static void
R_InitTextures (void)
R_InitNoTexture(void)
{
int x,y, m;
int m;
// create a simple checkerboard texture for the default
r_notexture_mip = &r_notexture_buffer.image;
@ -785,6 +800,7 @@ R_InitTextures (void)
for (m=0 ; m<NUM_MIPS ; m++)
{
int x, y;
byte *dest;
dest = r_notexture_mip->pixels[m];
@ -793,13 +809,42 @@ R_InitTextures (void)
{
if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
*dest++ = 0;
*dest++ = d_16to8table[0x0000];
else
*dest++ = 0xff;
*dest++ = d_16to8table[0xFFFF];
}
}
}
static void
R_InitWhiteTexture(void)
{
// create a simple white texture for the default
r_whitetexture_mip = &r_whitetexture_buffer.image;
r_whitetexture_mip->width = r_whitetexture_mip->height = 16;
r_whitetexture_mip->asset_width = r_whitetexture_mip->asset_height = 16;
r_whitetexture_mip->pixels[0] = r_whitetexture_buffer.buffer;
R_RestoreImagePointers(r_whitetexture_mip, 0);
memset(r_whitetexture_buffer.buffer, d_16to8table[0xFFFF],
sizeof(r_whitetexture_buffer.buffer));
}
/*
==================
R_InitTextures
==================
*/
static void
R_InitTextures (void)
{
R_InitNoTexture();
/* empty white texture for r_lightmap = 1*/
R_InitWhiteTexture();
}
/*
===============
R_InitImages

View file

@ -134,6 +134,7 @@ float se_time1, se_time2, de_time1, de_time2;
cvar_t *r_lefthand;
cvar_t *r_gunfov;
cvar_t *r_farsee;
cvar_t *r_lightmap;
static cvar_t *sw_aliasstats;
cvar_t *sw_clearcolor;
cvar_t *sw_drawflat;
@ -394,6 +395,7 @@ R_RegisterVariables (void)
r_lefthand = ri.Cvar_Get( "hand", "0", CVAR_USERINFO | CVAR_ARCHIVE );
r_gunfov = ri.Cvar_Get( "r_gunfov", "80", CVAR_ARCHIVE );
r_farsee = ri.Cvar_Get("r_farsee", "0", CVAR_LATCH | CVAR_ARCHIVE);
r_lightmap = ri.Cvar_Get("r_lightmap", "0", 0);
r_speeds = ri.Cvar_Get ("r_speeds", "0", 0);
r_fullbright = ri.Cvar_Get ("r_fullbright", "0", 0);
r_drawentities = ri.Cvar_Get ("r_drawentities", "1", 0);
@ -1341,7 +1343,7 @@ RE_RenderFrame (refdef_t *fd)
// For each dlight_t* passed via r_newrefdef.dlights, mark polygons affected by a light.
R_PushDlights (r_worldmodel);
// TODO: rearange code same as in GL*_DrawWorld?
// TODO: rearrange code same as in GL*_DrawWorld?
/* auto cycle the world frame for texture animation */
memset(&ent, 0, sizeof(ent));
ent.frame = (int)(r_newrefdef.time * 2);

View file

@ -258,16 +258,17 @@ static void
Mod_LoadLighting (model_t *loadmodel, byte *mod_base, lump_t *l)
{
int i, size;
byte *in;
pixel_t *in;
if (!l->filelen)
{
loadmodel->lightdata = NULL;
return;
}
size = l->filelen/3;
loadmodel->lightdata = Hunk_Alloc(size);
in = (void *)(mod_base + l->fileofs);
in = mod_base + l->fileofs;
for (i=0 ; i<size ; i++, in+=3)
{
if (in[0] > in[1] && in[0] > in[2])
@ -1008,7 +1009,8 @@ Mod_LoadBrushModel(model_t *mod, void *buffer, int modfilelen)
if(surfEdgeCount < MAX_MAP_SURFEDGES) // else it errors out later anyway
hunkSize += calcLumpHunkSize(&header->lumps[LUMP_SURFEDGES], sizeof(int), sizeof(int), 24);
// lighting is a special case, because we keep only 1 byte out of 3 (=> no colored lighting in soft renderer)
// lighting is a special case, because we keep only 1 byte out of 3
// (=> no colored lighting in soft renderer by default)
{
int size = header->lumps[LUMP_LIGHTING].filelen/3;
size = (size + 31) & ~31;

View file

@ -78,7 +78,7 @@ static zvalue_t d_ziextrastep, d_zibasestep;
static byte *skintable[MAX_LBM_HEIGHT];
int skinwidth;
static byte *skinstart;
static pixel_t *skinstart;
void (*d_pdrawspans)(const entity_t *currententity, spanpackage_t *pspanpackage);
@ -429,8 +429,8 @@ R_PolysetDrawSpans8
void
R_PolysetDrawSpans8_33(const entity_t *currententity, spanpackage_t *pspanpackage)
{
byte *lpdest;
byte *lptex;
pixel_t *lpdest;
pixel_t *lptex;
int lsfrac, ltfrac;
int llight;
zvalue_t lzi;
@ -470,7 +470,7 @@ R_PolysetDrawSpans8_33(const entity_t *currententity, spanpackage_t *pspanpackag
{
int temp = vid_colormap[*lptex + ( llight & 0xFF00 )];
*lpdest = vid_alphamap[temp+ *lpdest*256];
*lpdest = vid_alphamap[temp + *lpdest*256];
}
lpdest++;
lzi += r_zistepx;
@ -719,11 +719,14 @@ R_PolysetDrawSpans8_Opaque (const entity_t *currententity, spanpackage_t *pspanp
{
if ((lzi >> SHIFT16XYZ) >= *lpz)
{
if(r_newrefdef.rdflags & RDF_IRGOGGLES && currententity->flags & RF_IR_VISIBLE)
*lpdest = ((byte *)vid_colormap)[irtable[*lptex]];
else
*lpdest = ((byte *)vid_colormap)[*lptex + (llight & 0xFF00)];
int color_value;
if(r_newrefdef.rdflags & RDF_IRGOGGLES && currententity->flags & RF_IR_VISIBLE)
color_value = irtable[*lptex];
else
color_value = *lptex + (llight & 0xFF00);
*lpdest = vid_colormap[color_value];
*lpz = lzi >> SHIFT16XYZ;
zdamaged = true;
}
@ -798,7 +801,7 @@ R_RasterizeAliasPolySmooth(const entity_t *currententity)
v = plefttop[1];
d_aspancount = plefttop[0] - prighttop[0];
d_ptex = (byte *)r_affinetridesc.pskin + (plefttop[2] >> SHIFT16XYZ) +
d_ptex = r_affinetridesc.pskin + (plefttop[2] >> SHIFT16XYZ) +
(plefttop[3] >> SHIFT16XYZ) * r_affinetridesc.skinwidth;
{
d_sfrac = plefttop[2] & 0xFFFF;
@ -866,7 +869,7 @@ R_RasterizeAliasPolySmooth(const entity_t *currententity)
u = plefttop[0];
v = plefttop[1];
d_aspancount = plefttop[0] - prighttop[0];
d_ptex = (byte *)r_affinetridesc.pskin + (plefttop[2] >> SHIFT16XYZ) +
d_ptex = r_affinetridesc.pskin + (plefttop[2] >> SHIFT16XYZ) +
(plefttop[3] >> SHIFT16XYZ) * r_affinetridesc.skinwidth;
d_sfrac = 0;
d_tfrac = 0;

View file

@ -88,6 +88,7 @@ replacement_t replacements[] = {
{"gl_retexturing", "r_retexturing"},
{"gl_shadows", "r_shadows"},
{"gl_anisotropic", "r_anisotropic"},
{"gl_lightmap", "r_lighmap"},
{"intensity", "gl1_intensity"}
};