Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL ES 2 : use an additional texture unit to blend this image with the current one
    text
    copied!<p>I am trying to figure out a tutorial on OpenGL ES 2.0</p> <p>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."</p> <p>But I am lost... </p> <p>This is the code:</p> <pre><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(); } </code></pre> <p>where</p> <p>textureProgram is:</p> <pre><code>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; } } </code></pre> <p>this is the fragment shader</p> <pre><code>precision mediump float; uniform sampler2D u_TextureUnit; varying vec2 v_TextureCoordinates; void main() { gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates); } </code></pre> <p>this is the vertex shader:</p> <pre><code>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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload