2011-12-30 12:11:59 +00:00
|
|
|
/*
|
|
|
|
glsl_sprite.c
|
|
|
|
|
|
|
|
Sprite drawing support for GLSL
|
|
|
|
|
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2011/12/30
|
|
|
|
|
|
|
|
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 "QF/cvar.h"
|
|
|
|
#include "QF/draw.h"
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/vid.h"
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
#include "QF/scene/entity.h"
|
|
|
|
|
2011-12-30 12:11:59 +00:00
|
|
|
#include "QF/GLSL/defines.h"
|
|
|
|
#include "QF/GLSL/funcs.h"
|
2021-07-22 06:39:28 +00:00
|
|
|
#include "QF/GLSL/qf_sprite.h"
|
2011-12-30 12:11:59 +00:00
|
|
|
#include "QF/GLSL/qf_textures.h"
|
|
|
|
#include "QF/GLSL/qf_vid.h"
|
|
|
|
|
2012-02-14 08:28:09 +00:00
|
|
|
#include "r_internal.h"
|
2011-12-30 12:11:59 +00:00
|
|
|
|
2014-01-28 03:31:27 +00:00
|
|
|
static const char *sprite_vert_effects[] =
|
|
|
|
{
|
|
|
|
"QuakeForge.Vertex.sprite",
|
|
|
|
0
|
|
|
|
};
|
2011-12-30 12:11:59 +00:00
|
|
|
|
2014-01-28 03:31:27 +00:00
|
|
|
static const char *sprite_frag_effects[] =
|
|
|
|
{
|
2014-01-28 04:00:28 +00:00
|
|
|
"QuakeForge.Fragment.fog",
|
|
|
|
"QuakeForge.Fragment.palette",
|
2014-01-28 03:31:27 +00:00
|
|
|
"QuakeForge.Fragment.sprite",
|
|
|
|
0
|
|
|
|
};
|
2011-12-30 12:11:59 +00:00
|
|
|
|
|
|
|
//static float proj_matrix[16];
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int program;
|
|
|
|
shaderparam_t spritea;
|
|
|
|
shaderparam_t spriteb;
|
|
|
|
shaderparam_t palette;
|
|
|
|
shaderparam_t matrix;
|
|
|
|
shaderparam_t vertexa;
|
|
|
|
shaderparam_t vertexb;
|
|
|
|
shaderparam_t uvab;
|
|
|
|
shaderparam_t colora;
|
|
|
|
shaderparam_t colorb;
|
|
|
|
shaderparam_t blend;
|
2012-01-29 01:27:28 +00:00
|
|
|
shaderparam_t fog;
|
2011-12-30 12:11:59 +00:00
|
|
|
} quake_sprite = {
|
|
|
|
0,
|
|
|
|
{"spritea", 1},
|
|
|
|
{"spriteb", 1},
|
|
|
|
{"palette", 1},
|
|
|
|
{"mvp_mat", 1},
|
|
|
|
{"vertexa", 0},
|
|
|
|
{"vertexb", 0},
|
|
|
|
{"uvab", 0},
|
|
|
|
{"vcolora", 0},
|
|
|
|
{"vcolorb", 0},
|
|
|
|
{"vblend", 0},
|
2012-01-29 01:35:23 +00:00
|
|
|
{"fog", 1},
|
2011-12-30 12:11:59 +00:00
|
|
|
};
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-18 13:28:42 +00:00
|
|
|
void
|
2012-02-22 07:32:34 +00:00
|
|
|
glsl_R_InitSprites (void)
|
2011-12-30 12:11:59 +00:00
|
|
|
{
|
2014-01-28 03:31:27 +00:00
|
|
|
shader_t *vert_shader, *frag_shader;
|
2011-12-30 12:11:59 +00:00
|
|
|
int frag, vert;
|
|
|
|
|
2014-01-28 03:31:27 +00:00
|
|
|
vert_shader = GLSL_BuildShader (sprite_vert_effects);
|
|
|
|
frag_shader = GLSL_BuildShader (sprite_frag_effects);
|
|
|
|
vert = GLSL_CompileShader ("quakespr.vert", vert_shader,
|
2012-02-17 09:57:13 +00:00
|
|
|
GL_VERTEX_SHADER);
|
2014-01-28 03:31:27 +00:00
|
|
|
frag = GLSL_CompileShader ("quakespr.frag", frag_shader,
|
2012-02-17 09:57:13 +00:00
|
|
|
GL_FRAGMENT_SHADER);
|
|
|
|
quake_sprite.program = GLSL_LinkProgram ("quakespr", vert, frag);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.spritea);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.spriteb);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.palette);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.matrix);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.vertexa);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.vertexb);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.colora);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.colorb);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.uvab);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.blend);
|
|
|
|
GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.fog);
|
2014-01-28 03:31:27 +00:00
|
|
|
GLSL_FreeShader (vert_shader);
|
|
|
|
GLSL_FreeShader (frag_shader);
|
2011-12-30 12:11:59 +00:00
|
|
|
}
|
|
|
|
|
2011-12-31 05:38:03 +00:00
|
|
|
static void
|
2012-01-04 09:31:35 +00:00
|
|
|
R_GetSpriteFrames (entity_t *ent, msprite_t *sprite, mspriteframe_t **frame1,
|
2011-12-31 05:38:03 +00:00
|
|
|
mspriteframe_t **frame2, float *blend)
|
|
|
|
{
|
2021-07-22 06:39:28 +00:00
|
|
|
animation_t *animation = &ent->animation;
|
|
|
|
int framenum = animation->frame;
|
2012-01-04 09:31:35 +00:00
|
|
|
int pose;
|
2011-12-31 05:38:03 +00:00
|
|
|
int i, numframes;
|
|
|
|
float *intervals;
|
2012-01-04 09:31:35 +00:00
|
|
|
float frame_interval;
|
|
|
|
float fullinterval, targettime, time;
|
|
|
|
mspritegroup_t *group = 0;
|
|
|
|
mspriteframedesc_t *framedesc;
|
|
|
|
|
|
|
|
if (framenum >= sprite->numframes || framenum < 0)
|
|
|
|
framenum = 0;
|
|
|
|
|
|
|
|
framedesc = &sprite->frames[framenum];
|
|
|
|
if (framedesc->type == SPR_SINGLE) {
|
|
|
|
frame_interval = 0.1;
|
|
|
|
pose = framenum;
|
2011-12-31 05:38:03 +00:00
|
|
|
} else {
|
2021-12-11 08:05:40 +00:00
|
|
|
group = framedesc->group;
|
2012-01-04 09:31:35 +00:00
|
|
|
intervals = group->intervals;
|
|
|
|
numframes = group->numframes;
|
2011-12-31 05:38:03 +00:00
|
|
|
fullinterval = intervals[numframes - 1];
|
|
|
|
|
2021-07-22 06:39:28 +00:00
|
|
|
time = vr_data.realtime + animation->syncbase;
|
2011-12-31 05:38:03 +00:00
|
|
|
targettime = time - ((int) (time / fullinterval)) * fullinterval;
|
|
|
|
|
|
|
|
for (i = 0; i < numframes - 1; i++) {
|
|
|
|
if (intervals[i] > targettime)
|
|
|
|
break;
|
|
|
|
}
|
2012-01-04 09:31:35 +00:00
|
|
|
frame_interval = intervals[i];
|
|
|
|
if (i)
|
|
|
|
frame_interval = intervals[i - 1];
|
|
|
|
pose = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME this will break if the sprite changes between single frames and
|
|
|
|
//group frames.
|
2021-07-22 06:39:28 +00:00
|
|
|
*blend = R_EntityBlend (animation, pose, frame_interval);
|
2012-01-04 09:31:35 +00:00
|
|
|
if (group) {
|
2021-07-22 06:39:28 +00:00
|
|
|
*frame1 = group->frames[animation->pose1];
|
|
|
|
*frame2 = group->frames[animation->pose2];
|
2012-01-04 09:31:35 +00:00
|
|
|
} else {
|
2021-12-11 08:05:40 +00:00
|
|
|
*frame1 = sprite->frames[animation->pose1].frame;
|
|
|
|
*frame2 = sprite->frames[animation->pose2].frame;
|
2011-12-31 05:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-03-17 02:55:58 +00:00
|
|
|
make_quad (mspriteframe_t *frame, vec4f_t origin, vec4f_t sright, vec4f_t sup, float verts[6][3])
|
2011-12-31 05:38:03 +00:00
|
|
|
{
|
2021-03-10 12:17:34 +00:00
|
|
|
vec4f_t left, up, right, down;
|
|
|
|
vec4f_t ul, ur, ll, lr;
|
2011-12-31 05:38:03 +00:00
|
|
|
|
|
|
|
// build the sprite poster in worldspace
|
|
|
|
// first, rotate the sprite axes into world space
|
2022-03-17 02:55:58 +00:00
|
|
|
right = frame->right * sright;
|
|
|
|
up = frame->up * sup;
|
|
|
|
left = frame->left * sright;
|
|
|
|
down = frame->down * sup;
|
2011-12-31 05:38:03 +00:00
|
|
|
// next, build the sprite corners from the axes
|
2021-03-10 12:17:34 +00:00
|
|
|
ul = up + left;
|
|
|
|
ur = up + right;
|
|
|
|
ll = down + left;
|
|
|
|
lr = down + right;
|
2011-12-31 05:38:03 +00:00
|
|
|
// finally, translate the sprite corners, creating two triangles
|
2021-03-19 11:18:45 +00:00
|
|
|
VectorAdd (origin, ul, verts[0]); // first triangle
|
|
|
|
VectorAdd (origin, ur, verts[1]);
|
|
|
|
VectorAdd (origin, lr, verts[2]);
|
|
|
|
VectorAdd (origin, ul, verts[3]); // second triangle
|
|
|
|
VectorAdd (origin, lr, verts[4]);
|
|
|
|
VectorAdd (origin, ll, verts[5]);
|
2011-12-31 05:38:03 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:11:59 +00:00
|
|
|
void
|
2021-07-22 06:39:28 +00:00
|
|
|
glsl_R_DrawSprite (entity_t *ent)
|
2011-12-30 12:11:59 +00:00
|
|
|
{
|
2021-03-09 14:52:40 +00:00
|
|
|
msprite_t *sprite = (msprite_t *) ent->renderer.model->cache.data;
|
2011-12-31 05:38:03 +00:00
|
|
|
mspriteframe_t *frame1, *frame2;
|
2021-12-10 05:34:04 +00:00
|
|
|
float blend;
|
|
|
|
vec4f_t cameravec = {};
|
2022-03-17 02:55:58 +00:00
|
|
|
vec4f_t spn = {}, sright = {}, sup = {};
|
2011-12-31 05:38:03 +00:00
|
|
|
static quat_t color = { 1, 1, 1, 1};
|
|
|
|
float vertsa[6][3], vertsb[6][3];
|
|
|
|
static float uvab[6][4] = {
|
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 1, 0, 1, 0 },
|
|
|
|
{ 1, 1, 1, 1 },
|
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 1, 1, 1, 1 },
|
|
|
|
{ 0, 1, 0, 1 },
|
|
|
|
};
|
|
|
|
|
2021-12-10 05:34:04 +00:00
|
|
|
vec4f_t origin = Transform_GetWorldPosition (ent->transform);
|
[renderer] Clean up use of vup/vright/vpn
This moves the common camera setup code out of the individual drivers,
and completely removes vup/vright/vpn from the non-software renderers.
This has highlighted the craziness around AngleVectors with it putting
+X forward, -Y right and +Z up. The main issue with this is it requires
a 90 degree pre-rotation about the Z axis to get the camera pointing in
the right direction, and that's for the native sw renderer (vulkan needs
a 90 degree pre-rotation about X, and gl and glsl need to invert an
axis, too), though at least it's just a matrix swizzle and vector
negation. However, it does mean the camera matrices can't be used
directly.
Also rename vpn to vfwd (still abbreviated, but fwd is much clearer in
meaning (to me, at least) than pn (plane normal, I guess, but which
way?)).
2022-03-14 00:34:24 +00:00
|
|
|
cameravec = r_refdef.frame.position - origin;
|
2021-12-10 05:34:04 +00:00
|
|
|
|
2022-03-30 14:50:12 +00:00
|
|
|
if (!R_BillboardFrame (ent, sprite->type, cameravec, &sup, &sright, &spn)) {
|
2021-12-10 05:34:04 +00:00
|
|
|
// the orientation is undefined so can't draw the sprite
|
|
|
|
return;
|
2011-12-31 05:38:03 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 09:31:35 +00:00
|
|
|
R_GetSpriteFrames (ent, sprite, &frame1, &frame2, &blend);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 0);
|
|
|
|
qfeglBindTexture (GL_TEXTURE_2D, frame1->gl_texturenum);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfeglBindTexture (GL_TEXTURE_2D, frame2->gl_texturenum);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglVertexAttrib4fv (quake_sprite.colora.location, color);
|
|
|
|
qfeglVertexAttrib4fv (quake_sprite.colorb.location, color);
|
|
|
|
qfeglVertexAttrib1f (quake_sprite.blend.location, blend);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2022-03-17 02:55:58 +00:00
|
|
|
make_quad (frame1, origin, sright, sup, vertsa);
|
|
|
|
make_quad (frame2, origin, sright, sup, vertsb);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglVertexAttribPointer (quake_sprite.vertexa.location, 3, GL_FLOAT,
|
2011-12-31 05:38:03 +00:00
|
|
|
0, 0, vertsa);
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglVertexAttribPointer (quake_sprite.vertexb.location, 3, GL_FLOAT,
|
2011-12-31 05:38:03 +00:00
|
|
|
0, 0, vertsb);
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglVertexAttribPointer (quake_sprite.uvab.location, 4, GL_FLOAT,
|
2011-12-31 05:38:03 +00:00
|
|
|
0, 0, uvab);
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglDrawArrays (GL_TRIANGLES, 0, 6);
|
2011-12-30 12:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All sprites are drawn in a batch, so avoid thrashing the gl state
|
|
|
|
void
|
2022-03-14 05:31:23 +00:00
|
|
|
glsl_R_SpriteBegin (void)
|
2011-12-30 12:11:59 +00:00
|
|
|
{
|
2021-03-09 14:52:40 +00:00
|
|
|
mat4f_t mat;
|
2012-01-29 01:27:28 +00:00
|
|
|
quat_t fog;
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglUseProgram (quake_sprite.program);
|
|
|
|
qfeglEnableVertexAttribArray (quake_sprite.vertexa.location);
|
|
|
|
qfeglEnableVertexAttribArray (quake_sprite.vertexb.location);
|
|
|
|
qfeglEnableVertexAttribArray (quake_sprite.uvab.location);
|
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.colora.location);
|
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.colorb.location);
|
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.blend.location);
|
2011-12-30 12:11:59 +00:00
|
|
|
|
2022-03-07 17:10:47 +00:00
|
|
|
Fog_GetColor (fog);
|
|
|
|
fog[3] = Fog_GetDensity () / 64.0;
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglUniform4fv (quake_sprite.fog.location, 1, fog);
|
2012-01-29 01:27:28 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglUniform1i (quake_sprite.spritea.location, 0);
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 0);
|
|
|
|
qfeglEnable (GL_TEXTURE_2D);
|
2011-12-30 12:11:59 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglUniform1i (quake_sprite.spriteb.location, 1);
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfeglEnable (GL_TEXTURE_2D);
|
2011-12-30 12:11:59 +00:00
|
|
|
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglUniform1i (quake_sprite.palette.location, 2);
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 2);
|
|
|
|
qfeglEnable (GL_TEXTURE_2D);
|
|
|
|
qfeglBindTexture (GL_TEXTURE_2D, glsl_palette);
|
2011-12-31 05:38:03 +00:00
|
|
|
|
2021-03-09 14:52:40 +00:00
|
|
|
mmulf (mat, glsl_projection, glsl_view);
|
2022-03-30 15:07:20 +00:00
|
|
|
qfeglUniformMatrix4fv (quake_sprite.matrix.location, 1, false, (vec_t*)&mat[0]);//FIXME
|
2011-12-30 12:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-03-14 05:31:23 +00:00
|
|
|
glsl_R_SpriteEnd (void)
|
2011-12-30 12:11:59 +00:00
|
|
|
{
|
2012-02-22 07:48:57 +00:00
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.vertexa.location);
|
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.vertexb.location);
|
|
|
|
qfeglDisableVertexAttribArray (quake_sprite.uvab.location);
|
|
|
|
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 0);
|
|
|
|
qfeglDisable (GL_TEXTURE_2D);
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfeglDisable (GL_TEXTURE_2D);
|
|
|
|
qfeglActiveTexture (GL_TEXTURE0 + 2);
|
|
|
|
qfeglDisable (GL_TEXTURE_2D);
|
2011-12-30 12:11:59 +00:00
|
|
|
}
|