Binding Index Buffer Before Binding Vertex Array Causes Black Screen.

Hello!

I’m new to this forum and in OpenGL.

After playing a little with what I’ve learn so far in OpenGL, I noticed that if i bind an Index Buffer, before binding a Vertex Array, nothing get’s drawn on the screen. I can’t understand why this is happening. Generally, by playing a little with the binding order, these was my results:

  1. Binding VAO before VBO and IBO (works)
  2. Binding VAO after VBO and before IBO (works)
  3. Binding VAO after VBO and after IBO (black screen)

Of course, the atrribPointers are being set up after i bind VBO and VAO.

Since VAO relates VBO’s with atrribPointers, why does the IBO binding order matters?

This is the code that causes the black screen:


      float vertices[] = {

		//Positions           //Colors             //Texture Coordinates.
		-0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,    0.0f, 0.0f,
		0.5f,  -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,    1.0f, 0.0f,
		0.0f,   0.5f, 0.0f,   0.0f, 0.0f, 1.0f,    0.5f, 1.0f
	};

	unsigned int indicies[] = {
		0, 1, 2,
	};


        unsigned int VBO;
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        unsigned int IBO;
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);


        unsigned int VAO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(sizeof(float) * 3));
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(sizeof(float) * 6));
	glEnableVertexAttribArray(2);

Thank you!!!

Since VAO relates VBO’s with atrribPointers, why does the IBO binding order matters?

Because VAOs store all relevant vertex specification state. Including index buffers. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER modifies the currently bound VAO. Binding a new VAO changes the state of the current GL_ELEMENT_ARRAY_BUFFER binding.

Thank you for your answer!! I didn’t know that, now it makes sense!