Weird behavior with glDrawArrays generating multiple forms

Hey everyone,
I’m trying to program OpenGL since a few weeks. Now I’ve started to use VBO to increase the performance of my program.
I started to write an OpenGL class within a small program where everything worked out well. I also was able to generate multiple instances of my class in multiple forms without any issues.
Then I’ve tried to move the class into a bigger project. At the beginning (with just one instance of the OpenGL class) everything worked out well too. But when I started to use multiple instances of my class it always crashes at the function glDrawArrays.

Here is some example code of how I store my data to the buffer and how I render it.

Can maybe somebody of you see any mistake?

Storing to buffer:

void TOpenGL::vao_fill_buffer(std::vector<GLfloat> &xyz_val,
															std::vector<GLfloat> &xyz_norm,
															std::vector<GLfloat> &xyz_col)
{
	// Setting render context to form!
	if (fully_initialized)
  	ogl_refresh_context();

	GLuint i;
	glGenVertexArrays(3, vao);
	glGenBuffers(3, vbo);
	glBindVertexArray(vao[0]);

	i = 0; // vertex
	gl_data_size = xyz_val.size() * sizeof(GLfloat);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
	glBufferData(GL_ARRAY_BUFFER, gl_data_size, &xyz_val[0], GL_STATIC_DRAW);
	glEnableVertexAttribArray(i);
	glVertexAttribPointer(i, 3, GL_FLOAT, GL_FALSE, 0, 0);

	i = 1; // normal
	gl_normal_size = xyz_norm.size() * sizeof(GLfloat);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
	glBufferData(GL_ARRAY_BUFFER, gl_normal_size, &xyz_norm[0], GL_STATIC_DRAW);
	glEnableVertexAttribArray(i);
	glVertexAttribPointer(i, 3, GL_FLOAT, GL_FALSE, 0, 0);

	i = 2; // color
	gl_color_size = xyz_col.size() * sizeof(GLfloat);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
	glBufferData(GL_ARRAY_BUFFER, gl_color_size, &xyz_col[0], GL_STATIC_DRAW);
	glEnableVertexAttribArray(i);
	glVertexAttribPointer(i, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindVertexArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(2);
}

And the rendering routine:

void TOpenGL::ogl_draw(void)
{
	if (!ogl_initialized || !fully_initialized || drawing)
		return;

  // Flag that function gets just called ones
  drawing = true;

  // Clear canvas
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Setup camera
	ogl_camera();

	// Scene translation and rotation
	glTranslatef(x_trans, y_trans, zoom_pos);

	// Apply rotation on x
	glRotatef(x_rot, 0.0, 1.0, 0.0);

	// Apply rotation on y
	glRotatef(y_rot, 1.0, 0.0, 0.0);

	// Apply rotation on y
	glRotatef(z_rot, 0.0, 0.0, 1.0);

	// Apply scaling in z direction
	if (auto_scale)
		glScalef(1, 1, hscal_3D);
	else
		glScalef(1, 1, scale_z);

	////////////////////////////////////////////////////////////////////////////
	/////////////////////////// Draw vbo buffer matrix /////////////////////////
	////////////////////////////////////////////////////////////////////////////
	if(wglGetCurrentContext() != hrc)
		return;
	if(wglGetCurrentDC() != hdc)
		return;

	glBindBuffer(GL_ARRAY_BUFFER, vao[0]);
	glVertexPointer(3, GL_FLOAT, 0, 0L);

	glBindBuffer(GL_ARRAY_BUFFER, vao[1]);
	glNormalPointer(GL_FLOAT, 0, 0L);

	glBindBuffer(GL_ARRAY_BUFFER, vao[2]);
	glColorPointer(3, GL_FLOAT, 0, 0L);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);

	glDrawArrays(GL_QUADS, 0, gl_data_size);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

	// Synchronize GPU and CPU
	glFinish();

	// Flush buffer
	glFlush();
	SwapBuffers(hdc);

	// Bring panel to front
	Panel->BringToFront();
	Panel->Update();

  // Flag that function just gets called ones
  drawing = false;
}

Thank you very much for any help!
Thomas

You need to read up on what a “Vertex Array Object (VAO)” is, what a “Buffer Object” is, what’s stored in each, and how you use them.

Among other issues, you don’t bind VAOs with glBindBuffer().

Also, make sure you’re Checking for GL errors. This won’t catch all GL API abuse. But it will catch some of it.

Once you get the basics down, you should consider interleaving your vertex attributes, and using generic vertex attributes rather than the old fixed-function vertex attributes (assuming your rendering with shaders and not the legacy fixed-function shading pipeline).

Thanks for your help!
Unfortunatly I used vao[…] as second argument for glBindBuffer() in the rendering routine instead of vbo[…]. My bad. But it didn’t workout anyways.

But I was able to fix it with make sure (at different parts of my code) that I’m using the right rendering context → using wglMakeCurrent(hdc, hrc).

Also I added some error/exception handles. This will propably help me with finding bugs earlier.

Best regards Thomas