English 中文(简体)
Using GL_SHORT instead of GL_FLOAT in an OpenGL ES vertex array
原标题:

I have this vertex array of a cube

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,

};

At the moment I render it using

glVertexPointer(3, GL_FLOAT, 0, vertex__coordinates);

// texture pointer ...

// colour pointer

glDrawArrays(GL_TRIANGLES, 0, size);

How would I go about converting the vertex array into values that would render precisely the same cube but instead using GL_SHORT as the second parameter to glVertexPointer in order to speed up my code?

最佳回答

In a preprocessing step we calculate the min and max of the object and use this to maximize the utilization of the precision in a short:

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);
    }
}

And when drawing we do the following:

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.

问题回答

instead of (+-)12.43796 use (+-)1

then apply a glScalef operation on your modelview matrix of 12.43796

I doubt this would speed up your code, however. All it will do is reduce your vertex array to half of its original size.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签