2012-01-07 05:53:23 +00:00
|
|
|
/*
|
|
|
|
glsl_lightmap.c
|
|
|
|
|
|
|
|
GLSL lightmaps
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2000 Joseph Carter <knghtbrd@debian.org>
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/1/6
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/render.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
#include "QF/scene/entity.h"
|
|
|
|
|
2012-01-07 05:53:23 +00:00
|
|
|
#include "QF/GLSL/defines.h"
|
|
|
|
#include "QF/GLSL/funcs.h"
|
|
|
|
#include "QF/GLSL/qf_lightmap.h"
|
|
|
|
#include "QF/GLSL/qf_textures.h"
|
|
|
|
#include "QF/GLSL/qf_vid.h"
|
|
|
|
|
2012-02-14 08:28:09 +00:00
|
|
|
#include "r_internal.h"
|
2012-01-07 05:53:23 +00:00
|
|
|
|
|
|
|
#define BLOCK_SIZE (BLOCK_WIDTH * BLOCK_HEIGHT)
|
|
|
|
|
|
|
|
static scrap_t *light_scrap;
|
|
|
|
static unsigned *blocklights;
|
|
|
|
static int bl_extents[2];
|
|
|
|
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
void (*glsl_R_BuildLightMap) (const vec4f_t *transform, mod_brush_t *brush,
|
2021-07-22 06:39:28 +00:00
|
|
|
msurface_t *surf);
|
2012-01-07 05:53:23 +00:00
|
|
|
|
|
|
|
static void
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
R_AddDynamicLights_1 (const vec4f_t *transform, msurface_t *surf)
|
2012-01-07 05:53:23 +00:00
|
|
|
{
|
|
|
|
unsigned lnum;
|
|
|
|
int sd, td;
|
|
|
|
float dist, rad, minlight;
|
|
|
|
vec3_t impact, local, lightorigin;
|
2021-03-19 11:18:45 +00:00
|
|
|
vec4f_t entorigin = { 0, 0, 0, 1};
|
2012-01-07 05:53:23 +00:00
|
|
|
int s, t;
|
|
|
|
int smax, tmax;
|
|
|
|
mtexinfo_t *tex;
|
|
|
|
|
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
|
|
|
tex = surf->texinfo;
|
|
|
|
|
2021-07-22 06:39:28 +00:00
|
|
|
if (transform) {
|
2021-03-19 11:18:45 +00:00
|
|
|
//FIXME give world entity a transform
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
entorigin = transform[3];
|
2021-03-19 11:18:45 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 05:53:23 +00:00
|
|
|
for (lnum = 0; lnum < r_maxdlights; lnum++) {
|
2012-07-21 04:58:54 +00:00
|
|
|
if (!(surf->dlightbits[lnum / 32] & (1 << (lnum % 32))))
|
2012-01-07 05:53:23 +00:00
|
|
|
continue; // not lit by this light
|
|
|
|
|
2021-03-19 11:18:45 +00:00
|
|
|
VectorSubtract (r_dlights[lnum].origin, entorigin, lightorigin);
|
2012-01-07 05:53:23 +00:00
|
|
|
rad = r_dlights[lnum].radius;
|
|
|
|
dist = DotProduct (lightorigin, surf->plane->normal)
|
|
|
|
- surf->plane->dist;
|
|
|
|
rad -= fabs (dist);
|
|
|
|
minlight = r_dlights[lnum].minlight;
|
|
|
|
if (rad < minlight)
|
|
|
|
continue;
|
|
|
|
minlight = rad - minlight;
|
|
|
|
|
|
|
|
VectorMultSub (lightorigin, dist, surf->plane->normal, impact);
|
|
|
|
|
|
|
|
local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3];
|
|
|
|
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3];
|
|
|
|
|
|
|
|
local[0] -= surf->texturemins[0];
|
|
|
|
local[1] -= surf->texturemins[1];
|
|
|
|
|
|
|
|
for (t = 0; t < tmax; t++) {
|
|
|
|
td = local[1] - t * 16;
|
|
|
|
if (td < 0)
|
|
|
|
td = -td;
|
|
|
|
for (s = 0; s < smax; s++) {
|
|
|
|
sd = local[0] - s * 16;
|
|
|
|
if (sd < 0)
|
|
|
|
sd = -sd;
|
|
|
|
if (sd > td)
|
|
|
|
dist = sd + (td >> 1);
|
|
|
|
else
|
|
|
|
dist = td + (sd >> 1);
|
|
|
|
if (dist < minlight)
|
|
|
|
blocklights[t * smax + s] += (rad - dist) * 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
R_BuildLightMap_1 (const vec4f_t *transform, mod_brush_t *brush,
|
2021-07-22 06:39:28 +00:00
|
|
|
msurface_t *surf)
|
2012-01-07 05:53:23 +00:00
|
|
|
{
|
|
|
|
int smax, tmax, size;
|
2012-01-09 08:47:25 +00:00
|
|
|
unsigned scale;
|
|
|
|
int i, t;
|
2012-01-07 05:53:23 +00:00
|
|
|
byte *out;
|
|
|
|
|
2012-01-30 09:41:27 +00:00
|
|
|
// If we add dlights this frame, make sure they get removed next frame
|
|
|
|
// if the dlights disappear suddenly
|
|
|
|
surf->cached_dlight = (surf->dlightframe == r_framecount);
|
|
|
|
|
2012-01-07 05:53:23 +00:00
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
|
|
|
size = smax * tmax;
|
|
|
|
|
|
|
|
// clear to no light
|
|
|
|
memset (blocklights, 0, size * sizeof (blocklights[0]));
|
2021-02-01 10:31:11 +00:00
|
|
|
if (!brush->lightdata) {
|
2012-01-07 05:53:23 +00:00
|
|
|
// because we by-pass the inversion, "no light" = "full bright"
|
2012-07-02 11:01:28 +00:00
|
|
|
GLSL_SubpicUpdate (surf->lightpic, (byte *) blocklights, 1);
|
2012-01-07 05:53:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add all the lightmaps
|
|
|
|
if (surf->samples) {
|
|
|
|
int lmap;
|
|
|
|
byte *lightmap = surf->samples;
|
|
|
|
|
|
|
|
for (lmap = 0; lmap < MAXLIGHTMAPS && surf->styles[lmap] != 255;
|
|
|
|
lmap++) {
|
|
|
|
unsigned int *bl;
|
|
|
|
|
|
|
|
scale = d_lightstylevalue[surf->styles[lmap]];
|
|
|
|
surf->cached_light[lmap] = scale;
|
|
|
|
bl = blocklights;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
*bl++ += *lightmap++ * scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add all the dynamic lights
|
|
|
|
if (surf->dlightframe == r_framecount)
|
2021-07-22 06:39:28 +00:00
|
|
|
R_AddDynamicLights_1 (transform, surf);
|
2012-01-07 05:53:23 +00:00
|
|
|
|
|
|
|
// bound, invert, and shift
|
|
|
|
out = (byte *) blocklights;
|
|
|
|
for (i = 0; i < size; i++) {
|
2012-01-09 08:47:25 +00:00
|
|
|
t = (255 * 256 - (int) blocklights[i]);
|
|
|
|
t = max (t, 1 << (14 - VID_CBITS));
|
|
|
|
t = ((unsigned) t) >> (16 - VID_CBITS);
|
|
|
|
*out++ = t;
|
2012-01-07 05:53:23 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 11:01:28 +00:00
|
|
|
GLSL_SubpicUpdate (surf->lightpic, (byte *) blocklights, 1);
|
2012-01-07 05:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_surf_lightmap (msurface_t *surf)
|
|
|
|
{
|
|
|
|
int smax, tmax;
|
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
2012-02-17 09:57:13 +00:00
|
|
|
surf->lightpic = GLSL_ScrapSubpic (light_scrap, smax, tmax);
|
2012-01-07 05:53:23 +00:00
|
|
|
if (!surf->lightpic)
|
|
|
|
Sys_Error ("FIXME taniwha is being lazy");
|
|
|
|
if (smax > bl_extents[0])
|
|
|
|
bl_extents[0] = smax;
|
|
|
|
if (tmax > bl_extents[1])
|
|
|
|
bl_extents[1] = tmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-22 07:32:34 +00:00
|
|
|
glsl_R_BuildLightmaps (model_t **models, int num_models)
|
2012-01-07 05:53:23 +00:00
|
|
|
{
|
2021-08-01 12:54:05 +00:00
|
|
|
int size;
|
2012-01-07 05:53:23 +00:00
|
|
|
model_t *m;
|
2021-02-01 10:31:11 +00:00
|
|
|
mod_brush_t *brush;
|
2012-01-07 05:53:23 +00:00
|
|
|
|
|
|
|
//FIXME RGB support
|
|
|
|
if (!light_scrap) {
|
2022-05-21 08:45:22 +00:00
|
|
|
light_scrap = GLSL_CreateScrap (4096, GL_LUMINANCE, 1);
|
2012-01-17 04:02:15 +00:00
|
|
|
} else {
|
2012-02-17 09:57:13 +00:00
|
|
|
GLSL_ScrapClear (light_scrap);
|
2012-01-07 05:53:23 +00:00
|
|
|
}
|
2012-02-22 07:32:34 +00:00
|
|
|
glsl_R_BuildLightMap = R_BuildLightMap_1;
|
2012-01-07 05:53:23 +00:00
|
|
|
|
|
|
|
bl_extents[1] = bl_extents[0] = 0;
|
2021-08-01 12:54:05 +00:00
|
|
|
for (int j = 1; j < num_models; j++) {
|
2012-01-07 05:53:23 +00:00
|
|
|
m = models[j];
|
|
|
|
if (!m)
|
|
|
|
break;
|
2021-02-01 10:31:11 +00:00
|
|
|
if (m->path[0] == '*' || m->type != mod_brush) {
|
2012-01-07 05:53:23 +00:00
|
|
|
// sub model surfaces are processed as part of the main model
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 10:31:11 +00:00
|
|
|
brush = &m->brush;
|
2012-01-07 05:53:23 +00:00
|
|
|
// non-bsp models don't have surfaces.
|
2021-08-01 12:54:05 +00:00
|
|
|
for (unsigned i = 0; i < brush->numsurfaces; i++) {
|
2021-02-01 10:31:11 +00:00
|
|
|
msurface_t *surf = brush->surfaces + i;
|
2012-01-07 05:53:23 +00:00
|
|
|
surf->lightpic = 0; // paranoia
|
|
|
|
if (surf->flags & SURF_DRAWTURB)
|
|
|
|
continue;
|
|
|
|
if (surf->flags & SURF_DRAWSKY)
|
|
|
|
continue;
|
|
|
|
create_surf_lightmap (surf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = bl_extents[0] * bl_extents[1] * 3; // * 3 for rgb support
|
|
|
|
blocklights = realloc (blocklights, size * sizeof (blocklights[0]));
|
2021-08-01 12:54:05 +00:00
|
|
|
for (int j = 1; j < num_models; j++) {
|
2012-01-07 05:53:23 +00:00
|
|
|
m = models[j];
|
|
|
|
if (!m)
|
|
|
|
break;
|
2021-02-01 10:31:11 +00:00
|
|
|
if (m->path[0] == '*' || m->type != mod_brush) {
|
2012-01-07 05:53:23 +00:00
|
|
|
// sub model surfaces are processed as part of the main model
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 10:31:11 +00:00
|
|
|
brush = &m->brush;
|
2012-01-07 05:53:23 +00:00
|
|
|
// non-bsp models don't have surfaces.
|
2021-08-01 12:54:05 +00:00
|
|
|
for (unsigned i = 0; i < brush->numsurfaces; i++) {
|
2021-02-01 10:31:11 +00:00
|
|
|
msurface_t *surf = brush->surfaces + i;
|
2012-01-07 05:53:23 +00:00
|
|
|
if (surf->lightpic)
|
2021-07-22 06:39:28 +00:00
|
|
|
glsl_R_BuildLightMap (0, brush, surf);
|
2012-01-07 05:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 12:13:40 +00:00
|
|
|
|
|
|
|
int
|
2012-02-22 07:32:34 +00:00
|
|
|
glsl_R_LightmapTexture (void)
|
2012-01-09 12:13:40 +00:00
|
|
|
{
|
2012-02-17 09:57:13 +00:00
|
|
|
return GLSL_ScrapTexture (light_scrap);
|
2012-01-09 12:13:40 +00:00
|
|
|
}
|
2012-07-02 11:01:28 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
glsl_R_FlushLightmaps (void)
|
|
|
|
{
|
|
|
|
GLSL_ScrapFlush (light_scrap);
|
|
|
|
}
|