Vertex Specification
In "Modern" OpenGL |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
// 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 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 };
void glGenBuffers(GLsizei n,
GLuint* buffers)
where
n
is the inbound number of buffers to create
and
buffers
is the outbound array of
names/handles/IDsvoid glDeleteBuffers(GLsizei n,
const GLuint* buffers)
where
n
is the inbound number of buffers to create
and
buffers
is the inbound array of
names/handles/IDs to deletevoid glBindBuffer(enum target,
uint bufferName)
binds the buffer object to the context so its state can be changed
(where
target
is
typically GL_ARRAY_BUFFER
)
glBufferStorage()
,
glBufferData()
, glClearBufferData()
, etc...
then change the state
// 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);
// 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);
void glGenVertexArrays(GLsizei n,
GLuint* arrays);
where
n
is the inbound number of buffers to create
and
arrays
is the outbound array of
names/handles/IDsvoid glDeleteVertexArrays(GLsizei n, const
GLuint *arrays);
where
n
is the inbound number of buffers to create
and
arrays
is the inbound array of
names/handles/IDs to deletevoid glBindVertexArray(GLuint array);
binds the VAO to the context so it's state can be changedvoid glEnableVertexAttribArray(GLuint index);
allows the attribute numbered index
to be changedglVertexAttribPointer()
can then be used to
set the format of the VBO and associate the currently bound VBO
with the currently bound VBAvoid glDisableVertexAttribArray(
GLuint index);
prevents the attribute numbered
index
from being
changed
// 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) );
// 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) );