1
\$\begingroup\$

I code game on Android where I have several sprites.

My goal is to set gl.glColor4f(1f, 1f, 1f, 0.2f) a.e. transparency only for specific image. (I don't like Photoshop).

In my case its image that represents a "number" and moves up and begin to be transparent till disappear at all.

However, when I use it, I see that all sprite images are transparent or not what I want.

What I do wrong? Does my code seem right? (i'm junior in OPenGL)

Here is relevant part of code:

Renderer

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig confid) {
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glShadeModel(GL10.GL_SMOOTH);
    //gl.glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
    //gl.glClearColor(0,0,0,0);
    gl.glClearDepthf(1.0f);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glEnable(GL10.GL_BLEND);
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,                /*GL10.GL_REPLACE*/ GL10.GL_MODULATE);  
}

[text sprite image]

gl.glPushMatrix();
try {
    float transx = ...'
    float transy = ...;

    gl.glTranslatef(transx, transy, 0.0f);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);


    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glColor4f(1f, 1f, 1f, 0.2f); // <---- alpha 0.2f
} catch (NullPointerException e) {
   Log.e("Fess", e.getMessage());
}
gl.glPopMatrix();

[other sprite images with alpha 1f]

gl.glPushMatrix();
try {
    float transx = ...'
    float transy = ...;

    gl.glTranslatef(transx, transy, 0.0f);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);


    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glColor4f(1f, 1f, 1f, 1f);
} catch (NullPointerException e) {
   Log.e("Fess", e.getMessage());
}
gl.glPopMatrix();

and this is how im load them:

public void loadTextures(GL10 gl, Context context) {
    Log.e(LOG_TAG, "ExplosionSprite :: loadTextures");  

    mFrame = 0;

    InputStream is;
    Bitmap bitmap;
    is = context.getResources().openRawResource(DRAW_SOURCE);


    bitmap = BitmapFactory.decodeStream(is);
    try {
        is.close();
        is = null;
    } catch (IOException e) {
    }

    gl.glGenTextures(TEXTURE_COUNT, textures, 0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle(); 

}

Thank you

\$\endgroup\$
1
\$\begingroup\$

Have you tried calling glColor4f(1,1,1,1) after you have drawn the image? You have to do that to reset the drawing color back to solid.

EDIT: Now that I read your code more carefully, it seems that you set the color only AFTER you have drawn the sprites. Call the glColor function before glDrawArrays, and it should work.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank you mate, works like charm, it ran away from me about to set alpaha before GL10.GL_VERTEX_ARRAY \$\endgroup\$ – Maxim Shoustin Aug 16 '13 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.