Sandra
Sandra

Reputation: 4259

opengl es 2.0 texture loading

I am making a OpenGL ES 2.0 android carousel. I have a "for" function that in each iteration calls "drawSquares" function that draws squares in a circle. That is done by changing the modelMatrix every time a new square is drawn. (For drawing the squares I use the same vertex and fragment shader)

I need to apply textures on the squares, but for every square to have a different texture. I can't seem to be able to do this. I tried changing the value of the handle for the texture data right before the call drawSquare.

mTextureDataHandle = TextureHelper.loadTexture(context, item.getTexture());  

But every square has the same texture.

Can some suggest something, or tell me the best way to implement this. I am reading about opengl es 2.0 for about two months now, but still fill that there are many things I don't understand. Please help, i would deeply appreciate every advice. Thank u!!!

Upvotes: 0

Views: 2145

Answers (1)

eskimo9
eskimo9

Reputation: 753

Start Here. You have a couple of options -

  1. glBindTexture - every time you load a new texture into your handle, you will have to bind it again
  2. Use an array of handles - you can set mTextureDataHandle up as an int[], fill it in and then bind each texture to a separate GL Texture:

    mTextureDataHandle[0] = ...; //Load textures - repeat for [1..N]
    /* 
    *  Select which GL texture spot to use - GL has 30 pointers to use, your device 
    *  likely won't support that many
    */
    GLES20.glSetActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GL_TEXTURE_2D, mTextureDataHandle[0]);
    
    GLES20.glSetActiveTexture(GLES20.GL_TEXTURE1);
    GLES20.gBindTexture(GL_TEXTURE_2D, mTextureDataHandle[1]);
    
    //To draw with the right texture select which GL texture to point to (index):
    GLES20.glUniform1i(mtextureUniformHandle, index);
    drawStuff();
    

    You will be restricted here by the number of textures supported by your device - use

    IntBuffer max = IntBuffer.allocate(1);
    GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_IMAGE_UNITS, max);
    Log.d("tag","Texture units: " + max.get());
    
  3. Or some combination of the two options - i.e. save processor time from loading by having an array of pre-loaded textures, but you may need to bind at least some of them on the fly to avoid running out of available textures.

Upvotes: 2

Related Questions