Jimmy Chi Kin Chau
Jimmy Chi Kin Chau

Reputation: 185

OpenGL normal vector from vertex location only

I am handling some OpenGL lighting tutorial, now on the topic of diffuse lighting. Wondering is it possible to calculate normal vector from "surface" of verticies purely by their location? (i.e. for each triangle, get p1 - p2 and p1 - p3 then cross product them).

Am I correct by thinking that it is prohibited by OpenGL's nature of parallel processing (verticies does not know who they are with, and no way to put their processing in sorted order)? And this is more efficiently handled by the actual verticies model providing them(i.e. vertex data containing x, y, z, nx, ny, nz) since it is mostly static, such that calculation is done in model generation (non-runtime) saving shader resource? Will this change when the model and thus vertex data is dynamically generated(and let me think, able to be handled by the geometry shader)?

Upvotes: 0

Views: 583

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473212

Vertex normals are as unchanging as vertex positions (that is, if your positions don't change, the normals won't either). As such, unless the vertex positions change (and I don't mean by applying matrices to them), it's best to calculate normals once. On the CPU. Then put them in your mesh data.

Pretty much every art package can export vertex normals with a mesh.

To your specific question, no. Neither vertex shaders nor geometry shaders can generate normals. GS's have adjacency primitives that may be able to generate them, but the restrictions on such generation are very tight. Also, it would be slow.

Upvotes: 1

Related Questions