JMU
Vertex Specification
In "Modern" OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Important Concepts
The Vertex Stream
The Vertex Stream (cont.)

A Scene Object Consisting of Two Triangles with a Non-Interleaved Stream

openglexamples/modern/multiple.cpp (Fragment: stream)
          // The positions of the vertices of two triangles
  GLfloat positions[] = {
    -0.5f, -0.5f, 0.0f, // Vertex 0 of triangle 0
     0.5f, -0.5f, 0.0f, // Vertex 1 of triangle 0
     0.0f,  0.5f, 0.0f, // Vertex 2 of triangle 0
    -0.5f, -0.5f, 0.0f, // Vertex 0 of triangle 1
     0.5f, -0.5f, 0.0f, // Vertex 1 of triangle 1
     0.0f, -1.0f, 0.0f, // Vertex 2 of triangle 1
  };

  // The colors of the vertices of two triangles
  GLfloat colors[] = {
    1.0f,  0.0f, 0.0f, // Vertex 0 of triangle 0
    0.0f,  1.0f, 0.0f, // Vertex 1 of triangle 0
    0.0f,  0.0f, 1.0f, // Vertex 2 of triangle 0
    1.0f,  0.0f, 0.0f, // Vertex 0 of triangle 1
    0.0f,  1.0f, 0.0f, // Vertex 1 of triangle 1
    0.0f,  0.0f, 1.0f, // Vertex 2 of triangle 1
  };
        
The Vertex Stream (cont.)

A Scene Object Consisting of Two Triangles with an Interleaved Stream

openglexamples/modern/interleaved.cpp (Fragment: stream)
          // The interleaved positions and colors of the vertices of two triangles
  GLfloat vertices[] = {
    -0.5f, -0.5f, 0.0f, // Vertex 0 of triangle 0: Position
     1.0f,  0.0f, 0.0f, // Vertex 0 of triangle 0: Color
     0.5f, -0.5f, 0.0f, // Vertex 1 of triangle 0: Position
     0.0f,  1.0f, 0.0f, // Vertex 1 of triangle 0: Color
     0.0f,  0.5f, 0.0f, // Vertex 2 of triangle 0: Position
     0.0f,  0.0f, 1.0f, // Vertex 2 of triangle 0: Color
    -0.5f, -0.5f, 0.0f, // Vertex 0 of triangle 1: Position
     1.0f,  0.0f, 0.0f, // Vertex 0 of triangle 1: Color
     0.5f, -0.5f, 0.0f, // Vertex 1 of triangle 1: Position
     0.0f,  1.0f, 0.0f, // Vertex 1 of triangle 1: Color
     0.0f, -1.0f, 0.0f, // Vertex 2 of triangle 1: Position
     0.0f,  0.0f, 1.0f, // Vertex 2 of triangle 1: Color
  };
        
Vertex Specification
Vertex Buffer Objects
Vertex Buffer Objects (cont.)
Vertex Buffer Objects (cont.)

The VBOs for the Non-Interleaved Stream

openglexamples/modern/multiple.cpp (Fragment: vbo)
          // Create a vertex buffer object (VBO) for the positions
  glGenBuffers(1, &positionBufferID);
  // Bind the VBO so it's state can be changed
  glBindBuffer(GL_ARRAY_BUFFER, positionBufferID);
  // Change the state of the VBO
  glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
  
  // Create a vertex buffer object (VBO) for the colors
  glGenBuffers(1, &colorBufferID);
  // Bind the VBO so it's state can be changed
  glBindBuffer(GL_ARRAY_BUFFER, colorBufferID);
  // Change the state of the VBO
  glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
        
Vertex Buffer Objects (cont.)

The VBO for the Interleaved Stream

openglexamples/modern/interleaved.cpp (Fragment: vbo)
          // Create a vertex buffer object (VBO) for the vertices
  glGenBuffers(1, &vertexBufferID);
  // Bind the VBO so it's state can be changed
  glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
  // Change the state of the VBO
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        
Vertex Array Objects (VAOs)
Vertex Array Objects (VAOs)
Vertex Array Objects (cont.)

The VAOs for the Non-Interleaved Stream

openglexamples/modern/multiple.cpp (Fragment: vao)
          // Bind the VAO so that it's state can be changed
  // (by calls to glEnableVertexAttribArray() and glVertexAttribPointer())
  glBindVertexArray(vertexArrayID);
  
  // Describe the positions in the vertex array buffer (changing the state
  // of the VAO)
  glEnableVertexAttribArray(0);                                   
  glBindBuffer(GL_ARRAY_BUFFER, positionBufferID); // Doesn't modify the VAO's state                
  glVertexAttribPointer( // Modify's the VAO's state
    0,                          // attribute 0
    3,                          // size                             
    GL_FLOAT,                   // type                             
    GL_FALSE,                   // normalized or not?                      
    0,                          // stride (0 because not interleaved)
    reinterpret_cast<void*>(0)  // offset into the buffer
  );                                                              

  // Describe the colors in the vertex array buffer (changing the state
  // of the VAO)
  glEnableVertexAttribArray(1);
  glBindBuffer(GL_ARRAY_BUFFER, colorBufferID);
  glVertexAttribPointer( // Modify's the VAO's state
    1,
    3,
    GL_FLOAT,
    GL_FALSE,
    0,       
    reinterpret_cast<void*>(0)
  );
        
Vertex Array Objects (cont.)

The VAOs for the Interleaved Stream

openglexamples/modern/interleaved.cpp (Fragment: vao)
          // Bind the VAO so that it's state can be modified
  // (by calls to glEnableVertexAttribArray() and glVertexAttribPointer())
  glBindVertexArray(vertexArrayID);

  // Describe the positions in the vertex array buffer (changing the state
  // of the VAO)
  glEnableVertexAttribArray(0);                                   
  glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); // Doesn't modify the VAO's state
  glVertexAttribPointer( // Modify's the VAO's state
    0,                          // attribute 0
    3,                          // size
    GL_FLOAT,                   // type
    GL_FALSE,                   // normalized or not
    sizeof(GLfloat)*6,          // stride (6 GLfloat values are interleaved)
    reinterpret_cast<void*>(0)  // offset (positions start at 0)
  );                                                              
  
  // Describe the colors in the vertex array buffer (changing the state
  // of the VAO)
  glEnableVertexAttribArray(1);
  glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); // Doesn't modify the VAO's state
  glVertexAttribPointer( // Modify's the VAO's state
    1,                                          // attribute 1
    3,
    GL_FLOAT,
    GL_FALSE,
    sizeof(GLfloat)*6,
    reinterpret_cast<void*>(3*sizeof(GLfloat)) // offset (colors start at 3)
  );