From a57835866abcca5f1aa0b283852dd72366a455ca Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 2 Jan 2012 13:15:51 +0900 Subject: [PATCH] Build the vertex normals texture. --- libs/video/renderer/glsl/glsl_alias.c | 35 ++++++++++++++++++++++++++ libs/video/renderer/glsl/quakemdl.vert | 6 ++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/libs/video/renderer/glsl/glsl_alias.c b/libs/video/renderer/glsl/glsl_alias.c index e9b74bd22..4ce6b5444 100644 --- a/libs/video/renderer/glsl/glsl_alias.c +++ b/libs/video/renderer/glsl/glsl_alias.c @@ -39,12 +39,20 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$"; #ifdef HAVE_STRINGS_H # include #endif +#include #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); diff --git a/libs/video/renderer/glsl/quakemdl.vert b/libs/video/renderer/glsl/quakemdl.vert index 4d983baf0..32ec32d5a 100644 --- a/libs/video/renderer/glsl/quakemdl.vert +++ b/libs/video/renderer/glsl/quakemdl.vert @@ -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; }