我正在为计算机科学课程制作一款游戏。出于简单起见,我只制作了一组迷你游戏。为了好玩,我尝试制作了一个3D版本的经典贪吃蛇游戏。物理和碰撞检测很好,而且在学校的电脑上(中等品质的苹果电脑)游戏运行非常流畅。但是,在我的家用电脑上运行时,帧率只有8FPS。我的家用电脑配置为GTX 470,并使用最新驱动程序,程序中的查询确认代码正在GTX 470上以OpenGL 4.2运行。
这是渲染代码(在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),但游戏仍在运行。我是在两次对所有内容进行动画吗?尽管如此,游戏仍然很慢。