试图加载两个纹理,但只加载一个

时间:2011-07-08 21:46:42

标签: android opengl-es

我做了一个游戏,其中所有对象都使用一张精灵表,到目前为止工作正常。现在我制作了一个有自己纹理的物体,但(至少在我的手机上,一个Evo)它只是显示一个白色的平面。但是,这在模拟器上工作正常。这是我加载纹理的代码:

public void LoadTexture(GL10 gl, Context context) throws IOException {
    InputStream is = context.getAssets().open("Image.png");
    Bitmap bitmap = null;
    try{
        bitmap = BitmapFactory.decodeStream(is);
    }finally{
        try{
            is.close();
            is = null;
            gl.glGenTextures(1, texture,0);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[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);

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


            gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            //Clean up


            bitmap.recycle();
        }catch(IOException e){

        }
    }

}

而且,在另一个班级:

public void LoadTexture(GL10 gl, Context context) throws IOException{
    InputStream is = context.getAssets().open("end.png");
    Bitmap bitmap = null;
    try{
        bitmap = BitmapFactory.decodeStream(is);
    }finally{
        try{
            is.close();
            is = null;
            gl.glGenTextures(1, texture,0);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[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);

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


            gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            //Clean up


            bitmap.recycle();
        }catch(IOException e){

        }
    }
}

两个类都有一个启用/禁用纹理的并条机例程和一个私有成员(int[] texture = new int[1])。我尝试更改变量,但无论如何都会发生同样的事情。两个纹理都加载在onSurfaceCreated(GL10 gl,EGLConfig config);

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,但可能不是你的情况。我试图加载的图像也是我的应用程序的图标,不知怎的,它没有加载它我改变了我的图像,没有被os引用,它工作正常