-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbilateralfilterwidget.cpp
161 lines (136 loc) · 4.37 KB
/
bilateralfilterwidget.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
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "bilateralfilterwidget.h"
#include <QVector>
typedef struct {
QVector2D position;
QVector2D textcoord;
} VertexData;
BilateralFilterWidget::BilateralFilterWidget(QWidget *parent)
: QOpenGLWidget(parent),
texture(Q_NULLPTR),
indexBuf(QOpenGLBuffer::IndexBuffer)
{
}
BilateralFilterWidget::~BilateralFilterWidget(){
makeCurrent();
vao.destroy();
buffer.destroy();
indexBuf.destroy();
if (texture != Q_NULLPTR) delete texture;
doneCurrent();
}
void BilateralFilterWidget::initializeGL(){
initializeOpenGLFunctions();
//background
glClearColor(0, 0, 0, 1);
glBindFragDataLocation(program.programId(), 0, "FragColor");
//init program
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.vert")){
qCritical() << program.log();
close();
}
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.frag")){
qCritical() << program.log();
close();
}
if (!program.link()){
qCritical() << program.log();
close();
}
if (!program.bind()){
qCritical() << program.log();
close();
}
// buffer
if (!buffer.create()) close();
if (!buffer.bind()) close();
buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
QVector<VertexData> rect({
{QVector2D(-1.f, -1.f), QVector2D(0.0f, 0.f)},
{QVector2D(-1.f, 1.f), QVector2D(0.f, 1.f)},
{QVector2D(1.f, 1.f), QVector2D(1.f, 1.f)},
{QVector2D(1.f, -1.f), QVector2D(1.f, 0.f)}
});
buffer.allocate(rect.data(), rect.length() * sizeof(VertexData));
//vao
if (!vao.create()) close();
vao.bind();
//bind attributes
int vertexLocation = program.attributeLocation("a_position");
program.enableAttributeArray(vertexLocation);
program.setAttributeBuffer(vertexLocation, GL_FLOAT, offsetof(VertexData, position), 2, sizeof(VertexData));
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program.attributeLocation("a_texcoord");
program.enableAttributeArray(texcoordLocation);
program.setAttributeBuffer(texcoordLocation, GL_FLOAT, offsetof(VertexData, textcoord), 2, sizeof(VertexData));
vao.release();
buffer.release();
program.release();
//bind index
indexBuf.create();
indexBuf.bind();
QVector<ushort> indices({0, 1, 2, 3, 0, 1, 2, 3});
indexBuf.allocate(indices.data(), indices.length() * sizeof(ushort));
indexBuf.release();
emit glInited();
}
void BilateralFilterWidget::resizeGL(int w, int h){
program.bind();
glViewport(0, 0, w, h);
viewport.setHeight(h);
viewport.setWidth(w);
//setup mvp
QMatrix4x4 projection;
projection.setToIdentity();
projection.scale((float)h/qMax(w, h), (float)w/qMax(w, h), 1);
program.setUniformValue("mvp_matrix", projection);
program.release();
}
void BilateralFilterWidget::setColorParam(float sigmaL){
makeCurrent();
if (!program.bind()) close();
program.setUniformValue("sigmaL", sigmaL);
program.release();
doneCurrent();
}
void BilateralFilterWidget::setLocationParam(float sigmaS){
makeCurrent();
if (!program.bind()) close();
program.setUniformValue("sigmaS", sigmaS);
program.release();
doneCurrent();
}
void BilateralFilterWidget::paintGL(){
if (!program.bind()) close();
if (texture != Q_NULLPTR) texture->bind();
vao.bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
indexBuf.bind();
glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
indexBuf.release();
vao.release();
program.release();
}
void BilateralFilterWidget::setImage(QImage const& img){
if (texture != Q_NULLPTR){
delete texture;
texture = Q_NULLPTR;
}
makeCurrent();
texture = new QOpenGLTexture(img.mirrored());
// Set nearest filtering mode for texture minification
texture->setMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification
texture->setMagnificationFilter(QOpenGLTexture::Linear);
texture->setWrapMode(QOpenGLTexture::Repeat);
program.bind();
//setup mvp
QMatrix4x4 projection;
projection.setToIdentity();
float a = img.height() * viewport.width();
float b = img.width() * viewport.height();
float c = qMax(a, b);
projection.scale(b / c, a / c, 1);
program.setUniformValue("mvp_matrix", projection);
program.release();
doneCurrent();
}