2009-11-19 133 views
1

我有一個立方體在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, 

}; 

的這個頂點數組目前使用GL_SHORT代替GL_FLOAT我使其使用

glVertexPointer(3, GL_FLOAT, 0, vertex__coordinates); 

// texture pointer ... 

// colour pointer 

glDrawArrays(GL_TRIANGLES, 0, size); 

我怎麼會去轉換頂點數組轉換成的值可以精確地呈現相同的 立方體,但是使用GL_SHORT作爲glVertexPointer的第二個參數,以便加速 我的代碼?

回答

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

/AB

0

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

然後就12.43796

您的模型視圖矩陣應用glScalef操作我懷疑然而,這會加速你的代碼。它所要做的就是將頂點數組縮小爲其原始大小的一半。

+0

謝謝!然而,如果我有多個不同尺寸(例如+ -6)的多個 立方體,我將如何應用此邏輯? – Dimitris 2009-11-19 12:44:38

+2

在iPhone上,減少頂點陣列的尺寸可顯着提升性能。從花車轉換到短褲本身提升了我的渲染性能30%。 Dimitris,爲了將其應用於各種情況,您只需將浮點值標準化爲+ -32767即可。找到任何頂點座標的最大絕對值,並將所有頂點值乘以32767除以該值。然後,您可以調整模型視圖或投影矩陣以匹配新的縮放大小。 – 2009-11-19 17:05:26

+0

布拉德的建議可行,但前提是你的模型大致集中在(0,0,0)左右。如果不是,你需要定期的規模和偏見。 – Alan 2009-11-19 19:45:52