2013-05-03 14 views
0

我想要獲得至少120fps的應用程序,但它目前坐落在30fps左右。如果刪除我的球體創建方法,則fps將變爲120.改善opengl球形發生器的性能

我的球體方法會檢查每個球體以查看它是否超出邊界。 使用結構不會提高性能。

xc = x coord 
yc = y coord 
zc = y coord 
xd = x direction 
yd = y direction 
zd = z direction 

有什麼辦法可以大幅提高效率嗎?

這段代碼被稱爲每一幀。

void createSpheres() 
{ 
    for(int i = 0; i < spheres.size(); i+=6) 
    { 
     xc = spheres[i]; 
     yc = spheres[i+1]; 
     zc = spheres[i+2]; 
     xd = spheres[i+3]; 
     yd = spheres[i+4]; 
     zd = spheres[i+5]; 


       if((xc+xd)>= 45 || (xc+xd)<= -45) 
       { 
           xd = 0-xd; 
       } 
       if((yc+yd)>= 9.5 || (yc+yd)<= -9.5) 
       { 
           yd = 0-yd; 
       } 
       if((zc+zd)>= 45 || (zc+zd)<= -45) 
       { 
           zd = 0-zd; 
       } 
       glEnable(GL_TEXTURE_2D); 
       glBindTexture (GL_TEXTURE_2D, texture_id[6]); 
       glPushMatrix(); 
       glTranslatef(xc+(xd/10), yc+(yd/10), zc+(zd/10)); 
       glRotatef(-80,1,0,0); 
       glScalef(0.10f, 0.10f, 0.10f); 
       gluQuadricTexture(quadric,1); 
       gluSphere(quadric,10.0,72,72); 
       glPopMatrix(); 
       glDisable(GL_TEXTURE_2D); 
       spheres[i] = xc+(xd/10); 
       spheres[i+1] = yc+(yd/10); 
       spheres[i+2] = zc+(zd/10); 
       spheres[i+3] = xd; 
       spheres[i+4] = yd; 
       spheres[i+5] = zd; 

    } 
} 
+3

您是否在每個框架上創建球體? – 2013-05-03 11:26:23

+0

乍一看,您可以在您的if之後爲xc +(xd/10),yc +(yd/10)和zc +(zd/10)設置單獨的變量並將它們傳遞給glTranslatef並將它們分配給球體[i],球體[我+ 1]和球[I + 2],所以你只需要計算一次。我懷疑它會產生你期望的影響,但取決於你的循環的長度,它可以提高性能。 – Kevin 2013-05-03 12:26:05

+0

@MichaelIV它被稱爲每幀是 – 2013-05-03 13:48:41

回答

1

不要使用舊固定功能管線,每個球的頂點屬性創建VAOs和維也納組織(http://www.opengl.org/wiki/Vertex_Specification)。

編輯:(添加了更多有關VBOs和VAOs的基礎知識) 當您不使用VBO進行渲染時,每次渲染幀時都會發送幾何數據。使用VBOs允許您將模型數據發送到您的圖形卡,然後在沒有cpu-gpu瓶頸的情況下進行渲染。

glGenBuffers(1, &vboHandle);   // create vbo 
glBindBuffer(target, vboHandle);  // bind vbo 
glBufferData(target, size, data, usage); // send vertex data (position, normals, ..) 

,那麼你必須樹立

// get location of the in-attribute of the shader program 
vertexLocation = glGetAttribLocation(programHandle,"vertex"); 
// activate desired VBO 
glBindBuffer(GL_ARRAY_BUFFER, vboHandle); 
// set attribute-pointer 
glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); 
// finally enable attribute-array 
glEnableVertexAttribArray(vertexLocation); 

VAOs用於組織你的維也納組織您的VBO數據和着色器之間的連接屬性 - 通常你結束了以下問題:

enable vertex attrib1 
enable vertex attrib2 
enable vertex attrib3 

renderModel 

disable vertex attrib1 
disable vertex attrib2 
disable vertex attrib3 

使用VAO您可以將代碼減少到

enable VAO 

renderModel 

disable VAO 

所以組合的代碼片段可能看起來像:

// Create and bind VAO 
glGenVertexArrays(1, &vaoId); 
glBindVertexArray(vaoId); 

// create and bind vbo 
glGenBuffers(1, &vboId); 
glBindBuffer(GL_ARRAY_BUFFER, vboId); 
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); 

GLint loc = glGetAttribLocation(programHandle, "attrib1"); 
glEnableVertexAttribArray(loc); 
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0); 
// other vbos, attrib pointers 
... 
// UnbindVAO 
glBindVertexArray(0); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 

這可能是信息的最小量你需要,我建議閱讀的章節8和OpenGL的OpenGL超級第五版的12。

+0

這並非假,但那不是一個答案。 – Calvin1602 2013-05-21 15:45:18