Jump to content

  • Log In with Google      Sign In   
  • Create Account

Texture coordinate scaling + TBN calculation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 irreversible   Crossbones+   -  Reputation: 1346

Like
0Likes
Like

Posted 20 January 2013 - 07:49 AM

My TBN (tangent-bitangent-normal) calculation works correctly for planar texture mapping and axis-aligned box mapping, including arbitrary rotations applied via a texture matrix. However, I'm getting an angular shift when I apply scaling to the texture coordinates and then try to calculate the TBN values.

 

My little brain can't seem to be able to chew through this - as best I can tell the distortion caused by scaling and box mapping (eg projection onto the primary plane) should be implicit in the coordinates themselves and should be handled naturally, but it isn't. The (currently per-face) TBN calculation is based on this and the code looks like this:

 

 
</p><div>face->vNormal = VecNormalize(VecCrossProduct(vertices[face->indexes[0]] - vertices[face->indexes[1]], vertices[face->indexes[1]] - vertices[face->indexes[2]]));</div>
<div> </div>
<div>IVector3D v0 = vertices[face->triindexes[0]];</div>
<div>IVector3D v1 = vertices[face->triindexes[1]];</div>
<div>IVector3D v2 = vertices[face->triindexes[2]];</div>
<div> </div>
<div>IUVCoords2D uv0, uv1, uv2;</div>
<div> </div>
<div>uv0 = face->uv0[0];</div>
<div>uv1 = face->uv0[1];</div>
<div>uv2 = face->uv0[2];</div>
<div> </div>
<div>TReal x1 = v1.x - v0.x;</div>
<div>TReal x2 = v2.x - v0.x;</div>
<div>TReal y1 = v1.y - v0.y;</div>
<div>TReal y2 = v2.y - v0.y;</div>
<div>TReal z1 = v1.z - v0.z;</div>
<div>TReal z2 = v2.z - v0.z;</div>
<div> </div>
<div>TReal s1 = uv1.u - uv0.u;</div>
<div>TReal s2 = uv2.u - uv0.u;</div>
<div>TReal t1 = uv1.v - uv0.v;</div>
<div>TReal t2 = uv2.v - uv0.v;</div>
<div> </div>
<div>TReal r = 1.0f / (s1 * t2 - s2 * t1);</div>
<div>IVector3D tan0((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);</div>
<div>IVector3D tan1((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);</div>
<div> </div>
<div>IVector3D n = face->vNormal;</div>
<div> </div>
<div>face->vTangent = (tan0 - n * VecDotProduct(n, tan0)).Normalize();</div>
<div> </div>
<div>//calculate handedness</div>
<div>face->vTangent.w = (VecDotProduct(VecCrossProduct(n, tan0), tan1) < 0.0f) ? -1.0f : 1.0f;</div>
<div>face->vBitangent = VecCrossProduct(face->vNormal.AsNormalized(), IVector3D(face->vTangent).AsNormalized());IEditablePolyFace* face = faces[i];</div>
<div>
 
Since the texture looks exactly the way it should, I'm assuming the fault lies with the TBN calculation. I've attached a screenshot taken at an oblique angle of a wall with planar UV mapping. The texture has been arbitrarily rotated. The left image has vertical scaling of 1.4, the right image is unscaled. Red = normal, blue = tangent, green = bitangent.
 
Any ideas what might be wrong?

 

 

Attached Thumbnails

  • tbn2.png


Sponsor:

#2 Eric Lengyel   Crossbones+   -  Reputation: 2324

Like
1Likes
Like

Posted 20 January 2013 - 08:00 PM

Are you scaling after the rotation, so as to cause a skew? This would cause the tangent and bitangent to no longer be perpendicular, so calculating the bitangent as the cross product between the normal and tangent won't quite work. Instead, if you calculate the bitangent in terms of tan1 just like you calculated the tangent in terms of tan0, you'll get the right vector. But then you can no longer assume that the inverse of the TBN matrix is just its transpose when you do your shading.



#3 irreversible   Crossbones+   -  Reputation: 1346

Like
0Likes
Like

Posted 20 January 2013 - 09:12 PM

Ah ! Yes, I was indeed scaling after rotation. Swapping the order fixes the problem. Can't believe the solution was so obvious :).

 

How would I go about conforming the box UV mapper (UV-s projected onto the geometry from an axis-aligned cube) to a stable TBN matrix, though? As best I can tell it's not possible as the projection introduces warping. If anything, I'm wondering how 3D programs like Maya and 3DS max handle this (or do they?).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS