OpenGL,如何将单色纹理设置为彩色形状? [英] OpenGL, how to set a monochrome texture to a colored shape?

查看:431
本文介绍了OpenGL,如何将单色纹理设置为彩色形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用OpenGL ES开发,我绘制一些立方体,我用glColor4f改变他们的颜色。现在,我想要的是给立方体一个更现实的效果,所以我创建一个单色的8bit深度,64x64像素大小的PNG文件。我加载了一个纹理,这里是我的问题,这是组合颜色和纹理,以获得一个彩色和纹理的多维数据集到屏幕上的方式?

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?

我不是OpenGL的专家,我尝试过:

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

创建时:

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);
}

和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 :)

推荐答案

当设置纹理环境模式时,使用GL_MODULATE而不是GL_REPLACE。 MODULATE将内插颜色与采样的纹理颜色相乘,其中REPLACE完全舍弃内插颜色,并用采样的纹理颜色替换它。

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.

这篇关于OpenGL,如何将单色纹理设置为彩色形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