Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
thank you, back when Ill try –  Maxim Shoustin Aug 16 '13 at 3:41
    
thank you mate, works like charm, it ran away from me about to set alpaha before GL10.GL_VERTEX_ARRAY –  Maxim Shoustin Aug 16 '13 at 6:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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