# calculate binormal / tangeant at run time

hello

I’m currently confronted with a new problem.

I have a lot of procedurals and dynamics meshes in my program ( polygonal area that have texture, etc… )
I used to just create a Geom Primitive with my vertex and texcoord.
With as the shader used tangeant space, I must have tangeant/binormal calculated.

I know how to created them on .egg file ( option -tball )

I’m just can’t find if there are already a way to calculate binormal and tangeant at run time or if i must write it myself.

thank

There’s nothing built-in, unless you use the EggPrimitive etc. classes to generate your geometry. (This is a little slower than GeomPrimitive, but it gives you a higher level of access.)

If you want to continue to use GeomPrimitive, you’ll have to write the necessary code yourself. Fortunately, it’s a very simple calculation, and there are lots of references on the internet to give you pointers.

David

thank
How can I transform my geom and primitive to use the eggPrimitive ? I mean, is there a way to convert it automatically or should I recreate them completely ?

on the other hand, I have found this :
terathon.com/code/tangent.html
The only problem is how averaging tangent and binormal per vertex since the equation give them per triangle?
I guest I could just calculate them, then doing an average, but I’m afraid that will be slow. ( especially in python)

No, the interfaces are completely different and have little in common. You’d have to completely rewrite it, sorry. As to precisely how to do this, there are a few examples in the manual, or search for EggData or EggPolygon in the forums; and all of the egg classes are documented in the API reference.

The equation gives you the three solutions for the three vertices of a triangle. You always need to involve the other vertices in the calculation, because that’s the whole point of the tangent and binormal: they show how each vertex relates to the vertices around it. But, yeah, you’ll need to perform this calculation yourself. Presumably you’re already performing some calculations to generate the geometry in the first place, though, so it may not be that much worse than what you’re already doing.

Depending on the shape of the geometry you’re creating, you may be able to take advantage of some a priori knowledge to simplify this calculation, just as you don’t need to use the most general calculation for the surface normal when you know you’re generating a sphere and you therefore know that all of the normals face directly away from the center.

Basically, the tangent is the vector that is perpendicular to the normal and parallel to the s texture direction, and the binormal is the vector that is perpendicular to the normal and parallel to the t texture direction (or maybe the other way around).

David

thank for the info. I still have some problems though.

My trying to create a function that will take a geom and add tangent and binormal to it ( to the vertexData )

Problem: my geom seems to be const, at least each time I get one ( from a nodePath) and I can’t change the vertexData.

modifyVertexData or ( setVertexData or anything that will modify the geom) give me this error:
*** TypeError: Cannot call Geom.modifyVertexData() on a const object.

Is there a way to get a geom that is not const ? ( a copy maybe ?)

I absolutely prefer modifying the existing data than copying all and creating a new geom with all the primitives,…

Your geom is const because you got it via GeomNode.getGeom(). Use GeomNode.modifyGeom() in order to get a non-const geom.

David