2012-09-27 56 views
0

我正在通過OpenGLES 2.0教程進行工作,其中作者渲染了一個帶有紋理的球體。該球體看起來很好,並且是從Wavefront OBJ文件生成的。他所用的標題是這樣的:OpenGLES使用glDrawArrays對頂點進行排序以繪製正確的形狀

/* 
created with obj2opengl.pl 

source file : sphere.obj 
vertices  : 326 
faces   : 648 
normals  : 326 
texture coords : 390 

*/ 

unsigned int sphereNumVerts = 1944; 

float sphereVerts [] = { 
    // f 28/1/1 1/2/2 21/3/3 
    7.6687116638888e-09, 0.466097, 0.115308018404908, 
    -0.0265919923312883, 0.466097, 0.112200018404908, 
    7.6687116638888e-09, 0.4795745, 1.84049079729628e-08, 
    // f 21/4/3 1/2/2 2/5/4 
    7.6687116638888e-09, 0.4795745, 1.84049079729628e-08, 
    -0.0265919923312883, 0.466097, 0.112200018404908, 
    -0.0517499923312883, 0.466097, 0.103043018404908, 
    // f 21/6/3 2/5/4 3/7/5 
    7.6687116638888e-09, 0.4795745, 1.84049079729628e-08, 
    -0.0517499923312883, 0.466097, 0.103043018404908, 
    -0.0741184923312883, 0.466097, 0.088331018404908, 

... TRUNCATED ... 

    // f 326/388/326 323/361/324 322/360/323 
    7.6687116638888e-09, -0.5204255, 1.84049079729628e-08, 
    0.0767490076687117, -0.490272, 0.152820018404908, 
    0.109923007668712, -0.490272, 0.131001518404908, 
    // f 326/389/326 324/362/325 323/361/324 
    7.6687116638888e-09, -0.5204255, 1.84049079729628e-08, 
    0.0394375076687117, -0.490272, 0.166400518404908, 
    0.0767490076687117, -0.490272, 0.152820018404908, 
    // f 326/390/326 325/363/299 324/362/325 
    7.6687116638888e-09, -0.5204255, 1.84049079729628e-08, 
    7.6687116638888e-09, -0.490272, 0.171010018404908, 
    0.0394375076687117, -0.490272, 0.166400518404908, 
}; 

float sphereNormals [] = { 
    // f 28/1/1 1/2/2 21/3/3 
    -0.00802618948953466, 0.967367838494132, 0.253248978930753, 
    -0.0662268425664943, 0.967371914269316, 0.244551803932402, 
    0, 1, -0, 
    // f 21/4/3 1/2/2 2/5/4 
    0, 1, -0, 
    -0.0662268425664943, 0.967371914269316, 0.244551803932402, 
    -0.120826192029109, 0.967370556337772, 0.222699883364347, 
    // f 21/6/3 2/5/4 3/7/5 
    0, 1, -0, 
    -0.120826192029109, 0.967370556337772, 0.222699883364347, 
    -0.168924852220773, 0.967372786949467, 0.18882342379009, 
    // f 21/8/3 3/7/5 4/9/6 

... TRUNCATED ... 

    0, -1, -0, 
    0.163004478217867, -0.939604578019474, 0.300953114364626, 
    0.22801091084251, -0.939601478627697, 0.255264736885873, 
    // f 326/389/326 324/362/325 323/361/324 
    0, -1, -0, 
    0.0891781381121659, -0.939606064036581, 0.330435627783284, 
    0.163004478217867, -0.939604578019474, 0.300953114364626, 
    // f 326/390/326 325/363/299 324/362/325 
    0, -1, -0, 
    0.0105903724426618, -0.939606044104724, 0.342094030777207, 
    0.0891781381121659, -0.939606064036581, 0.330435627783284, 
}; 

float sphereTexCoords [] = { 
    // f 28/1/1 1/2/2 21/3/3 
    0.000000, 0.925926, 
    0.037037, 0.925926, 
    0.000000, 1.000000, 
    // f 21/4/3 1/2/2 2/5/4 
    0.037037, 1.000000, 
    0.037037, 0.925926, 
    0.074074, 0.925926, 
    // f 21/6/3 2/5/4 3/7/5 
    0.074074, 1.000000, 
    0.074074, 0.925926, 
    0.111111, 0.925926, 

... TRUNCATED ... 

    // f 326/388/326 323/361/324 322/360/323 
    0.888889, 0.037037, 
    0.925926, 0.111111, 
    0.888889, 0.111111, 
    // f 326/389/326 324/362/325 323/361/324 
    0.925926, 0.037037, 
    0.962963, 0.111111, 
    0.925926, 0.111111, 
    // f 326/390/326 325/363/299 324/362/325 
    0.962963, 0.037037, 
    1.000000, 0.111111, 
    0.962963, 0.111111, 
}; 

