Vertex Formats

From OpenGL Wiki
Revision as of 18:00, 27 December 2008 by V-man (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

It's possible that you have a complex scene with different objects having different formats. Perhaps we should call it layout instead of format since this topic is not about using float or int or unsigned int.

Some object might just need vertices and texcoords.
Padding is added to make the vertex structure a multiple of 32 bytes since some GPUs prefer it that way, such as ATI/AMD.

 struct Vertex_VT
 {
  float x, y, z;
  float s0, t0;
  float padding[3];
 };

Some vertices might need normals as well

 struct Vertex_VNT
 {
  float x, y, z;
  float nx, ny, nz;
  float s0, t0;
 };

What you could do is

 if(object_needs == VT)
 {
  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VT), xxx);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex_VT), zzz);
  //Notice that would must disable normals
  glDisableClientState(GL_NORMAL_ARRAY);
 }
 else (object_needs == VNT)
 {
  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VNT), xxx);
  glEnableClientState(GL_NORMAL_ARRAY);
  glNormalPointer(2, GL_FLOAT, sizeof(Vertex_VNT), yyy);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex_VT), zzz);
 }

or to reduce further the number of calls to make

 if(object_needs == VT)
 {
  //glEnableClientState(GL_VERTEX_ARRAY);  //already enabled
  glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VT), xxx);
  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);   //already enabled
  glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex_VT), zzz);
  //Notice that would must disable normals
  glDisableClientState(GL_NORMAL_ARRAY);
 }
 else (object_needs == VNT)
 {
  //glEnableClientState(GL_VERTEX_ARRAY);  //already enabled
  glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VNT), xxx);
  glEnableClientState(GL_NORMAL_ARRAY);
  glNormalPointer(2, GL_FLOAT, sizeof(Vertex_VNT), yyy);
  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);   //already enabled
  glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex_VNT), zzz);
 }