OpenGL中立即模式的性能问题

时间:2015-09-13 17:53:58

标签: c++ performance opengl graphics glfw

如何将这段直接代码转换为VBO?

我的代码存在性能问题,我需要帮助才能通过使用VBO而不是立即模式来加快速度。有人能告诉我如何将其转换为VBO以提高性能吗?

此代码在代码的主循环while(true)中执行,以绘制一系列正方形,这些正方形可能会更改其颜色以表示名为cmp的矩阵。 这个cmp矩阵表示来自蛇游戏的场,可能有NADA(Nothing),Snake,Apple或MURO(Wall),并且根据矩阵的值,它会在开关上改变颜色。

我是GLFW和Opengl的新手,因此我不知道如何使用VBO和高级图形技术。

如果您愿意,可以找到整个评论代码here

for(it a=0;a<(camp.Getlar())/2;a++)//line of quadrant -+
{
    for(int b=0;b<(camp.Getalt())/2;b++)//column
    {
        glBegin(GL_POLYGON);

        switch (cmp[a][b])
        {
        case camp.NADA:
            glColor3f(0,0,0);
            break;
        case camp.SNAKE:
            glColor3f(0,0.85f,0.20f);
            break;
        case camp.APPLE:
            glColor3f(1.0f,0,0.1f);
            break;
        case camp.MURO:
            glColor3f(0.3f,0.3f,0.3f);
            break;
        }

        glVertex2f(-1.0f+a/(camp.Getlar()/2.0f),        1.0f-b/(camp.Getalt()/2.0f) );
        glVertex2f(-1.0f+a/(camp.Getlar()/2.0f),        1.0f-(1+b)/(camp.Getalt()/2.0f) );
        glVertex2f(-1.0f+((1+a)/(camp.Getlar()/2.0f)),  1.0f-(1+b)/(camp.Getalt()/2.0f));
        glVertex2f(-1.0f+((1+a)/(camp.Getlar()/2.0f)),  1.0f-b/(camp.Getalt()/2.0f ));

        glEnd();
    }
}



for(int a=0;a<(camp.Getlar())/2;a++)//line of quadrant ++
{
    for(int b=0;b<(camp.Getalt())/2;b++)//column
    {
        glBegin(GL_POLYGON);

        switch (cmp[a+camp.Getlar()/2][b])
        {
        case camp.NADA:
            glColor3f(0,0,0);
            break;
        case camp.SNAKE:
            glColor3f(0,0.85f,0.20f);
            break;
        case camp.APPLE:
            glColor3f(1.0f,0,0.1f);
            break;
        case camp.MURO:
            glColor3f(0.3f,0.3f,0.3f);
            break;
        }

        glVertex2f(a/(camp.Getlar()/2.0f),        1.0f-b/(camp.Getalt()/2.0f) );
        glVertex2f(a/(camp.Getlar()/2.0f),        1.0f-(1+b)/(camp.Getalt()/2.0f) );
        glVertex2f(((1+a)/(camp.Getlar()/2.0f)),  1.0f-(1+b)/(camp.Getalt()/2.0f));
        glVertex2f(((1+a)/(camp.Getlar()/2.0f)),  1.0f-b/(camp.Getalt()/2.0f) );

        glEnd();
    }
}

for(int a=0;a<(camp.Getlar())/2;a++)//line of quadrant +-
{
    for(int b=0;b<(camp.Getalt())/2;b++)//column
    { 
        glBegin(GL_POLYGON);

        switch (cmp[a+camp.Getlar()/2][b+camp.Getlar()/2])
        {
        case camp.NADA:
            glColor3f(0,0,0);
            break;
        case camp.SNAKE:
            glColor3f(0,0.85f,0.20f);
            break;
        case camp.APPLE:
            glColor3f(1.0f,0,0.1f);
            break;
        case camp.MURO:
            glColor3f(0.3f,0.3f,0.3f);
            break;
        }

        glVertex2f(a/(camp.Getlar()/2.0f),        -b/(camp.Getalt()/2.0f) );
        glVertex2f(a/(camp.Getlar()/2.0f),        -(1+b)/(camp.Getalt()/2.0f) );
        glVertex2f(((1+a)/(camp.Getlar()/2.0f)),  -(1+b)/(camp.Getalt()/2.0f));
        glVertex2f(((1+a)/(camp.Getlar()/2.0f)),  -b/(camp.Getalt()/2.0f ));

        glEnd();
    }
}

for(int a=0;a<(camp.Getlar())/2;a++)//line of quadrant --
{
    for(int b=0;b<(camp.Getalt())/2;b++)//column
    {    
        glBegin(GL_POLYGON);

        switch (cmp[a][b+camp.Getlar()/2])
        {
        case camp.NADA:
            glColor3f(0,0,0);
            break;
        case camp.SNAKE:
            glColor3f(0,0.85f,0.20f);
            break;
        case camp.APPLE:
            glColor3f(1.0f,0,0.1f);
            break;
        case camp.MURO:
            glColor3f(0.3f,0.3f,0.3f);
            break;
        }

        glVertex2f(-1.0f+a/(camp.Getlar()/2.0f),        -b/(camp.Getalt()/2.0f) );
        glVertex2f(-1.0f+a/(camp.Getlar()/2.0f),        -(1+b)/(camp.Getalt()/2.0f) );
        glVertex2f(-1.0f+((1+a)/(camp.Getlar()/2.0f)),  -(1+b)/(camp.Getalt()/2.0f));
        glVertex2f(-1.0f+((1+a)/(camp.Getlar()/2.0f)),  -b/(camp.Getalt()/2.0f ));

        glEnd();
    }
}

1 个答案:

答案 0 :(得分:2)

你应该更好地对待a tutorial。无论如何,你使用VBO实现的是一次上传所有几何体,只使用一个opengl调用而不是调用大量glVertex

如果您的几何图形不断变化,一个选项就是更新每一帧GPU上的所有几何图形,这肯定比在每一帧上进行所有这些调用更快。