title: android.grapics.matrix to OpenGL 2.0 ES texture translation
body: <p>Currently, the application displays an ImageView that successfully zooms and pans around on the screen.  On top of that image, I would like to provide a texture that I would like to update itself to zoom or pan when the image below it zooms or pans.</p>
  
  <p>As I understand, this should be possible with using the getImageMatrix() of my current setup on the ImageView and then applying that to my textured bitmap that is on top of the original image.</p>
  
  <p><strong>Edit &amp; Resolution:</strong> (with strong aide from the selected answer below)</p>
  
  <p>Currently, the panning in the texture occurs at a different speed than that of the ImageView, but when that has been resolved I will update this posting with additional edits and provide the solution to the entirety of the application.  Then only the solution and a quick problem description paragraph will remain. Perhaps I'll even post a surface view and renderer source code for using a zoomable surface view.</p>
  
  <p>In order to accomplish a mapping of an ImageView to an OpenGL texture, there were a couple of things that needed to be done in order to accomplish this correctly. And hopefully, this will aide other people who might want to use a zoomable SurfaceView in the future.</p>
  
  <p>The shader code is written below.  The gl_FragColor takes a translation matrix of a 3x3 from the getImageMatrix of any ImageView in order to update the screen from within the onDraw() method.</p>
  
  <pre><code>private final String vertexShader_ =
          "attribute vec4 a_position;\n" +
          "attribute vec4 a_texCoord;\n" +
          "varying vec2 v_texCoord;\n" +
          "void main() {\n" +
          "  gl_Position = a_position;\n" +
          "  v_texCoord = a_texCoord.xy;\n" +
          "}\n";
  
  private final String fragmentShader_ =
          "precision mediump float;\n" +
          "varying vec2 v_texCoord;\n" +
          "uniform sampler2D texture;\n" +
          "uniform mat3 transform;\n" +
          "void main() {\n" +
          "  vec2 uv = (transform * vec3(v_texCoord, 1.0)).xy;\n" +
          "  gl_FragColor = texture2D( texture, uv );\n" +
          "}\n";
  </code></pre>
  
  <p>At the start, the translation matrix for the OpenGL is simply an identify matrix and our ImageView will update these shared values with the shader through a listener.  </p>
  
  <pre><code>private float[] translationMatrix = {1.0f, 0.0f, 0.0f,
                                       0.0f, 1.0f, 0.0f,
                                       0.0f, 0.0f, 1.0f};
  ...
  uniforms_[TRANSLATION_UNIFORM] = glGetUniformLocation(program_, "transform");
  checkGlError("glGetUniformLocation transform");
  if (uniforms_[TRANSLATION_UNIFORM] == -1) {
      throw new RuntimeException("Could not get uniform location for transform");
  }
  ....
  glUniformMatrix3fv(uniforms_[TRANSLATION_UNIFORM], 1, false, FloatBuffer.wrap(translationMatrix));
  ....
  </code></pre>
  
  <p>However, the OpenGL code needs an inverse of the OpenGL matrix calculations that come from Android, as well as a transpose operation performed on them before they are handed off to the renderer for being displayed on the screen. This has to do with the way that that information is stored.</p>
  
  <pre><code>protected void onMatrixChanged()
  {
      //...
      float[] matrixValues = new float[9];
      Matrix imageViewMatrix = getImageViewMatrix();
      Matrix invertedMatrix = new Matrix();
      imageViewMatrix.invert(invertedMatrix);
      invertedMatrix.getValues(matrixValues);
              transpose(matrixValues);
      matrixChangedListener.onTranslation(matrixValues);
      //...
  }
  </code></pre>
  
  <p>In addition to the fact that the values are in the wrong locations for input into the OpenGL renderer, we also have the problem that we are dealing with our translations on the scale of imageHeight and imageWidth instead of the normalized [0, 1] range that OpenGL expects.  So, in order to correct with this we have to target the last column with divisible numbers of our width and height.</p>
  
  <pre><code>matrixValues[6] = matrixValues[6] / getImageWidth();
  matrixValues[7] = matrixValues[7] / getImageHeight();
  </code></pre>
  
  <p>Then, after you have accomplished this, you can start mapping with the ImageView matrix functions or the updated ImageTouchView matrix functions (which have some additional nice features when handling images on Android).</p>
  
  <p>The effect can be seen below with the ImageView behind it taking up the whole screen and the texture in front of it being updated based upon updates from the ImageView.  This handles zooming and panning translations between the two.</p>
  
  <p><img src="https://i.stack.imgur.com/jHEbm.jpg" alt="imageview updated opengl texture"></p>
  
  <p>The only other step in the process would be to change the size of the SurfaceView to match that of the ImageView in order to get the full screen experience when the user is zooming in.  In order to get only a surfaceview shown, you can simply put an '<em>invisible</em>' imageview behind it in the background.  And just let the ImageTouchView updates change the way that your OpenGL SurfaceView is shown.</p>
  
  <p><strong>Vote up on the answer below</strong>, as they helped me offline through chat significantly to get to this point in understanding what needs to be done for linking an imageview with a texture.  They deserve it.</p>
  
  <p>If you do end up going with the vertex shader modification below, instead of the fragment shader resolution, then it would seem that the values no longer need to be inverted to work with the pinch zoom anymore (and in fact work backwards if you leave it as is).  Additionally, panning seems to work backwards than is expected as well.</p>