# Custom mesh and tangents calculation

Hi everybody,
I have little project going on and I am creating a custom mesh. I recently started with all the lighting and texturing stuff to make it look nice. My first approach was to use the “Common/MatDefs/Light/Lighting.j3md” material without any additions and everything worked fine. Than I tried to use a normal map what in most cases is working fine, too. But in some cases I get this warning:
WARNING: Normal and tangent are parallel for vertex 1.

After some research and tryouts I think I understand that message but anyways I don’t understand why it is not working for my mesh. Look at theses screenshots:

I am reusing vertices so in total the mesh has 17 verts and normals (pink arrows) and 20 triangles. The inner normals go into positive z direction. As you can see the left and right corners are black (thats the vertices for which I get the warnings) while the top and bottom corners are rendered perfectly correct. Now my question is: WHY? I mean they are equivalent, the only difference is they are rotated 90° but how can that effect the tangent calculation the way that they are parallel to the normal? It seems to just appear with normals in +/- x direction.

I hope someone could tell me a little more about these tangents like how they are calculated or what besides the vertex and normal is used to calculate them. Actually I wonder if I can calculate them manually as I am creating the vertices, normals and indexes manually anyways and what is the absolute minimum required to calculate the normals the way that a normal map works. Is there something like a tangent buffer? And also it might just feel like that but I think this TangentBinormalGenerator takes longer than it actually has to. Considering that afaik calculating a tangent to a normal is not that big of an issue. And I kind of wonder that my algorithm creating the geometry through lots of calculations and lookups is way faster then generating just a few tangents for it afterwards.

Hopefully you guys can enlighten me. I would really like to understand how all that is working.

greetz Xeratos

Hmm the pictures are maybe a little small here they are a little bigger:

And just for the record this is my light:
sunLight = new DirectionalLight();
sunLight.setColor(ColorRGBA.Gray);
sunLight.setDirection(new Vector3f(.5f, -1f, 0));

When one normal of a triangle points straight up and another points 90 degrees from that one then it is tough to automatically calculate a tangent vector, I guess.

You may want to set the tangents yourself as you probably have a better idea of what they should be than the algorithm will.

Thanks for your answer. Unfortunately it didn’t really help me but telling me to calculate them myself, what indeed sounds reasonable the more I read about that. My problem was that I didn’t have a visual idea of how the tangents should look like in relation to the normals and what their actual purpose is other then it is for calculating some lighting related stuff. I mean thats pretty obvious as the mesh won’t be lit if you don’t calcualte them but isn’t quite an answer to what they are doing explicitly.
Anyways I have found this nice video giving a nice visualisation of the local/directional relation between tangents, normals and binormals and how to calculate them:
Calculating Tangents, Normals, Binormals

Maybe this video can also help others to get an idea of this. For me it was really helpful as it shows that the actual calculation is not that big magic and also that, considering my algorithm, it is most likely to be possible to use lookup tables which would be very nice in terms of performance.

Now the only thing I couldn’t find out so far is what the fourth component of the tangent is doing. On my first tests I couldn’t see any difference so far whether it is 1 or -1. Maybe someone can give me hint on that.

A negative 1 fourth component will invert the calculated binormal. For some geometry this is necessary I guess but probably not in your case.

@pspeed
Hey thanks for your help, for me this topic is solved.

Last question can I mark it as solved myself or does an admin has to do it? If so please do so.

Greetz

To finish this here are some results generated from a simple test heightmap:

260 Vertices, 360 Triangles, 19ms to generate
Normals red, tangents green, binormals blue