c++ - OpenGL:在每次迭代中绘制所有场景?

标签 c++ opengl

我用 C++ 编写了一些包装器以在 OpenGL 场景中绘制一些 3D 点。我在 OpenGL 方面不是很有经验,我不确定我是否按预期遵循管道。这或多或少是我的代码:

  • 主要功能:

    std::vector<Element> elements(1);
    while (true) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      for (auto& element : elements) element.Draw();
      ... // some other operations and buffer swapping
    }
    
  • 元素类:

    class Element {
     public:
       Element() {
         ... // fill m_xyz, m_rgb and m_pose
       }
       void Draw() {
         // translate
         glPushMatrix();
         glRotatef(m_pose[3], m_pose[4], m_pose[5], m_pose[6]);
         glTranslatef(m_pose[0], m_pose[1], m_pose[2]);
         // draws 3D points
         glEnableClientState(GL_VERTEX_ARRAY);
         glEnableClientState(GL_COLOR_ARRAY);
         glVertexPointer(3, GL_FLOAT, 0, m_xyz.data());
         glColorPointer(3, GL_FLOAT, 0, m_rgb.data());
         glDrawArrays(GL_POINTS, 0, m_xyz.size() / 3);
         glDisableClientState(GL_COLOR_ARRAY);
         glDisableClientState(GL_VERTEX_ARRAY);
         // retrieve previous pose
         glPopMatrix();
       }
     private:
       std::vector<float> m_xyz, m_rgb, m_pose;
    };
    

我是否必须在主循环的每次迭代中调用 glDrawArrays

Draw函数有没有不需要一直执行的代码?

这就是所有这些功能的预期使用方式吗?

有没有更快的方法?

最佳答案

Do I have to call glDrawArrays at every iteration of the main loop?

是的。事实上,您将在每次绘制单个帧时调用它多次。

Is there any code of the Draw function that doesn't need to be executed all the time?

在您的代码中:不。您在 draw 中拥有的所有内容都属于那里。

Is this how the all these functions are expected to be used?

是的,大部分情况下。

Is there a faster way of doing this?

速度慢吗?你有介绍过吗?

关于c++ - OpenGL:在每次迭代中绘制所有场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882275/

相关文章:

c++ - 如何启用共享对象在运行时访问数据文件 (UNIX)

c++ - 面向对象 : Method for cout output

c++ - 如何将 OpenGL 绘图分成类

c++ - 为什么 c++ 中的分配器为 void 类型提供专门化

c++ - 如何为 makefile 创建目标

边界框的 OpenGL 深度计算

c++ - 使用 OpenGL 绘图,无需杀死 CPU 且无需并行化

Java - 通过设置绘制立方体面的点的位置来设置旋转

c++ - 使用 glVertex2i() 绘制图形显示使用 glScaled() 缩放时的人工制品

c++ - 现在有哪些应用程序是用 C++ 编写的