Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Vertex Normals

I have searched in google a lot of hours without an answer well explained, I am workign with lighting, and I need the vertex normals, let's suppose we have this vertex buffer (It's a cube):

static const GLfloat g_vertex_buffer_data[] = {
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f
};

So, how can I calculate the vertex normals? That's all. Thanks

like image 376
Spamdark Avatar asked Dec 21 '22 09:12

Spamdark


2 Answers

It's not a matter of calculating them per se, the normals are part of the artwork of the object. If you make each corner normal point "outward" from the cube it will have a sort of puffy, rounded appearance, while if you make them point squarely out (normal to the plane of that side of the cube) you'll have a sharply defined cube.

If you had a more complex (smooth) surface with many triangles you could compute normals by averaging the values of the cross products of the sides of the triangles. That would give the smoothest appearance. You are still making an artistic judgement in that case. There's no one answer for every object.

like image 133
Ben Jackson Avatar answered Dec 24 '22 01:12

Ben Jackson


If you only have the geometry, but not the normals, then you can write a little program to generate the normal at each vertex. The general process is to average the face normals of the faces that share the given vertex. However, for more 'discrete' shapes (sorry mathematicians) such as cubes, it may be preferable to only use one face normal and just use that for one side's vertex normals.

This Lighthouse3D tutorial explains the maths behind vertex normals as does Slide 22 of this USC Lecture.

like image 22
Ephemera Avatar answered Dec 24 '22 02:12

Ephemera