GillesDebunne / libQGLViewer

libQGLViewer is an open source C++ library based on Qt that eases the creation of OpenGL 3D viewers.
Other
241 stars 94 forks source link

Axis and grid are drawn over custom mesh in libQGLViewer >2.6.x after using drawText() #78

Closed dennis2society closed 11 months ago

dennis2society commented 11 months ago

Hi, I am using libQGLViewer in one of my own projects and after upgrading it to >2.6.x I see the axis and grid drawn over my own custom mesh. This does not happen in 2.6.3. The issue could be reproduced with libQGLViewer 2.8.0 and 2.9.1. For me it looks like there is a depth test missing. Funnily enough this does not happen for the spiral that is drawn as default. I have tried adding additional depth tests to my own drawing code but to no avail. Any idea what could be causing this?

Screenshot with libQGLViewer 2.6.3 (this is how it should look): libQGLViewer_2 6 3_axis_and_grid

Screenshot with libQGLViewer 2.8.0: libQGLViewer_2 8 0_axis_and_grid

dennis2society commented 11 months ago

I have subsequently disabled parts of my own code to ensure that none of my own code breaks things to find the actual cause of this and finally had success after disabling all my calls to the QGLViewer::drawText() function. By adding a simple drawText() call I can even reproduce the issue with the default spiral: libQGLViewer_2 8 0_broken_grid_after_drawText This happens before any of my own drawing code is even called. Example code (the implemented draw() function in my QGLViewer app):

//// Draws a spiral
    const float nbSteps = 200.0;
    const uint nb = static_cast<uint>(nbSteps);
    glBegin(GL_QUAD_STRIP);
    for (auto i = 0; i < nb; ++i) {
      float ratio = i / nbSteps;
      float angle = 21.0 * ratio;
      float c = cos(angle);
      float s = sin(angle);
      float r1 = 1.0 - 0.8 * ratio;
      float r2 = 0.8 - 0.8 * ratio;
      float alt = ratio - 0.5;
      const float nor = .5;
      const float up = sqrt(1.0 - nor * nor);
      glColor3f(1.0 - ratio, 0.2f, ratio);
      glNormal3f(nor * c, up, nor * s);
      glVertex3f(r1 * c, alt, r1 * s);
      glVertex3f(r2 * c, alt + 0.05, r2 * s);
    }
    glEnd();
    glColor3f(0.8f, 0.8f, 0.1f);
    QFont f;
    f.setPointSize(18);
    drawText(10, 75, "This text messes up the axis and grid drawing...", f);
dennis2society commented 11 months ago

Never mind... After digging around in qglviewer.cpp and thoroughly looking at the code for drawText I have found a sequence of glDisable/Enable(GL_LIGHTING/GL_DEPTH_TEST/CULL_FACE) before+after the drawText() calls which mostly fix the issue. Obviously this is not an issue in QGLViewer but in my own code.