-
Notifications
You must be signed in to change notification settings - Fork 23
/
glutils.cpp
616 lines (486 loc) · 19.3 KB
/
glutils.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "glutils.h"
#include <QThread>
#include <QOpenGLTexture>
#include <QOpenGLShaderProgram>
#include "ccamera.h"
#include <assert.h>
#include <math.h>
#include "aogproperty.h"
//module-level symbols
QOpenGLShaderProgram *simpleColorShader = 0;
QOpenGLShaderProgram *texShader = 0;
QOpenGLShaderProgram *interpColorShader = 0;
QVector<QOpenGLTexture *> texture;
static int GlyphsPerLine = 16;
//static int GlyphLineCount = 16;
static int GlyphWidth = 16;
static int GlyphHeight = 32;
static int CharXSpacing = 14;
int textureWidth;
int textureHeight;
#ifdef LOCAL_QML
#define PREFIX "local:"
#else
#define PREFIX ":"
#endif
bool isFontOn = true;
bool texturesLoaded = false;
void initializeShaders() {
//GL context must be bound by caller, and this must be called from
//a QThread context
//All shader memory will be managed by the current QThread
if (!simpleColorShader) {
simpleColorShader = new QOpenGLShaderProgram(QThread::currentThread()); //memory managed by Qt
assert(simpleColorShader->addShaderFromSourceFile(QOpenGLShader::Vertex, PREFIX "/shaders/color_vshader.vsh"));
assert(simpleColorShader->addShaderFromSourceFile(QOpenGLShader::Fragment, PREFIX "/shaders/color_fshader.fsh"));
assert(simpleColorShader->link());
}
if (!texShader) {
texShader = new QOpenGLShaderProgram(QThread::currentThread()); //memory managed by Qt
assert(texShader->addShaderFromSourceFile(QOpenGLShader::Vertex, PREFIX "/shaders/colortex_vshader.vsh"));
assert(texShader->addShaderFromSourceFile(QOpenGLShader::Fragment, PREFIX "/shaders/colortex_fshader.fsh"));
assert(texShader->link());
}
if (!interpColorShader) {
interpColorShader = new QOpenGLShaderProgram(QThread::currentThread()); //memory managed by Qt
assert(interpColorShader->addShaderFromSourceFile(QOpenGLShader::Vertex, PREFIX "/shaders/colors_vshader.vsh"));
assert(interpColorShader->addShaderFromSourceFile(QOpenGLShader::Fragment, PREFIX "/shaders/colors_fshader.fsh"));
assert(interpColorShader->link());
}
}
void initializeTextures() {
QOpenGLTexture *t;
// Background
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Landscape.png"));
texture.append(t); //position 0 SKY?
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/floor.png"));
t->setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear);
texture.append(t); //position 1 FLOOR
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Font.png"));
textureWidth = t->width();
textureHeight= t->height();
texture.append(t); //position 2 FONT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Turn.png"));
texture.append(t); //position 3 TURN
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/TurnCancel.png"));
texture.append(t); //position 4 TURNCANCEL
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/TurnManual.png"));
texture.append(t); //position 5 TURNMANUAL
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Compass.png"));
texture.append(t); //position 6 COMPASS
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/speedo.png"));
texture.append(t); //position 7 SPEEDO
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/SpeedoNedle.png"));
texture.append(t); //position 8 SPEEDONEEDLE
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Lift.png"));
texture.append(t); //position 9 HYDIFT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/LandscapeNight.png"));
texture.append(t); //position 10 SKYNIGHT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_SteerPointer.png"));
texture.append(t); //position 11 STEER_POINTER
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_SteerDot.png"));
texture.append(t); //position 12 STEER_DOT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_Tractor.png"));
texture.append(t); //position 13 TRACTOR
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_QuestionMark.png"));
texture.append(t); //position 14 QUESTION_MARK
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/FrontWheels.png"));
texture.append(t); //position 15 FRONT_WHEELS
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Tractor4WDFront.png"));
texture.append(t); //position 16 TRACTOR_4WD_FRONT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Tractor4WDRear.png"));
texture.append(t); //position 17 TRACTOR_4WD_REAR
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/Harvester.png"));
texture.append(t); //position 18 HARVESTER
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_LateralManual.png"));
texture.append(t); //position 19 LATERAL_MANUAL
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/bingMap.png"));
texture.append(t); //position 20 BING_MAP
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_NoGPS.png"));
texture.append(t); //position 21 NOGPS
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/bingMap.png")); //unused I think
texture.append(t); //position 22 ZOOM_IN
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/bingMap.png"));
texture.append(t); //position 23 ZOOM_OUT
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/bingMap.png"));
texture.append(t); //position 24 PAN
t = new QOpenGLTexture(QImage(PREFIX "/images/textures/z_Tool.png"));
texture.append(t); //position 25 TOOLWHEELS
}
void destroyShaders() {
//OpenGL context must be bound by caller.
if(simpleColorShader) {
delete simpleColorShader;
simpleColorShader = 0;
}
if(texShader) {
delete texShader;
texShader = 0;
}
if(interpColorShader) {
delete interpColorShader;
interpColorShader = 0;
}
}
void destroyTextures() {
for(const QOpenGLTexture *t: texture) {
delete t;
}
texture.clear();
}
void glDrawArraysColor(QOpenGLFunctions *gl,
QMatrix4x4 mvp,
GLenum operation,
QColor color,
QOpenGLBuffer &vertexBuffer,
GLenum GL_type,
int count,
float pointSize)
{
//bind shader
assert(simpleColorShader->bind());
//set color
simpleColorShader->setUniformValue("color", color);
//set mvp matrix
simpleColorShader->setUniformValue("mvpMatrix", mvp);
simpleColorShader->setUniformValue("pointSize", pointSize);
vertexBuffer.bind();
//TODO: these require a VBA to be bound; we need to create them I suppose.
//enable the vertex attribute array in shader
simpleColorShader->enableAttributeArray("vertex");
//use attribute array from buffer, using non-normalized vertices
gl->glVertexAttribPointer(simpleColorShader->attributeLocation("vertex"),
3, //3D vertices
GL_type, //type of data GL_FLAOT or GL_DOUBLE
GL_FALSE, //not normalized vertices!
0, //no spaceing between vertices in data
0 //start at offset 0 in buffer
);
//draw primitive
gl->glDrawArrays(operation,0,count);
//release buffer
vertexBuffer.release();
//release shader
simpleColorShader->release();
}
//Buffer should be a list of 7D tuples. 3 values for x,y,z,
//and 4 values for color: r,g,b,a.
void glDrawArraysColors(QOpenGLFunctions *gl,
QMatrix4x4 mvp,
GLenum operation,
QOpenGLBuffer &vertexBuffer,
GLenum GL_type,
int count,
float pointSize)
{
//bind shader
assert(interpColorShader->bind());
//set mvp matrix
interpColorShader->setUniformValue("mvpMatrix", mvp);
interpColorShader->setUniformValue("pointSize", pointSize);
vertexBuffer.bind();
//enable the vertex attribute array in shader
interpColorShader->enableAttributeArray("vertex");
interpColorShader->enableAttributeArray("color");
//use attribute array from buffer, using non-normalized vertices
gl->glVertexAttribPointer(interpColorShader->attributeLocation("vertex"),
3, //3D vertices
GL_type, //type of data GL_FLAOT or GL_DOUBLE
GL_FALSE, //not normalized vertices!
7*sizeof(float), //vertex+color
0 //start at offset 0 in buffer
);
gl->glVertexAttribPointer(interpColorShader->attributeLocation("color"),
4, //4D color
GL_type, //type of data GL_FLAOT or GL_DOUBLE
GL_FALSE, //not normalized vertices!
7*sizeof(float), //vertex+color
(void *)(sizeof(float) * 3) //start at 3rd float in buffer
);
//draw primitive
gl->glDrawArrays(operation,0,count);
//release buffer
vertexBuffer.release();
//release shader
interpColorShader->release();
}
//Buffer should consist of 5D values. 3D for x,y,z, and 2D for
//texture x,y coordinate.
void glDrawArraysTexture(QOpenGLFunctions *gl,
QMatrix4x4 mvp,
GLenum operation,
QOpenGLBuffer &vertexBuffer,
GLenum GL_type,
int count,
bool useColor = false,
QColor color = QColor::fromRgbF(1,1,1))
{
gl->glEnable(GL_TEXTURE_2D);
//bind shader
assert(texShader->bind());
//set mvp matrix
texShader->setUniformValue("color", color);
texShader->setUniformValue("texture", 0);
texShader->setUniformValue("mvpMatrix", mvp);
texShader->setUniformValue("useColor", useColor);
vertexBuffer.bind();
//enable the vertex attribute array in shader
texShader->enableAttributeArray("vertex");
texShader->enableAttributeArray("texcoord_src");
//use attribute array from buffer, using non-normalized vertices
gl->glVertexAttribPointer(texShader->attributeLocation("vertex"),
3, //3D vertices
GL_type, //type of data GL_FLAOT or GL_DOUBLE
GL_FALSE, //not normalized vertices!
5*sizeof(float), //vertex+color
0 //start at offset 0 in buffer
);
gl->glVertexAttribPointer(texShader->attributeLocation("texcoord_src"),
2, //2D coordinate
GL_type, //type of data GL_FLAOT or GL_DOUBLE
GL_FALSE, //not normalized vertices!
5*sizeof(float), //vertex+color
(void *)(sizeof(float) * 3) //start at 3rd float in buffer
);
//draw primitive
gl->glDrawArrays(operation,0,count);
//release buffer
vertexBuffer.release();
//release shader
texShader->release();
gl->glDisable(GL_TEXTURE_2D);
}
void DrawPolygon(QOpenGLFunctions *gl, QMatrix4x4 mvp, QVector<Vec2> &polygon, float size, QColor color)
{
GLHelperOneColor gldraw;
if (polygon.count() > 2)
{
for (int i = 0; i < polygon.count() ; i++)
{
gldraw.append(QVector3D(polygon[i].easting, polygon[i].northing, 0));
}
gldraw.draw(gl, mvp, color, GL_LINE_LOOP, size);
}
}
void DrawPolygon(QOpenGLFunctions *gl, QMatrix4x4 mvp, QVector<Vec3> &polygon, float size, QColor color)
{
GLHelperOneColor gldraw;
if (polygon.count() > 2)
{
for (int i = 0; i < polygon.count() ; i++)
{
gldraw.append(QVector3D(polygon[i].easting, polygon[i].northing, 0));
}
gldraw.draw(gl, mvp, color, GL_LINE_LOOP, size);
}
}
void drawText(QOpenGLFunctions *gl, QMatrix4x4 mvp, double x, double y, QString text, double size, bool colorize, QColor color)
{
//GL.Color3(0.95f, 0.95f, 0.40f);
GLHelperTexture gldraw;
VertexTexcoord vt;
double u_step = (double)GlyphWidth / (double)textureWidth;
double v_step = (double)GlyphHeight / (double)textureHeight;
for (int n = 0; n < text.length(); n++)
{
gldraw.clear();
char idx = text.at(n).toLatin1();
double u = (double)(idx % GlyphsPerLine) * u_step;
double v = (double)(idx / GlyphsPerLine) * v_step;
//1
vt.texcoord = QVector2D(u, v + v_step);
vt.vertex = QVector3D(x, y + GlyphHeight * size, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y + GlyphHeight * size, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y + GlyphHeight * size, 0);
gldraw.append(vt);
//3
vt.texcoord = QVector2D(u + u_step, v);
vt.vertex = QVector3D(x + GlyphWidth * size, y, 0);
gldraw.append(vt);
x += CharXSpacing * size;
}
gldraw.draw(gl, mvp, Textures::FONT, GL_TRIANGLE_STRIP, colorize, color);
}
void drawText3D(const CCamera &camera, QOpenGLFunctions *gl,
QMatrix4x4 mvp, double x1, double y1, QString text,
double size, bool colorize, QColor color)
{
GLHelperTexture gldraw;
VertexTexcoord vt;
double x = 0, y = 0;
mvp.translate(x1, y1, 0);
if ((double)property_setDisplay_camPitch < -45)
{
mvp.rotate(90, 1, 0, 0);
if (camera.camFollowing) mvp.rotate(-camera.camHeading, 0, 1, 0);
size = -camera.camSetDistance;
size = pow(size, 0.8);
size /= 800;
}
else
{
if (camera.camFollowing) mvp.rotate(-camera.camHeading, 0, 0, 1);
size = -camera.camSetDistance;
size = pow(size, 0.85);
size /= 1000;
}
double u_step = (double)GlyphWidth / (double)textureWidth;
double v_step = (double)GlyphHeight / (double)textureHeight;
for (int n = 0; n < text.length(); n++)
{
char idx = text.at(n).toLatin1();
double u = (double)(idx % GlyphsPerLine) * u_step;
double v = (double)(idx / GlyphsPerLine) * v_step;
gldraw.clear();
//1
vt.texcoord = QVector2D(u, v + v_step);
vt.vertex = QVector3D(x, y, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y + GlyphHeight * size, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y + GlyphHeight * size, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y, 0);
gldraw.append(vt);
//3
vt.texcoord = QVector2D(u + u_step, v);
vt.vertex = QVector3D(x + GlyphWidth * size, y + GlyphHeight * size, 0);
gldraw.append(vt);
x += CharXSpacing * size;
}
gldraw.draw(gl, mvp, Textures::FONT, GL_TRIANGLES, colorize, color);
}
void drawTextVehicle(const CCamera &camera, QOpenGLFunctions *gl, QMatrix4x4 mvp,
double x, double y, QString text, double size, bool colorize, QColor color)
{
GLHelperTexture gldraw;
VertexTexcoord vt;
size *= -camera.camSetDistance;
size = pow(size, 0.8)/800;
//2d
if ((double)property_setDisplay_camPitch > -58)
{
if (!camera.camFollowing)
{
mvp.rotate(camera.camHeading, 0, 0, 1);
y *= 1.2;
}
else
{
y *= 1.2;
mvp.translate(x, y, 0);
x = y = 0;
}
}
//3d
else
{
if (!camera.camFollowing)
{
mvp.rotate(90, 1, 0, 0);
mvp.rotate(camera.camHeading, 0, 1, 0);
y *= 0.3;
}
else
{
mvp.rotate(-(double)property_setDisplay_camPitch, 1, 0, 0);
y *= 0.3;
}
}
double u_step = (double)GlyphWidth / (double)textureWidth;
double v_step = (double)GlyphHeight / (double)textureHeight;
for (int n = 0; n < text.length(); n++)
{
char idx = text.at(n).toLatin1();
double u = (double)(idx % GlyphsPerLine) * u_step;
double v = (double)(idx / GlyphsPerLine) * v_step;
//1
vt.texcoord = QVector2D(u, v + v_step);
vt.vertex = QVector3D(x, y, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y + GlyphHeight * size, 0);
gldraw.append(vt);
//4
vt.texcoord = QVector2D(u, v);
vt.vertex = QVector3D(x, y + GlyphHeight * size, 0);
gldraw.append(vt);
//2
vt.texcoord = QVector2D(u + u_step, v + v_step);
vt.vertex = QVector3D(x + GlyphWidth * size, y, 0);
gldraw.append(vt);
//3
vt.texcoord = QVector2D(u + u_step, v);
vt.vertex = QVector3D(x + GlyphWidth * size, y + GlyphHeight * size, 0);
gldraw.append(vt);
x += CharXSpacing * size;
}
gldraw.draw(gl, mvp, Textures::FONT, GL_TRIANGLES, colorize, color);
}
GLHelperOneColor::GLHelperOneColor() {
}
void GLHelperOneColor::draw(QOpenGLFunctions *gl, QMatrix4x4 mvp, QColor color, GLenum operation, float point_size) {
QOpenGLBuffer vertexBuffer;
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(data(), size()*sizeof(QVector3D));
vertexBuffer.release();
glDrawArraysColor(gl, mvp,operation,
color, vertexBuffer, GL_FLOAT,
size(),point_size);
}
GLHelperColors::GLHelperColors() {
}
void GLHelperColors::draw(QOpenGLFunctions *gl, QMatrix4x4 mvp, GLenum operation, float point_size) {
QOpenGLBuffer vertexBuffer;
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(data(), size()*sizeof(ColorVertex));
vertexBuffer.release();
glDrawArraysColors(gl, mvp,operation,
vertexBuffer, GL_FLOAT,
size(),point_size);
}
GLHelperTexture::GLHelperTexture() {
}
void GLHelperTexture::draw(QOpenGLFunctions *gl, QMatrix4x4 mvp, Textures textureno, GLenum operation, bool colorize, QColor color)
{
QOpenGLBuffer vertexBuffer;
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(data(),size() * sizeof(VertexTexcoord));
vertexBuffer.release();
texture[textureno]->bind();
glDrawArraysTexture(gl, mvp, operation, vertexBuffer, GL_FLOAT,size(),
colorize, color);
texture[textureno]->release();
}