calculating normals

I’m creating some procedural terrain and calculating per-vertex normals is working out to be a headache. Is there any way I could leave the normals blank and then calculate them automatically? I’m using the following code:

vertex = GeomVertexWriter(vdata, "vertex")
normal = GeomVertexWriter(vdata, "normal")
texcoord = GeomVertexWriter(vdata, "texcoord")

width = Scenery.numBlocksWide   #width = hight (square terrain)
for i in range(width):
    for j in range(width):
        vertex.addData3f(i*Scenery.blockWidth, j*Scenery.blockWidth,  random())
        normal.addData3f(0, 0, 1)    #temporary
        texcoord.addData2f(i, j)

for i in range(width-1):
    for j in range(width-1):
        def _2d_to_1d(row, column):
            return (row*width)+column

        prim.addVertex(_2d_to_1d(i+1, j))
        prim.addVertex(_2d_to_1d(i, j+1))
        prim.addVertex(_2d_to_1d(i, j))
        prim.closePrimitive()

        prim.addVertex(_2d_to_1d(i, j+1))
        prim.addVertex(_2d_to_1d(i+1, j))
        prim.addVertex(_2d_to_1d(i+1, j+1))
        prim.closePrimitive()

using EggData you can do that. Afaik there is no automatic method using the GeomVertexWriter’s.

But you could edit the generated data in a second step. Or first add your vertices into a list, calculate normal using them ((v1-v0)*(v1-v2), or similar), and in a second loop create the primitives.

how does the built-in terrain system do it? I’m assuming not the hard way. Also, if I calculated face normals I’d have too interpolate them to get vertex normals (since the triangles are sharing vertex data)

The GeoMipTerrain and HeightfieldTesselator classes calculate the normal vectors themselves - in the case of heightfield terrain, it’s a lot easier to calculate it.