title: Unwarping a texture in OpenGL
body: <p>Say I've got a texture mapped to a grid screen-aligned mesh. It looks something like:</p>
  
  <p><img src="https://i.stack.imgur.com/CMly2.jpg" alt="original image"></p>
  
  <p>The vertex positions are:</p>
  
  <pre><code>(-1, -1), (1, -1), (-1, 1), (1, 1)
  </code></pre>
  
  <p>The UVs:</p>
  
  <pre><code>(0, 0), (1, 0), (0, 1), (1,1)
  </code></pre>
  
  <p>I warp the image by moving around the vertices and save the output by doing a <code>glReadPixels()</code>.</p>
  
  <p>The new warped vertex positions are:</p>
  
  <pre><code>(-1, -1), (0.8, -0.8), (-0.6, 0.6), (0.4, 0.4)
  </code></pre>
  
  <p>And the produced output is like:</p>
  
  <p><img src="https://i.stack.imgur.com/9ticA.jpg" alt="warped image"></p>
  
  <p>Next time as an input I use the <em>warped image</em> that I've just saved. What I'm trying to do is reverse the warping effect I did before, by modifying the vertex coordinates. Initially I thought that the coordinates that would unwarp the image must be something of the sort:</p>
  
  <pre><code>x_unwarp = 2 * x_original - x_warped
  </code></pre>
  
  <p>But it's not working. The warping effect doesn't get undone. What I get is something like:</p>
  
  <p><img src="https://i.stack.imgur.com/5pwkd.jpg" alt="attempted unwarp"></p>
  
  <p>Any idea what I'm doing wrong and how is should modify the vertex coords or perhaps uvs? I'm sure I've got the math wrong.</p>
  
  <p>Thanks!</p>
  
  <h2>UPDATE</h2>
  
  <p>It seems that my formula is wrong. I should've used matrices:</p>
  
  <pre><code>transform_matrix * x_original = x_warped
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>x_unwarped = inverse(tranform_matrix) * x_original
  </code></pre>
  
  <p>As the transformation I'm doing is pure scaling, the transform matrix is like:</p>
  
  <pre><code>/     \
  | S  0 |
  | 0  S |
  \      /
  </code></pre>
  
  <p>Where S is the scale factor. Therefore the inverse would be:</p>
  
  <pre><code>/         \
  | 1/S  0  |
  | 0   1/S |
  \         /
  </code></pre>
  
  <p>Thus this would give the unwarping vertex positions as:</p>
  
  <pre><code>(-1, -1), (1.25, -1.25), (-1.67, 1.67), (2.5, 2.5)
  </code></pre>
  
  <p>Seems straighter, but still incorrect:</p>
  
  <p><img src="https://i.stack.imgur.com/bodgV.png" alt="second attempt to unwarp"></p>
  
  <h2>Second update</h2>
  
  <p>I seem to have started it wrong from the beginning. There's been some random factor in all the renders. I'm going to fix that and try it again.</p>
  
  <h2>Update: UV warp</h2>
  
  <p>Done it using UV warping this time.</p>
  
  <p>Warped using these UVs:</p>
  
  <pre><code>(0.0, 0.0), (0.9, 0.0), (0.0, 0.8), (0.7, 0.7)
  </code></pre>
  
  <p>This produced:</p>
  
  <p><img src="https://i.stack.imgur.com/vIpqd.png" alt="uv warped"></p>
  
  <p>And then tried to unwarp using the inverses:</p>
  
  <pre><code>(0.0, 0.0), (1/0.9, 0.0), (0.0, 1/0.8), (1/0.7, 1/0.7)
  </code></pre>
  
  <p>Which looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/9dqII.png" alt="unsuccessful uv unwarp"></p>
  
  <p>This didn't do it either. I'm starting to worry about the 1/0 cases that I just overlook.</p>
  
  <p>Completely clueless.</p>