title: Need help doing simple rendering with Qt5 Qml + OpenGL
body: <p>I need to make it so part of my Qml view is “taken over” by some non-Qt OpenGL rendering, and I was having issues getting a texture to display properly so I thought I would just draw a line and get that to work before moving on to more complicated code.</p>
  
  <p>For those not familiar with Qt5, the entire window is drawn using OpenGL, and I'm hooking into Qt's OpenGL drawing mechanism using their QQuickWindow::beforeRendering() signal which means my painting code is executed every redraw (every vertical sync).</p>
  
  <p>I took the Squircle sample code ( <a href="http://qt-project.org/doc/qt-5/qtquick-scenegraph-openglunderqml-example.html" rel="nofollow">http://qt-project.org/doc/qt-5/qtquick-scenegraph-openglunderqml-example.html</a> ) and modified it slightly so it would draw in a specified portion of the screen (instead of the entire screen) and that is working perfectly. Then, I modified just the renderer::paint() function to initially draw three green lines, and after 2 seconds to instead draw one blue line:</p>
  
  <pre><code>void CtRenderer::paint()
  {
  static int n = 0;
  
  if (n == 0)
  {
      glViewport(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      glEnable(GL_SCISSOR_TEST);
      glScissor(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      glDisable(GL_DEPTH_TEST);
      glDisable(GL_LIGHTING);
  
      // glClearColor(1, 0, 0, 1);
      // glClear(GL_COLOR_BUFFER_BIT);
  
      glLineWidth(10);
      glColor4f(0.0, 1.0, 0.0, 1);
      glBegin(GL_LINES);
      glVertex3f(0.0, 0.0, 1);        glVertex3f(1, 0.5, 1);
      glVertex3f(0.0, 0.0, 1);        glVertex3f(0.5, 0.5, 1);
      glVertex3f(0.0, 0.0, 1);        glVertex3f(0.5, 1, 1);
      glEnd();
  }
  else if (n == 120)
  {
      glViewport(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      glEnable(GL_SCISSOR_TEST);
      glScissor(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      glDisable(GL_DEPTH_TEST);
      glDisable(GL_LIGHTING);
  
      // glClearColor(1, 0, 0, 1);
      // glClear(GL_COLOR_BUFFER_BIT);
  
      glLineWidth(10);
      glColor4f(0.0, 0.0, 1.0, 1);
      glBegin(GL_LINES);
      glVertex3f(0.0, 0.0, 1);        glVertex3f(0.4, 0.8, 1);
      glEnd();
  
  }
  n++;
  
  return;
  </code></pre>
  
  <p>}`</p>
  
  <p>What I get instead are three gray lines that flicker continuously and never change to being a single line. After some online research, I thought that maybe I should not use glBegin()/glEnd() so I changed the code :</p>
  
  <pre><code>void CtRenderer::paint()
  {
  static bool bOnce = true;
  
  if (bOnce)
  {
      glViewport(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      glEnable(GL_SCISSOR_TEST);
      glScissor(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
      float vertices[] = {-0.5f, -0.5f, 0.5f, 0.5f};
  
      glLineWidth(10);
      glColor4f(0.0, 1.0, 0.0, 1);
  
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, vertices);
  
      glDrawArrays(GL_LINES, 0, 2);
  
      glDisableClientState(GL_VERTEX_ARRAY);
  
      bOnce = false;
  }
  
  return;
  }
  </code></pre>
  
  <p>This still gives me a flickering gray line.</p>
  
  <p>When I try this code in a simple GLUT application, outside of Qt, it works just fine, so it seems to be some interaction between OpenGL and Qt5 Qml.  What can I try next?</p>
  
  <p>p.s. I'm using Qt version 5.3 on a Linux Ubuntu box</p>
  
  <p>p.p.s. In response to some comments, I updated my code to look like this:</p>
  
  <pre><code>void dumpGlErrors(int iLine)
  {
  for (;;)
  {
      GLenum err = glGetError();
      if (err == GL_NO_ERROR)
          return;
      std::cout &lt;&lt; "GL error " &lt;&lt; err &lt;&lt; " detected in line " &lt;&lt; iLine &lt;&lt; std::endl;
  }
  }
  
  void CtRenderer::paint()
  {
  glPushMatrix();
  
  glLoadIdentity();
  
  glViewport(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
  glEnable(GL_SCISSOR_TEST);
  glScissor(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
  dumpGlErrors(__LINE__);
  
  glClearColor(1, 0, 1, 1);       // Magenta
  glClear(GL_COLOR_BUFFER_BIT);
  
  bool bDepth = glIsEnabled(GL_DEPTH_TEST);
  glDisable(GL_DEPTH_TEST);
  
  bool bLighting = glIsEnabled(GL_LIGHTING);
  glDisable(GL_LIGHTING);
  
  bool bTexture = glIsEnabled(GL_TEXTURE_2D);
  glDisable(GL_TEXTURE_2D);
  
  dumpGlErrors(__LINE__);
  
  float vertices[] = { -0.5f, -0.5f, 0.1f,  0.5f, 0.5f, 0.1f,  0.5f, 0.5f, 1.0f,  0.5f, -0.5f, 1.0f };
  float colors[] = { 0.0f, 1.0f, 0.0f, 1.0f,  1.0f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f, 1.0f, 1.0f,  1.0f, 1.0f, 0.0f, 1.0f };
  
  glLineWidth(10);
  glColor4f(0.0, 0.0, 1.0, 1);
  
  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3, GL_FLOAT, 0, vertices);
  
  glEnableClientState(GL_COLOR_ARRAY);
  glColorPointer(4, GL_FLOAT, 0, colors);
  
  dumpGlErrors(__LINE__);
  
  glDrawArrays(GL_LINES, 0, 4);       // 4, not 2.
  
  dumpGlErrors(__LINE__);
  
  // glFlush();
  
  glDisableClientState(GL_COLOR_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);
  
  if (bTexture)
      glEnable(GL_TEXTURE_2D);
  
  if (bLighting)
      glEnable(GL_LIGHTING);
  
  if (bDepth)
      glEnable(GL_DEPTH_TEST);
  
  glPopMatrix();
  
  dumpGlErrors(__LINE__);
  
  // In case somebody else calls glClear()
  glClearColor(0, 1, 1, 1);       // Cyan
  }
  </code></pre>
  
  <p>Now I get a nice magenta background and I see my line flash once and then it goes away and I'm left with only magenta.</p>
  
  <p>p.p.p.s.  I tried using VBO type functions:</p>
  
  <pre><code>class Point
  {
  public:
  float m_vertex[3];
  float m_color[4];
  };
  
  void SquircleRenderer::drawBuffer()
  {
  QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
  
  if (!m_bBufInit)
  {
      std::cout &lt;&lt; "SquircleRenderer::drawBuffer()" &lt;&lt; std::endl;
      // Adding these two lines doesn't change anything
      /*
      glFuncs.initializeOpenGLFunctions();
      glFuncs.glUseProgram(0);
      */
  
      // Create a new VBO
      glFuncs.glGenBuffers(1, &amp;m_buf);
  
      // Make the new VBO active
      glFuncs.glBindBuffer(GL_ARRAY_BUFFER, m_buf);
  
      static const Point points[4] = {
          { { -0.5f, -0.5f, 0.1f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
          { {  0.5f,  0.5f, 0.1f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
          { {  0.5f,  0.5f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
          { {  0.5f, -0.5f, 1.0f }, { 1.0f, 1.0f, 0.0f, 1.0f } }
      };
  
      // Upload vertex data to the video device
      glFuncs.glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Point), points, GL_STATIC_DRAW);
  
      dumpGlErrors(__LINE__);
  
      m_bBufInit = true;
  }
  
  glPushMatrix();
  
  glLoadIdentity();
  
  glViewport(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
  glEnable(GL_SCISSOR_TEST);
  glScissor(m_rect.x() + 10, m_rect.y() + 10, m_rect.width() - 20, m_rect.height() - 20);
  
  glClearColor(1, 1, 0, 1);       // Yellow
  glClear(GL_COLOR_BUFFER_BIT);
  
  glFuncs.glBindBuffer(GL_ARRAY_BUFFER, m_buf);
  
  dumpGlErrors(__LINE__);
  
  /*
   * "If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target
   *  (see glBindBuffer) while a vertex array is
   *  specified, pointer is treated as a byte offset into the buffer object's data store"
   */
  
  glLineWidth(10);
  glColor4f(0.0, 0.0, 1.0, 1);
  
  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3, GL_FLOAT, sizeof(Point), 0);
  
  glEnableClientState(GL_COLOR_ARRAY);
  glColorPointer(4, GL_FLOAT, sizeof(Point), (void*)offsetof(Point, m_color));
  
  dumpGlErrors(__LINE__);
  
  glDrawArrays(GL_LINES, 0, 4);       // 4, not 2.
  
  dumpGlErrors(__LINE__);
  
  glDisableClientState(GL_COLOR_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);
  
  // In case somebody else calls glClear()
  glClearColor(0, 1, 1, 1);       // Cyan
  
  glPopMatrix();
  
  dumpGlErrors(__LINE__);
  }
  </code></pre>
  
  <p>As with all my other efforts, I get a yellow background and my lines appear for what I assume is 1/60 second before the lines disappear and all I have is my yellow background.</p>