-1
當前編寫的代碼在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();
}
}
任何有關的代碼的指針?
是的,這聽起來像是一個相當大的問題。乾杯! – Michael 2014-12-05 01:16:23