DarthCoder

Hi,

I was just wondering, what is the easiest method of generating tangents for use in xna I recall there being a function in the mdx(or maybe dx C++) that could generate tangent based on a set of arbtiary vertex data. Does Xna have this

If I have to manually create them, are there any C# samples for doing this Even if it's not for xna, I can always port.



Re: XNA Framework Geneating Tangents?

Derek Nedelman

I have a working implementation in C# that you should be able to adapt with little trouble. It's part of my 3DS Viewer which you can find at http://www.threesixbox.com/project/ id=6c3eea10eb

Look in the Mesh.cs file (in the Viewer project) at the CalculateTangents() function. It's not commented, but the routine is fairly short.





Re: XNA Framework Geneating Tangents?

DarthCoder

Nice, thank you. Just checked out screenshots are it looks really nice, is the code public domain (Including the 3ds loader )





Re: XNA Framework Geneating Tangents?

DarthCoder

Ok newbie question, but i see your code literally just generates the tangent, which is the hard part, but how do I generate the bi-normal I take it it is based on the normal. And the normal is just the regular vertex normal

just to be clear

T = Tangent = Generated By your code.

B = Bi-Normal = Generated from Normal (N)

N = Normal = Obtained from vertex normal data.

Is that correct Any code in your engine to gen the bi-normal would hate to get it all right except that part.

Thanks.





Re: XNA Framework Geneating Tangents?

Derek Nedelman

The binormal is just the normalized cross product of the tangent and normal, which I calculate per pixel in the Viewer3DS\Shaders\Standard.fx effect file. Look for the StandardMaterialPixelShader. You'll see the binormal code near the very beginning

The 3DS importer code is in the public domain but the viewer code is under the LGPL. I really don't want that to prevent you or anybody else from using, modifying or building on it though, so if LGPL is a problem let me know and I'll change it up in whatever way you need.





Re: XNA Framework Geneating Tangents?

DarthCoder

Thanks again, i've just finished this morning integerating your tangent code, and it works great.  One thing though, i couldn't find your importer Load method, dyou remember which file it's in

 

btw some uv combos caused your tangent gen to produce all NAN tangents. caused by a devide by zero. i fixed it by checking for the problerm and reseting the zero to 1. Is this a good idea or will it just produce incorrect tangents it appears to work. (The model in question was just a 12 tri cube, nothing complex)





Re: XNA Framework Geneating Tangents?

Derek Nedelman

There are a couple of Load() methods. You can easily jump around in the source code by using the F12 key (go to definition).

As for the tangent problem, I couldn't really say whether setting the 0 to 1 is a permanent fix, but if it looks right then I guess it's good enough for now. I've never noticed the problem but I'll take a look and see if I can duplicate/fix it.