-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomGLWidget.cpp
131 lines (109 loc) · 3.67 KB
/
CustomGLWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*************************************************************************
Copyright (c) 2010 Rafael Palomar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/** \file CustomGLWidget.cpp
*
* This file contains the implementation for the CustomGLWidget class
*
*/
#include "CustomGLWidget.h"
#include <iostream>
#include <QTimer>
#include <QMessageBox>
#include "cv.h"
#include "highgui.h"
/** \brief Constructor
*
* This function initializes the member variables for the CustomGLWidget class. In addition
* the function performs the connection between signals and slots, which in this case is referred
* to the connection between the timer signal and the capture/drawing slot.
*
* \arg parent parent widget.
*
*/
CustomGLWidget::CustomGLWidget(QWidget *parent):QGLWidget(parent)
{
//Initialize variable members
m_timer = new QTimer();
m_image = 0;
m_capture = cvCaptureFromCAM(0);
if(!m_capture)
QMessageBox::critical(this,"Error","Error initializing capture from WebCam");
//Get an initial frame from the webcam
m_image = cvQueryFrame(m_capture);
//Connect the timer signal with the capture action
connect(m_timer, SIGNAL(timeout()), this, SLOT(captureFrame()));
//Start the timer scheduled for firing every 33ms (30fps)
m_timer->start(33);
}
/** \brief Destructor
*
* This function releases the capture from the webcam by calling
* the proper OpenCV function.
*/
CustomGLWidget::~CustomGLWidget()
{
//Release the capture
cvReleaseCapture(&m_capture);
}
/** \brief Initialize OpenGL
*
* This function is the responsible for setting up the dimensions of the viewport
* and projection matrix (in this context). Initializes the viewport to the size of
* the widget and the projection is set up to get a "world" of size 'width' x 'height'
*
*/
void CustomGLWidget::initializeGL()
{
//Adjust the viewport
glViewport(0,0,this->width(), this->height());
//Adjust the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-this->width()/2, this->width()/2, this->height()/2, -this->height()/2, -1, 1);
}
/** \brief Paint GL
*
* This function draws the scene which in this case is the capture from the webcam.
* in order to adapt the output from the OpenCV's capture, an inversion is needed.
*
*/
void CustomGLWidget::paintGL()
{
//Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
//Set the raster position
/*
The position seems to be the inverse because the rendering is
affected by the glPixelZoom call.
*/
glRasterPos2i(this->width()/2,-this->height()/2);
//Inver the image (the data coming from OpenCV is inverted)
glPixelZoom(-1.0f,-1.0f);
//Draw image from OpenCV capture
glDrawPixels(m_image->width, m_image->height, GL_RGB, GL_UNSIGNED_BYTE,m_image->imageData);
}
/** \brief Capture Frame
*
* This function performs the capture of a frame from the webcam and
* calls the function to render the scene.
*
*/
void CustomGLWidget::captureFrame()
{
//Get an image from the webcam
m_image = cvQueryFrame(m_capture);
//Draw the scene
glDraw();
}