Skip to content

Commit

Permalink
OpenGL2: Reimplement MD3 tangent space calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileTheory committed May 28, 2014
1 parent 347b429 commit 318d45c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
2 changes: 0 additions & 2 deletions code/renderergl2/tr_init.c
Expand Up @@ -143,7 +143,6 @@ cvar_t *r_baseNormalY;
cvar_t *r_baseParallax;
cvar_t *r_baseSpecular;
cvar_t *r_baseGloss;
cvar_t *r_recalcMD3Normals;
cvar_t *r_mergeLightmaps;
cvar_t *r_dlightMode;
cvar_t *r_pshadowDist;
Expand Down Expand Up @@ -1204,7 +1203,6 @@ void R_Register( void )
r_baseGloss = ri.Cvar_Get( "r_baseGloss", "0.3", CVAR_ARCHIVE | CVAR_LATCH );
r_dlightMode = ri.Cvar_Get( "r_dlightMode", "0", CVAR_ARCHIVE | CVAR_LATCH );
r_pshadowDist = ri.Cvar_Get( "r_pshadowDist", "128", CVAR_ARCHIVE );
r_recalcMD3Normals = ri.Cvar_Get( "r_recalcMD3Normals", "0", CVAR_ARCHIVE | CVAR_LATCH );
r_mergeLightmaps = ri.Cvar_Get( "r_mergeLightmaps", "1", CVAR_ARCHIVE | CVAR_LATCH );
r_imageUpsample = ri.Cvar_Get( "r_imageUpsample", "0", CVAR_ARCHIVE | CVAR_LATCH );
r_imageUpsampleMaxSize = ri.Cvar_Get( "r_imageUpsampleMaxSize", "1024", CVAR_ARCHIVE | CVAR_LATCH );
Expand Down
6 changes: 3 additions & 3 deletions code/renderergl2/tr_local.h
Expand Up @@ -1804,7 +1804,6 @@ extern cvar_t *r_baseSpecular;
extern cvar_t *r_baseGloss;
extern cvar_t *r_dlightMode;
extern cvar_t *r_pshadowDist;
extern cvar_t *r_recalcMD3Normals;
extern cvar_t *r_mergeLightmaps;
extern cvar_t *r_imageUpsample;
extern cvar_t *r_imageUpsampleMaxSize;
Expand Down Expand Up @@ -1865,8 +1864,9 @@ void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader,
void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader,
int fogIndex, int dlightMap, int pshadowMap, int cubemap );

void R_CalcTangentSpace(vec3_t tangent, vec3_t bitangent, vec3_t normal,
const vec3_t v0, const vec3_t v1, const vec3_t v2, const vec2_t t0, const vec2_t t1, const vec2_t t2);
void R_CalcTexDirs(vec3_t sdir, vec3_t tdir, const vec3_t v1, const vec3_t v2,
const vec3_t v3, const vec2_t w1, const vec2_t w2, const vec2_t w3);
void R_CalcTbnFromNormalAndTexDirs(vec3_t tangent, vec3_t bitangent, vec3_t normal, vec3_t sdir, vec3_t tdir);
qboolean R_CalcTangentVectors(srfVert_t * dv[3]);

