I'm trying to draw a rectangle using vbo.
If I draw triangle - it works fine
Character::Character() {
float arr[9] = {
-0.5, -0.5, 0,
0.5, -0.5, 0,
-0.5, 0.5, 0,
};
vertices.insert(vertices.begin(), arr, arr + 9);
}
void Character::init() {
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof (vertices) * 3, &vertices[0], GL_STATIC_DRAW);
}
void Character::draw() {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
But when I try to draw a rectangle - it still shows only first triangle. The only difference is in the constructor
Character::Character() {
float arr[18] = {
-0.5, -0.5, 0,
0.5, -0.5, 0,
-0.5, 0.5, 0,
-0.5, 0.5, 0,
0.5, 0.5, 0,
0.5, -0.5, 0
};
vertices.insert(vertices.begin(), arr, arr + 18);
}
What's the problem?
EDIT If I draw rectangle like this
Character::Character() {
float arr[12] = {
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0,
-0.5, 0.5, 0
};
vertices.insert(vertices.begin(), arr, arr + 12);
}
and change glDrawArrays(GL_TRIANGLES, 0, 3); toglDrawArrays(GL_QUADS, 0, 4); it shows a rectangle correctly. But I want to draw it as two triangles.