Add colour attribute to triangle vertices
This commit is contained in:
12
src/main.cc
12
src/main.cc
@@ -115,8 +115,11 @@ int init(App &app) {
|
||||
void create_vertex_spec(App &app) {
|
||||
const std::vector<GLfloat> vertices = {
|
||||
-0.8f, -0.8f, 0.0f, // position
|
||||
1.0f, 0.0f, 0.0f, // colour
|
||||
0.8f, -0.8f, 0.0f, // position
|
||||
0.0f, 1.0f, 0.0f, // colour
|
||||
0.0f, 0.8f, 0.0f, // position
|
||||
0.0f, 0.0f, 1.0f, // colour
|
||||
};
|
||||
|
||||
// Create and activate the VAO
|
||||
@@ -131,13 +134,19 @@ void create_vertex_spec(App &app) {
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
|
||||
GLsizei vertex_stride = 6 * sizeof(GLfloat);
|
||||
GLvoid *position_start_offset = (void *)0;
|
||||
GLvoid *colour_start_offset = (void *)(3 * sizeof(GLfloat));
|
||||
|
||||
// Defines the vertex attributes and their indices. This defines the attribute for the currently
|
||||
// bound VAO
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Defines the number of components for the attribute, its type, the stride between the component
|
||||
// for each vertex and the offset of the first instance of the component in the buffer
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex_stride, position_start_offset);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertex_stride, colour_start_offset);
|
||||
|
||||
// Cleanup.
|
||||
// The order matters. Unbind VAO first then disable the attributes. If you do it the
|
||||
@@ -146,6 +155,7 @@ void create_vertex_spec(App &app) {
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
void create_graphics_pipeline(App &app) {
|
||||
|
||||
Reference in New Issue
Block a user