Android 2.2上的白色(错误)纹理,但在Android 1.5到2.1上使用普通纹理

时间:2011-10-03 17:26:02

标签: android opengl-es textures

我有一个应用程序,在屏幕上用纹理绘制正方形,用户可以旋转触摸屏幕的方块。它适用于Android 1.5到2.1。但是当我在带有Android 2.2.2的摩托罗拉Droid上测试应用程序时,有些东西失败了,因为正方形装有白色纹理。

有什么问题?

一些提示: - 纹理为256x256,然后不是POT问题

这是我班级广场的代码:

public class Square {

/** The buffer holding the vertices */
private FloatBuffer vertexBuffer;
/** The buffer holding the texture coordinates */
private FloatBuffer textureBuffer;
/** Our texture pointer */
private int[] textures = new int[3];

/** The initial vertex definition */
private float vertices[] = { 
                    -1.0f, -1.0f, 0.0f,     //Bottom Left
                    1.0f, -1.0f, 0.0f,      //Bottom Right
                    -1.0f, 1.0f, 0.0f,      //Top Left
                    1.0f, 1.0f, 0.0f        //Top Right
                                    };
/** The initial texture coordinates (u, v) */   

private float texture[] = {         
                    //Mapping coordinates for the vertices
                    0.0f, 0.0f,
                    0.0f, 1.0f,
                    1.0f, 0.0f,
                    1.0f, 1.0f
                    };

/**
 * The Square constructor.
 * 
 * Initiate the buffers.
 */
public Square() {
    //
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuf.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);
    //
    byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuf.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);
}

/**
 * The object own drawing function.
 * Called from the renderer to redraw this instance
 * with possible changes in values.
 * 
 * @param gl - The GL Context
 */
public void draw(GL10 gl) {     
    //Set the face rotation
    //gl.glFrontFace(GL10.GL_CW);
    gl.glFrontFace(GL10.GL_CCW);
    //Bind our only previously generated texture in this case
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    //Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    //Set The Color To Blue
    //gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); 
    //Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    ///
    //Bind the texture according to the set texture filter
    //gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
}

/**
 * Load the textures
 * 
 * @param gl - The GL Context
 * @param context - The Activity context
 */
public void loadGLTexture(GL10 gl, Context context) {
    //Generate one texture pointer...
    gl.glGenTextures(1, textures, 0);       
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Get the texture from the Android resource directory
    InputStream is = context.getResources().openRawResource(R.drawable.radiocd2);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    /*
     * This is a change to the original tutorial, as buildMipMap does not exist anymore
     * in the Android SDK.
     * 
     * We check if the GL context is version 1.1 and generate MipMaps by flag.
     * Otherwise we call our own buildMipMap implementation
     */
    if(gl instanceof GL11) {
        gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    //
    } else {
        buildMipmap(gl, bitmap);
    }   

    //Clean up
    bitmap.recycle();
}

/**
 * Our own MipMap generation implementation.
 * Scale the original bitmap down, always by factor two,
 * and set it as new mipmap level.
 * 
 * Thanks to Mike Miller (with minor changes)!
 * 
 * @param gl - The GL Context
 * @param bitmap - The bitmap to mipmap
 */
private void buildMipmap(GL10 gl, Bitmap bitmap) {
    //
    int level = 0;
    //
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    //
    while(height >= 1 || width >= 1) {
        //First of all, generate the texture from our bitmap and set it to the according level
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);

        //
        if(height == 1 || width == 1) {
            break;
        }

        //Increase the mipmap level
        level++;

        //
        height /= 2;
        width /= 2;
        Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

        //Clean up
        bitmap.recycle();
        bitmap = bitmap2;
    }
    }
}

1 个答案:

答案 0 :(得分:2)

解决了将图像存储在资产上的问题......令人惊讶