forked from karlphillip/GraphicsProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
187 lines (150 loc) · 5.15 KB
/
widget.cpp
File metadata and controls
187 lines (150 loc) · 5.15 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (C) 2013-2020 Karl Phillip Buhr <karlphillip@gmail.com>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "widget.h"
#include <iostream>
#include <QKeyEvent>
#include <QTimer>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// Enables the use of 3D perspective
//#define USE_PERSPECTIVE
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
_width = 0;
_height = 0;
_texture = 0;
_fps = 0;
}
GLWidget::~GLWidget()
{
glDeleteTextures(1, &_texture);
}
void GLWidget::_tick()
{
// triggers paintGL()
updateGL();
// Set timer according to FPS
QTimer::singleShot(1000/_fps, this, SLOT(_tick()));
}
void GLWidget::initializeGL()
{
// Set clear color as black
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
// Select pixel storage mode used by glTexImage2D
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Create the texture
glGenTextures(1, &_texture);
/* Open default camera device */
cv_capture.open(0);
if (!cv_capture.isOpened())
{
std::cout << "GLWidget::initializeGL: !!! Failed to open camera" << std::endl;
return;
}
// Retrieve FPS from the camera
_fps = cv_capture.get(cv::CAP_PROP_FPS);
if (!_fps) // if the function fails, fps is set to 15
_fps = 15;
std::cout << "GLWidget::initializeGL: " << _fps << " fps" << std::endl;
// Retrieve the width and height of the captured frame
_width = cv_capture.get(cv::CAP_PROP_FRAME_WIDTH);
_height = cv_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
resize(QSize(_width, _height));
std::cout << "GLWidget::initializeGL: capture size=" << _width << "x" << _height << std::endl;
std::cout << "GLWidget::initializeGL: window size=" << this->width() << "x" << this->height() << std::endl;
/* Start the timer */
_tick();
}
void GLWidget::paintGL()
{
// Abort drawing if OpenCV was unable to open the camera
if (!cv_capture.isOpened())
{
std::cout << "GLWidget::paintGL: !!! Failed to open camera" << std::endl;
return;
}
// Clear the screen and depth buffer (with black) then S«select the model view matrix and reset it
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#ifdef USE_PERSPECTIVE
glTranslatef(0.0f, 0.0f, -1.5);
#endif
// Note: trying to retrieve more frames than the camera can give you makes the output video blink a lot
cv_capture >> cv_frame;
if (cv_frame.empty())
{
std::cout << "GLWidget::paintGL: !!! Failed to retrieve frame" << std::endl;
return;
}
//std::cout << "GLWidget::paintGL: frame size " << cv_frame.cols << "x" << cv_frame.rows << std::endl;
cv::cvtColor(cv_frame, cv_frame, cv::COLOR_BGR2RGBA);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// Typical texture generation using data from the bitmap
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _texture);
// Transfer image data to the GPU
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
GL_RGBA, cv_frame.cols, cv_frame.rows, 0,
GL_RGBA, GL_UNSIGNED_BYTE, cv_frame.data);
if (glGetError() != GL_NO_ERROR)
{
std::cout << "GLWidget::paintGL: !!! Failed glTexImage2D" << std::endl;
}
// Draw a 2D face with texture
glBegin(GL_QUADS);
#ifdef USE_PERSPECTIVE
// glPerspective (3D)
glTexCoord2f(0, 0); glVertex2f(1.0, 1.0);
glTexCoord2f(cv_frame.cols, 0); glVertex2f(-1.0, 1.0);
glTexCoord2f(cv_frame.cols, cv_frame.rows); glVertex2f(-1.0, -1.0);
glTexCoord2f(0, cv_frame.rows); glVertex2f(1.0, -1.0);
#else
// glOrtho (2D)
glTexCoord2f(0, 0); glVertex2f( 0.0, 0.0);
glTexCoord2f(cv_frame.cols, 0); glVertex2f(_width, 0.0);
glTexCoord2f(cv_frame.cols, cv_frame.rows); glVertex2f(_width, _height);
glTexCoord2f(0, cv_frame.rows); glVertex2f( 0.0, _height);
#endif
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glFlush();
}
void glPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble ymax = zNear * tan( fovy * M_PI / 360.0 );
GLdouble ymin = -ymax;
GLdouble xmin = ymin * aspect;
GLdouble xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}
void glOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glOrtho(left, right, bottom, top, -1, 1);
}
void GLWidget::resizeGL(int w, int h)
{
_width = w;
_height = h;
glViewport ( 0, 0, _width, _height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
#ifdef USE_PERSPECTIVE
if (h == 0)
glPerspective( 60, ( float ) w, 1.0, 100.0 );
else
glPerspective( 60, ( float ) w / ( float ) h, 1.0, 100.0 );
#else
glOrtho(0, _width, _height, 0, 1, -1);
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}