opengl vertices not rendering - c++

int main() {
using namespace game1;
using namespace graphics;
using namespace maths;
using namespace util;
using namespace std;
Window window("Game 1", 960, 540);
glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
GLfloat vertices[] = {
4, 3, 5,
12, 3, 5,
4, 6, 5,
4, 6, 5,
12, 6, 5,
4, 3, 5
};
Matrix4f ortho = Matrix4f::genOrtho(0.0f, 16.0f, 0.0f, 9.0f, -1, 1);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
ShaderProgram s;
s.attachShader(File("res/testVert.vert"), GL_VERTEX_SHADER);
s.attachShader(File("res/testFrag.frag"), GL_FRAGMENT_SHADER);
s.link();
s.enable();
s.setUniformMat4f("model", Matrix4f::identity());
s.setUniformMat4f("world", Matrix4f::identity());
s.setUniformMat4f("proj", ortho);
while (!window.closed())
{
window.clear();
glDrawArrays(GL_TRIANGLES, 0, 3);
window.update();
}
return 0;
}
I am quite new to opengl, and i am not sure what i am doing wrong. I suspect it has something to do with the vbo or vertexAttribArrayPointer and i've tested lots of things but i cant fins what is wrong. It works with a normal 3 vertex triangle and i'm sure my error is somewhere in main

The ortho-matrix that gets defined in the code has a visible range along the z-axes from -1 to 1. The triangle on the other hand is at z=5 which means it is outside of the near-plane/far-plane range.

Related

opengl drawing a 3d cube with with EBO

I am trying to draw a cube with OPENGL by using EBO, VAO, and VBO.
the first function init the VAO of the cube
initVAO()
{
GLfloat cube_vertices[] = {
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0
};
GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// right
1, 5, 6,
6, 2, 1,
// back
7, 6, 5,
5, 4, 7,
// left
4, 0, 3,
3, 7, 4,
// bottom
4, 5, 1,
1, 0, 4,
// top
3, 2, 6,
6, 7, 3
};
vertices.insert(vertices.begin(), std::begin(cube_vertices), std::end(cube_vertices));
indices.insert(indices.begin(), std::begin(cube_elements), std::end(cube_elements));
/* create vao */
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
/* create ebo */
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
/* create vbo */
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
/* unbind buffers */
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
and the second function use it to draw
draw()
{
shader->activateShader();
/* calculate projection matrix */
//TODO:: find a new place for the projection matrix calculation
int width = (GLfloat)ResourceManager::getInstance()->getWindowSize().x;
int height = (GLfloat)ResourceManager::getInstance()->getWindowSize().y;
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / height, 0.1f, 100.0f);
/* update shader uniforms*/
glUniformMatrix4fv(shader->projectionUniform, 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(shader->viewUniform, 1, GL_FALSE, glm::value_ptr(Engine::getInstance()->camera->createViewMatrix()));
glUniformMatrix4fv(shader->modelUniform, 1, GL_FALSE, glm::value_ptr(transform->createModleMatrix()));
glUniform4f(shader->colorUniform, color.x, color.y, color.z, 1);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
}
right now the program is not drawing anything and I cant figure out what is wrong with it
I know that the other parts of the code works because I tested it without a EBO and it worked but I don't know what to do.
See Index buffers. The index buffer binding is stated within the Vertex Array Object. When a buffer is bound to the target ELEMENT_ARRAY_BUFFER, then this buffer is associated to the vertex array object which is currently bound. When calling glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); the binding of the element buffer to the currently bound VAO is broken. Remove this line of code:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
The type specification in the glDrawElements must match the type of the indices. Since the type of the indexes is GLushort, the specified type must be GL_UNSIGNED_SHORT:
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);

Having issues drawing 3D cube's edges with OpenGL 3

