title: Find out the texture portion needed for a mesh
body: <p>I've got a very specific problem. I have an OpenGL application that is used to render video onto 3D meshes. As it turns out, I can make my video sources send me rectangular portions of the image, reducing memory usage. These portions are specified as a <code>Rectangle2D(int x, int y, int width, int height)</code> with <code>0 &lt;= x &lt;= w &lt;= sourceVideoWidth</code> and <code>0 &lt;= y &lt;= h &lt;= sourceVideoHeight</code>.</p>
  
  <p>With that said, I want to find out, for each frame, and for each mesh the following:</p>
  
  <ul>
  <li>Whether the mesh is visible</li>
  <li>If so, what portion of image should I request</li>
  </ul>
  
  <p>The benefit is reducint the texture upload to GPU, this operation is often the bottleneck in my application.</p>
  
  <p>In order to <strong>simplify</strong> the problem let's make the assumption that all meshes are 3D rectangles arbitrarily positioned. A 3D rectangle is defined by four points:</p>
  
  <pre><code>class Rectangle3D
  {
  public:
      Vec3 topLeft;
      Vec3 topRight;
      Vec3 botLeft;
      Vec3 botRight;
  }
  </code></pre>
  
  <hr>
  
  <p>Possible solutions:</p>
  
  <p>A) Split the mesh into a point grid of points with known texture coordinates, and run frustum culling for each point, then, from the visible points find the top left and bottom right texture coordinates that we must request. This is rather inefficient, and the number of points to test multiplies when we add another mesh to the scene. Solutions that use just the four corners of the rectangle might be preferable.</p>
  
  <p>B) Using the frustum defining planes (see <a href="http://www.lighthouse3d.com/tutorials/view-frustum-culling/" rel="nofollow">frustum culling</a>). For further simplicity, using only the four planes that correspond to the screen sides. Finding out whether the mesh is visible is rather simple. Finding the visible texture coordinates would need several cases:
   - One or more frustum sides intersect with the mesh
   - No frustum sides intersect with the mesh
    - Either the mesh is fully visible
    - Or the mesh is surrounding the screen sides
  In any case I need several plane-plane and plane-line segment intersections. Which are not necessarily efficient.</p>
  
  <p>C) Make a 2D projection of the Rectangle3D lines, resulting into a four side polygon, then using line segment intersection between the screen sides and the polygon sides. Also accounting for cases where we have no intersection and the mesh is still visible.</p>
  
  <p>D) Using OpenGL occlusion query objects, this way a render pass could generate information about the visible mesh portion.</p>
  
  <p>Is there any other solution that best solves this problem? If not which one would you use and why?</p>