user1843915
user1843915

Reputation: 15

OpenGL arrangement of normal data in VBOs

When reading examples of simple VBOs programs I've noticed there seems to be an association of normal data with vertex data. But from the definition of a normal, I would have thought that the normal data should be associated with the face data.

From the code segment below I can noticed that the normal data for each MyVertex is the same, so the normal for the "triangle face" would make sense. But I am unsure of how one would store the normal data for larger objects where several faces may share the same vertices as stored in GL_ELEMENT_ARRAY_BUFFER.

Questions: How does OpenGL conceptually handle the normal data? Or have I made a wrong assumption in how normals should work somewhere?

(code below from http://www.opengl.org/wiki/VBO_-_just_examples)

struct MyVertex
  {
    float x, y, z;        //Vertex
    float nx, ny, nz;     //Normal
    float s0, t0;         //Texcoord0
  };

  MyVertex pvertex[3];
  //VERTEX 0
  pvertex[0].x = 0.0;
  pvertex[0].y = 0.0;
  pvertex[0].z = 0.0;
  pvertex[0].nx = 0.0;
  pvertex[0].ny = 0.0;
  pvertex[0].nz = 1.0;
  pvertex[0].s0 = 0.0;
  pvertex[0].t0 = 0.0;
  //VERTEX 1    

Thanks in advance

Upvotes: 2

Views: 667

Answers (1)

Kos
Kos

Reputation: 72281

In OpenGL, normals are vector attributes, just like position or texture coordinates.

Having per-face normals may seem reasonable, but wouldn't work in practice.

Reason: One triangle is physically flat, but is often an approximation of a curved surface. Having normal vectors different among the vertices of a triangle allows you to interpolate between them to get an approximated normal vector at any point of the surface.

Think of a vertex normal as a sample of the normal at some particular points of a smooth surface.

(Of course, when rendering surfaces with hard edges, like a cube, the above doesn't really help and many require you to have duplicate vertices differing only by the normal.)

Upvotes: 2

Related Questions