我做我自己的對象,我用Cheetah3D它導出到一個頭文件,它看起來像這樣:

// Headerfile *.h (generated by Cheetah3D) 
// 
// There are the following name conventions: 
// NAME   =name of the object in Cheetah3D. Caution!! Avoid giving two objects the same name 
// NAME_vertex  =float array which contains the vertex,normal and uvcoord data 
// NAME_index  =int array which contains the polygon index data 
// NAME_vertexcount =number of vertices 
// NAME_polygoncount =number of triangles 
// 
// The vertex data is saved in the following format: 
// u0,v0,normalx0,normaly0,normalz0,x0,y0,z0 
// u1,v1,normalx1,normaly1,normalz1,x1,y1,z1 
// u2,v2,normalx2,normaly2,normalz2,x2,y2,z2 
// ... 
// You can draw the mesh the following way: 
// glEnableClientState(GL_INDEX_ARRAY); 
// glEnableClientState(GL_NORMAL_ARRAY); 
// glEnableClientState(GL_VERTEX_ARRAY); 
// glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
// glInterleavedArrays(GL_T2F_N3F_V3F,0,NAME_vertex); 
// glDrawElements(GL_TRIANGLES,NAME_polygoncount*3,GL_UNSIGNED_INT,NAME_index); 
// 

#define Box_vertexcount  24 
#define Box_polygoncount 12 


float Box_vertex[Box_vertexcount][8]={ 
     {0.00000, 1.00000, 0.00000, 0.00000, 1.00000, -0.15000, -0.15000, 0.15000}, 
     {0.00000, 0.00000, 0.00000, 0.00000, 1.00000, -0.15000, 0.15000, 0.15000}, 
     {1.00000, 0.00000, 0.00000, 0.00000, 1.00000, 0.15000, 0.15000, 0.15000}, 
     {1.00000, 1.00000, 0.00000, 0.00000, 1.00000, 0.15000, -0.15000, 0.15000}, 
     {0.00000, 1.00000, 0.00000, 0.00000, -1.00000, 0.15000, -0.15000, -0.15000}, 
     {0.00000, 0.00000, 0.00000, 0.00000, -1.00000, 0.15000, 0.15000, -0.15000}, 
     {1.00000, 0.00000, 0.00000, 0.00000, -1.00000, -0.15000, 0.15000, -0.15000}, 
     {1.00000, 1.00000, 0.00000, 0.00000, -1.00000, -0.15000, -0.15000, -0.15000}, 
     {0.00000, 1.00000, -1.00000, 0.00000, 0.00000, -0.15000, -0.15000, -0.15000}, 
     {0.00000, 0.00000, -1.00000, 0.00000, 0.00000, -0.15000, 0.15000, -0.15000}, 
     {1.00000, 0.00000, -1.00000, 0.00000, 0.00000, -0.15000, 0.15000, 0.15000}, 
     {1.00000, 1.00000, -1.00000, 0.00000, 0.00000, -0.15000, -0.15000, 0.15000}, 
     {0.00000, 1.00000, 1.00000, 0.00000, 0.00000, 0.15000, -0.15000, 0.15000}, 
     {0.00000, 0.00000, 1.00000, 0.00000, 0.00000, 0.15000, 0.15000, 0.15000}, 
     {1.00000, 0.00000, 1.00000, 0.00000, 0.00000, 0.15000, 0.15000, -0.15000}, 
     {1.00000, 1.00000, 1.00000, 0.00000, 0.00000, 0.15000, -0.15000, -0.15000}, 
     {0.00000, 1.00000, 0.00000, 1.00000, 0.00000, -0.15000, 0.15000, 0.15000}, 
     {0.00000, 0.00000, 0.00000, 1.00000, 0.00000, -0.15000, 0.15000, -0.15000}, 
     {1.00000, 0.00000, 0.00000, 1.00000, 0.00000, 0.15000, 0.15000, -0.15000}, 
     {1.00000, 1.00000, 0.00000, 1.00000, 0.00000, 0.15000, 0.15000, 0.15000}, 
     {0.00000, 1.00000, 0.00000, -1.00000, 0.00000, -0.15000, -0.15000, -0.15000}, 
     {0.00000, 0.00000, 0.00000, -1.00000, 0.00000, -0.15000, -0.15000, 0.15000}, 
     {1.00000, 0.00000, 0.00000, -1.00000, 0.00000, 0.15000, -0.15000, 0.15000}, 
     {1.00000, 1.00000, 0.00000, -1.00000, 0.00000, 0.15000, -0.15000, -0.15000}, 
     }; 


