0
votes

I have a GLSurfaceView where I render my scene. My scene display correctly except for some textures where I have white squares instead of the real texture. I use 2 types of texture :

  • Some textures are in the resources of the project. I create them in onSurfaceCreated, everything works fine with them
  • Some other needs to be downloaded from a remote location. Those are the textures that don't display correctly.

I download and create the "lazy texture" with the following code :

public void onDrawFrame(GL10 gl) {
    ...
    Texture3D.getDynamicTexture(gl, object.avatar).bind(gl);
    ...
}


public static Texture3D getDynamicTexture(GL10 gl, int key) {
    Texture3D texture =  dynamicTextures.get(key);

    if (texture == null) {
        dynamicTextures.put(key, new Texture3D(gl, key, Type.DYNAMIC));
        texture = getStaticTexture(R.drawable.broken_img);
    }

    return texture;
}


public Texture3D(final GL10 gl, final int id, Type type) {
    gl.glEnable(GL10.GL_TEXTURE_2D);

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    texture = newTextureID(gl);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    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);

    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    Utils.loadImage(id, new SimpleImageLoadingListener() {

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            _pBitmap = loadedImage;
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, _pBitmap, 0);

            _pBitmap = null;
            gl.glEnable(GL10.GL_BLEND);
        }

    });

I see that the download works because R.drawable.broken_img got replaced by a white square. The textures seems to appears but they are all stacked on top of the screen on a single line (see the picture).

Stacked textures on top of the screen

I also tried Android OpenGL ES 1.1 white box textures but that didn't work either. The only difference I saw was that the textures didn't appear anymore on top of the screen.

1

1 Answers

0
votes

I solved this issue by adding 2 boolean flags (imageLoaded, textureCreated). Those flags allow me to first download the image, then as soon as I have the image I create the texture (all the code to create the texture is executed at the "same" time).