-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlauharriscornerdetectorglwidget.cpp
285 lines (246 loc) · 13.4 KB
/
lauharriscornerdetectorglwidget.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "lauharriscornerdetectorglwidget.h"
#include "locale.h"
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUHarrisCornerDetectorGLWidget::~LAUHarrisCornerDetectorGLWidget()
{
if (wasInitialized()) {
makeCurrent();
for (int n = 0; n < 5; n++) {
if (frameBufferObjects[n]) {
delete frameBufferObjects[n];
}
}
}
qDebug() << "LAUHarrisCornerDetectorGLWidget::~LAUHarrisCornerDetectorGLWidget()";
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUHarrisCornerDetectorGLWidget::process()
{
// SEE IF WE NEED NEW FBOS
if (videoTexture) {
for (int n = 0; n < 5; n++) {
if (frameBufferObjects[n] == NULL) {
// CREATE A FORMAT OBJECT FOR CREATING THE FRAME BUFFER
QOpenGLFramebufferObjectFormat frameBufferObjectFormat;
frameBufferObjectFormat.setInternalTextureFormat(GL_RGBA32F);
frameBufferObjects[n] = new QOpenGLFramebufferObject(videoTexture->width(), videoTexture->height(), frameBufferObjectFormat);
frameBufferObjects[n]->release();
} else if (frameBufferObjects[n]->width() != videoTexture->width() || frameBufferObjects[n]->height() != videoTexture->height()) {
delete frameBufferObjects[n];
// CREATE A FORMAT OBJECT FOR CREATING THE FRAME BUFFER
QOpenGLFramebufferObjectFormat frameBufferObjectFormat;
frameBufferObjectFormat.setInternalTextureFormat(GL_RGBA32F);
frameBufferObjects[n] = new QOpenGLFramebufferObject(videoTexture->width(), videoTexture->height(), frameBufferObjectFormat);
frameBufferObjects[n]->release();
}
}
// SET CLEAR COLOR AS NOT A NUMBERS
glClearColor(NAN, NAN, NAN, NAN);
// CALCULATE THE GRADIENT BUFFER
if (frameBufferObjects[0]->bind()) {
if (programA.bind()) {
// CLEAR THE FRAME BUFFER OBJECT
glViewport(2, 2, frameBufferObjects[0]->width() - 4, frameBufferObjects[0]->height() - 4);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// BIND VBOS FOR DRAWING TRIANGLES ON SCREEN
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// BIND THE TEXTURE FROM THE ORIGINAL SCAN
glActiveTexture(GL_TEXTURE0);
videoTexture->bind();
programA.setUniformValue("qt_texture", 0);
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
glVertexAttribPointer(programA.attributeLocation("qt_vertex"), 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
programA.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// RELEASE THE FRAME BUFFER OBJECT AND ITS ASSOCIATED GLSL PROGRAMS
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
programA.release();
}
frameBufferObjects[0]->release();
}
// SMOOTH THE GRADIENT BUFFER WITH A GAUSSIAN FILTER
if (frameBufferObjects[1]->bind()) {
if (programB.bind()) {
// CLEAR THE FRAME BUFFER OBJECT
glViewport(4, 4, frameBufferObjects[1]->width() - 8, frameBufferObjects[1]->height() - 8);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// BIND VBOS FOR DRAWING TRIANGLES ON SCREEN
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// BIND THE TEXTURE FROM THE FRAME BUFFER OBJECT A
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, frameBufferObjects[0]->texture());
programB.setUniformValue("qt_texture", 1);
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
glVertexAttribPointer(programB.attributeLocation("qt_vertex"), 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
programB.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// RELEASE THE FRAME BUFFER OBJECT AND ITS ASSOCIATED GLSL PROGRAMS
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
programB.release();
}
frameBufferObjects[1]->release();
}
// FIND LOCAL MAXIMUMS
if (frameBufferObjects[2]->bind()) {
if (programC.bind()) {
// CLEAR THE FRAME BUFFER OBJECT
glViewport(5, 5, frameBufferObjects[2]->width() - 10, frameBufferObjects[2]->height() - 10);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// BIND VBOS FOR DRAWING TRIANGLES ON SCREEN
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// BIND THE TEXTURE FROM THE FRAME BUFFER OBJECT B
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, frameBufferObjects[1]->texture());
programC.setUniformValue("qt_texture", 2);
// SET THE HARRIS CORNER MATRIX SCALE FACTOR K
programC.setUniformValue("qt_scaleFactor", qtScaleFactor);
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
glVertexAttribPointer(programC.attributeLocation("qt_vertex"), 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
programC.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// RELEASE THE FRAME BUFFER OBJECT AND ITS ASSOCIATED GLSL PROGRAMS
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
programC.release();
}
frameBufferObjects[2]->release();
}
// DISPLAY CORNERS ON THE INPUT SCAN
if (frameBufferObjects[3]->bind()) {
if (programD.bind()) {
// CLEAR THE FRAME BUFFER OBJECT
glViewport(0, 0, frameBufferObjects[3]->width(), frameBufferObjects[3]->height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// BIND VBOS FOR DRAWING TRIANGLES ON SCREEN
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// BIND THE TEXTURE FROM THE FRAME BUFFER OBJECT C
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, frameBufferObjects[2]->texture());
programD.setUniformValue("qt_texture", 3);
// SET THE MINIMUM CORNER STRENGTH THRESHOLD
programD.setUniformValue("qt_threshold", qtCornerThreshold);
programD.setUniformValue("qt_radius", 10);
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
glVertexAttribPointer(programD.attributeLocation("qt_vertex"), 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
programD.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// RELEASE THE FRAME BUFFER OBJECT AND ITS ASSOCIATED GLSL PROGRAMS
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
programD.release();
}
frameBufferObjects[3]->release();
}
// DISPLAY CORNERS ON THE INPUT SCAN
if (frameBufferObjects[4]->bind()) {
if (programE.bind()) {
// CLEAR THE FRAME BUFFER OBJECT
glViewport(0, 0, frameBufferObjects[4]->width(), frameBufferObjects[4]->height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// BIND VBOS FOR DRAWING TRIANGLES ON SCREEN
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// BIND THE ORIGINAL SCAN AS WE MERGE THE TWO
glActiveTexture(GL_TEXTURE0);
videoTexture->bind();
programE.setUniformValue("qt_textureA", 0);
// BIND THE TEXTURE FROM THE FRAME BUFFER OBJECT C
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, frameBufferObjects[3]->texture());
programE.setUniformValue("qt_textureB", 4);
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
glVertexAttribPointer(programE.attributeLocation("qt_vertex"), 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
programE.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// RELEASE THE FRAME BUFFER OBJECT AND ITS ASSOCIATED GLSL PROGRAMS
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
programE.release();
}
frameBufferObjects[4]->release();
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUHarrisCornerDetectorGLWidget::initialize()
{
LAUVideoGLWidget::initialize();
// NOW ADD OUR LIST OF HARRIS CORNER SHADER PROGRAMS
setlocale(LC_NUMERIC, "C");
programA.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Shaders/filterHarrisCornersA.vert");
programA.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Shaders/filterHarrisCornersA.frag");
programA.link();
programB.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Shaders/filterHarrisCornersB.vert");
programB.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Shaders/filterHarrisCornersB.frag");
programB.link();
programC.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Shaders/filterHarrisCornersC.vert");
programC.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Shaders/filterHarrisCornersC.frag");
programC.link();
programD.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Shaders/filterHarrisCornersD.vert");
programD.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Shaders/filterHarrisCornersD.frag");
programD.link();
programE.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Shaders/filterHarrisCornersE.vert");
programE.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Shaders/filterHarrisCornersE.frag");
programE.link();
setlocale(LC_ALL, "");
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUHarrisCornerDetectorGLWidget::paint()
{
if (frameBufferObjects[4] == NULL) {
LAUVideoGLWidget::paint();
} else {
// SET THE VIEW PORT AND CLEAR THE SCREEN BUFFER
glViewport(0, 0, localWidth, localHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// DISPLAY THE LAST FBO IN OUR LIST
if (videoTexture) {
if (program.bind()) {
if (quadVertexBuffer.bind()) {
if (quadIndexBuffer.bind()) {
// SET THE ACTIVE TEXTURE ON THE GPU
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, frameBufferObjects[4]->texture());
program.setUniformValue("qt_texture", 0);
#ifdef Q_OS_WIN
program.setUniformValue("qt_flip", false);
#else
program.setUniformValue("qt_flip", true);
#endif
// TELL OPENGL PROGRAMMABLE PIPELINE HOW TO LOCATE VERTEX POSITION DATA
program.setAttributeBuffer("qt_vertex", GL_FLOAT, 0, 4, 4 * sizeof(float));
program.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
quadIndexBuffer.release();
}
quadVertexBuffer.release();
}
program.release();
}
}
}
}