Thursday, June 9, 2016

android - Which texture do I bind to when implementing multi-texturing?


I have been having some issues getting multi-texturing to work using OpenGL ES 1.1. I have two PKMs - one with RGB's and one for alphas and I am trying to combine the two as below:


            int rgbTexture = textureId[0];

gl.glActiveTexture(GLES10.GL_TEXTURE0);
gl.glBindTexture(GLES11.GL_TEXTURE_2D, rgbTexture);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_TEXTURE_ENV_MODE, GLES11.GL_MODULATE);

ETC1Texture etcTexture = ETC1Util.createTexture(inputStream);
ETC1Util.loadTexture(GLES11.GL_TEXTURE_2D, 0, 0, GLES11.GL_RGB, GLES11.GL_UNSIGNED_SHORT_5_6_5, etcTexture);


int alphaTexture = textureId[1];

gl.glActiveTexture(GLES11.GL_TEXTURE1);
gl.glBindTexture(GLES11.GL_TEXTURE_2D, alphaTexture);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_TEXTURE_ENV_MODE, GLES11.GL_COMBINE);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_COMBINE_RGB, GLES11.GL_REPLACE);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_SRC0_RGB, GLES11.GL_PREVIOUS);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_OPERAND0_RGB, GLES11.GL_SRC_COLOR);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_COMBINE_ALPHA, GLES11.GL_MODULATE);

gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_SRC0_ALPHA, GLES11.GL_TEXTURE);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_OPERAND0_ALPHA, GLES11.GL_SRC_ALPHA);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_SRC1_ALPHA, GLES11.GL_PREVIOUS);
gl.glTexEnvf(GLES11.GL_TEXTURE_ENV, GLES11.GL_OPERAND1_ALPHA, GLES11.GL_SRC_ALPHA);

InputStream inputStreamAlpha = fileIO.readAsset(fileName + "_alpha.pkm");
ETC1Texture etcAlphaTexture = ETC1Util.createTexture(inputStreamAlpha);
ETC1Util.loadTexture(GLES11.GL_TEXTURE_2D, 0, 0, GLES11.GL_RGB, GLES11.GL_UNSIGNED_SHORT_5_6_5, etcAlphaTexture);

One of the problems that I am having is I don't know which texture ID to bind to when rendering this multi-texture and if I should make GL_TEXTURE0 or GL_TEXTURE1 active when rendering. The code above is from the load() method within my Texture class. On startup, I load all of the necessary textures using the code above. I then keep a reference to these Texture objects while I still need them and when I want to render them, I bind to the ID of the rgbTexture (as shown above) and I make GL_TEXTURE0 active. Is this correct? Should I be binding to the alphaTexture's ID? Should GL_TEXTURE1 be active when rendering? Or both? Or is multi-texturing supposed to happen at render time? Ideally I am looking for a solution that combines these textures into one at startup and then when rendering, I just bind to this one combined texture.




Answer



Well, think of texture units (GL_TEXTURE0, GL_TEXTURE1), as layers, each texture layer can have any texture assigned to it (texture id). This should be done when rendering and not when loading the textures.


When rendering you need to:


for each used texture unit
Activate texture unit using glActiveTexture;
glBind the appropriate texture;

Blend the textures using Modulate, Add etc;
And finally draw your object;

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...