1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
data = new VMR_VERTEX[numVertices];
Vector3 *tan1 = new Vector3[numVertices * 2];
Vector3 *tan2 = tan1 + numVertices;
for (int i=0,j=0,k=0; i < Size_Vertex,j<Size_Texture ; i+=9,j+=6)
{
data[k ].position = &vertex_data[i];
data[k ].normal = &norm_data[i];
data[k ].s = text_data[j];
data[k ].t = text_data[j+1];
data[k+1].position = &vertex_data[i+3];
data[k+1].normal = &norm_data[i+3];
data[k+1].s = text_data[j+2];
data[k+1].t = text_data[j+3];
data[k+2].position = &vertex_data[i+6];
data[k+2].normal = &norm_data[i+6];
data[k+2].s = text_data[j+4];
data[k+2].t = text_data[j+5];
Vector3 v1,v2,v3;
Vector2 w1,w2,w3;
v1 = data[k].position;
v2 = data[k+1].position;
v2 = data[k+2].position;
w1 = Vector2(data[k].s , data[k].t);
w2 = Vector2(data[k+1].s , data[k+1].t);
w3 = Vector2(data[k+2].s , data[k+2].t);
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);
Vector3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
(t2 * z1 - t1 * z2) * r);
Vector3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
(s1 * z2 - s2 * z1) * r);
tan1[k ] += sdir;
tan1[k+1] += sdir;
tan1[k+2] += sdir;
tan2[k ] += tdir;
tan2[k+1] += tdir;
tan2[k+2] += tdir;
k = k + 3;
}
long count = numVertices;
for (long a = 0; a < count; a++)
{
const Vector3 & n = data[a].normal;
const Vector3 & t = tan1[a];
// Gram-Schmidt orthogonalize
Vector3 tmp = t - n * (n.DotProduct(t) );
data[a].sTangent = tmp.GetNormalized();
// Calculate handedness
data[a].tTangent = ((n.CrossProduct(t)).DotProduct(tan2[a]) < 0.0f) ? -1.0f : 1.0f;
}
delete[] tan1; |
Partager