我正在为一个小型2D游戏编程图形。 这样做是使用OpenGL ES 2.0。 我有一个包含构建矩形的顶点的Object。对于我需要的每个障碍物,我需要缩放,平移和绘制这个物体。 我这样做是为了不必为每个绘图和每个障碍物上传顶点。 ("等级"总是像飞鸟或喷气背包兜风一样移动) 但现在我必须为每一帧中的每个对象上传我的矩阵。 大多数情况下,在我的lg g2上绘制一个Frame需要5-17 ms。但是每帧有100毫秒的峰值。
此时我只绘制矩形,所以我真的不知道为什么我会遇到这些性能问题。以下是我的ObstacleGL类和我的渲染器的代码。
package com.example.jump;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import android.opengl.GLES20;
import android.opengl.Matrix;
public class ObstacleGL {
static final int COORDS_PER_VERTEX = 3;
static final int vertexStride = COORDS_PER_VERTEX * 4;
private static final String vertexShaderCode =
"uniform mat4 uMVPMatrix; \n" +
"attribute vec4 vPosition; \n" +
"attribute vec4 color; \n" +
"varying vec4 fragColor; \n"+
"void main() { \n" +
" gl_Position = uMVPMatrix * vPosition; \n" +
" fragColor = color; \n"+
"}";
private static final String fragmentShaderCode =
"precision mediump float; \n" +
"varying vec4 fragColor; \n" +
"void main() { \n" +
" gl_FragColor = fragColor;\n" +
"}";
private float [] coords={
//x,y,z z=0
-1,1,0, //upper left
-1,-1,0, //bottom left
1,-1,0, //upper right
1,1,0 //bottom right
};
private final short[] drawOrder = { 0, 1, 2, 0, 2, 3 };
private int fragmentShader;
private int vertexShader;
private int mProgram;
private float[] color={0.33f,1f,0.33f,0.5f};
private int colorHandle;
private int positionHandle;
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;
private int matrixHandle;
private int vertexCount;
private float[] translationMatrix=new float[16];
private float[] scaleMatrix=new float[16];
public void initialize(){
fragmentShader=GameRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode);
vertexShader=GameRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
ByteBuffer bb;
ByteBuffer dlb;
bb= ByteBuffer.allocateDirect(coords.length*4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer=bb.asFloatBuffer();
vertexBuffer.put(coords);
vertexBuffer.position(0);
dlb=ByteBuffer.allocateDirect(12); //drawOrder.length*2
dlb.order(ByteOrder.nativeOrder());
drawListBuffer=dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
mProgram=GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
colorHandle=GLES20.glGetAttribLocation(mProgram, "color");
positionHandle=GLES20.glGetAttribLocation(mProgram, "vPosition");
matrixHandle=GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GLES20.glVertexAttrib4fv(colorHandle, color, 0);
GLES20.glEnableVertexAttribArray(positionHandle);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,vertexStride , vertexBuffer);
GLES20.glDisableVertexAttribArray(positionHandle);
}
public ObstacleGL()
{
vertexCount=coords.length/COORDS_PER_VERTEX;
Matrix.setIdentityM(translationMatrix, 0);
Matrix.setIdentityM(scaleMatrix, 0);
initialize();
}
public void prepare(){
//prepare Obstacle Drawing
GLES20.glUseProgram(mProgram);
GLES20.glEnableVertexAttribArray(positionHandle);
}
public void finish(){
GLES20.glDisableVertexAttribArray(positionHandle);
}
public void draw(float posx, float posy, float height, float width)
{
//new position and scale
float[] matrix= new float[16];
Matrix.setIdentityM(translationMatrix, 0);
Matrix.setIdentityM(scaleMatrix, 0);
Matrix.scaleM(scaleMatrix, 0, width, height, 1);
Matrix.translateM(translationMatrix, 0, 2*posx-1,2*posy-1+height, 0);
Matrix.multiplyMM(matrix, 0,translationMatrix , 0,scaleMatrix , 0);
//here I give the new position
GLES20.glUniformMatrix4fv(matrixHandle, 1,false, matrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);
}
}
这是我的GameRenderer。被重写的方法被我们的游戏模拟使用,它在另一个线程上运行。
package com.example.jump;
import java.util.LinkedList;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;
public class GameRenderer implements GLSurfaceView.Renderer,GameVisualisation {
private ObstacleGL obstacle;
private LinkedList<Obstacle> obstacleList= new LinkedList<Obstacle>();
Obstacle o;
//Renderer Methoden
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
obstacle.prepare();
for (int i = 0; i < obstacleList.size(); i++) {
if(i==obstacleList.size())
break;
o=obstacleList.get(i);
obstacle.draw(o.xPosition, o.yPosition, o.height, o.width);
}
obstacle.finish();
}
public static int loadShader(int type, String shadercode)
{
int shader=GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shadercode);
GLES20.glCompileShader(shader);
return shader;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
initialize();
}
private void initialize() {
obstacle=new ObstacleGL();
}
//GameVisualisationInterfaceMethoden
@Override
public void changePunktestand(int punkte) {
// TODO Auto-generated method stub
}
@Override
public void moveCharacter(double height) {
// TODO Auto-generated method stub
}
@Override
public void changeCharacterAnimation(int animation) {
// TODO Auto-generated method stub
}
@Override
public void addObstacle(int id, float xpos, float ypos, float width, float height) {
obstacleList.add(new Obstacle(id, height, width, xpos, ypos));
if(o==null)
o=new Obstacle(id, height, width, xpos, ypos);
}
@Override
public void moveObstacle(int id, double xPos, double yPos) {
// TODO
}
@Override
public void moveObstaclesX(float x) {
for (int i = 0; i < obstacleList.size(); i++) {
Obstacle o=obstacleList.get(i);
o.xPosition+=-x;
}
}
@Override
public void deleteObstacle(int id) {
for (int i = 0; i < obstacleList.size(); i++) {
if(obstacleList.get(i).id==id){
obstacleList.remove(i);
return;
}
}
}
@Override
public void gameOver() {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
所以我找到了一个现在运作良好的解决方案: 首先,我将矩阵外包给了Obstacle类,因此不需要每次绘制都计算它们。 这有3个效果: