# Vertex normal generation

Hi,
We create some heightfield procedurally in panda using GeomTriangles and adding them to a Geom and this Geom to a GeomNode, it works great.

But we have a problem to add normals: we see how to add them to the vertices but can’t we add them do the triangles?
Or does a function to calcul all the normals for a vertex pool exist?

Do we have to add the normals to each vertex?

Gruikally

At the fundamental rendering engine, normals are associated with vertices, not triangles. If you want the effect of flat-shaded triangles, where lighting reveals the facets of the surface, you have two options:

(1) Replicate each vertex so that it is unique to each triangle, and set the same normal on each of the three vertices of the triangle.

(2) Use the “flat” shade model. This means you may share vertices between triangles as usual, but arrange it so that the last (or first) vertex of each triangle has the normal for that particular triangle. Call primitive->setShadeModel(Geom.SMFlatLastVertex) (or SMFlatFirstVertex). Also call geomNode.setAttrib(ShadeModelAttrib.make(ShadeModelAttrib.MFlat)).

The best choice between 1 or 2 depends on the nature of your scene and your graphics card. Option 1 adds more vertices, but option 2 adds more state. Sometimes the former is better, sometimes the latter.

If you use the egg library to generate your geometry, as you suggested in another post, the egg library will take care of this detail for you (it will pick either option 1 or 2, depending on the setting of egg-flat-shading). Of course, this is likely to be a suitable solution only when you are generating your geometry as an offline process.

David

Hi,

I want to do the same thing (modify my vertex and then recompute the normal) and I wanted to use the second option. But, some of my vertices are used in last (or first) position by more than one triangle. So it can’t store the normal for that particular triangle. So, will it works ?

``````GeomTriangles (indexed):
[ 0 1 2 ]
[ 0 2 3 ]
[ 4 5 6 ]
[ 7 5 8 ]
[ 9 10 11 ]
[ 12 13 14 ]
[ 12 14 11 ]
[ 15 16 17 ]
[ 15 17 18 ]
[ 15 18 19 ]
[ 20 18 21 ]
[ 20 21 22 ]
[ 23 24 25 ]
[ 26 24 27 ]
[ 28 29 30 ]
[ 28 30 31 ]
[ 32 33 31 ]
[ 34 35 36 ]
[ 34 36 37 ]
[ 38 39 37 ]
[ 40 41 42 ]
[ 40 42 43 ]``````

So I was thinking of doing it manually, But is there a method to know all the triangles referancing a vertice ? For example, if I ask for vertice number 2, it respond triangles 1 and 2

Thanks.

No, you’ll have to build that reverse-lookup index yourself.

The GeomTriangles etc. primitives are very low-level primitive classes, optimized for rendering quickly, but not optimized for easy high-level manipulation. If you want easy high-level manipulation, that’s what the egg library is for.

You can still manipulate the primitives at the low level, but you have to do a lot of the work to manage it.

David