Add colour attribute to triangle vertices

This commit is contained in:
Abdelrahman Said 2024-09-21 19:33:23 +01:00
parent a711521f17
commit d50a06d215
3 changed files with 19 additions and 4 deletions

View File

@ -1,7 +1,8 @@
#version 460 core
in vec3 vert_colours;
out vec4 color;
void main() {
color = vec4(0.93f, 0.42f, 0.35f, 1.0f);
color = vec4(vert_colours.r, vert_colours.g, vert_colours.b, 1.0f);
};

View File

@ -1,7 +1,11 @@
#version 460 core
in vec4 position;
layout(location=0) in vec3 position;
layout(location=1) in vec3 vert_colour;
out vec3 vert_colours;
void main() {
gl_Position = vec4(position.x, position.y, position.z, position.w);
vert_colours = vert_colour;
gl_Position = vec4(position.x, position.y, position.z, 1.0f);
};

View File

@ -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) {