3

I have a subclass "GLWidget" of QGLWidget where I render a 3D scene. I can do rotations and zoom on it.

I call with a QTimer the following function :

void GLWidget::processCurrent()
{
    draw();
    printStats();
    glFlush();
    swapBuffers();
}

and the main OpenGL render function "draw()" is :

void GLWidget::draw()
    {
      if (isDisplayFirst)
      {
        isDisplayFirst = false;
        glViewport(0, 0, w_width, w_height);

        glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
        glLoadIdentity(); // Reset The Projection Matrix

        glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
        glLoadIdentity(); // Reset The Modelview Matrixi

        gluPerspective(45.0f, (float)w_width / w_height, g_nearPlane, g_farPlane);
        gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
        glScalef(0.03f, 0.03f, 0.03f);
      }

      rotateScene();

      glClearColor(0.0 ,0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      glEnable(GL_POINT_SPRITE);
      glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
      glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);

      glEnable(GL_BLEND);
      glBlendFunc (GL_SRC_ALPHA, GL_ONE);

      GLuint vbo_disk = 0;

        glBindBuffer(GL_ARRAY_BUFFER, vbo_disk);
        glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos);
        glEnableClientState(GL_VERTEX_ARRAY);

        glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
        glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glDisableClientState(GL_VERTEX_ARRAY);

        glDisable(GL_BLEND);
        glDisable(GL_POINT_SPRITE);
    }

I have already rendered 2D text (statistics) on this GLWidget with "printStats()" function. I would like now to draw a "2D scale line" on this 3D scene.

I succeed in draw this line by doing into printStats():

 glDisable(GL_TEXTURE_2D);
  glColor3f(1.0f, 1.0f, 1.0f);
  glLineWidth(3.0f);
  glBegin(GL_LINES);
  glVertex2d(5, 40);
  glVertex2d(20, 40);
  glEnd();
  glEnable(GL_TEXTURE_2D);

but my problem is that when I do rotations or zoom, this line moves with the 3D scene.

What is the way to lock this 2D line while being able to do operations on the 3D object ?

I want to avoid to use "overpainting" with overriding paintEvent.

I also try to use glPush/Pop Matrix but there are conflicts between 2D and 3D.

Could you give me some advice to get it ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

3

You are trying to implement HUD functionality, in principle what you need to to draw your 3D scene normaly, then backup all the OpenGL states that you want to reuse, and set up a new projection matrix with gluOrtho2D or glOrtho. Additionally you want to turn off the depth check.

After the call to the ortho function you will most likely draw in screen scale, so you will have to calculate the size of your line accordingly. Once you are done with the hud, you can pop the changes off the stack so you can draw again.

There are a couple of questions on stackoverflow that deal with drawing an OpenGl HUD

and a pretty decent discussion on the gamedev forum

Community
  • 1
  • 1
Harald Scheirich
  • 9,676
  • 29
  • 53