在OpenGL ES顶点数组中使用GL_SHORT而不是GL_FLOAT

时间:2009-11-19 12:11:16

标签: iphone opengl-es

我有一个立方体的顶点数组

float vertex_coordinates [] = {

-12.43796, -12.43796, 12.43796, -12.43796, 12.43796, 12.43796, 12.43796, 12.43796, 12.43796,
12.43796, 12.43796, 12.43796, 12.43796, -12.43796, 12.43796, -12.43796, -12.43796, 12.43796,
12.43796, -12.43796, -12.43796, 12.43796, 12.43796, -12.43796, -12.43796, 12.43796, -12.43796,
-12.43796, 12.43796, -12.43796, -12.43796, -12.43796, -12.43796, 12.43796, -12.43796, -12.43796,
-12.43796, -12.43796, -12.43796, -12.43796, 12.43796, -12.43796, -12.43796, 12.43796, 12.43796,
-12.43796, 12.43796, 12.43796, -12.43796, -12.43796, 12.43796, -12.43796, -12.43796, -12.43796,
12.43796, -12.43796, 12.43796, 12.43796, 12.43796, 12.43796, 12.43796, 12.43796, -12.43796,
12.43796, 12.43796, -12.43796, 12.43796, -12.43796, -12.43796, 12.43796, -12.43796, 12.43796,
-12.43796, 12.43796, 12.43796, -12.43796, 12.43796, -12.43796, 12.43796, 12.43796, -12.43796,
12.43796, 12.43796, -12.43796, 12.43796, 12.43796, 12.43796, -12.43796, 12.43796, 12.43796,
-12.43796, -12.43796, -12.43796, -12.43796, -12.43796, 12.43796, 12.43796, -12.43796, 12.43796,
12.43796, -12.43796, 12.43796, 12.43796, -12.43796, -12.43796, -12.43796, -12.43796, -12.43796,

};

目前我使用

渲染它
glVertexPointer(3, GL_FLOAT, 0, vertex__coordinates);

// texture pointer ...

// colour pointer

glDrawArrays(GL_TRIANGLES, 0, size);

如何将顶点数组转换为精确渲染的值 立方体,而是使用GL_SHORT作为glVertexPointer的第二个参数,以加快速度 我的代码?

2 个答案:

答案 0 :(得分:3)

在预处理步骤中,我们计算对象的最小值和最大值,并使用它来最大限度地利用精度:

float modelMin[3] = {FLT_MAX, FLT_MAX, FLT_MAX}; //or std::numeric_limits<float>
float modelMax[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
for (int i = 0; i < size; ++i) {
    for (int j = 0; j < 3; ++j) {
        const float v = vertex_coordinates[i * 3 + j];
        modelMin[j] = std::min(modelMin[j], v);
        modelMax[j] = std::max(modelMax[j], v);
    }
}

short* short_coordinates = new short[size * 3];
for (int i = 0; i < size; ++i) {
    for (int j = 0; j < 3; ++j) {
        const float src = vertex_coordinates[i * 3 + j];
        short& dst = short_coordinats[i * 3 + j];
        dst = (short)floorf(((src - modelMin[j]) / (modelMax[j] - modelMin[j])) * 65536.0f - 32768.0f + 0.5f);
    }
}

绘图时我们执行以下操作:

const float scale[3], bias[3];
for (int i = 0; i < 3; ++i) {
    scale[i] = (modelMax[j] - modelMin[j]) / 65536.0f;
    bias[i] = (32768.0f / 65536.0f) * (modelMax[j] - modelMin[j]) + modelMin[j];
}

glTranslatef(bias[0], bias[1], bias[2]);
glScalef(scale[0], scale[1], scale[2]);
glVertexPointer(3, GL_SHORT, 0, short_coordinates);
glDrawArrays(GL_TRIANGLES, 0, size);

/A.B。

答案 1 :(得分:0)

代替(+ - )12.43796使用(+ - )1

然后在您的modelview矩阵12.43796上应用glScalef操作

我怀疑这会加速你的代码。它所做的只是将顶点数组减少到原始大小的一半。