title: OpenCV + OpenGL + Qt
body: <p>I'm designing my own augmented reality application.
  I have already detected the 4 corners for the patterns I'm working with. After detecting the 4 corners in a proper order I'm passing them into cvFindExtrinsicCameraParams2.
  I'm getting nice results for the rotation and translation of the object respect to the camera frame.
  Now I have to put that information (Rotation Vector and Translation Vector) into OpenGL to draw something. Naturally, I'm using cvRodrigues2 to get the rotation matrix from rotation vector.
  In addition to this I'm watching the camera with QGlWidget in this way:</p>
  
  <p><strong>GLWidget.h</strong></p>
  
  <pre><code>#ifndef _GLWIDGET_H
  #define _GLWIDGET_H
  
  #include &lt;QtOpenGL/QGLWidget&gt;
  #include &lt;cv.h&gt;
  #include &lt;cxcore.h&gt;
  
  class GLWidget : public QGLWidget {
  
      Q_OBJECT // must include this if you use Qt signals/slots
  
  public:
      GLWidget(QWidget *parent = NULL);
      IplImage *img;
      void setImage(IplImage *imagen);
  protected:
      void initializeGL();
      void resizeGL(int w, int h);
      void paintGL();
  };
  
  #endif  /* _GLWIDGET_H */
  </code></pre>
  
  <p><strong>GLWidget.cpp</strong></p>
  
  <pre><code>#include &lt;QtGui/QMouseEvent&gt;
  #include "GLWidget.h"
  
  GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
      this-&gt;img = 0;
  }
  
  void GLWidget::initializeGL() {
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_DEPTH_TEST);
      glDisable(GL_COLOR_MATERIAL);
      glEnable(GL_BLEND);
      glEnable(GL_POLYGON_SMOOTH);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glClearColor(0, 0, 0, 0);
  }
  
  void GLWidget::resizeGL(int w, int h) {
      glViewport(0, 0, w, h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
  //    gluPerspective(52.0f, 1.3333f, 0.1f, 100.0f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
  
  }
  
  void GLWidget::paintGL() {
  
      if(this-&gt;img)
      {
          glClear (GL_COLOR_BUFFER_BIT);
          glClearColor(0.0,0.0,0.0,1.0);
          glMatrixMode(GL_PROJECTION);
          glPushMatrix();
          glLoadIdentity();
          gluOrtho2D(0.0,img-&gt;width, 0.0, img-&gt;width);
          glMatrixMode(GL_MODELVIEW);
          glPushMatrix();
          glLoadIdentity();
          glDrawPixels(img-&gt;width,img-&gt;height,GL_RGB,GL_UNSIGNED_BYTE,img-&gt;imageData);
          glMatrixMode(GL_PROJECTION);
          glPopMatrix();
          glMatrixMode(GL_MODELVIEW);
          glPopMatrix();
      }
  }
  void GLWidget::setImage(IplImage *imagen)
  {
      this-&gt;img = imagen;
      this-&gt;updateGL();
  }
  </code></pre>
  
  <p>Ok, so, as an additional note in the constructor of my MainWindow I have something like:</p>
  
  <pre><code>windowGL = new GLWidget();
  windowGL.setParent(this);
  </code></pre>
  
  <p>In order to put the GL window inside the MainWindow. Also I created a new variable inside GLViewer.h called :</p>
  
  <pre><code>double modelViewMatrix[16] = {0.0};
  </code></pre>
  
  <p>So, in order to display a 3D object I created a method inside GLViewer like this:</p>
  
  <pre><code>void GlViewer::setModelView(double cameraMatrix[]){
      for(int i=0;i&lt;16;i++){
      this-&gt;modelViewMatrix[i] = cameraMatrix[i];
      }
      this-&gt;updateGL();
  }
  </code></pre>
  
  <p>After that, I'm putting into GLViewer::paintGL() method something like this after drawing the image with glDrawPixels() and obviously after popping matrices:</p>
  
  <pre><code>    glMatrixMode(GL_MODELVIEW);
      glLoadMatrixd(this-&gt;modelViewMatrix);
      glPushMatrix();
      glColor3f(1,0,0);
  
      glPushAttrib(GL_COLOR_BUFFER_BIT | GL_POLYGON_BIT | GL_ENABLE_BIT) ;
      glDisable(GL_LIGHTING) ;
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) ;
      glLineWidth(3);
  
      glBegin(GL_LINES) ;
              glColor3f(1,0,0) ;
              glVertex3f(0,0,0) ;
              glVertex3f(100,0,0);
  
              glColor3f(0,1,0) ;
              glVertex3f(0,0,0) ;
              glVertex3f(0,100,0);
  
              glColor3f(0,0,1) ;
              glVertex3f(0,0,0) ;
              glVertex3f(0,0,100);
      glEnd() ;
  
      glLineWidth(1) ;
      glPopAttrib() ;
      glMatrixMode(GL_MODELVIEW);
      glPopMatrix();
  </code></pre>
  
  <p>So, for modelViewMatrix I'm taking into account that I have to order into columns instead of rows, or transpose, whatever you want...
  So, naturally, as the last step I'm calling the created function GLWidget::setModelView after I converted the camera intrinsic and extrinsic into an array:</p>
  
  <pre><code>windowGL.setModelView(convertedFromIntrExtr);
  </code></pre>
  
  <p>However I'm seeing nothing... I've tried a code and I get that working drawing the axes but without the projection matrix... However, when I use glutPerspective() (obviously after glMatrixMode(GL_PROJECTION)) I just can't see anything... so, I don't know if someone out there has a working code in Qt with augmented reality without using ARToolkit because, as I said, I'm getting the extrinsic camera parameters for my own... So, If anyone has a working code in which you get the extrinsic camera parameters and translate them into openGL projection and modelview matrices... hmm well, would be very helpful... this is because I found this example:</p>
  
  <pre><code>http://old.uvr.gist.ac.kr/wlee/web/techReports/ar/Camera%20Models.html
  </code></pre>
  
  <p>I followed all steps to achieve that conversion between the 2 camera models without success...
  I'll really appreciate if someone has a working code... thanks!!!</p>