-
Notifications
You must be signed in to change notification settings - Fork 0
/
glwidget.cpp
150 lines (131 loc) · 3.41 KB
/
glwidget.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "glwidget.h"
#include <QDebug>
GLWidget::GLWidget(QList<QSharedPointer<DistancePoint>> *_distancePointList, ThreadSafeQueue<QImage> *q_, QWidget *parent):
QGLWidget(QGLFormat(QGL::DoubleBuffer),parent),
distancePointList(_distancePointList),
imageDistanceQueue(q_),
texture(NULL)
{
_image = QImage(QSize(320,240), QImage::Format_RGB888);
_image.fill(Qt::white);
viewHeight = height();
viewWidth = width();
setAutoFillBackground(false);
}
QSize GLWidget::minimumSizeHint() const
{
return QSize(320, 240);
}
QSize GLWidget::sizeHint() const
{
return QSize(640, 480);
}
void GLWidget::mousePressEvent(QMouseEvent *e)
{
emit measuringPointCoordsChanged(e->pos(), QSize(viewWidth, viewHeight));
}
/**
When new frame is sent to the widget
* @brief GLWidget::onNewFrame
*/
void GLWidget::onNewFrame()
{
// skipping empty queue..that should never happen
if(imageDistanceQueue->empty()) return;
QImage cameraFrame = imageDistanceQueue->dequeue();
if(texture == NULL){
texture = new QOpenGLTexture(cameraFrame);
texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
}
else{
texture->destroy();
texture->create();
texture->setMipLevels(0);
texture->setData(cameraFrame);
}
texture->bind();
update();
}
/**
* Initializing openGL options
* @brief GLWidget::initializeGL
*/
void GLWidget::initializeGL()
{
initializeOpenGLFunctions();
qglClearColor(Qt::black);
setupViewPort(width(), height());
}
/**
* HW accelerated rendering (GL + QT drawing)
* @brief GLWidget::paintEvent
* @param event
*/
void GLWidget::paintEvent(QPaintEvent *event)
{
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
// draw a textured quad
drawFramePicture();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
QPainter painter(this);
// distance list (not when depth map showing)
if(distancePointList != NULL){
// render all checked points
for(int i = 0; i < distancePointList->length(); i++){
QSharedPointer<DistancePoint> point = distancePointList->at(i);
point->render(&painter);
}
}
painter.end();
}
/**
* Draws rectangle with picture texture on it
* @brief GLWidget::makeFrameObject
*/
void GLWidget::drawFramePicture(){
glBegin(GL_QUADS);
// -- start specifying vertices
glTexCoord2d(0, 0);
glVertex2i(-1, -1);
glTexCoord2d(0, 1);
glVertex2i(-1, 1);
glTexCoord2d(1, 1);
glVertex2i(1, 1);
glTexCoord2d(1, 0);
glVertex2i(1, -1);
// -- end specifying vertices
glEnd();
}
/**
* When GL recognizes resize
* @brief GLWidget::resizeGL
* @param width
* @param height
*/
void GLWidget::resizeGL(int width, int height)
{
viewHeight = height;
viewWidth = width;
setupViewPort(width, height);
}
/**
* Setups openGL viewport
* @brief GLWidget::setupViewPort
* @param width
* @param height
*/
void GLWidget::setupViewPort(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
if(distancePointList != NULL){
for(int i = 0; i < distancePointList->length(); i++){
QSharedPointer<DistancePoint> point = distancePointList->at(i);
point->updateWidgetPos(width, height);
}
}
}