PDA

View Full Version : Multitexture Gl_Replace Problems



tomastyv
08-09-2010, 04:21 AM
hi everybody.

I am making a game for iphone, and I am using OpenGl ES.

Now I am having trouble using multitexturing. I havent been doing lots of multitexturing but I have read tutorials and I think I understand the important things about multitexturing.

What I want to do is to perform a simple mask operation: TextureUnit0 holds the mask ( in the alpha channel ), and TextureUnit1 holds the image to be masked( in the color channel). hereunder you will see what I do:




// -----------------------------------------
// PREPARE TEXTUREUNIT 0 (MASK)
glActiveTexture(GL_TEXTURE0);

glEnable(GL_TEXTURE_2D);
glClientActiveTexture(GL_TEXTURE0);

glVertexPointer(3, GL_FLOAT, 0, mVertices);
glTexCoordPointer(2, GL_FLOAT, 0, mUV_MASK);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glEnable( GL_TEXTURE_2D );
mTestAtlas->getTexture()->activate();

// -----------------------------------------
// PREPARE TEXTUREUNIT 1 (IMAGE)
glActiveTexture(GL_TEXTURE1);

glEnable(GL_TEXTURE_2D);
glClientActiveTexture(GL_TEXTURE1);

glVertexPointer(3, GL_FLOAT, 0, mVertices);
glTexCoordPointer(2, GL_FLOAT, 0, mUV_IMAGE);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glEnable( GL_TEXTURE_2D );
mTestAtlas->getTexture()->activate();

// ---------------------------------
// SETUP COMBINE PARAMS FOR COLOR CHANNEL

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

// SETUP COMBINE PARAMS FOR ALPHA CHANNEL
// REPLACE DOESNT WORK!!!
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);



// DRAW
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



// RESTORE OPENGL STATES
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, Half.texID);
glDisable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);


What happens is that the IMAGE layer (TEX UNIT 1) is doing what I expected, but the Mask (TEX UNIT 0) is not !! .
The mask operation is not performed.

Does the code snippet look right to you?
Can anyone help me here? -

trinitrotoluene
08-09-2010, 05:35 PM
I think this glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); is wrong for the alpha channel because it will replace the previous texture. Maybe try with GL_MODULATE.And if your mask never change, why not use only one texture unit with GL_DECAL for your texture env mode (glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL));.

Most people here don't use anymore the fixed function pipeline for multi-texturing. So very few people may help you. Does the iphone can use shaders? Because writing a shader for that should be very easy.