From e9098440c58487c37f50420c0dbe83eb846bcc0d Mon Sep 17 00:00:00 2001 From: havoc Date: Thu, 12 Aug 2004 23:31:48 +0000 Subject: [PATCH] now checks tangent vector crossproduct against surface normal to see if the tangents came out backwards and flips them if so, this fixed all the nasty bumpmapping problems git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@4331 d7cf8633-e32d-0410-b094-e92efae38249 --- model_shared.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/model_shared.c b/model_shared.c index 369802d5..1fe8aa3a 100644 --- a/model_shared.c +++ b/model_shared.c @@ -608,7 +608,7 @@ void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, c void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, const float *tc0, const float *tc1, const float *tc2, float *svector3f, float *tvector3f, float *normal3f) { - float f; + float f, tangentcross[3]; normal3f[0] = (v1[1] - v0[1]) * (v2[2] - v0[2]) - (v1[2] - v0[2]) * (v2[1] - v0[1]); normal3f[1] = (v1[2] - v0[2]) * (v2[0] - v0[0]) - (v1[0] - v0[0]) * (v2[2] - v0[2]); normal3f[2] = (v1[0] - v0[0]) * (v2[1] - v0[1]) - (v1[1] - v0[1]) * (v2[0] - v0[0]); @@ -626,6 +626,13 @@ void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, con f = -DotProduct(svector3f, normal3f); VectorMA(svector3f, f, normal3f, svector3f); VectorNormalize(svector3f); + CrossProduct(svector3f, tvector3f, tangentcross); + // if texture is mapped the wrong way (counterclockwise), the tangents have to be flipped, this is detected by calculating a normal from the two tangents, and seeing if it is opposite the surface normal + if (DotProduct(tangentcross, normal3f) < 0) + { + VectorNegate(svector3f, svector3f); + VectorNegate(tvector3f, tvector3f); + } } // warning: this is an expensive function! -- 2.39.2