安卓2.2上出现白色(错误)纹理,但在安卓1.5到2.1上正常工作的纹理。

3
我有一个应用程序,可以在屏幕上绘制一个带有纹理的正方形,并且用户可以通过触摸屏幕旋转正方形。它在Android 1.5到2.1版本中运行良好。但是当我在装有Android 2.2.2的Motorola 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;
    }
    }
}

我们能否为给定的图像创建法线贴图以用于照明和扩散? - Umang Kothari
1个回答

2

解决了将图片存储在资源上的问题...太棒了。


你能详细说明一下吗?不清楚解决方案是什么。 - Bill the Lizard
请注意:纹理的大小必须是2的幂次方,如64、128、256、512或1024。http://stackoverflow.com/questions/5738580/textures-not-rendering-in-2-3-3 - AjOnFire
WTF... 这竟然是问题所在。我把文件从 res 目录移到了 assets 目录,并从那里加载,问题迎刃而解! - Robert Massaioli
我也发现这个可以解决问题,但是似乎不太合理,因为位图从res/drawable加载时大小和一切都是正确的。 - Brad Moore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接