OpenGL:动态更改加载的顶点

时间:2017-04-06 16:09:55

标签: c++ qt opengl

使用:Qt 5.8和QOpenGLxxx类。 OpenGL:Core 3.3。

我正在绘制一个球体并在其上投射函数图(Riemmann sphere)。用户可以通过滑块动态地改变其半径,但是我不确定在这种情况下如何处理已经评估和投影的函数的顶点。我个人认为有两种选择:

  1. 使用VBO数据的映射并修改数据。或者使用glBufferSubData。
  2. 删除VBO并使用更新的绘图信息再次创建。
  3. 至少有400个顶点,可能更多,所以性能问题至关重要(好吧,它在任何地方都至关重要)。你觉得怎么样?

1 个答案:

答案 0 :(得分:0)

I've implemented the first option with QOpenGLBuffer::map() and it worked just great. From given options, it's the only one that does not allocate/deallocate memory but merely maps it on the CPU memory, so, I think, for this case it's perfect. The code is sort of this:

void SphereRadiusChanged(float new_radius)
{
   m_vbo.bind();

   VertexClass* vbo_data = static_cast<VertexClass*>( m_vbo.map(QOpenGLBuffer::WriteOnly) );

   if(vbo_data == nullptr)
   {
      ...
   }

   // Here make necessary transformations.
   ...

   m_vbo.unmap();

   m_vbo.release();
}

Probably before that you'll have to make QOpenGLWidget's context current. That function executed inside that class's method, so I didn't do it.