Tangent and binormal for normal mapping

Hi!



I've just started fiddling around with jMonkey. So far, the engine has been pretty impressive.



I've run into a small speed bump however… I'm currently trying to add normal mapping to arbitrary models with the help of shaders and I need to add per vertex binormals and tangents to the models after I've loaded them with the help of any of the converters. With that in mind, I have two questions:


  1. Does any code that calculates binormals and tangents from a model with normals and uv-coordinates exist in the framework, or do I need to write it myself? The closest thing I've found in CVS is the class BumpMapColorController, but it does not seem to be intended for the normal mapping with shaders.


  2. How do send three-dimensional texcoords to the shader (binormals and tangents). If I understand theTestFragmentState program correctly, it uses method Geometry.setTextureBuffer method to add the binormals and tangents to the quad in the example. However, setTextureBuffer only sets two-dimensional texcoords, which is fine for a quad that is not rotated, but not applicable to an actual model. Is there a way to set a three-dimensional texcoord, or is it easier to just send two and get the third with a crossproduct in the shader?



    Great engine! Hopefully I will be able to contribute to it.



    Thanks in advance.

Hi!



2.

In case of GLSL shader (GLSLShaderObjectsState) you need to set attribute pointers to tangents and binormals buffers.

Such thing probably exists for vertex programs too. If not, then rearrange the 6 values into 3 texcoords. Or if you dont use vertex color, you can pass one coord in that.

That sounds pretty reasonable. Thanks for the help!

any sample-code here?

I ended up borrowing and slightly modifying the code in the function generateTangent in BumpMapColorController to generate tangents and binormals and then writing a shader in GLSL. It works very well, but requires reasonably new graphics hardware.



Try TyphoonLabs Shader Designer (free). It is an IDE for writing GLSL shaders that comes with a few samples that, among other things, include bump mapping. ATI's RenderMonkey also comes with similar samples. RenderMonkey is slightly more advanced than Shader Designer, but I prefer Shader Designer because it is less bloated.



Let me know if you need any more pointers and I'll try to help.

I'm using ATIs RenderMonkey as glsl-IDE. It work good for me.



Is BumpMapColorController in the cvs from jme's repository? I dont find a demo or test example to use it.

BumpMapColorController is in CVS as part of JME. It is used for bumpmapping without shaders, and is not directly applicable to shader based bumpmapping. However, there is a method in it that calculates binormals and tangents, which your bumpmapping shader will need, that you can more or less cut and paste into your own code.



If I remember correctly, my code pretty much does the following:


  1. Loads the model
  2. Creates a GLSLShaderState and attaches it to the model
  3. Calculates tangents for vertexes in the model with a modified version of the code from BumpMapController
  4. Stores tangents into a FloatBuffer (one tangent, or rather three floats, for every vertex)
  5. Attaches the tangents to the GLSLShaderState as an attributepointer
  6. Relinks the shaderstate



    Binormals are calculated in the shader by crossing the normal with the tangent. If you wish, you could do that in advance and attach another FloatBuffer to the shaderstate.



    Note that tangents and binormals will not be correct when your model is animated. For models animated with bones, the tangents and binormals can be rotated to their updated state in shaders, but with vertex animations you will either need to recalculate every frame or attempt to cheat somehow.

I understand.



Thx. :wink: