2023-10-03 04:49:25 +00:00
|
|
|
/*
|
|
|
|
glsl_trails.c
|
|
|
|
|
|
|
|
OpenGL trail system.
|
|
|
|
|
|
|
|
Copyright (C) 2013 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
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/alloc.h"
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/image.h"
|
|
|
|
#include "QF/mersenne.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/render.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
|
|
|
|
#include "QF/scene/entity.h"
|
|
|
|
|
|
|
|
#include "QF/GLSL/defines.h"
|
|
|
|
#include "QF/GLSL/funcs.h"
|
|
|
|
#include "QF/GLSL/qf_particles.h"
|
|
|
|
#include "QF/GLSL/qf_textures.h"
|
|
|
|
#include "QF/GLSL/qf_vid.h"
|
|
|
|
|
|
|
|
#include "r_internal.h"
|
|
|
|
#include "r_local.h"
|
|
|
|
|
|
|
|
static uint32_t maxparticles;
|
|
|
|
static GLushort *pVAindices;
|
|
|
|
static partvert_t *particleVertexArray;
|
|
|
|
|
|
|
|
static const char *particle_trail_vert_effects[] =
|
|
|
|
{
|
|
|
|
"QuakeForge.Screen.viewport",
|
|
|
|
"QuakeForge.Vertex.transform.view_projection",
|
|
|
|
"QuakeForge.Vertex.ScreenSpace.curve.width",
|
|
|
|
"QuakeForge.Vertex.particle.trail",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *particle_trail_frag_effects[] =
|
|
|
|
{
|
|
|
|
"QuakeForge.Math.InvSqrt",
|
|
|
|
"QuakeForge.Math.permute",
|
|
|
|
"QuakeForge.Noise.simplex",
|
|
|
|
"QuakeForge.Fragment.particle.trail",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2023-10-03 06:05:13 +00:00
|
|
|
static const char *particle_trail_debug_frag_effects[] =
|
|
|
|
{
|
|
|
|
"QuakeForge.Fragment.barycentric",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
typedef struct {
|
2023-10-03 04:49:25 +00:00
|
|
|
int program;
|
|
|
|
shaderparam_t proj;
|
|
|
|
shaderparam_t view;
|
|
|
|
shaderparam_t viewport;
|
|
|
|
shaderparam_t width;
|
|
|
|
shaderparam_t last;
|
|
|
|
shaderparam_t current;
|
|
|
|
shaderparam_t next;
|
|
|
|
shaderparam_t barycentric;
|
|
|
|
shaderparam_t texoff;
|
|
|
|
shaderparam_t colora;
|
|
|
|
shaderparam_t colorb;
|
2023-10-06 01:31:12 +00:00
|
|
|
} trailprog_t;
|
|
|
|
|
|
|
|
static trailprog_t trail = {
|
|
|
|
0,
|
2023-10-03 04:49:25 +00:00
|
|
|
{"projection_mat", 1},
|
|
|
|
{"view_mat", 1},
|
|
|
|
{"viewport", 1},
|
|
|
|
{"width", 1},
|
|
|
|
{"last", 0},
|
|
|
|
{"current", 0},
|
|
|
|
{"next", 0},
|
|
|
|
{"barycentric", 0},
|
|
|
|
{"texoff", 0},
|
|
|
|
{"vcolora", 0},
|
|
|
|
{"vcolorb", 0},
|
|
|
|
};
|
2023-10-06 01:31:12 +00:00
|
|
|
static trailprog_t trail_debug;
|
2023-10-03 04:49:25 +00:00
|
|
|
|
|
|
|
typedef struct trailvtx_s {
|
2023-10-06 01:31:12 +00:00
|
|
|
vec4f_t vertex;
|
2023-10-03 04:49:25 +00:00
|
|
|
vec3_t bary;
|
|
|
|
float texoff;
|
2023-10-06 01:31:12 +00:00
|
|
|
byte colora[4];
|
|
|
|
byte colorb[4];
|
2023-10-03 04:49:25 +00:00
|
|
|
} trailvtx_t;
|
|
|
|
|
|
|
|
static void
|
|
|
|
alloc_arrays (psystem_t *ps)
|
|
|
|
{
|
|
|
|
if (ps->maxparticles > maxparticles) {
|
|
|
|
maxparticles = ps->maxparticles;
|
|
|
|
if (particleVertexArray)
|
|
|
|
free (particleVertexArray);
|
|
|
|
printf ("alloc_arrays: %d\n", ps->maxparticles);
|
|
|
|
particleVertexArray = calloc (ps->maxparticles * 4,
|
|
|
|
sizeof (partvert_t));
|
|
|
|
|
|
|
|
if (pVAindices)
|
|
|
|
free (pVAindices);
|
|
|
|
pVAindices = calloc (ps->maxparticles * 6, sizeof (GLushort));
|
|
|
|
for (uint32_t i = 0; i < ps->maxparticles; i++) {
|
|
|
|
pVAindices[i * 6 + 0] = i * 4 + 0;
|
|
|
|
pVAindices[i * 6 + 1] = i * 4 + 1;
|
|
|
|
pVAindices[i * 6 + 2] = i * 4 + 2;
|
|
|
|
pVAindices[i * 6 + 3] = i * 4 + 0;
|
|
|
|
pVAindices[i * 6 + 4] = i * 4 + 2;
|
|
|
|
pVAindices[i * 6 + 5] = i * 4 + 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-10-06 01:31:12 +00:00
|
|
|
glsl_R_ShutdownTrails (void)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
|
|
|
free (particleVertexArray);
|
|
|
|
free (pVAindices);
|
|
|
|
}
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
static void
|
|
|
|
build_program (trailprog_t *prog, const char *name, int vert, int frag)
|
|
|
|
{
|
|
|
|
prog->program = GLSL_LinkProgram (name, vert, frag);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->proj);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->view);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->viewport);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->width);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->last);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->current);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->next);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->barycentric);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->texoff);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->colora);
|
|
|
|
GLSL_ResolveShaderParam (prog->program, &prog->colorb);
|
|
|
|
}
|
|
|
|
|
2023-10-03 04:49:25 +00:00
|
|
|
void
|
2023-10-06 01:31:12 +00:00
|
|
|
glsl_R_InitTrails (void)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
2023-10-03 06:05:13 +00:00
|
|
|
shader_t *vert_shader, *frag_shader, *debug_shader;
|
2023-10-03 04:49:25 +00:00
|
|
|
int vert;
|
|
|
|
int frag;
|
2023-10-03 06:05:13 +00:00
|
|
|
int debug;
|
2023-10-03 04:49:25 +00:00
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
|
2023-10-03 04:49:25 +00:00
|
|
|
vert_shader = GLSL_BuildShader (particle_trail_vert_effects);
|
|
|
|
frag_shader = GLSL_BuildShader (particle_trail_frag_effects);
|
2023-10-03 06:05:13 +00:00
|
|
|
debug_shader = GLSL_BuildShader (particle_trail_debug_frag_effects);
|
2023-10-03 04:49:25 +00:00
|
|
|
vert = GLSL_CompileShader ("trail.vert", vert_shader, GL_VERTEX_SHADER);
|
|
|
|
frag = GLSL_CompileShader ("trail.frag", frag_shader, GL_FRAGMENT_SHADER);
|
2023-10-03 06:05:13 +00:00
|
|
|
debug = GLSL_CompileShader ("trail.frag.debug", debug_shader,
|
|
|
|
GL_FRAGMENT_SHADER);
|
2023-10-06 01:31:12 +00:00
|
|
|
trail_debug = trail;
|
|
|
|
build_program (&trail, "trail", vert, frag);
|
|
|
|
build_program (&trail_debug, "trail.debug", vert, debug);
|
|
|
|
|
2023-10-03 04:49:25 +00:00
|
|
|
GLSL_FreeShader (vert_shader);
|
|
|
|
GLSL_FreeShader (frag_shader);
|
2023-10-06 01:31:12 +00:00
|
|
|
GLSL_FreeShader (debug_shader);
|
2023-10-03 04:49:25 +00:00
|
|
|
|
|
|
|
alloc_arrays (&r_psystem);
|
|
|
|
}
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
static int
|
|
|
|
count_bits (uint32_t v)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
int c = 0;
|
|
|
|
for (; v; c++) {
|
|
|
|
v &= v - 1;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
2023-10-06 01:31:12 +00:00
|
|
|
return c;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
static int
|
|
|
|
count_points (uint32_t block_mask)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
int num_blocks = count_bits (block_mask);
|
|
|
|
return num_blocks * 64;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-10-06 01:31:12 +00:00
|
|
|
build_verts (trailvtx_t *verts, int *counts,
|
|
|
|
const trailpnt_t *points, uint32_t block_mask)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
uint32_t id = ~0u;
|
|
|
|
for (int m = 0; m < 32; m++, points += 64) {
|
|
|
|
if (!(block_mask & (1 << m))) {
|
|
|
|
if (*counts) {
|
|
|
|
counts++;
|
|
|
|
}
|
2023-10-03 04:49:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-10-06 01:31:12 +00:00
|
|
|
if (id != ~0u && points->trailid != id) {
|
|
|
|
counts++;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
2023-10-06 01:31:12 +00:00
|
|
|
id = points->trailid;
|
|
|
|
*counts += 64;
|
|
|
|
for (int i = 0; i < 64; i++) {
|
|
|
|
verts[i] = (trailvtx_t) {
|
|
|
|
.vertex = points[i].pos,
|
|
|
|
.bary = {VectorExpand (points[i].bary)},
|
|
|
|
.texoff = points[i].pathoffset,
|
|
|
|
.colora = { QuatExpand (points[i].colora)},
|
|
|
|
.colorb = { QuatExpand (points[i].colorb)},
|
|
|
|
};
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
2023-10-06 01:31:12 +00:00
|
|
|
verts += 64;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-10-06 01:31:12 +00:00
|
|
|
draw_trails (trailpnt_t *points, uint32_t block_mask)
|
2023-10-03 04:49:25 +00:00
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
int num_verts = count_points (block_mask);
|
|
|
|
if (!num_verts) {
|
2023-10-03 04:49:25 +00:00
|
|
|
return;
|
2023-10-06 01:31:12 +00:00
|
|
|
}
|
2023-10-03 04:49:25 +00:00
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
trailvtx_t verts[num_verts];
|
|
|
|
int counts[33] = {};
|
|
|
|
build_verts (verts, counts, points, block_mask);
|
|
|
|
|
|
|
|
auto prog = trail;
|
|
|
|
qfeglUseProgram (prog.program);
|
|
|
|
qfeglEnableVertexAttribArray (prog.last.location);
|
|
|
|
qfeglEnableVertexAttribArray (prog.current.location);
|
|
|
|
qfeglEnableVertexAttribArray (prog.next.location);
|
|
|
|
if (prog.barycentric.location >= 0)
|
|
|
|
qfeglEnableVertexAttribArray (prog.barycentric.location);
|
|
|
|
qfeglEnableVertexAttribArray (prog.texoff.location);
|
|
|
|
if (prog.colora.location >= 0)
|
|
|
|
qfeglEnableVertexAttribArray (prog.colora.location);
|
|
|
|
if (prog.colorb.location >= 0)
|
|
|
|
qfeglEnableVertexAttribArray (prog.colorb.location);
|
|
|
|
|
|
|
|
qfeglUniformMatrix4fv (prog.proj.location, 1, false,
|
2023-10-03 04:49:25 +00:00
|
|
|
(vec_t*)&glsl_projection[0]);//FIXME
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglUniformMatrix4fv (prog.view.location, 1, false,
|
2023-10-03 04:49:25 +00:00
|
|
|
(vec_t*)&glsl_view[0]);//FIXME
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglUniform2f (prog.viewport.location, r_refdef.vrect.width,
|
2023-10-03 04:49:25 +00:00
|
|
|
r_refdef.vrect.height);
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglUniform1f (prog.width.location, 10);
|
2023-10-03 04:49:25 +00:00
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglVertexAttribPointer (prog.last.location, 4, GL_FLOAT,
|
2023-10-03 04:49:25 +00:00
|
|
|
0, sizeof (trailvtx_t), &verts[0].vertex);
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglVertexAttribPointer (prog.current.location, 4, GL_FLOAT,
|
2023-10-03 04:49:25 +00:00
|
|
|
0, sizeof (trailvtx_t), &verts[2].vertex);
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglVertexAttribPointer (prog.next.location, 4, GL_FLOAT,
|
2023-10-03 04:49:25 +00:00
|
|
|
0, sizeof (trailvtx_t), &verts[4].vertex);
|
2023-10-06 01:31:12 +00:00
|
|
|
if (prog.barycentric.location >= 0)
|
|
|
|
qfeglVertexAttribPointer (prog.barycentric.location, 3, GL_FLOAT,
|
2023-10-03 04:49:25 +00:00
|
|
|
0, sizeof (trailvtx_t), &verts[2].bary);
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglVertexAttribPointer (prog.texoff.location, 1, GL_FLOAT,
|
2023-10-03 04:49:25 +00:00
|
|
|
0, sizeof (trailvtx_t), &verts[0].texoff);
|
2023-10-06 01:31:12 +00:00
|
|
|
if (prog.colora.location >= 0)
|
|
|
|
qfeglVertexAttribPointer (prog.colora.location, 4, GL_UNSIGNED_BYTE,
|
|
|
|
1, sizeof (trailvtx_t), &verts[0].colora);
|
|
|
|
if (prog.colorb.location >= 0)
|
|
|
|
qfeglVertexAttribPointer (prog.colorb.location, 4, GL_UNSIGNED_BYTE,
|
|
|
|
1, sizeof (trailvtx_t), &verts[0].colorb);
|
|
|
|
|
|
|
|
int first = 0;
|
|
|
|
for (auto c = counts; *c; c++) {
|
|
|
|
qfeglDrawArrays (GL_TRIANGLE_STRIP, first, *c - 4);
|
|
|
|
first += *c;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
qfeglDisableVertexAttribArray (prog.last.location);
|
|
|
|
qfeglDisableVertexAttribArray (prog.current.location);
|
|
|
|
qfeglDisableVertexAttribArray (prog.next.location);
|
|
|
|
if (prog.barycentric.location >= 0)
|
|
|
|
qfeglDisableVertexAttribArray (prog.barycentric.location);
|
|
|
|
qfeglDisableVertexAttribArray (prog.texoff.location);
|
|
|
|
if (prog.colora.location >= 0)
|
|
|
|
qfeglDisableVertexAttribArray (prog.colora.location);
|
|
|
|
if (prog.colorb.location >= 0)
|
|
|
|
qfeglDisableVertexAttribArray (prog.colorb.location);
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
void
|
2023-10-03 04:49:25 +00:00
|
|
|
glsl_R_DrawTrails (psystem_t *psystem)
|
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
//FIXME abuse of psystem_t
|
|
|
|
draw_trails ((trailpnt_t *)psystem->particles, psystem->numparticles);
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 01:31:12 +00:00
|
|
|
psystem_t * __attribute__((const))//FIXME
|
2023-10-03 04:49:25 +00:00
|
|
|
glsl_TrailSystem (void)
|
|
|
|
{
|
2023-10-06 01:31:12 +00:00
|
|
|
return &r_tsystem;
|
2023-10-03 04:49:25 +00:00
|
|
|
}
|