I'm trying to draw 3D cube's vertices (edges only) using OpenGL (4.3 core profile), I know the glPolygonMode function but I'd like not to draw the intermediate diagonal lines. I declare my vertices and my indices like so :
struct Vertex {
glm::vec3 pos;
glm::vec3 color;
glm::vec3 normal;
glm::vec2 uv;
};
Vertex vertices[8];
// Front vertices
vertices[0].pos = glm::vec3(-0.5f, -0.5f, +0.5f);
vertices[1].pos = glm::vec3(+0.5f, -0.5f, +0.5f);
vertices[2].pos = glm::vec3(+0.5f, +0.5f, +0.5f);
vertices[3].pos = glm::vec3(-0.5f, +0.5f, +0.5f);
// Back vertices
vertices[4].pos = glm::vec3(-0.5f, -0.5f, -0.5f);
vertices[5].pos = glm::vec3(+0.5f, -0.5f, -0.5f);
vertices[6].pos = glm::vec3(+0.5f, +0.5f, -0.5f);
vertices[7].pos = glm::vec3(-0.5f, +0.5f, -0.5f);
GLuint indices[36] = {
0, 1, 2, 2, 3, 0, // Front
1, 5, 6, 6, 2, 1, // Right
7, 6, 5, 5, 4, 7, // Back
4, 0, 3, 3, 7, 4, // Left
4, 5, 1, 1, 0, 4, // Bottom
3, 2, 6, 6, 7, 3 // Top
};
My buffer is updated accordingly :
// Bind Vertex Array
glBindVertexArray(_VAO);
// Bind VBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use VBO
glBindBuffer(GL_ARRAY_BUFFER, _VBO);
// Upload vertices to VBO
glBufferData(GL_ARRAY_BUFFER, verticesNb * sizeof(Vertex), vertices, GL_STATIC_DRAW);
// Bind EBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
// Updload indices to EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesNb * sizeof(GLuint), indices, GL_STATIC_DRAW);
I'm using glDrawElements(GL_LINES, 36, GL_UNSIGNED_INT, 0); to draw my cube's edges, but for some reason it doesn't draw some edges and I don't understand why.
If I use GL_TRIANGLES, it works pretty well though when I want to render my 3D cube in fill-mode. Does anyone know what I'm missing here ? Is it an issue with the indices ?
(The "cube" below has a custom size of (1.0f, 2.0f, 3.0f))
Your indices form GL_TRIANGLES primitives rather than GL_LINES primitives. See GL_LINES:
Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on.
The indices form the primitives. Change the indices:
GLuint indices[] = {
0, 1, 1, 2, 2, 3, 3, 0, // Front
4, 5, 5, 6, 6, 7, 7, 4, // Back
0, 4, 1, 5, 2, 6, 3, 7
};
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);

Drawing a cube using Indexed Draw in modern OpenGL

I'm trying to draw a cube using indexed draw in OpenGL 3.3. But it does not show up right...
Here is what I tried doing.
GLfloat vertices01[] = {
-1.0f,1.0f,0.0f,
-1.0f,-1.0f,0.0f,
1.0f,1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,1.0f,-1.0f,
1.0f,-1.0f,-1.0f
};
unsigned int indices01[] = {
0, 2, 3, 1,
2, 6, 7, 3,
6, 4, 5, 7,
4, 0, 1, 5,
0, 4, 6, 2,
1, 5, 7, 3
};
Mesh* obj3 = new Mesh();
obj3->CreateMesh(vertices01, indices01, 24, 24);
meshList.push_back(obj3);
meshList[0]->RenderMesh();
//in mesh class
indexCount = numOfIndices;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0])*numOfIndices, indices, GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0])*numOfVertices, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindVertexArray(VAO);
//bind ibo
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES,indexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
the output shows a partial cube whose each side had one triangle each and also a triangle going through it's diagonal
If you use a compatibility profile context, then you can keep your indices an use GL_QUADS instead of GL_TRIANGLES. But that's deprecated (Legacy OpenGL ).
Since the primitive type is GL_TRIANGLES, each side of the cube has to be formed by 2 triangles. See Triangle primitives.
Change the index buffer to solve the issue:
unsigned int indices01[] = {
0, 2, 3, 0, 3, 1,
2, 6, 7, 2, 7, 3,
6, 4, 5, 6, 5, 7,
4, 0, 1, 4, 1, 5,
0, 4, 6, 0, 6, 2,
1, 5, 7, 1, 7, 3,
};
An alternative solution would be to use the primitive type GL_TRIANGLE_STRIP and a Primitive Restart index.
Enable primitive restart and define a restart index:
e.g.
glEnable( GL_PRIMITIVE_RESTART );
glPrimitiveRestartIndex( 99 );
Define 2 triangle strips, which are separated by the restart index:
unsigned int indices01[] = {
0, 1, 2, 3, 6, 7, 4, 5,
99, // 99 is the restart index
7, 3, 5, 1, 4, 0, 6, 2
};
int indexCOunt = 17;
And draw the elements:
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0);
Note, the Index buffer binding is stored in the Vertex Array Object.
So it is sufficient to bind the index buffer once, when the VAO is setup:
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// [...]
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
// [...]
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); <---- delete this
glBindVertexArray(0);
Then it is superfluous to bind the index buffer again, before the draw call:
glBindVertexArray(VAO);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); <---- now this is superfluous
glDrawElements(GL_TRIANGLES,indexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);

