title: How to make OpenGL viewport have the exact size and position of my QML item which renders into it?
body: <p>I'm trying to render a <code>640x360</code> red square inside a <code>1280x720</code> window. The problem is that OpenGL's viewport won't automatically be inside the area occupied by the square into the Qt window system. That is, if my video object have <code>640x360</code> dimensions, the OpenGl viewport still have the <code>1280x720</code> dimensions (WHY?).</p>
  
  <p>Anyways, I can fix that by using <code>glViewport(this-&gt;x, this-&gt;y, this-&gt;width, this-&gt;height);</code>. The problem is that in OpenGL, the coordinate system has origin at the left low corner, and <code>this-&gt;x, this-&gt;y, this-&gt;width, this-&gt;height</code> are from the object's coordinates in QML syntax, where the origin is in the upper left corner. The result is this:</p>
  
  <p><strong>main.qml</strong>:</p>
  
  <pre><code>import QtQuick 2.0
  
  import OpenGlVideoQtQuick2 1.0
  
  
  Grid {
      columns: 2
      spacing: 2    
      width: 1280
      height: 720
      OpenGlVideoQtQuick2 {
          width: 640
          height: 360
      }
  
  }
  </code></pre>
  
  <p><strong>but the red square occupies the 3rd grid instead of the 1st one:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/zIBLN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zIBLN.png" alt="enter image description here"></a></p>
  
  <p>I could <strong>easily</strong> fix that by doing <code>glViewport(this-&gt;x, WINDOW_HEIGHT-this-&gt;y, this-&gt;width, this-&gt;height);</code> where <code>WINDOW_HEIGHT</code> would have the value <code>720</code>, however I don't think I should trust the OpenGL viewport to always have the size of the window, so instead I should get the OpenGl viewport dimensions, but I don't think that that is possible. </p>
  
  <p>I tried</p>
  
  <pre><code>GLint dims[2] = {9999, 9999};
  glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
  std::cout &lt;&lt; "dimenson x:" &lt;&lt; dims[0] &lt;&lt; " dimenson y:" &lt;&lt; dims[1] &lt;&lt; std::endl;
  </code></pre>
  
  <p>which gives me <code>dimenson x:8192 dimenson y:8192</code> which makes no sense to me.</p>
  
  <p>Here is the OpenGL rendering code: <a href="https://github.com/lucaszanella/QQuickPaintedItemBug/blob/0a8ef6b5229afa7113ec1e4e3838981042792329/OpenGlVideoQtQuick2.cpp" rel="nofollow noreferrer">https://github.com/lucaszanella/QQuickPaintedItemBug/blob/0a8ef6b5229afa7113ec1e4e3838981042792329/OpenGlVideoQtQuick2.cpp</a></p>
  
  <p>You can view an entire buildable project here: <a href="https://github.com/lucaszanella/QQuickPaintedItemBug/tree/0a8ef6b5229afa7113ec1e4e3838981042792329" rel="nofollow noreferrer">https://github.com/lucaszanella/QQuickPaintedItemBug/tree/0a8ef6b5229afa7113ec1e4e3838981042792329</a></p>