/
qcvmatwidget.cpp
99 lines (84 loc) · 2.36 KB
/
qcvmatwidget.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
#include "qcvmatwidget.h"
#include "ui_qcvmatwidget.h"
QCVMatWidget::QCVMatWidget(QWidget *parent) :
QGLWidget(parent),
ui(new Ui::QCVMatWidget)
{
ui->setupUi(this);
this->parent = parent;
bgColor = QColor::fromRgb(0xe0,0x00,0x00);
}
QCVMatWidget::~QCVMatWidget()
{
glDeleteTextures(1, &texture);
}
void QCVMatWidget::initializeGL()
{
qglClearColor(bgColor);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glDisable(GL_TEXTURE_2D);
}
void QCVMatWidget::setImage(const cv::Mat& image)
{
// First make sure we're using the right GL context
makeCurrent();
// Now update textures
GLenum internalFormat = GL_RGBA;
GLenum inputType, inputColourFormat;
switch(image.channels())
{
case 1:
inputColourFormat = GL_LUMINANCE;
if (image.depth() == CV_8U)
inputType = GL_UNSIGNED_BYTE;
else
inputType = GL_UNSIGNED_SHORT;
break;
case 3:
default:
inputType = GL_UNSIGNED_BYTE;
inputColourFormat = GL_BGR;
break;
}
glBindTexture(GL_TEXTURE_2D, texture);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Set up the texture
glTexImage2D(GL_TEXTURE_2D,
0,
internalFormat,
image.cols,
image.rows,
0,
inputColourFormat,
inputType,
image.data);
glBindTexture(GL_TEXTURE_2D, 0);
// Tell the widget to redraw
updateGL();
}
void QCVMatWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
// Draw the textured quad
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2f(-1.0f, 1.0f);
glTexCoord2i(0, 1); glVertex2f(-1.0f, -1.0f);
glTexCoord2i(1, 1); glVertex2f( 1.0f, -1.0f);
glTexCoord2i(1, 0); glVertex2f( 1.0f, 1.0f);
glEnd();
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
}
void QCVMatWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
}