Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2010-02-03 22:16:18 +0300
committerCampbell Barton <ideasman42@gmail.com>2010-02-03 22:16:18 +0300
commitf5901517259637a7e6fda4a0897f6c96741796d7 (patch)
tree326612f3fb4ee842adfa61fd0974d2647fab4f35 /source/blender/blenlib/intern/math_geom.c
parent6bdfa434310b4904725d2811a078e664807dcbd0 (diff)
avoid nan tangents which happen with cubes that have generated UVs
Diffstat (limited to 'source/blender/blenlib/intern/math_geom.c')
-rw-r--r--source/blender/blenlib/intern/math_geom.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c
index 8fb7a0e1a55..5abd32a45ba 100644
--- a/source/blender/blenlib/intern/math_geom.c
+++ b/source/blender/blenlib/intern/math_geom.c
@@ -1848,28 +1848,33 @@ float *find_vertex_tangent(VertexTangent *vtang, float *uv)
void tangent_from_uv(float *uv1, float *uv2, float *uv3, float *co1, float *co2, float *co3, float *n, float *tang)
{
- float tangv[3], ct[3], e1[3], e2[3], s1, t1, s2, t2, det;
-
- s1= uv2[0] - uv1[0];
- s2= uv3[0] - uv1[0];
- t1= uv2[1] - uv1[1];
- t2= uv3[1] - uv1[1];
- det= 1.0f / (s1 * t2 - s2 * t1);
-
- /* normals in render are inversed... */
- sub_v3_v3v3(e1, co1, co2);
- sub_v3_v3v3(e2, co1, co3);
- tang[0] = (t2*e1[0] - t1*e2[0])*det;
- tang[1] = (t2*e1[1] - t1*e2[1])*det;
- tang[2] = (t2*e1[2] - t1*e2[2])*det;
- tangv[0] = (s1*e2[0] - s2*e1[0])*det;
- tangv[1] = (s1*e2[1] - s2*e1[1])*det;
- tangv[2] = (s1*e2[2] - s2*e1[2])*det;
- cross_v3_v3v3(ct, tang, tangv);
-
- /* check flip */
- if ((ct[0]*n[0] + ct[1]*n[1] + ct[2]*n[2]) < 0.0f)
- negate_v3(tang);
+ float s1= uv2[0] - uv1[0];
+ float s2= uv3[0] - uv1[0];
+ float t1= uv2[1] - uv1[1];
+ float t2= uv3[1] - uv1[1];
+
+ if(s1 && s2 && t1 && t2) { /* otherwise 'tang' becomes nan */
+ float tangv[3], ct[3], e1[3], e2[3], det;
+ det= 1.0f / (s1 * t2 - s2 * t1);
+
+ /* normals in render are inversed... */
+ sub_v3_v3v3(e1, co1, co2);
+ sub_v3_v3v3(e2, co1, co3);
+ tang[0] = (t2*e1[0] - t1*e2[0])*det;
+ tang[1] = (t2*e1[1] - t1*e2[1])*det;
+ tang[2] = (t2*e1[2] - t1*e2[2])*det;
+ tangv[0] = (s1*e2[0] - s2*e1[0])*det;
+ tangv[1] = (s1*e2[1] - s2*e1[1])*det;
+ tangv[2] = (s1*e2[2] - s2*e1[2])*det;
+ cross_v3_v3v3(ct, tang, tangv);
+
+ /* check flip */
+ if ((ct[0]*n[0] + ct[1]*n[1] + ct[2]*n[2]) < 0.0f)
+ negate_v3(tang);
+ }
+ else {
+ tang[0]= tang[1]= tang[0]= 0.0;
+ }
}
/********************************************************/