开发者

How to generate normals for VBO?

I've implemented a wavefront (OBJ) file loader for my OpenGL app which provides me face, texture and vertex information. However, to use lighting I obviously need normal information. I know the correct way to do this is to take the cross product of the 3 vertices that comprise a triangle and normalise. This results in a normal that you apply to all 3 vertices correct?

However, for faces/triangles that ar开发者_如何学Goe not on the edge of the polygon, each vertex is used 3 times. So I'm guessing you just average each newly calculated normal with the previously calculated normals?

This seems like it is a hugely expensive task the way I'm thinking of doing it; iterating through all the vertices and calculating the normals for each face that use that vertex and averaging. Is there a better/quicker way to do this?

Cheers

Chris


Yes, averaging the per-face normals of the faces adjacent to a vertex gives good results. To keep very hard edges 'hard', you'd typically introduce an angle limit into the calculation to keep >90 (or even lower angles) degree edges from receiving averaged normals.

A OSS project I'm involved with has a freely available implementation of this algorithm. It is O(nlogn) and operates on totally unique vertices (each face references 3 vertices, but each vertex is referenced by only one face - joining equal vertices together is done at a later stage).

If you have totally distinct vertices (i.e. each vertex position exists only once), it can be done in O(n) time, but limiting the edge angle for smoothing is more difficult to implement then.


As you can see here OBJ supports definitions of normals. Why not let the 3D program do the hard work? :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