简单的JOGL游戏在gtx 470上运行得非常慢

时间:2012-06-10 18:14:00

标签: java opengl jogl

我一直在为我的计算机科学课做游戏。为简单起见,我刚刚制作了一组迷你游戏。为了好玩,我尝试在3d中制作经典Snake游戏的版本。物理和碰撞检测工作正常,在学校计算机(中等质量的Mac)上游戏运行非常顺利。但是,在我的家用电脑上,它以8 fps运行。我的家用电脑运行在带有最新驱动程序的gtx 470上,程序中的查询确认代码运行在带有opengl 4.2的gtx 470上。

这是渲染代码(在GLCanvas中运行)

 GL2 gl = ( drawable.getGL()).getGL2();
     /*System.out.println(gl.glGetString(GL.GL_VENDOR)+"\n"+
                gl.glGetString(GL.GL_RENDERER)+"\n"+
                gl.glGetString(GL.GL_VERSION));*/

     gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    //Init camera
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();

    // Perspective.
    float widthHeightRatio = (float) getWidth() / (float) getHeight();
    glu.gluPerspective(75, widthHeightRatio, 1, 2000);

    double dX, dY, dZ;
    if (player.locs.size()==0)
    {
        dX=0.1*player.vel.x;
        dY=0.1*player.vel.y;
        dZ=0.1*player.vel.z;
    }
    else
    {
        dX=player.xHead-player.locs.get(0).x;
        dY=player.yHead-player.locs.get(0).y;
        dZ=player.zHead-player.locs.get(0).z;

    }
    player.up.normalizeDist();
    double xPos=4*dX-0.1*player.up.x;
    double yPos=4*dY-0.1*player.up.y;
    double zPos=4*dZ-0.1*player.up.z;
    double desiredDist=0.2;
    double totalDist=Math.sqrt(xPos*xPos+yPos*yPos+zPos*zPos);
    xPos=xPos*desiredDist/totalDist;
    yPos=yPos*desiredDist/totalDist;
    zPos=zPos*desiredDist/totalDist;
    double camX=player.xHead-xPos;
    double camY=player.yHead-yPos;
    double camZ=player.zHead-zPos;
    glu.gluLookAt(xWidth*(camX), yWidth*(camY),zWidth*(camZ), xWidth*(player.xHead+2*dX), yWidth*(player.yHead+2*dY), zWidth*(player.zHead+2*dZ), player.up.x, player.up.y, -player.up.z);
    // Change back to model view matrix.
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();


    float SHINE_ALL_DIRECTIONS = 1;
    float[] lightPos = {xWidth/2, yWidth/2, zWidth/2, SHINE_ALL_DIRECTIONS};
    float[] lightColorAmbient = {0.2f, 0.2f, 0.2f, 0.2f};
    float[] lightColorSpecular = {0.8f, 0.8f, 0.8f, 0.8f};

    // Set light parameters.
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);

    // Enable lighting in GL.

    gl.glEnable(GL2.GL_LIGHT1);
    gl.glEnable(GL2.GL_LIGHTING);


    // Set material properties.
    float[] rgba = {1f, 1f, 1f};
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
    gl.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 0.5f);
    /*gl.glMaterialfv(GL.GL_BACK, GL.GL_AMBIENT, rgba, 0);
    gl.glMaterialfv(GL.GL_BACK, GL.GL_SPECULAR, rgba, 0);
    gl.glMaterialf(GL.GL_BACK, GL.GL_SHININESS, 0.5f);*/


   // gl.glColor3f(1f,1f,1f);
    if (camX>0)
    {

        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(1,0,0);
        gl.glVertex3d(0, 0, 0);
        gl.glVertex3d(0, 0, zWidth);
        gl.glVertex3d(0, yWidth, zWidth);
        gl.glVertex3d(0, yWidth, 0);
        gl.glEnd();
    }
    if (camY>0)
    {
        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(0, 1, 0);
        gl.glVertex3d(0, 0, 0);
        gl.glVertex3d(0, 0, zWidth);
        gl.glVertex3d(xWidth, 0, zWidth);
        gl.glVertex3d(xWidth, 0, 0);
        gl.glEnd();
    }
    if (camZ>0)
    {
        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(0, 0, 1);
        gl.glVertex3d(0, 0, 0);
        gl.glVertex3d(xWidth, 0, 0);
        gl.glVertex3d(xWidth, yWidth, 0);
        gl.glVertex3d(0, yWidth, 0);
        gl.glEnd();
    }
    if (camX<1)
    {
        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(-1, 0, 0);
        gl.glVertex3d(xWidth, 0, 0);
        gl.glVertex3d(xWidth, 0, zWidth);
        gl.glVertex3d(xWidth, yWidth, zWidth);
        gl.glVertex3d(xWidth, yWidth, 0);
        gl.glEnd();
    }
    if (camY<1)
    {
        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(0, -1, 0);
        gl.glVertex3d(0, yWidth, 0);
        gl.glVertex3d(0, yWidth, zWidth);
        gl.glVertex3d(xWidth, yWidth, zWidth);
        gl.glVertex3d(xWidth, yWidth, 0);
        gl.glEnd();
    }
    if (camZ<1)
    {

        gl.glBegin(GL2.GL_POLYGON);
        gl.glNormal3d(0, 0, 1);
        gl.glVertex3d(0, 0, zWidth);
        gl.glVertex3d(xWidth, 0, zWidth);
        gl.glVertex3d(xWidth, yWidth, zWidth);
        gl.glVertex3d(0, yWidth, zWidth);
        gl.glEnd();
    }


    player.draw(xWidth, yWidth, zWidth, drawable, glu);
    for (int i=0; i<bullets.size(); i++)
    {
        bullets.get(i).draw(drawable, glu, xWidth, yWidth, zWidth);
    }
    for (int i=0; i<basicEntities.size(); i++)
    {
        basicEntities.get(i).draw( xWidth, yWidth, zWidth, drawable, glu);
    }

