1
votes

I'm trying to render the simplest triangle with shaders and vertex arrays, but when I try to draw the vertex array, nothing shows up. This is the code (By the way, the draw function is being called, i checked):

Array creation:

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

GLfloat verts[] = { 
    0, 0, /*vertex 1*/     1, 0, 0, /*color 1*/
    0, 100, /*vertex 2*/   0, 1, 0, /*color 2*/
    100, 0, /*vertex 3*/   0, 0, 1  /*color 3*/
};
glBufferData(GL_ARRAY_BUFFER, 5 * sizeof(GLfloat) * 3, verts, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, sizeof(GLfloat) * 2, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, sizeof(GLfloat) * 3, GL_FLOAT, false, 0, 0);

glBindVertexArray(0);

Array Render:

glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);

shader.vert

uniform float u_time;
uniform mat4 u_projMatrix;

in vec2 position;
in vec3 color;

out vec4 vertColor;
void main()
{
    gl_Position = u_projMatrix * vec4(position, 0.0, 1.0);
    vertColor = vec4(color, 1.0);
}

shader.frag

uniform float u_time;

in vec4 vertColor;
void main()
{
    gl_FragColor = vertColor;
}

Projection Matrix:

glm::mat4 projection = glm::scale(glm::vec3(2.0/800.0, 2.0/600.0, 1.0));

I haven't shown the entire code in my program, but is there anything that is definitely wrong with this code?

1

1 Answers

2
votes

you don't specify a stride or offset. and the size is the number of components not the number of byte.

stride is how many byte openGL should advance for the next set of data for the attribute in your case that is 5*sizeof(GLfloat) for both attributes.

offset is where the first set of data for the attribute is located, for position that is 0 for the color that is 2*sizeof(GLFoat).

so your glVertexAttribPointer calls should be:

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 5*sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 5*sizeof(GLfloat), 2*sizeof(GLfloat));