4维游戏渲染优化

时间:2011-08-24 16:49:02

标签: c++ opengl game-engine

最近我开始编写游戏Snake 4d(Game explanationC++/openGL version of the game)的C ++版本。我真的是一个编程初学者...所以每个评论,甚至编程风格都是值得赞赏的。

在“蛇”(6)立方体之后我有一个帧落 游戏逻辑留在蛇存储在一个名为snake.p_list的向量中的事实,每当蛇与食物碰撞时(在程序cibo中调用),它将向p_list添加一个新对象。 可能是由于游戏引擎?或者你对于什么会导致帧丢失有另一个想法?你知道如何使这些功能看起来更好吗?

好的我跑了探查器...并且: 申请资料:

  • 23.0%23.0%GeForceGLDriver glrCompExecuteKernel
  • 19.3%19.3%GeForceGLDriver gldCopyTexSubImage
  • 5.4%5.4%GeForceGLDriver gldFinish
  • 5.3%5.3%GLEngine gleUpdateLightRGBASumsUnconditional
  • 3.9%3.9%GLEngine gleUpdateDeferredState
  • 2.1%2.1%GLEngine gleGetVertexSubmitFuncObjectAndKey
  • 2.1%2.1%GeForceGLDriver gldUpdateDispatch
  • 1.8%1.8%4维度MyGLBox :: paintGL()

可能是绘图功能:(

绘图功能:

for(p = p_list.begin();p != p_list.end(); p++){
    Polygon4 p1 = *p;

    if(prj == PRJ_2D){
        //parameters to project in 2d
        double angle = PI/4;
        double Cx = 0.;
        double Lx = 50./2;
        double p_cam = 10.;
        //actually simply fits...
        for(int i = 0; i < 32; ++i){
            V4 v1_2d = p1.v_list[p1.e_list[i].e[0]];
            V4 v2_2d = p1.v_list[p1.e_list[i].e[1]];

            double x1 = Cx + (Lx*v1_2d[c_i])/(p_cam*2*tan(angle));
            double y1 = Cx + (Lx*v1_2d[c_j])/(p_cam*2*tan(angle));
            double x2 = Cx + (Lx*v2_2d[c_i])/(p_cam*2*tan(angle));
            double y2 = Cx + (Lx*v2_2d[c_j])/(p_cam*2*tan(angle));

            float mat_specular[] ={0.1,0.1,0.1,0.1};
            glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

            GLfloat mat_shininess[] = { 0 };
            glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

            GLfloat mat_color[] = {p1.color.r,p1.color.g,p1.color.b,0.5};
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);

            glBegin(GL_LINES);
            glVertex3f(x1,y1,0.);
            glVertex3f(x2,y2,0.);
            glEnd();


        }
    }

    else{

        for(vertex = p1.v_list.begin(); vertex != p1.v_list.end(); vertex++){
            v3d = cam.prj_p(*vertex);
            tesseract_prj.v_list.push_back(v3d);
        }
        tesseract_prj.e_list = p1.e_list;
        tesseract_prj.f_list = p1.f_list;

        //edges render
        for(int i = 0; i <32; ++i){
            V3 v1 = tesseract_prj.v_list[tesseract_prj.e_list[i].e[0]];
            V3 v2 = tesseract_prj.v_list[tesseract_prj.e_list[i].e[1]];
            float mat_specular[] ={0.1,0.1,0.1,0.1};

          glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

            GLfloat mat_shininess[] = { 0 };
            glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

            GLfloat mat_color[] = {0.0,0.0,0.0,1.};
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);

            glBegin(GL_LINES);
            glVertex3f(v1[0],v1[1],v1[2]);
            glVertex3f(v2[0],v2[1],v2[2]);
            glEnd();
        }
        //faces render

        for(int i = 0; i<24; ++i){
            V3 v1 = tesseract_prj.v_list[tesseract_prj.f_list[i].f[0]];
            V3 v2 = tesseract_prj.v_list[tesseract_prj.f_list[i].f[1]];
            V3 v3 = tesseract_prj.v_list[tesseract_prj.f_list[i].f[2]];
            V3 v4 = tesseract_prj.v_list[tesseract_prj.f_list[i].f[3]];

            GLfloat mat_color[] = {p1.color.r,p1.color.g,p1.color.b,0.5};
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);

            glBegin(GL_QUADS);
            glVertex3f(v1[0],v1[1],v1[2]);
            glVertex3f(v2[0],v2[1],v2[2]);
            glVertex3f(v3[0],v3[1],v3[2]);
            glVertex3f(v4[0],v4[1],v4[2]);
            glEnd();
        }
        tesseract_prj.e_list.clear();
        tesseract_prj.v_list.clear();
    }

1 个答案:

答案 0 :(得分:1)

如果循环中每个元素的材质属性没有变化,您可以改为:

float mat_specular[] ={0.1,0.1,0.1,0.1};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

GLfloat mat_shininess[] = { 0 };
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

GLfloat mat_color[] = {0.0,0.0,0.0,1.};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);

glBegin(GL_LINES);
    for(int i = 0; i <32; ++i)
    {
        glVertex3f(v1[0],v1[1],v1[2]); //note: drawing multiple lines with one
        glVertex3f(v2[0],v2[1],v2[2]); //glBegin/glEnd pair will be much faster
    }
glEnd();

使用glColorMaterial也可能比glMaterialfv更快:

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL); //now  ambient and diffuse is set by glColor4f()

glBegin(GL_QUADS);
for(int i = 0; i<24; ++i)
{
    glColor4f(r,g,b,0.5);
    glVertex3f(v1[0],v1[1],v1[2]);
    glVertex3f(v2[0],v2[1],v2[2]);
    glVertex3f(v3[0],v3[1],v3[2]);
    glVertex3f(v4[0],v4[1],v4[2]);
}
glEnd();

glDisable(GL_COLOR_MATERIAL);//now use glMaterialfv() for ambient and diffuse (optional)

此外,这允许您更改每个四边形(甚至每个顶点)的颜色,但只使用一个glBegin(GL_QUADS)/ glEnd()对而不是24(这可能会产生很大的速度差异)。此外,如果所有四边形的颜色相同,你可以在循环之外调用glColor4f()。