Vertex array issue with classes

I'm learning openGL programming from tutorials of Chernov # sparky engine on youtube. currently I have a vertex & a fragment shader with required classes for window ,shader etc management. I can use single vertex array object to draw on screen but when the same is done by creating a vertexArray class , it fails.
vertex array class :
#include"vertexArray.h"
namespace graphics{
VertexArray::VertexArray(){
glGenVertexArrays(1,&arrayID);
}
void VertexArray::addBuffer(Buffer* buffer, GLint index){
bind();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrayID);
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, buffer->getComCount(), GL_FLOAT, GL_FALSE, 0, 0);
}
void VertexArray::bind() const{
glBindVertexArray(arrayID);
}
void VertexArray::unbind() const{
glBindVertexArray(0);
}
VertexArray::~VertexArray(){
}
}
my main.cpp file
#include"graphics\window.h"
#include"utils\reader.h"
#include"graphics\shader.h"
#include"math\vec.h"
#include"math\mat4.h"
#include"graphics\buffers\buffer.h"
#include"graphics\buffers\indexbuffer.h"
#include"graphics\buffers\vertexArray.h"
using namespace graphics;
using namespace utils;
using namespace math;
int main(){
Window window;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
Reader read1("src/shaders/test.vert");
Reader read2("src/shaders/test.frag");
char * r1 = read1.getData();
char * r2 = read2.getData();
GLfloat vert[] = {
0, 0, 0,
0, 3, 0,
8, 0, 0,
8, 3, 0,
};
Buffer* vbo = new Buffer(vert,4*3,3);
GLushort indices[] = {
0,1,2,
1,3,2
};
indexBuffer ibo(indices,6);
Shader shader(r1, r2);
shader.enable();
#if 0
GLuint sprite1;
glGenVertexArrays(1, &sprite1);
glBindVertexArray(sprite1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite1);
vbo->bind();
glVertexAttribPointer(0, vbo->getComCount(), GL_FLOAT, 0, 0, 0);
shader.setUniformMat4("pr_matrix", mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f));
shader.setUniformMat4("ml_matrix", mat4::translation(vec3(0, 0, 0)));
shader.setUniform2f("light_pos", vec2(8.0f, 4.5f));
shader.setUniform4f("colour", vec4(0.2, 0.0, 0.4, 1));
glEnableVertexAttribArray(0);
glBindVertexArray(0);
GLuint sprite2;
glGenVertexArrays(1, &sprite2);
glBindVertexArray(sprite2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite2);
vbo->bind();
glVertexAttribPointer(5, vbo->getComCount(), GL_FLOAT, 0, 0, 0);
shader.setUniformMat4("pr_matrix", mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f));
shader.setUniformMat4("ml_matrix", mat4::translation(vec3(4, 3, 0)));
shader.setUniform2f("light_pos", vec2(8.0f, 4.5f));
shader.setUniform4f("colour", vec4(0.3, 0.0, 0.2, 1));
glEnableVertexAttribArray(0);
glBindVertexArray(0);
#endif
VertexArray vao;
vao.addBuffer(vbo, 0);
while (!window.closed()){
#if 0
window.clear();
glDrawArrays(GL_TRIANGLES, 0,6);
#endif
double x, y;
x = window.getX();
y = window.getY();
vao.bind();
ibo.bind();
shader.setUniform2f("light_pos", vec2((float) (x*16.0f/960.0f) , (float) (9- 9*y/540.0f)));
glDrawElements(GL_TRIANGLES, ibo.getCount(), GL_UNSIGNED_SHORT, 0);
window.update();
vao.unbind();
ibo.unbind();
}
return 0;
}
Please note that Everything works if I just create a vertex array to an GLuint variable in the main & use it .
I cant seem to find the issue here.
Any help is highly appreciated ..
You have to bind the data to the buffer (See glBufferData).
Vertex attribute buffers can be created like this:
GLfloat vert[] = {
0, 0, 0,
0, 3, 0,
8, 0, 0,
8, 3, 0,
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*4, vert, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Element buffers can be created like this:
GLushort indices[] = {
0,1,2,
1,3,2
};
GLuint ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*3*2, indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
A Vertex Array Object and the vertex attribute pointers are specified like this:
GLuint attr_index = 0; // attribute index according to the shader program
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(attr_index , 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_index );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray( 0 );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Note, a Vertex Array Object stores all the information which you specify about the vertex attributes (format, size, attribute index ...) and it refers to the element array buffer.
Finally you can draw the mesh like this:
glBindVertexArray( vao );
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glBindVertexArray( 0 );

Drawing Two Objects in OpenGL4?

I am trying to draw 2 objects in OpenGL. The window/viewport is (0,0,950,1050). I am not sure this is the right way of doing it, but I thought so. I thought the idea was to create a VBO/VAO per object, bind it, set the data, and repeat that operation for each object.
Then when the objects are to be drawn:
set the shader
bind the data of the first object we want to draw using its vbo (say vbo1)
do the drawing call
bind the data of the next object we want to draw using its vbo (say vbo2)
do the drawing call
...
When I do this, I only get the points of the second object drawn on the screen, but with the color of the first object (it's blue instead of red).
My mistake must be obvious to any expert out there. What do I miss?
// BLUE -----------------------------------
GLuint vbo1, vao1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
float arr1[] = { 10, 10, 10, 110, 110, 110, 110, 10 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr1, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// RED -----------------------------------
GLuint vbo2, vao2;
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
float arr2[] = { 400, 400, 400, 800, 800, 800, 800, 400 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr2, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window))
{
glClearColor(1, 1, 1, 0.1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(pointShader);
GLint loc;
loc = glGetUniformLocation(pointShader, "pointColor");
// it draws this one but with the color blue!
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
float ptColor2[3] = { 1, 0, 0 };
glUniform3fv(loc, 1, ptColor2);
glDrawArrays(GL_POINTS, 0, 4);
// it doesn't draw this one???
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
float ptColor1[3] = { 0, 0, 1 };
glUniform3fv(loc, 1, ptColor1);
glDrawArrays(GL_POINTS, 0, 4);
glfwSwapBuffers(window);
glfwWaitEvents();
}
EDIT 2 WORKING CODE
Thank you very much to both Reto Koradi and Datenwolf. Combining the answers, helped to come with the right answer. It's sad, these things are not explained properly in books. Hope this post will help other beginners (sorry if the result is a bit misleading, I swapped color between when I asked the question, and when I got the answer).
// RED -----------------------------------
GLuint vbo1, vao1;
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
float arr1[] = { 10, 10, 10, 110, 110, 110, 110, 10 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr1, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// BLUE -----------------------------------
GLuint vbo2, vao2;
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
float arr2[] = { 400, 400, 400, 800, 800, 800, 800, 400 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glClearColor(1, 1, 1, 0.1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(pointShader);
GLint loc;
loc = glGetUniformLocation(pointShader, "pointColor");
// red
glBindVertexArray(vao1);
float ptColor1[3] = { 1, 0, 0 };
glUniform3fv(loc, 1, ptColor1);
glDrawArrays(GL_POINTS, 0, 4);
// blue
glBindVertexArray(vao2);
float ptColor2[3] = { 0, 0, 1 };
glUniform3fv(loc, 1, ptColor2);
glDrawArrays(GL_POINTS, 0, 4);
glfwSwapBuffers(window);
glfwWaitEvents();
}
EDIT 3
Note though the order from the first code fragment for the VAO/VBO order declaration would also work. So the above version and the one below are both valid:
// RED -----------------------------------
GLuint vbo1, vao1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
float arr1[] = { 10, 10, 10, 110, 110, 110, 110, 10 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr1, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// BLUE -----------------------------------
GLuint vbo2, vao2;
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
float arr2[] = { 400, 400, 400, 800, 800, 800, 800, 400 };
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, arr2, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
The only thing that was really missing in the code was glBindVertexArray.
The problem is in your draw loop, where you call:
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
It does not matter which GL_ARRAY_BUFFER is currently bound when you make the draw call. The correct buffer needs to be bound when you call glVertexAttribPointer(), which you correctly did in your setup code.
The VAOs track all your vertex setup state. So before each draw call, you need to bind the corresponding VAO by calling glBindVertexArray(), instead of the glBindBuffer() calls you have in the posted code:
glBindVertexArray(vao2);
glUniform3fv(...);
glDrawArrays(...);
glBindVertexArray(vao1);
glUniform3fv(...);
glDrawArrays(...);
You must create and bind the Vertex Array Object before specifying the vertex attribute data locations (glVertexAttribPointer). The VAO kind of takes ownership of the data that belongs to the VBO currently bound when making those calls.
(EDIT accidently submitted while still typing)
Assuming you have a core profile context, when you attempt to create that first buffer object, there's no VAO to bind it to yet, so the whole creation of the BO fails. Hence when you try to draw it, nothing gets drawn at all. But what you see drawn is the BO you intended to bind to the second VAO, but because you got the order of operations wrong it ends up in the first VAO.