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

柔情痞子 提交于 2019-12-11 06:37:01

问题


I'm developing on Android with OpenGL ES, I draw some cubes and I change their 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, which 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 it's a plus :)


回答1:


When you set the texture environment mode, use GL_MODULATE instead of GL_REPLACE. MODULATE will multiply the interpolated color with the sampled texture color, where REPLACE discards the interpolated color entirely, and replaces it with the sampled texture color.



来源:https://stackoverflow.com/questions/2841316/opengl-how-to-set-a-monochrome-texture-to-a-colored-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!