boiler

05-02-2005, 09:24 AM

Hi all!

I have a problem to calculate the correct tangent vector. The result is looking very odd, please have look at the image where i have used the tangent vector as the color channel. You will notice the sharp vertical ploygon edges.

http://www.xyphur.de/Clipboard.png

Im using the following code to generate the tangents:

void CalculateTangentArray(long vertexCount, const Point3D *vertex, const Vector3D *normal, const Point2D *texcoord,

long triangleCount, const Triangle *triangle, Vector4D *tangent) const

{

Vector3D *tan1 = new Vector3D[vertexCount * 2];

Vector3D *tan2 = tan1 + vertexCount;

ClearMemory(tan1, vertexCount * sizeof(Vector3D) * 2);

for (long a = 0; a < triangleCount; a++)

{

long i1 = triangle->index[0];

long i2 = triangle->index[1];

long i3 = triangle->index[2];

const Point3D& v1 = vertex[i1];

const Point3D& v2 = vertex[i2];

const Point3D& v3 = vertex[i3];

const Point2D& w1 = texcoord[i1];

const Point2D& w2 = texcoord[i2];

const Point2D& w3 = texcoord[i3];

float x1 = v2.x - v1.x;

float x2 = v3.x - v1.x;

float y1 = v2.y - v1.y;

float y2 = v3.y - v1.y;

float z1 = v2.z - v1.z;

float z2 = v3.z - v1.z;

float s1 = w2.x - w1.x;

float s2 = w3.x - w1.x;

float t1 = w2.y - w1.y;

float t2 = w3.y - w1.y;

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

Vector3D sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);

Vector3D tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

tan1[i1] += sdir;

tan1[i2] += sdir;

tan1[i3] += sdir;

tan2[i1] += tdir;

tan2[i2] += tdir;

tan2[i3] += tdir;

triangle++;

}

for (long a = 0; a < vertexCount; a++)

{

const Vector3D& n = normal[a];

const Vector3D& t = tan1[a];

tangent[a] = (t - n * (n * t)).Normalize();

tangent[a].w = (n % t * tan2[a] < 0.0F) ? -1.0F : 1.0F;

}

delete[] tan1;Has anyone an idea whats wrong? :confused:

Thank you in advance!

Greetings Mario

I have a problem to calculate the correct tangent vector. The result is looking very odd, please have look at the image where i have used the tangent vector as the color channel. You will notice the sharp vertical ploygon edges.

http://www.xyphur.de/Clipboard.png

Im using the following code to generate the tangents:

void CalculateTangentArray(long vertexCount, const Point3D *vertex, const Vector3D *normal, const Point2D *texcoord,

long triangleCount, const Triangle *triangle, Vector4D *tangent) const

{

Vector3D *tan1 = new Vector3D[vertexCount * 2];

Vector3D *tan2 = tan1 + vertexCount;

ClearMemory(tan1, vertexCount * sizeof(Vector3D) * 2);

for (long a = 0; a < triangleCount; a++)

{

long i1 = triangle->index[0];

long i2 = triangle->index[1];

long i3 = triangle->index[2];

const Point3D& v1 = vertex[i1];

const Point3D& v2 = vertex[i2];

const Point3D& v3 = vertex[i3];

const Point2D& w1 = texcoord[i1];

const Point2D& w2 = texcoord[i2];

const Point2D& w3 = texcoord[i3];

float x1 = v2.x - v1.x;

float x2 = v3.x - v1.x;

float y1 = v2.y - v1.y;

float y2 = v3.y - v1.y;

float z1 = v2.z - v1.z;

float z2 = v3.z - v1.z;

float s1 = w2.x - w1.x;

float s2 = w3.x - w1.x;

float t1 = w2.y - w1.y;

float t2 = w3.y - w1.y;

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

Vector3D sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);

Vector3D tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

tan1[i1] += sdir;

tan1[i2] += sdir;

tan1[i3] += sdir;

tan2[i1] += tdir;

tan2[i2] += tdir;

tan2[i3] += tdir;

triangle++;

}

for (long a = 0; a < vertexCount; a++)

{

const Vector3D& n = normal[a];

const Vector3D& t = tan1[a];

tangent[a] = (t - n * (n * t)).Normalize();

tangent[a].w = (n % t * tan2[a] < 0.0F) ? -1.0F : 1.0F;

}

delete[] tan1;Has anyone an idea whats wrong? :confused:

Thank you in advance!

Greetings Mario