int Box_index[Box_polygoncount][3]={ 
     {0, 1, 2}, 
     {2, 3, 0}, 
     {4, 5, 6}, 
     {6, 7, 4}, 
     {8, 9, 10}, 
     {10, 11, 8}, 
     {12, 13, 14}, 
     {14, 15, 12}, 
     {16, 17, 18}, 
     {18, 19, 16}, 
     {20, 21, 22}, 
     {22, 23, 20}, 
     }; 

我把它轉換爲相同的格式從OBJ文件的例子頭文件,所以它看起來像:

// sphere_3d.h Header File created from Cheetah 3d sphere File 


unsigned int sphereNumVerts = 24; 


float sphereTexCoords [] = { 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
     0.00000, 1.00000, 
     0.00000, 0.00000, 
     1.00000, 0.00000, 
     1.00000, 1.00000, 
}; 


float sphereNormals [] = { 
     0.00000, 0.00000, 1.00000, 
     0.00000, 0.00000, 1.00000, 
     0.00000, 0.00000, 1.00000, 
     0.00000, 0.00000, 1.00000, 
     0.00000, 0.00000, -1.00000, 
     0.00000, 0.00000, -1.00000, 
     0.00000, 0.00000, -1.00000, 
     0.00000, 0.00000, -1.00000, 
     -1.00000, 0.00000, 0.00000, 
     -1.00000, 0.00000, 0.00000, 
     -1.00000, 0.00000, 0.00000, 
     -1.00000, 0.00000, 0.00000, 
     1.00000, 0.00000, 0.00000, 
     1.00000, 0.00000, 0.00000, 
     1.00000, 0.00000, 0.00000, 
     1.00000, 0.00000, 0.00000, 
     0.00000, 1.00000, 0.00000, 
     0.00000, 1.00000, 0.00000, 
     0.00000, 1.00000, 0.00000, 
     0.00000, 1.00000, 0.00000, 
     0.00000, -1.00000, 0.00000, 
     0.00000, -1.00000, 0.00000, 
     0.00000, -1.00000, 0.00000, 
     0.00000, -1.00000, 0.00000, 
}; 


float sphereVerts [] = { 
     -0.15000, -0.15000, 0.15000, 
     -0.15000, 0.15000, 0.15000, 
     0.15000, 0.15000, 0.15000, 
     0.15000, -0.15000, 0.15000, 
     0.15000, -0.15000, -0.15000, 
     0.15000, 0.15000, -0.15000, 
     -0.15000, 0.15000, -0.15000, 
     -0.15000, -0.15000, -0.15000, 
     -0.15000, -0.15000, -0.15000, 
     -0.15000, 0.15000, -0.15000, 
     -0.15000, 0.15000, 0.15000, 
     -0.15000, -0.15000, 0.15000, 
     0.15000, -0.15000, 0.15000, 
     0.15000, 0.15000, 0.15000, 
     0.15000, 0.15000, -0.15000, 
     0.15000, -0.15000, -0.15000, 
     -0.15000, 0.15000, 0.15000, 
     -0.15000, 0.15000, -0.15000, 
     0.15000, 0.15000, -0.15000, 
     0.15000, 0.15000, 0.15000, 
     -0.15000, -0.15000, -0.15000, 
     -0.15000, -0.15000, 0.15000, 
     0.15000, -0.15000, 0.15000, 
     0.15000, -0.15000, -0.15000, 
}; 

我想,這樣我就不會使用索引陣列可以使用調用glDrawArrays而非glDrawElements。當我在我的程序中通過試圖替代自定義對象的頭文件而不是示例程序中的示例對象時,該形狀通常(有點)看起來像我替換的對象,因爲許多三角形都是關閉的而且沒有連接到他們應該在的位置是。

我知道我的頂點被傳遞給glDrawArrays的順序不正確,但我不知道爲什麼。我如何對它們進行排序,以便它們符合glDrawArrays的要求?

回答

1

有什麼理由不想使用glDrawElements?如果您查看Cheetah3D頭文件中的Box_index數組,您會看到一些頂點被多次使用,這意味着要使用glDrawArrays繪製相同的對象,您將不得不復制一些頂點(因此需要更多內存)。使用Box_index數組,你可以使用glDrawElements繪製你的對象:

glDrawElements(GL_TRIANGLES, Box_polygoncount * 3, GL_UNSIGNED_INT, Box_index); 

(注意,如果你有0-256頂點,你應該使用指數個字節,短褲,如果你有257-65536之間)

+0

謝謝喬納森,我已經用Cheetah3D文件實際完成了(使用glDrawElements)。不過,我想用glDrawArrays這樣做,因爲它主要是一個學習練習。 glDrawElements調用中使用的頂點順序與glDrawArrays不相符。我很好奇爲什麼不呢?我如何對它們進行分類,使它們合理並且看起來正確?謝謝! – PhilBot

+0

您必須按照索引數組(Box_index)所描述的順序放置它們。 – jonathan

+0

感謝您的幫助 – PhilBot

相關問題