drawing multiple items with opengl (modern)

Viewed 288

A follow up question to the QtOpenGLWidget drawing triangle

I want to draw a triangle and some lines, I wrote this code (with the wonderful help of people in the stackoverflow community) it draws triangle and lines. However, when I want to draw triangle and lines at the same time, I dont see the lines.

  GLfloat vertices[] = { 0.0f, 0.707f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f };
 GLfloat colors[] = { 1.f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
 shader->setAttributeArray(posAttribute, vertices, 3, 0);
 shader->setAttributeArray(colAttribute, colors, 3, 0);
 glEnableVertexAttribArray(posAttribute);
 glEnableVertexAttribArray(colAttribute);
 glDrawArrays(GL_TRIANGLES, 0, 3);
 glDisableVertexAttribArray(posAttribute);
 glDisableVertexAttribArray(colAttribute);

 GLfloat lvertices[] = { 0.907f, 0.907f, 0.5f, -0.957f, -0.957f, 0.5f };
 GLfloat lcolors[] =   { 1.0f,    0.0f, 0.0f,    1.0f,    .0f , 0.0f};
 shader->setAttributeArray(posAttribute, lvertices, 3, 0);
 shader->setAttributeArray(colAttribute, lcolors, 3, 0);
 //glLineWidth(.03f);
 glDrawArrays(GL_LINE_STRIP, 0, 6);
 glDisableVertexAttribArray(posAttribute);
 glDisableVertexAttribArray(colAttribute);

What am I doing wrong ?

1 Answers

After drawing the polygons, you disable the vertex attributes (glDisableVertexAttribArray), but you don't enable them again, before drawing the lines.

Skip disabling the vertex attributes, after drawing the polygons, to solve the issue.


Further, the default depth function (glDepthFunc) is GL_LESS. If the Depth Test is enabled, then a fragment is discarded, if a fragment was draw at the same position before, whose depth (z coordinate) is less or equal, then the depth of the new fragment. This means the fragment is covered by the previous one.

If you want that the lines are always draw on top of the polygons, then you have to disable the depth test, before drawing the lines.

// clear color buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// enable the depth test
glEnable(GL_DEPTH_TEST);

// enable vertex attributes
glEnableVertexAttribArray(posAttribute);
glEnableVertexAttribArray(colAttribute);

// draw the polygons
GLfloat vertices[] = { 0.0f, 0.707f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f };
GLfloat colors[] = { 1.f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
shader->setAttributeArray(posAttribute, vertices, 3, 0);
shader->setAttributeArray(colAttribute, colors, 3, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);

// disable the depth test
glDisable(GL_DEPTH_TEST);

// draw the lines
GLfloat lvertices[] = { 0.907f, 0.907f, 0.5f, -0.957f, -0.957f, 0.5f };
GLfloat lcolors[] =   { 1.0f,    0.0f, 0.0f,    1.0f,    .0f , 0.0f};
shader->setAttributeArray(posAttribute, lvertices, 3, 0);
shader->setAttributeArray(colAttribute, lcolors, 3, 0);
glDrawArrays(GL_LINE_STRIP, 0, 6);

// disable vertex attributes
glDisableVertexAttribArray(posAttribute);
glDisableVertexAttribArray(colAttribute);
Related