2012-01-01 14:58:12 +00:00
|
|
|
/*
|
|
|
|
glsl_alias.c
|
|
|
|
|
|
|
|
GLSL Alias model rendering
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/1/1
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
static __attribute__ ((used)) const char rcsid[] = "$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2012-01-02 04:15:51 +00:00
|
|
|
#include <stdlib.h>
|
2012-01-01 14:58:12 +00:00
|
|
|
|
2012-01-02 10:15:01 +00:00
|
|
|
#include "QF/render.h"
|
2012-01-22 06:48:32 +00:00
|
|
|
#include "QF/skin.h"
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-01 14:58:12 +00:00
|
|
|
#include "QF/GLSL/defines.h"
|
|
|
|
#include "QF/GLSL/funcs.h"
|
|
|
|
#include "QF/GLSL/qf_alias.h"
|
2012-01-02 04:15:51 +00:00
|
|
|
#include "QF/GLSL/qf_textures.h"
|
2012-01-02 02:18:34 +00:00
|
|
|
#include "QF/GLSL/qf_vid.h"
|
|
|
|
|
2012-01-02 10:15:01 +00:00
|
|
|
#include "r_local.h"
|
2012-01-02 04:15:51 +00:00
|
|
|
|
2012-01-02 02:18:34 +00:00
|
|
|
static const char quakemdl_vert[] =
|
|
|
|
#include "quakemdl.vc"
|
|
|
|
;
|
|
|
|
|
|
|
|
static const char quakemdl_frag[] =
|
|
|
|
#include "quakemdl.fc"
|
|
|
|
;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int program;
|
|
|
|
shaderparam_t mvp_matrix;
|
|
|
|
shaderparam_t norm_matrix;
|
2012-01-02 04:41:12 +00:00
|
|
|
shaderparam_t skin_size;
|
2012-01-04 05:35:34 +00:00
|
|
|
shaderparam_t blend;
|
|
|
|
shaderparam_t colora;
|
|
|
|
shaderparam_t colorb;
|
|
|
|
shaderparam_t sta;
|
|
|
|
shaderparam_t stb;
|
|
|
|
shaderparam_t normala;
|
|
|
|
shaderparam_t normalb;
|
|
|
|
shaderparam_t vertexa;
|
|
|
|
shaderparam_t vertexb;
|
2012-01-02 02:18:34 +00:00
|
|
|
shaderparam_t colormap;
|
|
|
|
shaderparam_t skin;
|
|
|
|
shaderparam_t ambient;
|
|
|
|
shaderparam_t shadelight;
|
|
|
|
shaderparam_t lightvec;
|
2012-01-29 01:27:28 +00:00
|
|
|
shaderparam_t fog;
|
2012-01-02 02:18:34 +00:00
|
|
|
} quake_mdl = {
|
|
|
|
0,
|
|
|
|
{"mvp_mat", 1},
|
|
|
|
{"norm_mat", 1},
|
2012-01-02 04:41:12 +00:00
|
|
|
{"skin_size", 1},
|
2012-01-04 05:35:34 +00:00
|
|
|
{"blend", 1},
|
|
|
|
{"vcolora", 0},
|
|
|
|
{"vcolorb", 0},
|
|
|
|
{"vsta", 0},
|
|
|
|
{"vstb", 0},
|
|
|
|
{"vnormala", 0},
|
|
|
|
{"vnormalb", 0},
|
|
|
|
{"vertexa", 0},
|
|
|
|
{"vertexb", 0},
|
2012-01-02 02:18:34 +00:00
|
|
|
{"colormap", 1},
|
|
|
|
{"skin", 1},
|
|
|
|
{"ambient", 1},
|
|
|
|
{"shadelight", 1},
|
|
|
|
{"lightvec", 1},
|
2012-01-29 01:27:28 +00:00
|
|
|
{"fog", 1},
|
2012-01-02 02:18:34 +00:00
|
|
|
};
|
2012-01-01 14:58:12 +00:00
|
|
|
|
2012-01-03 09:09:33 +00:00
|
|
|
static mat4_t alias_vp;
|
2012-01-02 04:15:51 +00:00
|
|
|
|
2012-01-01 14:58:12 +00:00
|
|
|
VISIBLE void
|
|
|
|
R_InitAlias (void)
|
|
|
|
{
|
2012-01-02 02:18:34 +00:00
|
|
|
int vert;
|
|
|
|
int frag;
|
2012-01-02 04:15:51 +00:00
|
|
|
|
2012-01-02 02:18:34 +00:00
|
|
|
vert = GL_CompileShader ("quakemdl.vert", quakemdl_vert, GL_VERTEX_SHADER);
|
|
|
|
frag = GL_CompileShader ("quakemdl.frag", quakemdl_frag,
|
|
|
|
GL_FRAGMENT_SHADER);
|
|
|
|
quake_mdl.program = GL_LinkProgram ("quakemdl", vert, frag);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.mvp_matrix);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.norm_matrix);
|
2012-01-02 04:41:12 +00:00
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.skin_size);
|
2012-01-04 05:35:34 +00:00
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.blend);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.colora);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.colorb);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.sta);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.stb);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.normala);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.normalb);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.vertexa);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.vertexb);
|
2012-01-02 02:18:34 +00:00
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.colormap);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.skin);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.ambient);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.shadelight);
|
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.lightvec);
|
2012-01-29 01:27:28 +00:00
|
|
|
GL_ResolveShaderParam (quake_mdl.program, &quake_mdl.fog);
|
2012-01-01 14:58:12 +00:00
|
|
|
}
|
2012-01-03 11:36:07 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
calc_lighting (entity_t *ent, float *ambient, float *shadelight,
|
|
|
|
vec3_t lightvec)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
float add;
|
|
|
|
vec3_t dist;
|
|
|
|
|
|
|
|
VectorSet ( -1, 0, 0, lightvec); //FIXME
|
|
|
|
*ambient = max (R_LightPoint (ent->origin), max (ent->model->min_light,
|
|
|
|
ent->min_light) * 128);
|
|
|
|
*shadelight = *ambient;
|
|
|
|
|
|
|
|
for (i = 0; i < r_maxdlights; i++) {
|
|
|
|
if (r_dlights[i].die >= r_realtime) {
|
|
|
|
VectorSubtract (ent->origin, r_dlights[i].origin, dist);
|
|
|
|
add = r_dlights[i].radius - VectorLength (dist);
|
|
|
|
if (add > 0)
|
|
|
|
*ambient += add;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*ambient >= 128)
|
|
|
|
*ambient = 128;
|
|
|
|
if (*shadelight > 192 - *ambient)
|
|
|
|
*shadelight = 192 - *ambient;
|
|
|
|
}
|
2012-01-04 05:35:34 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_arrays (const shaderparam_t *vert, const shaderparam_t *norm,
|
|
|
|
const shaderparam_t *st, aliasvrt_t *pose)
|
|
|
|
{
|
|
|
|
byte *pose_offs = (byte *) pose;
|
2012-01-05 03:48:15 +00:00
|
|
|
qfglVertexAttribPointer (vert->location, 3, GL_UNSIGNED_SHORT,
|
2012-01-04 05:35:34 +00:00
|
|
|
0, sizeof (aliasvrt_t),
|
|
|
|
pose_offs + field_offset (aliasvrt_t, vertex));
|
|
|
|
qfglVertexAttribPointer (norm->location, 3, GL_SHORT,
|
2012-01-05 03:45:43 +00:00
|
|
|
1, sizeof (aliasvrt_t),
|
2012-01-04 05:35:34 +00:00
|
|
|
pose_offs + field_offset (aliasvrt_t, normal));
|
|
|
|
qfglVertexAttribPointer (st->location, 2, GL_SHORT,
|
|
|
|
0, sizeof (aliasvrt_t),
|
|
|
|
pose_offs + field_offset (aliasvrt_t, st));
|
|
|
|
}
|
2012-01-03 09:09:33 +00:00
|
|
|
//#define TETRAHEDRON
|
2012-01-02 10:15:01 +00:00
|
|
|
void
|
|
|
|
R_DrawAlias (void)
|
|
|
|
{
|
2012-01-02 14:32:30 +00:00
|
|
|
#ifdef TETRAHEDRON
|
|
|
|
static aliasvrt_t debug_verts[] = {
|
2012-01-04 00:42:00 +00:00
|
|
|
{{ 0, 0}, {-18918,-18918,-18918}, { 0, 0, 0}},
|
|
|
|
{{ 0,300}, { 18918, 18918,-18918}, {255,255, 0}},
|
|
|
|
{{300,300}, {-18918, 18918, 18918}, { 0,255,255}},
|
|
|
|
{{300, 0}, { 18918,-18918, 18918}, {255, 0,255}},
|
2012-01-02 14:32:30 +00:00
|
|
|
};
|
|
|
|
static GLushort debug_indices[] = {
|
|
|
|
0, 1, 2,
|
|
|
|
0, 3, 1,
|
|
|
|
1, 3, 2,
|
|
|
|
0, 2, 3,
|
|
|
|
};
|
|
|
|
#endif
|
2012-01-02 10:15:01 +00:00
|
|
|
static quat_t color = { 1, 1, 1, 1};
|
2012-01-03 11:36:07 +00:00
|
|
|
static vec3_t lightvec;
|
|
|
|
float ambient;
|
|
|
|
float shadelight;
|
2012-01-02 10:15:01 +00:00
|
|
|
float skin_size[2];
|
2012-01-04 07:36:43 +00:00
|
|
|
float blend;
|
2012-01-02 10:15:01 +00:00
|
|
|
entity_t *ent = currententity;
|
|
|
|
model_t *model = ent->model;
|
|
|
|
aliashdr_t *hdr;
|
|
|
|
vec_t norm_mat[9];
|
|
|
|
mat4_t mvp_mat;
|
2012-01-22 06:48:32 +00:00
|
|
|
int skin_tex;
|
2012-01-23 10:24:12 +00:00
|
|
|
int colormap;
|
2012-01-04 07:36:43 +00:00
|
|
|
aliasvrt_t *pose1 = 0; // VBO's are null based
|
|
|
|
aliasvrt_t *pose2 = 0; // VBO's are null based
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-17 02:44:28 +00:00
|
|
|
if (!(hdr = model->aliashdr))
|
|
|
|
hdr = Cache_Get (&model->cache);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-03 11:36:07 +00:00
|
|
|
calc_lighting (ent, &ambient, &shadelight, lightvec);
|
|
|
|
|
2012-01-02 10:15:01 +00:00
|
|
|
// we need only the rotation for normals.
|
|
|
|
VectorCopy (ent->transform + 0, norm_mat + 0);
|
|
|
|
VectorCopy (ent->transform + 4, norm_mat + 3);
|
|
|
|
VectorCopy (ent->transform + 8, norm_mat + 6);
|
|
|
|
|
|
|
|
// ent model scaling and offset
|
|
|
|
Mat4Zero (mvp_mat);
|
|
|
|
mvp_mat[0] = hdr->mdl.scale[0];
|
|
|
|
mvp_mat[5] = hdr->mdl.scale[1];
|
|
|
|
mvp_mat[10] = hdr->mdl.scale[2];
|
|
|
|
mvp_mat[15] = 1;
|
|
|
|
VectorCopy (hdr->mdl.scale_origin, mvp_mat + 12);
|
|
|
|
Mat4Mult (ent->transform, mvp_mat, mvp_mat);
|
2012-01-03 09:09:33 +00:00
|
|
|
Mat4Mult (alias_vp, mvp_mat, mvp_mat);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-23 10:24:12 +00:00
|
|
|
colormap = glsl_colormap;
|
|
|
|
if (ent->skin && ent->skin->auxtex)
|
|
|
|
colormap = ent->skin->auxtex;
|
|
|
|
if (ent->skin && ent->skin->texnum) {
|
2012-01-22 06:48:32 +00:00
|
|
|
skin_t *skin = ent->skin;
|
2012-01-23 07:16:30 +00:00
|
|
|
skin_tex = skin->texnum;
|
2012-01-22 06:48:32 +00:00
|
|
|
} else {
|
|
|
|
maliasskindesc_t *skindesc;
|
|
|
|
skindesc = R_AliasGetSkindesc (ent->skinnum, hdr);
|
|
|
|
skin_tex = skindesc->texnum;
|
|
|
|
}
|
2012-01-04 07:36:43 +00:00
|
|
|
blend = R_AliasGetLerpedFrames (ent, hdr);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-04 07:36:43 +00:00
|
|
|
pose1 += ent->pose1 * hdr->poseverts;
|
|
|
|
pose2 += ent->pose2 * hdr->poseverts;
|
2012-01-02 10:15:01 +00:00
|
|
|
|
|
|
|
skin_size[0] = hdr->mdl.skinwidth;
|
|
|
|
skin_size[1] = hdr->mdl.skinheight;
|
|
|
|
|
2012-01-23 10:24:12 +00:00
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfglBindTexture (GL_TEXTURE_2D, colormap);
|
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 0);
|
2012-01-22 06:48:32 +00:00
|
|
|
qfglBindTexture (GL_TEXTURE_2D, skin_tex);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-02 14:32:30 +00:00
|
|
|
#ifndef TETRAHEDRON
|
2012-01-02 10:15:01 +00:00
|
|
|
qfglBindBuffer (GL_ARRAY_BUFFER, hdr->posedata);
|
|
|
|
qfglBindBuffer (GL_ELEMENT_ARRAY_BUFFER, hdr->commands);
|
2012-01-02 14:32:30 +00:00
|
|
|
#endif
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-04 05:35:34 +00:00
|
|
|
qfglVertexAttrib4fv (quake_mdl.colora.location, color);
|
|
|
|
qfglVertexAttrib4fv (quake_mdl.colorb.location, color);
|
|
|
|
qfglUniform1f (quake_mdl.blend.location, blend);
|
2012-01-02 10:15:01 +00:00
|
|
|
qfglUniform1f (quake_mdl.ambient.location, ambient);
|
|
|
|
qfglUniform1f (quake_mdl.shadelight.location, shadelight);
|
|
|
|
qfglUniform3fv (quake_mdl.lightvec.location, 1, lightvec);
|
|
|
|
qfglUniform2fv (quake_mdl.skin_size.location, 1, skin_size);
|
2012-01-02 13:38:52 +00:00
|
|
|
qfglUniformMatrix4fv (quake_mdl.mvp_matrix.location, 1, false, mvp_mat);
|
|
|
|
qfglUniformMatrix3fv (quake_mdl.norm_matrix.location, 1, false, norm_mat);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-02 14:32:30 +00:00
|
|
|
#ifndef TETRAHEDRON
|
2012-01-04 07:36:43 +00:00
|
|
|
set_arrays (&quake_mdl.vertexa, &quake_mdl.normala, &quake_mdl.sta, pose1);
|
|
|
|
set_arrays (&quake_mdl.vertexb, &quake_mdl.normalb, &quake_mdl.stb, pose2);
|
2012-01-04 05:35:34 +00:00
|
|
|
qfglDrawElements (GL_TRIANGLES, 3 * hdr->mdl.numtris,
|
|
|
|
GL_UNSIGNED_SHORT, 0);
|
2012-01-02 14:32:30 +00:00
|
|
|
#else
|
2012-01-04 05:35:34 +00:00
|
|
|
set_arrays (&quake_mdl.vertexa, &quake_mdl.normala, &quake_mdl.sta,
|
|
|
|
debug_verts);
|
|
|
|
set_arrays (&quake_mdl.vertexb, &quake_mdl.normalb, &quake_mdl.stb,
|
|
|
|
debug_verts);
|
2012-01-02 14:32:30 +00:00
|
|
|
qfglDrawElements (GL_TRIANGLES,
|
|
|
|
sizeof (debug_indices) / sizeof (debug_indices[0]),
|
|
|
|
GL_UNSIGNED_SHORT, debug_indices);
|
|
|
|
#endif
|
2012-01-17 02:44:28 +00:00
|
|
|
if (!model->aliashdr)
|
|
|
|
Cache_Release (&model->cache);
|
2012-01-02 10:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All alias models are drawn in a batch, so avoid thrashing the gl state
|
|
|
|
void
|
|
|
|
R_AliasBegin (void)
|
|
|
|
{
|
2012-01-29 01:27:28 +00:00
|
|
|
quat_t fog;
|
|
|
|
|
2012-01-02 10:15:01 +00:00
|
|
|
// pre-multiply the view and projection matricies
|
2012-01-03 09:09:33 +00:00
|
|
|
Mat4Mult (glsl_projection, glsl_view, alias_vp);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
|
|
|
qfglUseProgram (quake_mdl.program);
|
2012-01-04 05:35:34 +00:00
|
|
|
qfglEnableVertexAttribArray (quake_mdl.vertexa.location);
|
|
|
|
qfglEnableVertexAttribArray (quake_mdl.vertexb.location);
|
|
|
|
qfglEnableVertexAttribArray (quake_mdl.normala.location);
|
|
|
|
qfglEnableVertexAttribArray (quake_mdl.normalb.location);
|
|
|
|
qfglEnableVertexAttribArray (quake_mdl.sta.location);
|
|
|
|
qfglEnableVertexAttribArray (quake_mdl.stb.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.colora.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.colorb.location);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
2012-01-29 01:27:28 +00:00
|
|
|
VectorCopy (Fog_GetColor (), fog);
|
|
|
|
fog[3] = Fog_GetDensity () / 64.0;
|
|
|
|
qfglUniform4fv (quake_mdl.fog.location, 1, fog);
|
|
|
|
|
2012-01-04 00:42:00 +00:00
|
|
|
qfglUniform1i (quake_mdl.colormap.location, 1);
|
2012-01-02 10:15:01 +00:00
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfglEnable (GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
qfglUniform1i (quake_mdl.skin.location, 0);
|
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 0);
|
|
|
|
qfglEnable (GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
R_AliasEnd (void)
|
|
|
|
{
|
|
|
|
qfglBindBuffer (GL_ARRAY_BUFFER, 0);
|
|
|
|
qfglBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
2012-01-04 05:35:34 +00:00
|
|
|
qfglDisableVertexAttribArray (quake_mdl.vertexa.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.vertexb.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.normala.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.normalb.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.sta.location);
|
|
|
|
qfglDisableVertexAttribArray (quake_mdl.stb.location);
|
2012-01-02 10:15:01 +00:00
|
|
|
|
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 0);
|
|
|
|
qfglDisable (GL_TEXTURE_2D);
|
|
|
|
qfglActiveTexture (GL_TEXTURE0 + 1);
|
|
|
|
qfglDisable (GL_TEXTURE_2D);
|
|
|
|
}
|