0

I am trying to learn how to program OpenGL. Right now I am writing a program that draws to cubes. I have each cube stored in a separate VBO. This is the code for this.

void
init()
{
enum { Vertices, Colors, Elements, NumVBOs };
GLuint buffers[NumVBOs];
glGenVertexArrays(NumVAOs,VAO);
{
GLfloat vertices[][3] = {
 { -0.5, -0.5, -0.5 },
 { 0.5, -0.5, -0.5 },
 { 0.5, 0.5, -0.5 },
 { -0.5, 0.5, -0.5 },
 { -0.5, -0.5, 0.5 },
 { 0.5, -0.5, 0.5 },
 { 0.5, 0.5, 0.5 },
 { -0.5, 0.5, 0.5 }
};

GLfloat cubeColors[][3] = {
 { 0.0, 0.0, 0.0 },
 { 0.0, 0.0, 1.0 },
 { 0.0, 1.0, 0.0 },
 { 0.0, 1.0, 1.0 },
 { 1.0, 0.0, 0.0 },
 { 1.0, 0.0, 1.0 },
 { 1.0, 1.0, 0.0 },
 { 1.0, 1.0, 1.0 },
 };

GLubyte indices[][4] = {
 { 0, 1, 2, 3 },
 { 4, 7, 6, 5 },
 { 0, 4, 5, 1 },
 { 3, 2, 6, 7 },
 { 0, 3, 7, 4 },
 { 1, 5, 6, 2 }
};
glBindVertexArray(VAO[Cube1]);
glGenBuffers(NumVBOs, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
 GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeColors),cubeColors,GL_STATIC_DRAW);
glColorPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
 GL_STATIC_DRAW);
}
{
GLfloat vertices2[][3] = {
 { -0.5, -0.5, -1.5 },
 { 0.5, -0.5, -1.5 },
 { 0.5, 0.5, -1.5 },
 { -0.5, 0.5, -1.5 },
 { -0.5, -0.5, -2.5 },
 { 0.5, -0.5, -2.5 }, 
 { 0.5, 0.5, -2.5 },
 { -0.5, 0.5, -2.5 }
}; 

GLfloat cubeColors2[][3] = {
 { 0.0, 0.0, 0.0 },
 { 0.0, 0.0, 1.0 },
 { 0.0, 1.0, 0.0 },
 { 0.0, 1.0, 1.0 },
 { 1.0, 0.0, 0.0 },
 { 1.0, 0.0, 1.0 },
 { 1.0, 1.0, 0.0 },
 { 1.0, 1.0, 1.0 },
 };

GLubyte indices2[][4] = {
 { 0, 1, 2, 3 }, 
 { 4, 7, 6, 5 }, 
 { 0, 4, 5, 1 }, 
 { 3, 2, 6, 7 },
 { 0, 3, 7, 4 },
 { 1, 5, 6, 2 }
};
glBindVertexArray(VAO[Cube2]);
glGenBuffers(NumVBOs, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2,
 GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeColors2),cubeColors2,GL_STATIC_DRAW);
glColorPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2,
GL_STATIC_DRAW);
}

glEnable(GL_DEPTH_TEST);

}

When this is called in display()

   glBindVertexArray[VAO[Cube1]];
   glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

   glBindVertexArray[VAO[Cube2]];
   glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

The program only draws Cube2. However, if I insert

 glBindVertexArray[VAO[Cube1]];

at the end of init(), it will draw Cube1 only instead.

Right now I am confused. I would like to be able to use VAOs for more complicated applications but am stuck at this point.

  • Why `glBindVertexArray[...]`, shouldn't it be `glBindVertexArray(...)`? – Rabbid76 Jan 31 '19 at 06:49
  • The usual way to draw to identically cubes one different positions, is to create one mesh and to draw it twice by setting a different model matrix, rather then generating 2 meshes with different vertex coordinates. – Rabbid76 Jan 31 '19 at 06:54
  • Yeah you were right about the brackets. That fixed my problem. It is strange that there was no compiler error. I should have double checked my code before posting this. –  Jan 31 '19 at 06:59
  • `glBindVertexArray` is a function pointer. – Rabbid76 Jan 31 '19 at 07:00

1 Answers1

0

glBindVertexArray[VAO[Cube1]]; Should have been glBindVertexArray(VAO[Cube1]);