libQGLViewer如何在不清除缓冲区的情况下绘制

libQGLViewer how to draw withouth clearing buffer

我正在使用示例中提供的 libQGLViewer in my project to draw the trajectory of a robot. To this end, I started from the code of the simpleViewer

为了完成我的工作,我将这些代码行放在 draw 函数中:

void Viewer::draw()
{

  glPushMatrix();
  for (auto pose : poses)
  {
    std::vector<float> m;
    poseToVector(pose, m);

    glPushMatrix();
    multMatrix(m);
    drawReferenceSystem();
    glPopMatrix();
  }
  glPopMatrix();
}

其中 poseToVectormultMatrixdrawReferenceSystem 函数是用于实现所需功能的 OpenGL 包装器。

现在,我的问题是 poses 向量在我的代码的每次迭代中都填充了一个新的姿势。因此,我现在正在做的是,在第 1 次迭代中,我绘制姿势 1,在第 2 次迭代中,我绘制姿势 1 和 2,在第 3 次迭代中,我绘制姿势 1,2 和 3,依此类推。

因此,我想知道是否有更有效的方法来执行此操作。但是,如果我只得到向量的 back,我将在每个时刻只看到最后一个姿势。我猜这是因为每次调用 draw 函数时帧缓冲区都会被清除。

因此,我的问题是:是否可以避免清除缓冲区,或者我只是在胡说八道?

preDraw 清除屏幕并重置矩阵。 您可以子类化 QGLViewer 并覆盖它以放弃清除行为。

default implementation是:

void QGLViewer::preDraw() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // this line should be removed here or in a sublass.

  // GL_PROJECTION matrix
  camera()->loadProjectionMatrix();
  // GL_MODELVIEW matrix
  camera()->loadModelViewMatrix();

  Q_EMIT drawNeeded();
}