Performance 提高opengl球体生成器的性能

Performance 提高opengl球体生成器的性能,performance,opengl,rendering,Performance,Opengl,Rendering,我试图用一个应用程序获得至少120fps的速度,但它目前大约是30fps。如果删除球体创建方法,fps将变为120 我的sphere方法检查每个球体是否超出边界。 使用结构并没有提高性能 xc = x coord yc = y coord zc = y coord xd = x direction yd = y direction zd = z direction 有什么方法可以大大提高效率吗 此代码称为每帧 void createSpheres() { for(int

我试图用一个应用程序获得至少120fps的速度,但它目前大约是30fps。如果删除球体创建方法,fps将变为120

我的sphere方法检查每个球体是否超出边界。 使用结构并没有提高性能

 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;  

    }
}
void createSpheres()
{
对于(int i=0;i如果((xc+xd)>=45 | |(xc+xd)=9.5 | |(yc+yd)=45 | |(zc+zd)不使用旧的固定函数管道,请为每个球体的顶点属性创建VAOs和VBOs()

编辑:(添加了有关VBO和VAO的更多基础知识) 在没有VBOs的情况下进行渲染时,每次渲染帧时都会发送几何体数据。使用VBOs可以将模型数据发送到图形卡,然后在没有cpu gpu瓶颈的情况下进行渲染

glGenBuffers(1, &vboHandle);            // create vbo
glBindBuffer(target, vboHandle);        // bind vbo
glBufferData(target, size, data, usage); // send vertex data (position, normals, ..)
然后,必须在VBO数据和着色器属性之间建立连接

// 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);
VAO用于组织VBO-通常会出现以下问题:

enable vertex attrib1
enable vertex attrib2
enable vertex attrib3

renderModel

disable vertex attrib1
disable vertex attrib2
disable vertex attrib3
使用VAOs,您可以将代码工作量减少到

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

这可能是您需要的最少信息量,我建议您阅读opengl superbible第五版的第8章和第12章。

您是否在每个帧上创建球体?乍一看,您可以为xc+(xd/10)、yc+(yd/10)和zc+(zd/10)设置单独的变量在if之后,将其传递给GLTRANSTEF,并将其分配给球体[i]、球体[i+1]和球体[i+2],因此您只需计算一次。我怀疑它是否会产生您希望的效果,但取决于循环的长度,它可以提高性能。@MichaelIV它被称为每帧是的为什么不创建球体几何体一次,然后根据需要进行变换?或者,根据您希望如何使用球体,您也不能使用冒名顶替者而不是大量三角形:改变gluSphere(二次曲面,10.0,72,72)如何;这不是错的,但这不是答案。