Reputation: 4595
I am trying to figure out a tutorial on OpenGL ES 2.0
Now the book requires as an exercise to "Try loading in a different image, and use an additional texture unit to blend this image with the current one. You can try adding or multiplying the values together when you assign them to gl_FragColor in your fragment shader."
But I am lost...
This is the code:
@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();
}
where
textureProgram is:
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;
}
}
this is the fragment shader
precision mediump float;
uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;
void main()
{
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
}
this is the vertex shader:
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;
}
Upvotes: 1
Views: 1962
Reputation: 2832
OpenGL ES has the ability to sample 2 or more textures to produce the final gl_FragColor. Your Java code seems to set this up OK, but your fragment shader only uses one of the textures. It should be like this to add two together:
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;
}
This adds the 2 vec4 color samples together: (R0 + R1, G0 + G1, B0 + B1, A0 + A1)
Upvotes: 3