android - OpenGL ES 2 : use an additional texture unit to blend this image with the current one

标签 android opengl-es-2.0

我正在尝试找出有关 OpenGL ES 2.0 的教程

现在本书要求作为练习“尝试加载不同的图像,并使用额外的纹理单元将此图像与当前图像混合。您可以在将值分配给 gl_FragColor 时尝试将它们相加或相乘你的 fragment 着色器。”

但是我迷路了...

这是代码:

@Override
public void onDrawFrame(GL10 gl) {
    // Clear the rendering surface.
    glClear(GL_COLOR_BUFFER_BIT);
    // Draw the table.
    textureProgram.useProgram();
    textureProgram.setUniforms(projectionMatrix, texture);
    table.bindData(textureProgram);
    table.draw();
}

在哪里

textureProgram 是:

public class TextureShaderProgram extends ShaderProgram {

// Uniform locations
private final int uMatrixLocation;
private final int uTextureUnitLocation;
private final int uTextureUnitLocation2;
// Attribute locations
private final int aPositionLocation;
private final int aTextureCoordinatesLocation;

public TextureShaderProgram(Context context) {
    super(context, R.raw.texture_vertex_shader,
    R.raw.texture_fragment_shader);
    // Retrieve uniform locations for the shader program.
    uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
    uTextureUnitLocation2 = glGetUniformLocation(program, "u_TextureUnit2");

    uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT);
    // Retrieve attribute locations for the shader program.
    aPositionLocation = glGetAttribLocation(program, A_POSITION);
    aTextureCoordinatesLocation =
    glGetAttribLocation(program, A_TEXTURE_COORDINATES);
    }

public void setUniforms(float[] matrix, int textureId) {
    // Pass the matrix into the shader program.
    glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
    glActiveTexture(GL_TEXTURE0);
    // Bind the texture to this unit.
    glBindTexture(GL_TEXTURE_2D, textureId);
    // Tell the texture uniform sampler to use this texture in the shader by
    // telling it to read from texture unit 0.
    glUniform1i(uTextureUnitLocation, 0);
    }

public int getPositionAttributeLocation() {
    return aPositionLocation;
    }
    public int getTextureCoordinatesAttributeLocation() {
    return aTextureCoordinatesLocation;
    }
}

这是 fragment 着色器

precision mediump float;
uniform sampler2D u_TextureUnit;

varying vec2 v_TextureCoordinates;
void main()
{
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
}

这是顶点着色器:

uniform mat4 u_Matrix;
attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;
varying vec2 v_TextureCoordinates;
void main()
{
v_TextureCoordinates = a_TextureCoordinates;
gl_Position = u_Matrix * a_Position;
}

最佳答案

OpenGL ES 能够对 2 个或更多纹理进行采样以生成最终的 gl_FragColor。您的 Java 代码似乎可以将其设置好,但您的 fragment 着色器仅使用其中一个纹理。两个加起来应该是这样的:

uniform sampler2D u_TextureUnit_0;
uniform sampler2D u_TextureUnit_1;
varying vec2 v_TextureCoordinates;

void main()
{
    vec4 vColor_0 = texture2D(u_TextureUnit_0, v_TextureCoordinates);
    vec4 vColor_1 = texture2D(u_TextureUnit_1, v_TextureCoordinates);
    gl_FragColor = vColor_0 + vColor_1;  
}

这会将 2 个 vec4 颜色样本加在一起:(R0 + R1, G0 + G1, B0 + B1, A0 + A1)

关于android - OpenGL ES 2 : use an additional texture unit to blend this image with the current one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17890271/

相关文章:

android - 无法为android图形 View 线图生成数据点系列

android - Jelly Bean 令人困惑的 LogCat

ios - 在结构中传递法线

iphone - 半透明白色看起来是灰色的 (OpenGL ES 2.0)

java - 使用引导类加载器找不到类

android - 使用 Sinch 发送消息总是失败

android - 线性布局顺序在某些设备上是相反的

android - 如果 ZoomCamera 从其原始位置移动,则 HUD 上的 Sprite 按钮不会检测到触摸事件

android - 在 OpenGL ES 2.0 的显示函数中生成纹理

c - OpenGL ES 2.0 : Can't seem to render second VBO?