Android中的渲染效率 - OpenGL ES 10
问题描述:
当前编写的代码在OpenGL()10中创建背景和3D对象。我已经遇到了一个问题,虽然在更改单位矩阵后渲染对象。无论出于何种原因,代码效率非常低,并且对象不会平稳移动,它会跳出一些零碎的东西。Android中的渲染效率 - OpenGL ES 10
public void onDrawFrame(GL10 gl) {
// Clear the screen to black
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
background.loadTexture(gl, mContext, R.drawable.room);
// Position model so we can see it
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -20.0f);
gl.glScalef(7.5f, 7.5f, 0);
//gl.glTranslatef(tempX, tempY, 0);
//Set rotation
gl.glRotatef(0, 0, 0, 0); //glRotatef(angle, x, y, z) rotates each axis by a certain angle
background.draw(gl);
// Position model so we can see it
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(tempX, tempY, -10f);
gl.glScalef(counter*1f,counter*1f,counter*1f);
System.out.println("Z value is now 1f*"+counter+" and tempX is "+tempX+" and tempY is "+tempY +"*****************************");
gl.glPushMatrix();
//gl.glTranslatef(tempX, tempY, 0);
//Set rotation
gl.glRotatef(-60, 1, 1, 0); //glRotatef(angle, x, y, z) rotates each axis by a certain angle
cube.loadTexture(gl, mContext, R.drawable.wood);
// Draw the model
cube.draw(gl);
}
立方体类如下所示:
class MyCube {
private final IntBuffer vertexBuffer; //Buffers store vertex data for improved performance
private final IntBuffer textureBuffer; //Buffers store texture data for improved performance
static int one = 65536; //Length of a side
int half = one/2;
//Cube data
public MyCube() {
int vertices[] = { //Sets coordinates of the vertices.
// FRONT
-half, -half, half, half, -half, half, //Sets x, y and z coordinates for each of the four vertices of the front face
-half, half, half, half, half, half,
// BACK
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,
// LEFT
-half, -half, half, -half, half, half,
-half, -half, -half, -half, half, -half,
// RIGHT
half, -half, -half, half, half, -half,
half, -half, half, half, half, half,
// TOP
-half, half, half, half, half, half,
-half, half, -half, half, half, -half,
// BOTTOM
-half, -half, half, -half, -half, -half,
half, -half, half, half, -half, -half,};
int texCoords[] = { //Sets values of texture coordinates. (Only u and v. Not x, y, and z.)
// FRONT
0, one, one, one, 0, 0, one, 0,
// BACK
one, one, one, 0, 0, one, 0, 0,
// LEFT
one, one, one, 0, 0, one, 0, 0,
// RIGHT
one, one, one, 0, 0, one, 0, 0,
// TOP
one, 0, 0, 0, one, one, 0, one,
// BOTTOM
0, 0, 0, one, one, 0, one, one,};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asIntBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer tbb = ByteBuffer.allocateDirect(vertices.length * 4);
tbb.order(ByteOrder.nativeOrder());
textureBuffer = tbb.asIntBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);
}
//Draws cube
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer); //glVertexPointer(size (number coords per vertex), type (data type of coords, stride (byte offset between consecutive vertices), pointer (pointer to first coord in the array)
gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); //Render primitives from array data. (mode (specifies kind of primitives, first (specifies starting index of arrays), count (number of indices to be rendered))
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
}
static void loadTexture(GL10 gl, Context context, int resource) {
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resource);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
bmp.recycle();
}
}
任何有关的代码的指针?
答
它看起来像你每帧调用cube.loadTexture()。这可能非常慢,在初始化期间只做一次就足够了。
另外,我不确定您是否正确加载纹理。通常,您在加载纹理之前生成纹理ID(使用glGenTextures)并将其绑定(使用glBindTexture)。你可能也想看看这个问题。
是的,这听起来像是一个相当大的问题。干杯! – Michael 2014-12-05 01:16:23