Build the vertex normals texture.

This commit is contained in:
Bill Currie 2012-01-02 13:15:51 +09:00
parent 6b4a10819a
commit a57835866a
2 changed files with 38 additions and 3 deletions

View File

@ -39,12 +39,20 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/GLSL/defines.h"
#include "QF/GLSL/funcs.h"
#include "QF/GLSL/qf_alias.h"
#include "QF/GLSL/qf_textures.h"
#include "QF/GLSL/qf_vid.h"
#include "r_shared.h"
static vec3_t vertex_normals[NUMVERTEXNORMALS] = {
#include "anorms.h"
};
static const char quakemdl_vert[] =
#include "quakemdl.vc"
;
@ -83,11 +91,38 @@ static struct {
{"lightvec", 1},
};
static int vnorms_tex;
static void
build_normals_texture (void)
{
vec3_t temp;
static const vec3_t one = { 1, 1, 1};
unsigned short norm[3];
int i, j;
byte *data;
data = malloc (NUMVERTEXNORMALS * 3 * 2);
for (i = 0; i < NUMVERTEXNORMALS; i++) {
VectorAdd (vertex_normals[i], one, temp); // temp is 0.0 .. 2.0
VectorScale (temp, 32767.5, norm); // norm is 0 .. 65535
for (j = 0; j < 3; j++) {
data[i * 6 + 0 + j] = norm[j] >> 8;
data[i * 6 + 3 + j] = norm[j] & 0xff;
}
}
vnorms_tex = GL_LoadRGBTexture ("vertex_normals", 2, NUMVERTEXNORMALS,
data);
free (data);
}
VISIBLE void
R_InitAlias (void)
{
int vert;
int frag;
build_normals_texture ();
vert = GL_CompileShader ("quakemdl.vert", quakemdl_vert, GL_VERTEX_SHADER);
frag = GL_CompileShader ("quakemdl.frag", quakemdl_frag,
GL_FRAGMENT_SHADER);

View File

@ -18,8 +18,8 @@ main (void)
gl_Position = mvp_mat * vec4 (vertex, 1.0);
st = stn.st;
nind = stn.p;
norma = texture2D (normals, vec2 (nind, 0.0)).xyz;
normb = texture2D (normals, vec2 (nind, 1.0)).xyz;
normal = norm_mat * (norma + normb / 256.0);
norma = texture2D (normals, vec2 (0.0, nind)).xyz;
normb = texture2D (normals, vec2 (1.0, nind)).xyz;
normal = norm_mat * (2.0 * norma + normb / 128.0 - vec3 (1.0, 1.0, 1.0));
color = vcolor;
}