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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <time.h>
#include <opencv2/opencv.hpp>
#include "lib.h"
#include "ringbuf.h"
#define PI 3.141593654
using namespace cv;
#define SOCKETPATH "/home/flo/uds_socket"
#define PX_PER_DEG 16.0
#define PX_PER_DEG_WINDOWSIZE 3
#define PX_PER_DEG_CANVAS 16
#define CANVAS_XDEG 450
#define CANVAS_YDEG 120
#define CANVAS_WIDTH CANVAS_XDEG*PX_PER_DEG_CANVAS
#define CANVAS_HEIGHT CANVAS_YDEG*PX_PER_DEG_CANVAS
#define SCREEN_WIDTH CANVAS_XDEG*PX_PER_DEG_WINDOWSIZE
#define SCREEN_HEIGHT CANVAS_YDEG*PX_PER_DEG_WINDOWSIZE
const char* justDrawASpriteVertexSource =
"#version 150\n"
"in vec2 position;\n"
"in vec2 texcoord;\n"
"out vec2 Texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
" Texcoord = texcoord;\n"
"}\n";
const char* justDrawASpriteFragmentSource =
"#version 150\n"
"uniform sampler2D texVideo;\n"
"in vec2 Texcoord;\n"
"out vec4 outColor;\n"
"void main()\n"
"{\n"
" outColor = texture(texVideo, Texcoord);\n"
"}\n";
const char* justDrawASpriteFragmentSourceGray =
"#version 150\n"
"uniform sampler2D texVideo;\n"
"in vec2 Texcoord;\n"
"out vec4 outColor;\n"
"void main()\n"
"{\n"
" float gray = (texture(texVideo, Texcoord).r+ texture(texVideo, Texcoord).g + texture(texVideo, Texcoord).b)/3.;\n"
" if (Texcoord.x < 0.5) gray=1.0-gray;\n"
" outColor = vec4(gray,gray,gray,1.0);\n"
"}\n";
float vertices[] = {
-1.f, 1.f, 0.0f,0.0f, // Vertex 1 (X, Y)
1.f, 1.f, 1.0f,0.0f, // Vertex 2 (X, Y)
1.f, -1.f, 1.0f,1.0f, // Vertex 3 (X, Y)
1.f, -1.f, 1.0f,1.0f, // Vertex 3 (X, Y)
-1.f, -1.f, 0.0f,1.0f, // Vertex 4 (X, Y)
-1.f, 1.f, 0.0f,0.0f // Vertex 1 (X, Y)
};
float quadVertices[] = {
-1.f, -1.f, 0.4f,0.4f, // Vertex 1 (X, Y)
1.f, -1.f, 0.6f,0.4f, // Vertex 2 (X, Y)
1.f, 1.f, 0.6f,0.6f, // Vertex 3 (X, Y)
1.f, 1.f, 0.6f,0.6f, // Vertex 3 (X, Y)
-1.f, 1.f, 0.4f,0.6f, // Vertex 4 (X, Y)
-1.f, -1.f, 0.4f,0.4f // Vertex 1 (X, Y)
};
void calcVerticesRotated(int xshift, int yshift, float angle, float* v)
{
Point2f pt;
pt = Point2f( -cos(angle)*1280./2 + sin(angle)*720./2, +sin(angle)*1280./2 + cos(angle)*720./2 );
v[0]=v[20]=(float) ( pt.x + xshift) / PX_PER_DEG / CANVAS_XDEG * 2;
v[1]=v[21]=(float) ( pt.y + yshift) / PX_PER_DEG / CANVAS_YDEG * 2;
v[8]=v[12]=(float) (-pt.x + xshift) / PX_PER_DEG / CANVAS_XDEG * 2;
v[9]=v[13]=(float) (-pt.y + yshift) / PX_PER_DEG / CANVAS_YDEG * 2;
pt = Point2f( cos(angle)*1280./2 + sin(angle)*720./2, -sin(angle)*1280./2 + cos(angle)*720./2 );
v[4] =(float) ( pt.x + xshift) / PX_PER_DEG / CANVAS_XDEG * 2;
v[5] =(float) ( pt.y + yshift)/ PX_PER_DEG / CANVAS_YDEG * 2;
v[16]=(float) (-pt.x + xshift)/ PX_PER_DEG / CANVAS_XDEG * 2;
v[17]=(float) (-pt.y + yshift) / PX_PER_DEG / CANVAS_YDEG * 2;
}
void compileShaderProgram(const GLchar* vertSrc, const GLchar* fragSrc, GLuint& vertexShader, GLuint& fragmentShader, GLuint& shaderProgram)
{
GLint status;
char buffer[512];
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertSrc, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
printf("vertex shader log:\n%s\n\n\n", buffer);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragSrc, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
printf("fragment shader log:\n%s\n\n\n", buffer);
// assemble shader program
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor"); // not neccessary
glLinkProgram(shaderProgram);
}
GLuint justDrawASpriteShaderProgram(GLuint vao, GLuint vbo, bool gray=false)
{
GLuint vertexShader, fragmentShader, shaderProgram;
compileShaderProgram(justDrawASpriteVertexSource, gray? justDrawASpriteFragmentSourceGray : justDrawASpriteFragmentSource, vertexShader, fragmentShader, shaderProgram);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// set up shaders
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(2*sizeof(float)));
return shaderProgram;
}
GLFWwindow* initOpenGL()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "OpenGL", NULL, NULL); // Windowed
// GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", glfwGetPrimaryMonitor(), nullptr); // Fullscreen
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
return window;
}
const int width = 1280, height = 720;
float fixup_range(float a, float low, float upp)
{
float tot=upp-low;
while (a < low) a+=tot;
while (a>= upp) a-=tot;
return a;
}
float fixup_angle(float a)
{
return fixup_range(a,-180,180);
}
void calc_undistort_maps(float px_per_deg, int width, int height, Mat& map1, Mat& map2)
{
Mat camera_matrix= Mat::zeros(3,3,CV_32FC1);
camera_matrix.at<float>(0,0)=1.0; //fx
camera_matrix.at<float>(1,1)=1.0; //fy
camera_matrix.at<float>(2,2)=1.0; // 1
camera_matrix.at<float>(0,2)=1280/2.; //cx
camera_matrix.at<float>(1,2)=720/2.; //cy
Mat camera_matrix2 = camera_matrix.clone();
camera_matrix2.at<float>(0,2)=width/2.; //cx
camera_matrix2.at<float>(1,2)=height/2.; //cy
float px_per_rad = px_per_deg * PI / 180.;
Matx<float,1,5> dist_coeffs(-px_per_rad*px_per_rad/3.f, px_per_rad*px_per_rad*px_per_rad*px_per_rad/5.f, 0.f, 0.f, -px_per_rad*px_per_rad*px_per_rad*px_per_rad*px_per_rad*px_per_rad/7.f);
initUndistortRectifyMap(camera_matrix, dist_coeffs, Mat(), camera_matrix2, Size(width,height), CV_32FC1, map1, map2);
}
int main(int argc, const char** argv)
{
GLFWwindow* window = initOpenGL();
GLuint vaoCanvas, vaoQuad;
glGenVertexArrays(1, &vaoCanvas);
glGenVertexArrays(1, &vaoQuad);
GLuint vboCanvas, vboQuad;
glGenBuffers(1, &vboCanvas);
glGenBuffers(1, &vboQuad);
calcVerticesRotated(0,0,PI/2,vertices);
glBindBuffer(GL_ARRAY_BUFFER, vboCanvas);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vboQuad);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
// compile shaders
GLuint shaderProgram = justDrawASpriteShaderProgram(vaoCanvas, vboCanvas);
GLuint quadShaderProgram = justDrawASpriteShaderProgram(vaoQuad, vboQuad);
// texture
GLuint texVideo;
glGenTextures(1, &texVideo);
glBindTexture(GL_TEXTURE_2D, texVideo);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Framebuffer stuff
GLuint frameBuffer;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
GLuint texColorBuffer;
glGenTextures(1, &texColorBuffer);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, CANVAS_WIDTH, CANVAS_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
DroneConnection drone(SOCKETPATH);
navdata_t navdata;
Mat white(Size(1280,720), CV_8UC3, Scalar(255,255,255));
Mat map1(Size(width,height), CV_32FC1), map2(Size(width,height), CV_32FC1);
calc_undistort_maps(80/1280., 1280,720, map1, map2);
float scale_factor = 0.2;
float diag = sqrt(1280*1280+720*720);
float px_per_deg = diag / 92.;
int virtual_canvas_width = 360. * px_per_deg;
int virtual_canvas_height = 90. * px_per_deg;
int real_canvas_extra_width = sqrt(1280*1280+720*720)*scale_factor/2 + 2;
int real_canvas_width = virtual_canvas_width * scale_factor + 2*real_canvas_extra_width;
int real_canvas_height = virtual_canvas_height * scale_factor;
int total_x = 100, total_y = 00;
float total_rot = 0.0;
Mat frame(Size(1280,720), CV_8UC3), frame_(Size(1280,720), CV_8UC3), gray, oldgray;
Mat screencontent(real_canvas_height,real_canvas_width, CV_8UC3);
Mat screencontent_(real_canvas_height,real_canvas_width, CV_8UC3);
Mat screencontent_mask(real_canvas_height,real_canvas_width, CV_8UC3);
#define RINGBUF_SIZE 10
ModuloRingbuffer ringbuf_x(RINGBUF_SIZE, -virtual_canvas_width/2, virtual_canvas_width/2);
Ringbuffer ringbuf_y(RINGBUF_SIZE);
ModuloRingbuffer ringbuf_a(RINGBUF_SIZE, -180,180);
ModuloRingbuffer ringbuf_phi(RINGBUF_SIZE, -180,180);
ModuloRingbuffer ringbuf_psi(RINGBUF_SIZE, -180,180);
ModuloRingbuffer ringbuf_theta(RINGBUF_SIZE, -180,180);
for (int i=0; i<400;i++)
{
drone.get(frame_, &navdata);
remap(frame_, frame, map1, map2, INTER_LINEAR);
cvtColor(frame, oldgray, COLOR_BGR2GRAY);
}
while (waitKey(1) != 'x')
{
drone.get(frame_, &navdata);
//for (int i=0; i<1280; i+=50) frame_.col(i)=Scalar(0,255,255);
//for (int i=0; i<720; i+=50) frame_.row(i)=Scalar(0,255,255);
remap(frame_, frame, map1, map2, INTER_LINEAR);
cvtColor(frame, gray, COLOR_BGR2GRAY);
Mat mat = estimateRigidTransform(gray, oldgray, false);
float angle; int shift_x, shift_y;
if (mat.total() > 0)
{
angle = atan2(mat.at<double>(0,1), mat.at<double>(0,0)) / PI * 180.;
shift_x = mat.at<double>(0,2) - width/2 + (mat.at<double>(0,0)*width/2 + mat.at<double>(0,1)*height/2);
shift_y = mat.at<double>(1,2) - height/2 + (mat.at<double>(1,0)*width/2 + mat.at<double>(1,1)*height/2);
}
else
{
angle = 0;
shift_x = 0;
shift_y = 0;
printf("no mat!\n");
}
total_x += shift_x;
total_y += shift_y;
total_rot = fixup_angle(total_rot+angle);
ringbuf_x.put(total_x);
ringbuf_y.put(total_y);
ringbuf_a.put(total_rot);
ringbuf_phi.put(navdata.phi);
ringbuf_psi.put(navdata.psi);
ringbuf_theta.put(navdata.theta);
double xdiff = fixup_range( ringbuf_x.get() - px_per_deg*ringbuf_psi.get(), -virtual_canvas_width/2, virtual_canvas_width/2);
double ydiff = ringbuf_y.get() + px_per_deg*ringbuf_theta.get();
double adiff = fixup_angle(ringbuf_a.get() - (-ringbuf_phi.get()));
//if (fabs(xdiff) < px_per_deg) xdiff = 0.0;
//if (fabs(ydiff) < px_per_deg) ydiff = 0.0;
//if (fabs(adiff) < 2) adiff = 0.0;
xdiff*=0.0;
ydiff*=0.0;
adiff*=0.1;
total_x = fixup_range(total_x - xdiff, -virtual_canvas_width/2, virtual_canvas_width/2);
total_y = total_y - ydiff;
total_rot = fixup_angle(total_rot - adiff);
ringbuf_x.add(-xdiff);
ringbuf_y.add(-ydiff);
ringbuf_a.add(-adiff);
glBindTexture(GL_TEXTURE_2D, texVideo);
Mat frame_gl = frame.clone();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame_gl.size().width, frame_gl.size().height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame_gl.ptr<unsigned char>(0));
calcVerticesRotated(total_x, -total_y,-total_rot*PI/180.,vertices);
glBindBuffer(GL_ARRAY_BUFFER, vboCanvas);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glViewport(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);
glBindVertexArray(vaoCanvas);
glUseProgram(shaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texVideo);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
glBindVertexArray(vaoQuad);
glUseProgram(quadShaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
imshow("dingens",frame_);
printf("sh: %i\t%i\t%f\n", shift_x, shift_y, angle);
printf("tot: %i\t%i\t%f\n", total_x, total_y, total_rot);
Mat rotmat = getRotationMatrix2D(Point2f(width/2,height/2), total_rot, scale_factor);
printf("dingskram %i\n", rotmat.type());
rotmat.at<double>(0,2) += total_x*scale_factor - width/2 + real_canvas_width/2;
rotmat.at<double>(1,2) += total_y*scale_factor - height/2 + real_canvas_height/2;
warpAffine(frame, screencontent_ , rotmat, Size(real_canvas_width, real_canvas_height));
warpAffine(white, screencontent_mask, rotmat, Size(real_canvas_width, real_canvas_height));
threshold(screencontent_mask, screencontent_mask, 254, 255, THRESH_BINARY);
erode(screencontent_mask, screencontent_mask, Mat::ones(2,2, CV_8U));
Mat screencontent_mask2;
erode(screencontent_mask, screencontent_mask2, Mat::ones(30,200, CV_8U));
screencontent = (screencontent & (~screencontent_mask2)) + (screencontent_ & screencontent_mask2);
Mat screencontent_displayed = (screencontent & (~screencontent_mask)) + (screencontent_ & screencontent_mask);
printf("%i/%i\n", screencontent.size().width, screencontent.size().height);
if (total_x > 0)
screencontent.colRange(0, (2*real_canvas_extra_width)) = screencontent.colRange( real_canvas_width - 2*real_canvas_extra_width, real_canvas_width);
else
screencontent.colRange( real_canvas_width - 2*real_canvas_extra_width, real_canvas_width) = screencontent.colRange(0, (2*real_canvas_extra_width));
imshow("screencontent", screencontent_displayed);
oldgray = gray.clone();
}
//glDeleteFramebuffers(1, &frameBuffer);
glfwTerminate();
return 0;
}
|