我使用 Eigen3 二维向量作为 opengl 绘图的 2D 点,将它们存储在列表中:
typedef Eigen::Vector2d Vec2D;
std::list<Vec2D> points;
现在,我需要一个 GLfloat 数组来将原始浮点坐标值的整个数据结构传递给显卡:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
_vertex = new GLfloat[points.size()*2];
_colors = new GLfloat[points.size()*4];
std::list<Vec2D>::const_iterator it;
int i=0, j=0;
for(it=points.begin(); it!=points.end(); ++it) {
_vertex[i] = it->x()+2;
_vertex[i+1] = it->y()+2;
i+=2;
_colors[j] = getRed(j/4.0f, it);
_colors[j+1] = getGreen(j/4.0f, it);
_colors[j+2] = getBlue(j/4.0f, it);
_colors[j+3] = getAlpha(j/4.0f, it);
j+=4;
}
glColorPointer(4, GL_FLOAT, 0, _colors);
glVertexPointer(2, GL_FLOAT, 0, _vertex);
glDrawArrays(GL_LINE_STRIP, 0, points.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
delete _vertex;
delete _colors;
有没有更有效的方法来创建阵列以传递给显卡?像通过points.begin()
并找出偏移量是什么并避免遍历所有点?
我的意思是..在内存中的x和y坐标Eigen::Vector2d
必须存储在一些连续的空间中..所以..我想我可以将它直接传递给显卡..但我无法理解如何。