我正在创建一个Opengl字体批量绘图.
我想知道为什么我的std :: vector比使用数组指针慢得多.
我试过添加矢量储备和我能想到的一切.
使用矢量时,我的FPS下降了一半.
向量似乎更易于管理并且在代码中看起来更好,但我真的坚持这一点.
不确定我错过了什么简单的东西?
或者只使用数组指针更好?
#ifdef USE_VECTORS
std::vector<Vertex> m_vertices;
#else
Vertex *m_pVertex;
Vertex m_vertices[MAX_VERTICES];
#endif
Run Code Online (Sandbox Code Playgroud)
绘图char函数
void GLFont::drawChar(char c, int x, int y)
{
// 1------4
// | | 1 = (x, y)
// | | 2 = (x, y + charHeight)
// | | 3 = (x + charWidth, y + charHeight)
// | | 4 = (x + charWidth, y)
// | |
// | |
// 2------3
//
const Glyph &glyph = getChar(c);
int charWidth = glyph.width;
int charHeight = m_charHeight;
#ifdef USE_VECTORS
Vertex vert[] = {
x, y,
glyph.upperLeft[0], glyph.upperLeft[1],
m_color[0], m_color[1], m_color[2], m_color[3],
x, y + charHeight,
glyph.lowerLeft[0], glyph.lowerLeft[1],
m_color[0], m_color[1], m_color[2], m_color[3],
x + charWidth, y + charHeight,
glyph.lowerRight[0], glyph.lowerRight[1],
m_color[0], m_color[1], m_color[2], m_color[3],
x + charWidth, y,
glyph.upperRight[0], glyph.upperRight[1],
m_color[0], m_color[1], m_color[2], m_color[3]
};
//unsigned dataArraySize = sizeof(vert) / sizeof(Vertex);
m_vertices.insert(m_vertices.end(), &vert[0], &vert[4]);
++m_numCharsToDraw;
#else
//1
m_pVertex->x = x;
m_pVertex->y = y;
m_pVertex->s = glyph.upperLeft[0];
m_pVertex->t = glyph.upperLeft[1];
m_pVertex->r = m_color[0];
m_pVertex->g = m_color[1];
m_pVertex->b = m_color[2];
m_pVertex->a = m_color[3];
++m_pVertex;
// 2
m_pVertex->x = x;
m_pVertex->y = y + charHeight;
m_pVertex->s = glyph.lowerLeft[0];
m_pVertex->t = glyph.lowerLeft[1];
m_pVertex->r = m_color[0];
m_pVertex->g = m_color[1];
m_pVertex->b = m_color[2];
m_pVertex->a = m_color[3];
++m_pVertex;
// 3
m_pVertex->x = x + charWidth;
m_pVertex->y = y + charHeight;
m_pVertex->s = glyph.lowerRight[0];
m_pVertex->t = glyph.lowerRight[1];
m_pVertex->r = m_color[0];
m_pVertex->g = m_color[1];
m_pVertex->b = m_color[2];
m_pVertex->a = m_color[3];
++m_pVertex;
// 4
m_pVertex->x = x + charWidth;
m_pVertex->y = y;
m_pVertex->s = glyph.upperRight[0];
m_pVertex->t = glyph.upperRight[1];
m_pVertex->r = m_color[0];
m_pVertex->g = m_color[1];
m_pVertex->b = m_color[2];
m_pVertex->a = m_color[3];
++m_pVertex;
if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
{
drawTextEnd();
drawBatchOfChars();
drawTextBegin();
}
#endif
}
void GLFont::drawBatchOfChars()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
#ifdef USE_VECTORS
glVertexPointer(2, GL_INT, sizeof(Vertex), &m_vertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &m_vertices[0].s);
glColorPointer(4, GL_FLOAT, sizeof(Vertex), &m_vertices[0].r);
#else
glVertexPointer(2, GL_INT, sizeof(Vertex), &m_vertices->x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &m_vertices->s);
glColorPointer(4, GL_FLOAT, sizeof(Vertex), &m_vertices->r);
#endif
glDrawArrays(GL_QUADS, 0, m_numCharsToDraw * 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
Run Code Online (Sandbox Code Playgroud)
您正在将苹果与橙子进行比较:
vec[123]如果索引超出范围,则访问via 会导致每个标准的未定义行为.这允许通过指向数组有效负载的指针将其实现为简单索引.通过额外的诊断,索引被验证,这只是一个小比较和分支,但在紧密循环中它会产生影响.也就是说,你证明某事的方法是有缺陷的.您首先必须实现尽可能减少的等效代码(本着MCVE的精神).将OpenGL后端挂钩到它上面并不会使事情重现.
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |