This tutorial allows different palette lmp files to be loaded in the console, "loadpalette mypal" will load mypal.lmp. It's simple but will need adjustment for your particular engine.
What I'm not sure of... should there be a way to load a colormap lmp?
EDIT: Removed ifdef around Q_snprintfz per discussion below.
In r_main.c:
Code: Select all
extern byte *host_basepal;
/*
===============
R_LoadPalette
===============
*/
void R_LoadPalette (char *name) //qbism - load a palette lmp
{
loadedfile_t *fileinfo;
char pathname[MAX_QPATH];
Q_snprintfz (pathname, sizeof(pathname), "gfx/%s.lmp", name);
fileinfo = COM_LoadHunkFile (pathname);
if (!fileinfo)
{
Con_Printf("Palette not found.\n");
return;
}
Q_memcpy (host_basepal, fileinfo->data, 768);
VID_SetPalette (host_basepal);
}
/*
===============
R_LoadPalette_f
===============
*/
void R_LoadPalette_f (void) //qbism - load an alternate palette
{
if (Cmd_Argc() != 2)
{
Con_Printf ("loadpalette <name> : load a color palette\n");
return;
}
R_LoadPalette(Cmd_Argv(1));
}
EDIT: Add regenerate colormap. This will greatly improve lighting quality for your new palette. Add the following code above loadpalette, it is indeed adapted from qlumpy.
Code: Select all
/*
=============================================================================
COLORMAP GRABBING
=============================================================================
*/
/*
===============
BestColor - qbism- from qlumpy
===============
*/
byte BestColor (int r, int g, int b, int start, int stop)
{
int i;
int dr, dg, db;
int bestdistortion, distortion;
int bestcolor;
byte *pal;
//
// let any color go to 0 as a last resort
//
bestdistortion = ( (int)r*r + (int)g*g + (int)b*b )*2;
bestcolor = 0;
pal = host_basepal + start*3;
for (i=start ; i<= stop ; i++)
{
dr = r - (int)pal[0];
dg = g - (int)pal[1];
db = b - (int)pal[2];
pal += 3;
distortion = dr*dr + dg*dg + db*db;
if (distortion < bestdistortion)
{
if (!distortion)
return i; // perfect match
bestdistortion = distortion;
bestcolor = i;
}
}
return bestcolor;
}
/*
==============
GrabColormap - qbism- from qlumpy
filename COLORMAP levels fullbrights
the first map is an identiy 0-255
the final map is all black except for the fullbrights
the remaining maps are evenly spread
fullbright colors start at the top of the palette.
==============
*/
void GrabColormap (void)
{
int levels, brights;
int l, c;
float frac, red, green, blue;
byte *colmap;
colmap = host_colormap;
levels = 32;
brights = 256;
// identity lump
for (l=0 ; l<256 ; l++)
*colmap++ = l;
// shaded levels
for (l=1;l<levels;l++)
{
frac = 1.0 - (float)l/(levels-1);
for (c=0 ; c<256-brights ; c++)
{
red = host_basepal[c*3];
green = host_basepal[c*3+1];
blue = host_basepal[c*3+2];
red = (int)(red*frac+0.5);
green = (int)(green*frac+0.5);
blue = (int)(blue*frac+0.5);
//
// note: 254 instead of 255 because 255 is the transparent color, and we
// don't want anything remapping to that
//
*colmap++ = BestColor(red,green,blue, 0, 254);
}
for ( ; c<256 ; c++)
*colmap++ = c;
}
*colmap++ = brights;
}
In R_LoadPalette, Add this above Vid_SetPalette
Code: Select all
GrabColormap();
If you want to add a palette field to the worldspawn, add a check for it in CL_ParseEntityLump.
A further step might be remipping textures using the new palette. So far I haven't noticed anything horrible enough to warrant this intensive step.