#define CULL_IN 0 // completely unclipped
Expand Down
37 changes: 19 additions & 18 deletions code/renderergl2/tr_main.c
Expand Up @@ -294,13 +294,11 @@ void R_CalcTangentSpaceFast(vec3_t tangent, vec3_t bitangent, vec3_t normal,
/*
http://www.terathon.com/code/tangent.html
*/
void R_CalcTBN(vec3_t tangent, vec3_t bitangent, vec3_t normal,
const vec3_t v1, const vec3_t v2, const vec3_t v3, const vec2_t w1, const vec2_t w2, const vec2_t w3)
void R_CalcTexDirs(vec3_t sdir, vec3_t tdir, const vec3_t v1, const vec3_t v2,
const vec3_t v3, const vec2_t w1, const vec2_t w2, const vec2_t w3)
{
vec3_t u, v;
float x1, x2, y1, y2, z1, z2;
float s1, s2, t1, t2;
float r, dot;
float s1, s2, t1, t2, r;

x1 = v2[0] - v1[0];
x2 = v3[0] - v1[0];
Expand All @@ -316,24 +314,27 @@ void R_CalcTBN(vec3_t tangent, vec3_t bitangent, vec3_t normal,

r = 1.0f / (s1 * t2 - s2 * t1);

VectorSet(tangent, (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
VectorSet(bitangent, (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

// compute the face normal based on vertex points
VectorSubtract(v3, v1, u);
VectorSubtract(v2, v1, v);
CrossProduct(u, v, normal);
VectorSet(sdir, (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
VectorSet(tdir, (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
}

VectorNormalize(normal);
void R_CalcTbnFromNormalAndTexDirs(vec3_t tangent, vec3_t bitangent, vec3_t normal, vec3_t sdir, vec3_t tdir)
{
vec3_t n_cross_t;
vec_t n_dot_t, handedness;

// Gram-Schmidt orthogonalize
//tangent[a] = (t - n * Dot(n, t)).Normalize();
dot = DotProduct(normal, tangent);
VectorMA(tangent, -dot, normal, tangent);
n_dot_t = DotProduct(normal, sdir);
VectorMA(sdir, -n_dot_t, normal, tangent);
VectorNormalize(tangent);

// B=NxT
//CrossProduct(normal, tangent, bitangent);
// Calculate handedness
CrossProduct(normal, sdir, n_cross_t);
handedness = (DotProduct(n_cross_t, tdir) < 0.0f) ? -1.0f : 1.0f;

// Calculate bitangent
CrossProduct(normal, tangent, bitangent);
VectorScale(bitangent, handedness, bitangent);
}

void R_CalcTBN2(vec3_t tangent, vec3_t bitangent, vec3_t normal,
Expand Down
81 changes: 34 additions & 47 deletions code/renderergl2/tr_model.c
Expand Up @@ -372,7 +372,7 @@ R_LoadMD3
*/
static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, const char *modName)
{
int f, i, j, k;
int f, i, j;

md3Header_t *md3Model;
md3Frame_t *md3Frame;
Expand Down Expand Up @@ -598,71 +598,58 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize,
#ifdef USE_VERT_TANGENT_SPACE
// calc tangent spaces
{
// Valgrind complaints: Conditional jump or move depends on uninitialised value(s)
// So lets Initialize them.
const float *v0 = NULL, *v1 = NULL, *v2 = NULL;
const float *t0 = NULL, *t1 = NULL, *t2 = NULL;
vec3_t tangent = { 0, 0, 0 };
vec3_t bitangent = { 0, 0, 0 };
vec3_t normal = { 0, 0, 0 };
vec3_t *sdirs, *tdirs;

for(j = 0, v = surf->verts; j < (surf->numVerts * mdvModel->numFrames); j++, v++)
sdirs = ri.Malloc(sizeof(*sdirs) * surf->numVerts * mdvModel->numFrames);
tdirs = ri.Malloc(sizeof(*tdirs) * surf->numVerts * mdvModel->numFrames);

for (j = 0; j < (surf->numVerts * mdvModel->numFrames); j++)
{
VectorClear(v->tangent);
VectorClear(v->bitangent);
if (r_recalcMD3Normals->integer)
VectorClear(v->normal);
VectorSet(sdirs[j], 0.0f, 0.0f, 0.0f);
VectorSet(tdirs[j], 0.0f, 0.0f, 0.0f);
}

for(f = 0; f < mdvModel->numFrames; f++)
{
for(j = 0, tri = surf->indexes; j < surf->numIndexes; j += 3, tri += 3)
{
v0 = surf->verts[surf->numVerts * f + tri[0]].xyz;
v1 = surf->verts[surf->numVerts * f + tri[1]].xyz;
v2 = surf->verts[surf->numVerts * f + tri[2]].xyz;
vec3_t sdir, tdir;
const float *v0, *v1, *v2, *t0, *t1, *t2;
glIndex_t index0, index1, index2;

index0 = surf->numVerts * f + tri[0];
index1 = surf->numVerts * f + tri[1];
index2 = surf->numVerts * f + tri[2];

v0 = surf->verts[index0].xyz;
v1 = surf->verts[index1].xyz;
v2 = surf->verts[index2].xyz;

t0 = surf->st[tri[0]].st;
t1 = surf->st[tri[1]].st;
t2 = surf->st[tri[2]].st;

if (!r_recalcMD3Normals->integer)
VectorCopy(v->normal, normal);
else
VectorClear(normal);

#if 1
R_CalcTangentSpace(tangent, bitangent, normal, v0, v1, v2, t0, t1, t2);
#else
R_CalcNormalForTriangle(normal, v0, v1, v2);
R_CalcTangentsForTriangle(tangent, bitangent, v0, v1, v2, t0, t1, t2);
#endif

for(k = 0; k < 3; k++)
{
float *v;

v = surf->verts[surf->numVerts * f + tri[k]].tangent;
VectorAdd(v, tangent, v);

v = surf->verts[surf->numVerts * f + tri[k]].bitangent;
VectorAdd(v, bitangent, v);

if (r_recalcMD3Normals->integer)
{
v = surf->verts[surf->numVerts * f + tri[k]].normal;
VectorAdd(v, normal, v);
}
}
R_CalcTexDirs(sdir, tdir, v0, v1, v2, t0, t1, t2);

VectorAdd(sdir, sdirs[index0], sdirs[index0]);
VectorAdd(sdir, sdirs[index1], sdirs[index1]);
VectorAdd(sdir, sdirs[index2], sdirs[index2]);
VectorAdd(tdir, tdirs[index0], tdirs[index0]);
VectorAdd(tdir, tdirs[index1], tdirs[index1]);
VectorAdd(tdir, tdirs[index2], tdirs[index2]);
}
}

for(j = 0, v = surf->verts; j < (surf->numVerts * mdvModel->numFrames); j++, v++)
{
VectorNormalize(v->tangent);
VectorNormalize(v->bitangent);
VectorNormalize(v->normal);
VectorNormalize(sdirs[j]);
VectorNormalize(tdirs[j]);

R_CalcTbnFromNormalAndTexDirs(v->tangent, v->bitangent, v->normal, sdirs[j], tdirs[j]);
}

ri.Free(sdirs);
ri.Free(tdirs);
}
#endif

Expand Down

0 comments on commit 318d45c

Please sign in to comment.