我用 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/