Can't get glColor4f working on a 2D project

Problems with Canvas, OpenGL, etc...

Can't get glColor4f working on a 2D project

Postby Johanovski » Mon May 30, 2011 11:51 am

Greetings!

I'm working in a 2D project using OpenGL but, as I've tried starting to play with alpha values, I've found that glColor4f doesn't work, and I have no idea on how get it working... I've searched over forums, guides and tutorials, but I'm not able to find what I'm doing wrong... I've split my code in some blocks:

Initializing OpenGL (onSurfaceCreated):
Code: Select all
// Initial OpenGL preparation
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
            gl.glDisable(GL10.GL_DEPTH_TEST);
            gl.glDisable(GL10.GL_DITHER);
            gl.glDisable(GL10.GL_LIGHTING);           

            gl.glEnable(GL10.GL_TEXTURE_2D);

            gl.glEnable(GL10.GL_BLEND);
            gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
            gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, /*GL10.GL_REPLACE*/ GL10.GL_MODULATE);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
           
            gl.glClearColor(0, 0, 0, 0);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);


Then, I'm loading some textures this way (with a "bitmap" object):
Code: Select all
// Creates an OpenGL texture from the bitmap
        GLES10.glEnable(GL10.GL_BLEND);
        GLES10.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        GLES10.glGenTextures(1, texID, 0);
        GLES10.glBindTexture(GLES10.GL_TEXTURE_2D, id);
        GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, GLES10.GL_RGBA, bitmap, 0);
        bitmap.recycle();


Next, in the "drawing" loop, I'm setting the OpenGL basics before drawing anything:
Code: Select all
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);


And, finally, I'm drawing the image while trying to change its colors through glColor4f:
Code: Select all
GLES10.glPushMatrix();
        GLES10.glLoadIdentity();

        GLES10.glTranslatef(px, py, 0);
        GLES10.glRotatef(rotation, 0, 0, 1.0f);
        GLES10.glScalef(1.0f, 1.0f, 1.0f);
        GLES10.glColor4f(colors[0], colors[1], colors[2], colors[3]); // I've set them to [1.0f, 0.0f, 0.0f, 0.5f]

        GLES10.glBindTexture(GL10.GL_TEXTURE_2D, texID[0]);

        GLES10.glVertexPointer(2, GL10.GL_FLOAT, 0, quadVertices);
        GLES10.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoord);

        GLES10.glEnable(GLES10.GL_BLEND);
        GLES10.glDrawElements(GL10.GL_TRIANGLES, sIndexBuffer.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
        GLES10.glDisable(GLES10.GL_BLEND);

        GLES10.glPopMatrix();


Well, image are well drawn, but when I try to change its color or add alpha seems that nothing happens, so the image is still rendered the same... I can't find what I'm doing wrong, I suppose (and hope! :P) it'll be a small problem related with some kind of glEnable or glDisable, but nothing I've read about seems to solve my problem... Any help will be really appreciated!

Thanks for your time and efforts! :)
User avatar
Johanovski
Freshman
Freshman
 
Posts: 8
Joined: Thu Jan 13, 2011 4:08 pm
Location: Vic (Barcelona), Spain

Top

Re: Can't get glColor4f working on a 2D project

Postby bxm » Tue May 31, 2011 5:06 am

I've found that OpenGL ES is very picky with how to handle blend functions. The one that I use, and works (for the most part) well, though sometimes it makes fully opaque colors somewhat transparent...

Code: Select all
glBlendFunc (GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);


Try that and see if it improves, and don't forget this only needs to be set once - not every time you create a texture.
bxm
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue May 10, 2011 2:51 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests