From 6479b41be7d7634a02be965269a094523dfd394d Mon Sep 17 00:00:00 2001 From: Sajt Date: Sun, 21 Jan 2024 23:59:06 +0200 Subject: [PATCH] models: Add normals convert code Based on https://icculus.org/projects/qshed/ --- src/client/refresh/files/mesh.c | 32 ++++++++++++++++++++++++++++++-- src/client/refresh/ref_shared.h | 1 + 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/client/refresh/files/mesh.c b/src/client/refresh/files/mesh.c index 8aa461fc..6430aade 100644 --- a/src/client/refresh/files/mesh.c +++ b/src/client/refresh/files/mesh.c @@ -26,10 +26,38 @@ #include "../ref_shared.h" -static float r_avertexnormals[NUMVERTEXNORMALS][3] = { +static const float r_avertexnormals[NUMVERTEXNORMALS][3] = { #include "../constants/anorms.h" }; +/* compressed vertex normals used by mdl and md2 model formats */ +byte +R_CompressNormalMDL(const float *normal) +{ + byte i, besti; + float dot, bestdot; + + bestdot = normal[0] * r_avertexnormals[0][0] + + normal[1] * r_avertexnormals[0][1] + + normal[2] * r_avertexnormals[0][2]; + besti = 0; + + for (i = 1; i < NUMVERTEXNORMALS; i++) + { + dot = normal[0] * r_avertexnormals[i][0] + + normal[1] * r_avertexnormals[i][1] + + normal[2] * r_avertexnormals[i][2]; + + if (dot > bestdot) + { + bestdot = dot; + besti = i; + } + } + + return besti; +} + void R_LerpVerts(qboolean powerUpEffect, int nverts, dxtrivertx_t *v, dxtrivertx_t *ov, dxtrivertx_t *verts, float *lerp, const float move[3], @@ -41,7 +69,7 @@ R_LerpVerts(qboolean powerUpEffect, int nverts, dxtrivertx_t *v, dxtrivertx_t *o { for (i = 0; i < nverts; i++, v++, ov++, lerp += 4) { - float *normal = r_avertexnormals[verts[i].lightnormalindex]; + const float *normal = r_avertexnormals[verts[i].lightnormalindex]; lerp[0] = move[0] + ov->v[0] * backv[0] + v->v[0] * frontv[0] + normal[0] * POWERSUIT_SCALE; diff --git a/src/client/refresh/ref_shared.h b/src/client/refresh/ref_shared.h index 7035b54d..ada9c105 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -382,6 +382,7 @@ extern qboolean R_CullAliasMeshModel(dmdx_t *paliashdr, cplane_t *frustum, extern void R_LerpVerts(qboolean powerUpEffect, int nverts, dxtrivertx_t *v, dxtrivertx_t *ov, dxtrivertx_t *verts, float *lerp, const float move[3], const float frontv[3], const float backv[3]); +extern byte R_CompressNormalMDL(const float *normal); /* Lights logic */ extern bspxlightgrid_t *Mod_LoadBSPXLightGrid(const bspx_header_t *bspx_header, const byte *mod_base);