glquake/source/gl_fog.c

517 lines
9.4 KiB
C
Raw Normal View History

2022-07-05 21:44:09 +00:00
/*
2023-07-22 18:26:45 +00:00
Fogging system based on FitzQuake's implementation
2023-10-16 17:10:27 +00:00
Now with Quakespasm bits thrown into it!
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
*/
#include "quakedef.h"
//==============================================================================
//
// GLOBAL FOG
//
//==============================================================================
2023-10-16 17:10:27 +00:00
#define DEFAULT_DENSITY 1.0
#define DEFAULT_GRAY 0.3
2022-07-05 21:44:09 +00:00
2023-10-16 17:10:27 +00:00
float density = 1.0;
float fog_density_gl;
float fog_start;
float fog_end;
float fog_red;
float fog_green;
float fog_blue;
float old_density;
2023-07-22 18:26:45 +00:00
float old_start;
float old_end;
float old_red;
float old_green;
float old_blue;
2022-07-05 21:44:09 +00:00
2023-07-22 18:26:45 +00:00
float fade_time; //duration of fade
float fade_done; //time when fade will be done
2022-07-05 21:44:09 +00:00
2022-06-19 16:53:12 +00:00
/*
=============
2022-07-05 21:44:09 +00:00
Fog_Update
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
update internal variables
=============
*/
2022-11-30 22:58:40 +00:00
void Fog_Update (float start, float end, float red, float green, float blue, float time)
2022-07-05 21:44:09 +00:00
{
//save previous settings for fade
if (time > 0)
{
//check for a fade in progress
if (fade_done > cl.time)
{
float f;
2022-06-19 16:53:12 +00:00
2022-07-05 21:44:09 +00:00
f = (fade_done - cl.time) / fade_time;
2023-10-16 17:10:27 +00:00
old_start = f * old_start + (1.0 - f) * fog_start;
old_end = f * old_end + (1.0 - f) * fog_end;
old_red = f * old_red + (1.0 - f) * fog_red;
old_green = f * old_green + (1.0 - f) * fog_green;
old_blue = f * old_blue + (1.0 - f) * fog_blue;
old_density = f * old_density + (1.0 - f) * fog_density_gl;
2022-07-05 21:44:09 +00:00
}
else
{
2023-10-16 17:10:27 +00:00
old_start = fog_start;
old_end = fog_end;
old_red = fog_red;
old_green = fog_green;
old_blue = fog_blue;
old_density = fog_density_gl;
2022-07-05 21:44:09 +00:00
}
}
2023-10-16 17:10:27 +00:00
fog_start = start;
fog_end = end;
fog_red = red;
fog_green = green;
fog_blue = blue;
2022-07-05 21:44:09 +00:00
fade_time = time;
fade_done = cl.time + time;
2023-10-16 17:10:27 +00:00
fog_density_gl = ((fog_start / fog_end))/3.5;
2022-07-05 21:44:09 +00:00
}
/*
=============
Fog_ParseServerMessage
2023-07-22 18:26:45 +00:00
2022-06-19 16:53:12 +00:00
handle an SVC_FOG message from server
=============
*/
void Fog_ParseServerMessage (void)
{
2022-11-30 22:58:40 +00:00
float start, end, red, green, blue, time;
2022-06-19 16:53:12 +00:00
2022-11-30 22:58:40 +00:00
start = MSG_ReadByte() / 255.0;
end = MSG_ReadByte() / 255.0;
2022-06-19 16:53:12 +00:00
red = MSG_ReadByte() / 255.0;
green = MSG_ReadByte() / 255.0;
blue = MSG_ReadByte() / 255.0;
2022-11-30 22:58:40 +00:00
time = MSG_ReadShort() / 100.0;
2022-07-05 21:44:09 +00:00
2022-11-30 22:58:40 +00:00
Fog_Update (start, end, red, green, blue, time);
2022-07-05 21:44:09 +00:00
}
/*
=============
Fog_FogCommand_f
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
handle the 'fog' console command
=============
*/
void Fog_FogCommand_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("usage:\n");
2022-11-30 22:58:40 +00:00
Con_Printf(" fog <fade>\n");
Con_Printf(" fog <start> <end>\n");
2022-07-05 21:44:09 +00:00
Con_Printf(" fog <red> <green> <blue>\n");
2022-11-30 22:58:40 +00:00
Con_Printf(" fog <fade> <red> <green> <blue>\n");
Con_Printf(" fog <start> <end> <red> <green> <blue>\n");
Con_Printf(" fog <start> <end> <red> <green> <blue> <fade>\n");
2022-07-05 21:44:09 +00:00
Con_Printf("current values:\n");
2023-10-16 17:10:27 +00:00
Con_Printf(" \"start\" is \"%f\"\n", fog_start);
Con_Printf(" \"end\" is \"%f\"\n", fog_end);
Con_Printf(" \"red\" is \"%f\"\n", fog_red);
Con_Printf(" \"green\" is \"%f\"\n", fog_green);
Con_Printf(" \"blue\" is \"%f\"\n", fog_blue);
2022-11-30 22:58:40 +00:00
Con_Printf(" \"fade\" is \"%f\"\n", fade_time);
2022-07-05 21:44:09 +00:00
break;
2022-11-30 22:58:40 +00:00
case 2: //TEST
2023-10-16 17:10:27 +00:00
Fog_Update(fog_start,
fog_end,
fog_red,
fog_green,
fog_blue,
2022-11-30 22:58:40 +00:00
atof(Cmd_Argv(1)));
2022-07-05 21:44:09 +00:00
break;
2022-11-30 22:58:40 +00:00
case 3:
Fog_Update(atof(Cmd_Argv(1)),
atof(Cmd_Argv(2)),
2023-10-16 17:10:27 +00:00
fog_red,
fog_green,
fog_blue,
2022-11-30 22:58:40 +00:00
0.0);
2022-07-05 21:44:09 +00:00
break;
case 4:
2023-10-16 17:10:27 +00:00
Fog_Update(fog_start,
fog_end,
2022-11-30 22:58:40 +00:00
CLAMP(0.0, atof(Cmd_Argv(1)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(2)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(3)), 100.0),
2022-07-05 21:44:09 +00:00
0.0);
break;
2022-11-30 22:58:40 +00:00
case 5: //TEST
2023-10-16 17:10:27 +00:00
Fog_Update(fog_start,
fog_end,
2022-11-30 22:58:40 +00:00
CLAMP(0.0, atof(Cmd_Argv(1)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(2)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(3)), 100.0),
atof(Cmd_Argv(4)));
break;
case 6:
Fog_Update(atof(Cmd_Argv(1)),
atof(Cmd_Argv(2)),
CLAMP(0.0, atof(Cmd_Argv(3)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(4)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(5)), 100.0),
2022-07-05 21:44:09 +00:00
0.0);
break;
2022-11-30 22:58:40 +00:00
case 7:
Fog_Update(atof(Cmd_Argv(1)),
atof(Cmd_Argv(2)),
CLAMP(0.0, atof(Cmd_Argv(3)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(4)), 100.0),
CLAMP(0.0, atof(Cmd_Argv(5)), 100.0),
atof(Cmd_Argv(6)));
2022-07-05 21:44:09 +00:00
break;
}
}
/*
=============
Fog_ParseWorldspawn
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
called at map load
=============
*/
void Fog_ParseWorldspawn (void)
{
char key[128], value[4096];
2023-10-16 17:10:27 +00:00
const char *data;
2022-07-05 21:44:09 +00:00
2023-10-16 17:10:27 +00:00
fog_density_gl = DEFAULT_DENSITY;
2022-07-05 21:44:09 +00:00
//initially no fog
2023-10-16 17:10:27 +00:00
fog_start = 0;
2022-11-30 22:58:40 +00:00
old_start = 0;
2023-11-17 02:46:59 +00:00
fog_end = 4000;
old_end = 4000;
2022-07-05 21:44:09 +00:00
2023-10-16 17:10:27 +00:00
fog_red = 0.0;
2022-11-30 22:58:40 +00:00
old_red = 0.0;
2023-10-16 17:10:27 +00:00
fog_green = 0.0;
2022-11-30 22:58:40 +00:00
old_green = 0.0;
2023-10-16 17:10:27 +00:00
fog_blue = 0.0;
2022-11-30 22:58:40 +00:00
old_blue = 0.0;
2022-07-05 21:44:09 +00:00
fade_time = 0.0;
fade_done = 0.0;
data = COM_Parse(cl.worldmodel->entities);
if (!data)
return; // error
if (com_token[0] != '{')
return; // error
while (1)
{
data = COM_Parse(data);
if (!data)
return; // error
if (com_token[0] == '}')
break; // end of worldspawn
if (com_token[0] == '_')
strcpy(key, com_token + 1);
else
strcpy(key, com_token);
while (key[strlen(key)-1] == ' ') // remove trailing spaces
key[strlen(key)-1] = 0;
data = COM_Parse(data);
if (!data)
return; // error
strcpy(value, com_token);
if (!strcmp("fog", key))
{
2023-10-16 17:10:27 +00:00
sscanf(value, "%f %f %f %f %f", &fog_start, &fog_end, &fog_red, &fog_green, &fog_blue);
2022-07-05 21:44:09 +00:00
}
2023-10-16 17:10:27 +00:00
fog_density_gl = ((fog_start / fog_end))/3.5;
2022-07-05 21:44:09 +00:00
}
}
/*
=============
2023-10-16 17:10:27 +00:00
Fog_GetColor
2023-07-22 18:26:45 +00:00
2023-10-16 17:10:27 +00:00
calculates fog color for this frame, taking into account fade times
2022-07-05 21:44:09 +00:00
=============
*/
2023-10-16 17:10:27 +00:00
float *Fog_GetColor (void)
2022-07-05 21:44:09 +00:00
{
2023-10-16 17:10:27 +00:00
static float c[4]; // = {0.1f, 0.1f, 0.1f, 1.0f}
float f;
int i;
2022-07-05 21:44:09 +00:00
if (fade_done > cl.time)
{
f = (fade_done - cl.time) / fade_time;
2023-10-16 17:10:27 +00:00
c[0] = f * old_red + (1.0 - f) * fog_red;
c[1] = f * old_green + (1.0 - f) * fog_green;
c[2] = f * old_blue + (1.0 - f) * fog_blue;
c[3] = 1.0;
2022-07-05 21:44:09 +00:00
}
else
{
2023-10-16 17:10:27 +00:00
c[0] = fog_red;
c[1] = fog_green;
c[2] = fog_blue;
2022-07-05 21:44:09 +00:00
c[3] = 1.0;
}
2023-10-16 17:10:27 +00:00
//find closest 24-bit RGB value, so solid-colored sky can match the fog perfectly
for (i=0;i<3;i++)
c[i] = (float)(rint(c[i] * 255)) / 255.0f;
2022-11-30 22:58:40 +00:00
2023-10-16 17:10:27 +00:00
for (i = 0; i < 3; i++)
c[i] /= 64.0;
2022-11-30 22:58:40 +00:00
2023-10-16 17:10:27 +00:00
return c;
}
/*
=============
Fog_GetDensity
returns current density of fog
=============
*/
float Fog_GetDensity (void)
{
float f;
if (fade_done > cl.time)
{
f = (fade_done - cl.time) / fade_time;
return f * old_density + (1.0 - f) * fog_density_gl;
}
else
return fog_density_gl;
}
/*
=============
Fog_SetupFrame
called at the beginning of each frame
=============
*/
void Fog_SetupFrame (void)
{
glFogfv(GL_FOG_COLOR, Fog_GetColor());
2023-11-17 02:46:59 +00:00
glFogf(GL_FOG_DENSITY, 0.2f);
2023-10-16 17:10:27 +00:00
glFogf(GL_FOG_START, fog_start);
glFogf(GL_FOG_END, fog_end);
2022-07-05 21:44:09 +00:00
}
2023-07-22 18:26:45 +00:00
/*
=============
Fog_SetColorForSkyS
Start called before drawing flat-colored sky
Crow_bar*
=============
*/
void Fog_SetColorForSkyS (void)
{
2023-10-16 17:10:27 +00:00
if (fog_end > 0.0f && r_skyfog.value)
2023-07-22 18:26:45 +00:00
{
2023-10-16 17:10:27 +00:00
float a = fog_end * 0.00025f;
float r = fog_red * 0.01f + (a * 0.25f);
float g = fog_green * 0.01f + (a * 0.25f);
float b = fog_blue * 0.01f + (a * 0.25f);
2023-07-22 18:26:45 +00:00
if (a > 1.0f)
a = 1.0f;
if (r > 1.0f)
r = 1.0f;
if (g > 1.0f)
g = 1.0f;
if (b > 1.0f)
b = 1.0f;
2023-10-16 17:10:27 +00:00
glColor4f(r, g, b, a);
}
2023-07-22 18:26:45 +00:00
}
2022-07-05 21:44:09 +00:00
/*
=============
2022-11-30 22:58:40 +00:00
Fog_GetStart
returns current start of fog
2022-07-05 21:44:09 +00:00
=============
*/
2022-11-30 22:58:40 +00:00
float Fog_GetStart (void)
2022-07-05 21:44:09 +00:00
{
float f;
if (fade_done > cl.time)
{
f = (fade_done - cl.time) / fade_time;
2023-10-16 17:10:27 +00:00
return f * old_start + (1.0 - f) * fog_start;
2022-07-05 21:44:09 +00:00
}
else
2023-10-16 17:10:27 +00:00
return fog_start;
2022-07-05 21:44:09 +00:00
}
2022-06-19 16:53:12 +00:00
2022-07-05 21:44:09 +00:00
/*
=============
2022-11-30 22:58:40 +00:00
Fog_GetEnd
returns current end of fog
2022-07-05 21:44:09 +00:00
=============
*/
2022-11-30 22:58:40 +00:00
float Fog_GetEnd (void)
2022-07-05 21:44:09 +00:00
{
2022-11-30 22:58:40 +00:00
float f;
if (fade_done > cl.time)
{
f = (fade_done - cl.time) / fade_time;
2023-10-16 17:10:27 +00:00
return f * old_start + (1.0 - f) * fog_end;
2022-11-30 22:58:40 +00:00
}
else
2023-10-16 17:10:27 +00:00
return fog_end;
}
/*
=============
Fog_SetColorForSky
End called before drawing flat-colored sky
Crow_bar*
=============
*/
void Fog_SetColorForSkyE (void)
{
if (fog_end > 0.0f && r_skyfog.value)
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
2022-07-05 21:44:09 +00:00
}
/*
=============
Fog_EnableGFog
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
called before drawing stuff that should be fogged
=============
*/
void Fog_EnableGFog (void)
{
2023-11-17 02:46:59 +00:00
if (!Fog_GetStart() == 0 || !Fog_GetEnd() <= 0) {
glEnable(GL_FOG);
}
2022-07-05 21:44:09 +00:00
}
/*
=============
Fog_DisableGFog
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
called after drawing stuff that should be fogged
=============
*/
void Fog_DisableGFog (void)
{
2023-11-17 02:46:59 +00:00
if (!Fog_GetStart() == 0 || !Fog_GetEnd() <= 0)
glDisable(GL_FOG);
2022-07-05 21:44:09 +00:00
}
2023-07-22 18:26:45 +00:00
/*
=============
Fog_SetColorForSky
called before drawing flat-colored sky
=============
*/
/*
void Fog_SetColorForSky (void)
{
float c[3];
float f, d;
if (fade_done > cl.time)
{
f = (fade_done - cl.time) / fade_time;
d = f * old_density + (1.0 - f) * fog_density;
c[0] = f * old_red + (1.0 - f) * fog_red;
c[1] = f * old_green + (1.0 - f) * fog_green;
c[2] = f * old_blue + (1.0 - f) * fog_blue;
}
else
{
d = fog_density;
c[0] = fog_red;
c[1] = fog_green;
c[2] = fog_blue;
}
if (d > 0)
glColor3fv (c);
}
*/
//==============================================================================
//
// VOLUMETRIC FOG
//
//==============================================================================
/*
void Fog_DrawVFog (void){}
void Fog_MarkModels (void){}
*/
2022-07-05 21:44:09 +00:00
//==============================================================================
//
// INIT
//
//==============================================================================
/*
=============
Fog_NewMap
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
called whenever a map is loaded
=============
*/
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
void Fog_NewMap (void)
{
Fog_ParseWorldspawn (); //for global fog
}
/*
=============
Fog_Init
2023-07-22 18:26:45 +00:00
2022-07-05 21:44:09 +00:00
called when quake initializes
=============
*/
void Fog_Init (void)
{
Cmd_AddCommand ("fog",Fog_FogCommand_f);
//set up global fog
2023-11-17 02:46:59 +00:00
fog_start = 300;
fog_end = 4000;
2023-10-16 17:10:27 +00:00
fog_red = 0.5;
fog_green = 0.5;
fog_blue = 0.5;
2022-11-30 22:58:40 +00:00
fade_time = 1;
2023-10-16 17:10:27 +00:00
fog_density_gl = DEFAULT_DENSITY;
fade_time = 1;
2023-11-17 02:46:59 +00:00
glFogi(GL_FOG_MODE, GL_LINEAR);
2022-06-19 16:53:12 +00:00
}