OpenGL DrawTexture and Alpha

by Rittz » Fri, 02 Jul 2010 05:18:22 GMT


Sponsored Links
 Hello,

I've been working on moving from Canvas over to OpenGL using
GLSurfaceView and have hit a bit of a roadblock in trying to change
the alpha of a texture at runtime.  I can display the texture with
transparency just fine but changing the RGBA via glColor isn't doing
anything at all.  All I'm trying to do is fade in and out some
textures by changing the alpha value and I know it's possible because
I've seen it done before.  I get the feeling like I'm just missing one
line of code to fix this.

Here is the actual draw method for the object

public void draw(GL10 gl)
{
                gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName);

                gl.glColor4f(1f, 1f, 1f, 0.5f); // No matter what I make these
values it doesn't change anything

                ((GL11) gl).glTexParameterfv(GL10.GL_TEXTURE_2D,
GL11Ext.GL_TEXTURE_CROP_RECT_OES, textureGrid, 0);
                ((GL11Ext) gl).glDrawTexfOES(x, y, z, width, height);
 }

and here is some of the initialization code, it's pretty much just
copied straight from the SpriteMethodTest example

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_LIGHTING);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
GL10.GL_DEPTH_BUFFER_BIT);

        // Load all the Textures
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, w, 0.0f, h, 0.0f, 1.0f);

        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glColor4f(1f, 1f, 1f, 1f);
        gl.glClearColor(1f, 1f, 1f, 1f);
        gl.glEnable(GL10.GL_TEXTURE_2D);
    }

Thanks in advance!

--



OpenGL DrawTexture and Alpha

by Lance Nanek » Fri, 02 Jul 2010 08:42:09 GMT


 You can try adding this:
gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Although I think it is the default and I don't see anything like this
turning it off in your posted code:
gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Which is what I was expecting.




--


Sponsored Links