然后很多复制粘贴到这样的代码的调用:(xHead,yHead和zHead是坐标)

GL gl=drawable.getGL();
GL2 gl2=gl.getGL2();        
        gl2.glPushMatrix();
        gl2.glTranslated(xHead*xWidth, yHead*yWidth, zHead*zWidth);
        float[] rgba = {0.3f, 0.5f, 1f};
        gl2.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl2.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl2.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.5f);
        GLUquadric head = glu.gluNewQuadric();
        glu.gluQuadricDrawStyle(head, GLU.GLU_FILL);
        glu.gluQuadricNormals(head, GLU.GLU_FLAT);
        glu.gluQuadricOrientation(head, GLU.GLU_OUTSIDE);
        final float radius = (float) (dotSize*xWidth);
        final int slices = 32;
        final int stacks = 32;
        glu.gluSphere(head, radius, slices, stacks);
        glu.gluDeleteQuadric(head);
        gl2.glPopMatrix();

编辑:我可以通过减少二次曲面中的切片和堆叠数量来让游戏运行得更快,但这会让游戏变得相当难看。 此外,我删除了a.add(this)(来自动画师),游戏仍在运行。我动了两次动画吗?但它仍然很慢。

1 个答案:

答案 0 :(得分:5)

我无法完全解释为什么它在您的学校计算机上运行得如此之好,但您使用OpenGL的方式是一种古老的方式,并且对于性能而言非常糟糕。

使用glBegin绘制将总是非常昂贵,因为它必须将每个顶点作为单独的API调用发送,这对性能不利。您应该考虑使用顶点阵列(好)或顶点缓冲对象(在大多数情况下更好)进行渲染。使用这些将需要略微改变思路,但我相信你可以找到许多使用这些搜索术语的教程。

虽然你使用gluSphere和gluQuadrics也让我产生怀疑,但我也不是glu的专家。 glu 函数的大部分工作可能不会在图形卡上执行,因此每次调用gluSphere时,CPU必须重新计算球体的所有顶点,然后才能对GPU执行任何操作。一个更好的解决方案是生成自己的球体顶点列表,将其作为VBO上传到GPU,然后在想要绘制球体时随时执行VBO绘制调用。这应该可以节省大量的计算时间。