KINECT STATS GENERATOR FOR SPORTS VISUALISATION  1.0
RGBboundsVisualization.cpp
Go to the documentation of this file.
00001 #include "RGBboundsVisualization.h"
00002 #include <iostream>
00003 #include <ngl/NGLInit.h>
00004 #include <libfreenect.hpp>
00005 #include <libfreenect_sync.h>
00006 #include <Mutex.h>
00007 #include <QDebug>
00008 #include <KinectInterface.h>
00009 #include <ngl/ShaderLib.h>
00010 
00011 /*
00012   LICENSING: You are free to use any part of this project provided I am informed about it via email
00013   at santoshwins@hotmail.com and referenced appropriately in your works
00014   */
00015 
00016 //---------------------------------------------------------------------------------------------------------------------
00017 RGBboundsVisualization::RGBboundsVisualization(
00018                       QWidget *_parent
00019                     ) : QGLWidget(new CreateCoreGLContext(QGLFormat::defaultFormat()), _parent)
00020 
00021 {
00022 
00023   // set this widget to have the initial keyboard focus
00024   // re-size the widget to that of the parent (in this case the GLFrame passed in on construction)
00025   this->resize(_parent->size());
00026   startTimer(5); // increasing this timer causes the stats window to lose its information
00027 
00028   m_rgb = cvCreateMat(640,480,CV_8UC3);
00029   m_drawingStatus = false;
00030 }
00031 
00032 // This virtual function is called once before the first call to paintGL() or resizeGL(),
00033 //and then once whenever the widget has been assigned a new QGLContext.
00034 // This function should set up any required OpenGL context rendering flags, defining display lists, etc.
00035 
00036 //----------------------------------------------------------------------------------------------------------------------
00037 void RGBboundsVisualization::initializeGL()
00038 {
00039   glClearColor(0.4f, 0.4f, 0.4f, 1.0f);        // Grey Background
00040   // enable depth testing for drawing
00041 
00042     ngl::NGLInit *Init = ngl::NGLInit::instance();
00043     Init->initGlew();
00044     // now we have valid GL can create shaders etc
00045     ngl::ShaderLib *shader = ngl::ShaderLib::instance();
00046     shader->createShaderProgram("TextureRGB");
00047 
00048     shader->attachShader("TextureVertex",ngl::VERTEX);
00049     shader->attachShader("TextureFragment",ngl::FRAGMENT);
00050     shader->loadShaderSource("TextureVertex","shaders/TextureVert.glsl");
00051     shader->loadShaderSource("TextureFragment","shaders/TextureFrag.glsl");
00052 
00053     shader->compileShader("TextureVertex");
00054     shader->compileShader("TextureFragment");
00055     shader->attachShaderToProgram("TextureRGB","TextureVertex");
00056     shader->attachShaderToProgram("TextureRGB","TextureFragment");
00057 
00058     shader->linkProgramObject("TextureRGB");
00059     (*shader)["TextureRGB"]->use();
00060     shader->setShaderParam1i("tex",0);
00061 
00062     m_screen = new ScreenQuad(640,480,"TextureRGB");
00063 
00064 
00065     //frame rate GUNDU
00066 //    m_text=new ngl::Text(QFont("Arial",14));
00067 //    currentTime = currentTime.currentTime();
00068 
00069 }
00070 
00071 //----------------------------------------------------------------------------------------------------------------------
00072 //This virtual function is called whenever the widget has been resized.
00073 // The new size is passed in width and height.
00074 void RGBboundsVisualization::resizeGL(
00075                         int _w,
00076                         int _h
00077                        )
00078 {
00079   glViewport(0,0,_w,_h);
00080 
00081    /*m_text->setScreenSize(_w,_h);*/
00082 }
00083 
00084 //----------------------------------------------------------------------------------------------------------------------
00085 //This virtual function is called whenever the widget needs to be painted.
00086 // this is our main drawing routine
00087 void RGBboundsVisualization::paintGL()
00088 {
00089     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00090 
00091 
00092     ngl::ShaderLib *shader = ngl::ShaderLib::instance();
00093     (*shader)["TextureRGB"]->use();
00094 
00095     // calculate the framerate //frame rate GUNDU
00096 //    QTime newTime = currentTime.currentTime();
00097 //    int msecsPassed = currentTime.msecsTo(newTime);
00098 //    currentTime = newTime;
00099 
00100 
00101 
00102     KinectInterface *kinect=KinectInterface::instance();
00103     kinect->getRGB(m_rgb);
00104 
00105     cv::rectangle(m_rgb, cv::Point(m_selectionBox.x, m_selectionBox.y), cv::Point(m_selectionBox.x+m_selectionBox.width,m_selectionBox.y+m_selectionBox.height),
00106                     cv::Scalar(255,0,0),5 );
00107 
00108     m_screen->draw(&m_rgb);
00109 
00110 
00111    /* QString text;
00112     // framerate GUNDU
00113     m_text->setColour(1,1,1);
00114     text.sprintf("framerate is %d",(int)(1000.0/msecsPassed));
00115     m_text->renderText(10,50,text);*/
00116 }
00117 
00118 //----------------------------------------------------------------------------------------------------------------------
00119 void RGBboundsVisualization::mouseMoveEvent (QMouseEvent * _event )
00120 {
00121  // Q_UNUSED(_event);
00122 
00123     if( m_drawingStatus ){
00124                     m_selectionBox.width = _event->x()-m_selectionBox.x;
00125                     m_selectionBox.height = _event->y()-m_selectionBox.y;
00126                 }
00127 
00128   updateGL();
00129 }
00130 
00131 
00132 //----------------------------------------------------------------------------------------------------------------------
00133 void RGBboundsVisualization::mousePressEvent ( QMouseEvent * _event  )
00134 {
00135   //Q_UNUSED(_event);
00136 
00137 //    std::cout<<_event->x()<<"\n";
00138 //    std::cout<<_event->y()<<"\n";
00139    // std::cout<<_event->globalX()<<"\n";
00140    // std::cout<<_event->globalY()<<"\n";
00141 
00142      if(_event->buttons() == Qt::LeftButton)
00143      {
00144 
00145                 m_drawingStatus = true;
00146                 m_selectionBox = cv::Rect(_event->x(), _event->y(), 0, 0 );
00147 
00148 
00149 
00150      }
00151      else if(_event->buttons() == Qt::RightButton)
00152      {
00153          KinectInterface *kinect=KinectInterface::instance();
00154 
00155          m_drawingStatus = false;
00156 
00157 
00158 
00159          kinect->setBoxObject(m_selectionBox.x,m_selectionBox.y,m_selectionBox.width,m_selectionBox.height);
00160 
00161 //         std::cout<<m_selectionBox.x<<"\n";
00162 //         std::cout<<m_selectionBox.y<<"\n";
00163 //         std::cout<<m_selectionBox.width<<"\n";
00164 //         std::cout<<m_selectionBox.height<<"\n";
00165 
00166      }
00167     updateGL();
00168 
00169    // cv::line(m_rgb,cv::Point(0,0),cv::Point(630,470),cv::Scalar(255,0,0),4);
00170 
00171 }
00172 
00173 //----------------------------------------------------------------------------------------------------------------------
00174 void RGBboundsVisualization::mouseReleaseEvent ( QMouseEvent * _event )
00175 {
00176   // this event is called when the mouse button is released
00177   // we then set Rotate to false
00178  // Q_UNUSED(_event);
00179 
00180     if(_event->buttons() == Qt::LeftButton)
00181     {
00182         m_drawingStatus = false;
00183         if( m_selectionBox.width < 0 ){
00184             m_selectionBox.x += m_selectionBox.width;
00185             m_selectionBox.width *= -1;
00186         }
00187         if( m_selectionBox.height < 0 ){
00188             m_selectionBox.y += m_selectionBox.height;
00189             m_selectionBox.height *= -1;
00190         }
00191 
00192 
00193 
00194     }
00195 
00196 
00197     updateGL();
00198 }
00199 
00200 void RGBboundsVisualization::timerEvent( QTimerEvent *_event)
00201 {
00202     Q_UNUSED(_event);
00203     // re-draw GL
00204     updateGL();
00205 }
00206 
00207 RGBboundsVisualization::~RGBboundsVisualization()
00208 {
00209     if(m_screen)
00210     {
00211         delete m_screen;
00212     }
00213 }
00214 
00215 
00216 
 All Classes Files Functions Variables Enumerations Enumerator