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