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
148 lines (120 loc) · 3.93 KB
/
widget.cpp
File metadata and controls
148 lines (120 loc) · 3.93 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
/* Copyright (C) 2013 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"
#ifndef __APPLE__
#include <GL/GLU.h>
#else
#include <glu.h>
#endif
#include <iostream>
#include <QKeyEvent>
#include <QTimer>
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
_width = 0;
_height = 0;
_texture = 0;
_fps = 0;
}
GLWidget::~GLWidget()
{
glDeleteTextures(1, &_texture);
}
void GLWidget::_tick()
{
// triggers paintGL()
update();
// Set timer according to FPS
QTimer::singleShot(1000/_fps, this, SLOT(_tick()));
}
void GLWidget::initializeGL()
{
// Set clear color as black
glClearColor(0.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;
/* Start the timer */
_tick();
}
void GLWidget::paintGL()
{
// Clear the screen and depth buffer (with black)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Select the model view matrix and reset it
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, 0.0f);
// 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;
}
// Note: trying to retrieve more frames than the camera can give you
// will make 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;
}
cv::cvtColor(cv_frame, cv_frame, CV_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);
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);
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
}
void GLWidget::resizeGL( int w, int h)
{
_width = w;
_height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); // Select the projection matrix
glLoadIdentity(); // Reset the projection matrix
if (h == 0) // Calculate aspect ratio of the window
gluPerspective(60, (float) w, 1.0, 50.0);
else
gluPerspective(60, (float) w / (float) h, 1.0, 50.0);
gluLookAt(0.0, 0.0, 2.0, // eye
0.0, 0.0, 0.0, // center
0.0, 1.0, 0.0); // up
glMatrixMode(GL_MODELVIEW); // Select the model view matrix
glLoadIdentity(); // Reset the model view matrix
}