OpenGL, how to set a monocrome texture to a colored shape?

Posted by Santiago on Stack Overflow See other posts from Stack Overflow or by Santiago
Published on 2010-05-15T18:45:48Z Indexed on 2010/05/15 18:54 UTC
Read the original article Hit count: 214

Filed under:
|
|
|

I'm developing on Android with OpenGL ES, I draw some cubes and I change its colors with glColor4f. Now, what I want is to give a more realistic effect on the cubes, so I create a monochromatic 8bit depth, 64x64 pixel size PNG file. I loaded on a texture, and here is my problem, witch is the way to combine the color and the texture to get a colorized and textured cubes onto the screen?

I'm not an expert on OpenGL, I tried this:

On create:

public void asignBitmap(GL10 gl, Bitmap bitmap)
{
    int[] textures = new int[1];
    gl.glGenTextures(1, textures, 0);

    mTexture = textures[0];
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_ALPHA, bitmap, 0);

    ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    mTexBuffer = tbb.asFloatBuffer();
    for (int i = 0; i < 48; i++) mTexBuffer.put(texCoords[i]);
    mTexBuffer.position(0);
}

And OnDraw:

public void draw(GL10 gl, int alphawires) {
    gl.glColor4f(1.0f, 0.0f, 0.0f, 0.5f); //RED
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnable(GL10.GL_BLEND);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);

    //Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    //Point to our buffers
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    //Enable the vertex and color state
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    //Draw the vertices as triangles, based on the Index Buffer information
    gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, indexBuffer);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_BLEND);
    gl.glDisable(GL10.GL_TEXTURE_2D);
}

I'm even not sure if I have to use a blend option, because I don't need transparency, but is a plus :)

Thank's

© Stack Overflow or respective owner

Related posts about android

Related posts about opengl-es