-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopengl-draw.cpp
427 lines (376 loc) · 18.5 KB
/
opengl-draw.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
/*#-------------------------------------------------
#
# Draw functions for OpenGL
#
# by AbsurdePhoton - www.absurdephoton.fr
#
# v1 - 2019/11/08
#
# - circles
# - cones
# - spheres
# - CMF in color spaces
#
#-------------------------------------------------*/
#include <QtOpenGL>
#include "opencv2/opencv.hpp"
#include "dominant-colors.h"
#include "color-spaces.h"
#include "mat-image-tools.h"
#include "opengl-draw.h"
using namespace std;
using namespace cv;
///////////////////////////////////////////////
//// Draw CMF in color spaces
///////////////////////////////////////////////
void DrawCMFinXYZ(const float &size3d, const bool spectrum_locus) // draw Color Matching Functions in XYZ color space
{
double R, G, B, X, Y, Z, W;
glLineWidth(4); // width of line
glBegin(GL_LINE_STRIP); // continuous lines
for (int w = 0; w < wavelength_XYZ_nb; w++) {
W = wavelength_XYZ[w][0];
if ((W >= 390) and (W <= 700)) { // some wavelengths are not visible - visible range 400-700nm
WavelengthToXYZ(W, X, Y, Z);
XYZtoRGB(X, Y, Z, R, G, B); // second option : convert XYZ to RGB
double sum;
if (spectrum_locus)
sum = X + Y + Z;
else
sum = 1;
glColor3d(R, G, B); // change color to computed RGB
if (spectrum_locus)
glVertex3f(Y / sum * size3d, -X / sum * size3d, Z / sum * size3d); // add vertex
else
glVertex3f(X / sum * size3d, -Y / sum * size3d, Z / sum * size3d); // add vertex
}
}
glEnd();
}
void DrawCMFinLuv(const float &size3d) // draw Color Matching Functions in L*u*v* color space
{
double W, X, Y, Z, R, G, B, L, u, v;
glLineWidth(4);
glBegin(GL_LINE_STRIP);
for (int w = 0; w < wavelength_XYZ_nb; w++) {
W = wavelength_XYZ[w][0];
if ((W >= 390) and (W <= 700)) {
WavelengthToXYZ(W, X, Y, Z);
XYZtoRGB(X, Y, Z, R, G, B); // convert XYZ to RGB
XYZtoLuv(X, Y, Z, L, u, v); // convert XYZ to Luv
glColor3d(R, G, B); // change color to computed RGB
glVertex3f(v * size3d, -u * size3d, L * size3d); // add vertex
}
}
glEnd();
}
void DrawCMFinLab(const float &size3d) // draw Color Matching Functions in L*a*b* color space
{
double W, X, Y, Z, R, G, B, L, a, b;
glLineWidth(4);
glBegin(GL_LINE_STRIP);
for (int w = 0; w < wavelength_XYZ_nb; w++) {
W = wavelength_XYZ[w][0];
if ((W >= 390) and (W <= 700)) {
WavelengthToXYZ(W, X, Y, Z);
XYZtoRGB(X, Y, Z, R, G, B); // convert XYZ to RGB
XYZtoLAB(X, Y, Z, L, a, b); // convert XYZ to L*a*b*
glColor3d(R, G, B); // change color to computed RGB
glVertex3f(-a * size3d, -b * size3d, L * size3d); // add vertex
}
}
glEnd();
}
void DrawCMFinLMS(const float &size3d) // draw Color Matching Functions in LMS color space
{
double W, X, Y, Z, R, G, B, L, M, S;
glLineWidth(4);
glBegin(GL_LINE_STRIP);
for (int w = 0; w < wavelength_XYZ_nb; w++) {
W = wavelength_XYZ[w][0];
if ((W >= 390) and (W <= 700)) {
WavelengthToXYZ(W, X, Y, Z);
XYZtoRGB(X, Y, Z, R, G, B); // convert XYZ to RGB
XYZtoLMS(X, Y, Z, L, M, S); // convert XYZ to LMS
glColor3d(R, G, B); // change color to computed RGB
glVertex3f(L * size3d, -M * size3d, S * size3d); // add vertex
}
}
glEnd();
}
void DrawCMFinHLAB(const float &size3d) // draw Color Matching Functions in Hunter LAB color space
{
double W, X, Y, Z, R, G, B, L, a, b;
glLineWidth(4);
glBegin(GL_LINE_LOOP);
for (int w = 0; w < wavelength_XYZ_nb; w++) {
W = wavelength_XYZ[w][0];
if ((W >= 443) and (W <= 630)) {
WavelengthToXYZ(W, X, Y, Z);
XYZtoRGB(X, Y, Z, R, G, B); // convert XYZ to RGB
XYZtoHLAB(X, Y, Z, L, a, b); // convert XYZ to Hunter LAB
glColor3d(R, G, B); // change color to computed RGB
glVertex3f(-a * size3d, -b * size3d, L * size3d); // add vertex
}
}
glEnd();
}
///////////////////////////////////////////////
//// Circle
///////////////////////////////////////////////
void DrawFilledCircleXY(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &radius, const int &segments, const float &R, const float &G, const float &B) // filled circle in XY plane
{
glColor3d(R, G, B); // color to draw
glBegin(GL_POLYGON); // simple polygon
glVertex3d(radius * cos(0.0) + center_x, radius * sin(0.0) + center_y, center_z); // add first vertex
for (int i = 0; i < segments; i++) // for n segments
{
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
glVertex3d(radius * cos(alpha) + center_x, radius * sin(alpha) + center_y, center_z); // add vertex
}
glEnd();
}
void DrawCircleXY(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &radius, const int &segments, const float &R, const float &G, const float &B, const float &width)
{
glColor3d(R, G, B); // color to draw
glLineWidth(width); // line width
glBegin(GL_LINE_LOOP); // simple line with ends connected
for (int i = 0; i < segments; i++) // for n segments
{
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
glVertex3d(radius * cos(alpha) + center_x, radius * sin(alpha) + center_y, center_z); // add vertex
}
glEnd();
}
void DrawCircleArcXY(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &radius, const int &segments, const float &begin, const float &end, const float &R, const float &G, const float &B, const float &width)
{
float arc_begin = begin / 360.0 * 2 * Pi;
float arc_end = end / 360.0 * 2 * Pi;
glColor3d(R, G, B); // color to draw
glLineWidth(width); // line width
glBegin(GL_LINE_STRIP); // simple line with ends connected
for (int i = 0; i < segments; i++) // for n segments
{
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
if ((alpha >= arc_begin) and (alpha <= arc_end))
glVertex3d(radius * cos(alpha) + center_x, radius * sin(alpha) + center_y, center_z); // add vertex
}
glEnd();
}
///////////////////////////////////////////////
//// Cone
///////////////////////////////////////////////
void DrawConeX(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &length, const float &radius, const int &segments, const float &R, const float &G, const float &B) // Draw cone along x axis
{
static const float angle = 2.0f * Pi / float(segments);
glColor3d(R, G, B); // change color to draw
for (int i = 0; i < segments; i++) { // for n segments
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
glBegin(GL_POLYGON); // first polygon : base
glVertex3d(center_x, center_y, center_z); // center
glVertex3d(center_x, radius * cos(alpha) + center_y, radius * sin(alpha) + center_z);
glVertex3d(center_x, radius * cos(alpha + angle) + center_y, radius * sin(alpha + angle) + center_z);
glEnd();
glBegin(GL_POLYGON); // second polygon : tip
glVertex3d(center_x + length, center_y, center_z); // tip
glVertex3d(center_x, radius * cos(alpha) + center_y, radius * sin(alpha) + center_z);
glVertex3d(center_x, radius * cos(alpha + angle) + center_y, radius * sin(alpha + angle) + center_z);
glEnd();
}
}
void DrawConeY(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &length, const float &radius, const int &segments, const float &R, const float &G, const float &B) // Draw cone along y axis
{
// see DrawConeX for comments, it's almost the same
static const float angle = 2.0f * Pi / float(segments);
glColor3d(R, G, B);
for (int i = 0; i < segments; i++) {
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
glBegin(GL_POLYGON);
glVertex3d(center_x, center_y, center_z);
glVertex3d(radius * sin(alpha) + center_x, center_y, radius * cos(alpha) + center_z);
glVertex3d(radius * sin(alpha + angle) + center_x, center_y, radius * cos(alpha + angle) + center_z);
glEnd();
glBegin(GL_POLYGON);
glVertex3d(center_x, center_y + length, center_z);
glVertex3d(radius * sin(alpha) + center_x, center_y, radius * cos(alpha) + center_z);
glVertex3d(radius * sin(alpha + angle) + center_x, center_y, radius * cos(alpha + angle) + center_z);
glEnd();
}
}
void DrawConeZ(const float ¢er_x, const float ¢er_y, const float ¢er_z, const float &length, const float &radius, const int &segments, const float &R, const float &G, const float &B) // Draw cone along z axis
{
// see DrawConeX for comments, it's almost the same
static const float angle = 2.0f * Pi / float(segments);
glColor3d(R, G, B);
for (int i = 0; i < segments; i++) {
float alpha = 2.0f * Pi * float(i) / float(segments); //current angle
glBegin(GL_POLYGON);
glVertex3d(center_x, center_y, center_z);
glVertex3d(radius * cos(alpha) + center_x, radius * sin(alpha) + center_y, center_z);
glVertex3d(radius * cos(alpha + angle) + center_x, radius * sin(alpha + angle) + center_y, center_z);
glEnd();
glBegin(GL_POLYGON);
glVertex3d(center_x, center_y, center_z + length);
glVertex3d(radius * cos(alpha) + center_x, radius * sin(alpha) + center_y, center_z);
glVertex3d(radius * cos(alpha + angle) + center_x, radius * sin(alpha + angle) + center_y, center_z);
glEnd();
}
}
///////////////////////////////////////////////
//// Sphere by recursive triangles subdivision
///////////////////////////////////////////////
#define X .525731112119133606 // pre-calculated matrix
#define Z .850650808352039932
static float vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
void normalize(float *a) // normalize a vector
{
float d = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]); // euclidian distance
a[0]/=d; a[1]/=d; a[2]/=d; // normalized
}
void DrawTri(float *a, float *b, float *c, int div, float r, float x, float y, float z) // used to subdivide surfaces
{
if (div <= 0) {
glNormal3fv(a);
glVertex3f(a[0]*r + x, a[1]*r - y, a[2]*r + z);
glNormal3fv(b);
glVertex3f(b[0]*r + x, b[1]*r - y, b[2]*r + z);
glNormal3fv(c);
glVertex3f(c[0]*r + x, c[1]*r - y, c[2]*r + z);
} else {
float ab[3], ac[3], bc[3];
for (int i = 0; i < 3; i++) { // half
ab[i] = (a[i]+b[i]) / 2.0;
ac[i] = (a[i]+c[i]) / 2.0;
bc[i] = (b[i]+c[i]) / 2.0;
}
normalize(ab);
normalize(ac);
normalize(bc);
DrawTri(a, ab, ac, div-1, r, x, y, z); // new divided triangles
DrawTri(b, bc, ab, div-1, r, x, y, z);
DrawTri(c, ac, bc, div-1, r, x, y, z);
DrawTri(ab, bc, ac, div-1, r, x, y, z); // comment this line and sphere looks really cool in wireframe
}
}
void DrawSphere(const int &ndiv, const float &radius, const float &x, const float &y, const float &z, const float &r, const float &g, const float &b) // draw a sphere with ndiv as number of subdivisions
{
glColor3d(r,g,b); // change color
glBegin(GL_TRIANGLES); // draw triangles
for (int i = 0; i < 20; i++) // 20 times
DrawTri(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], ndiv, radius, x, y, z); // recursive drawing with triangle subdivision with sphere data
glEnd();
}
///////////////////////////////////////////////
//// Matrix to lines
///////////////////////////////////////////////
void DrawLinesFromMatrix(const Mat &matrix, const float &x0, const float &y0, const float &z0, const float &scale, const float &R, const float &G, const float &B, const float &width)
{
glColor3f(R,G,B);
glLineWidth(width);
glBegin(GL_LINES);
for (int x = 0; x < matrix.cols; x++) // down
for (int y = 0; y < matrix.rows - 1; y++)
if ((matrix.at<uchar>(y, x) != 0) & (matrix.at<uchar>(y + 1, x) != 0)) {
glVertex3f(scale * x + x0, -scale * y + y0, z0);
glVertex3f(scale * x + x0, -scale * (y + 1) + y0, z0);
}
for (int x = 0; x < matrix.cols - 1; x++) // right
for (int y = 0; y < matrix.rows; y++)
if ((matrix.at<uchar>(y, x) != 0) & (matrix.at<uchar>(y, x + 1) != 0)) {
glVertex3f(scale * x + x0, -scale * y + y0, z0);
glVertex3f(scale * (x + 1) + x0, -scale * y + y0, z0);
}
for (int x = 0; x < matrix.cols - 1; x++) // down-right
for (int y = 0; y < matrix.rows - 1; y++)
if ((matrix.at<uchar>(y, x) != 0) & (matrix.at<uchar>(y + 1, x + 1) != 0) & (matrix.at<uchar>(y, x + 1) == 0) & (matrix.at<uchar>(y + 1, x) == 0)) {
glVertex3f(scale * x + x0, -scale * y + y0, z0);
glVertex3f(scale * (x + 1) + x0, -scale * (y + 1) + y0, z0);
}
for (int x = 1; x < matrix.cols; x++) // down-left
for (int y = 0; y < matrix.rows - 1; y++)
if ((matrix.at<uchar>(y, x) != 0) & (matrix.at<uchar>(y + 1, x - 1) != 0) & (matrix.at<uchar>(y, x - 1) == 0) & (matrix.at<uchar>(y + 1, x) == 0)) {
glVertex3f(scale * x + x0, -scale * y + y0, z0);
glVertex3f(scale * (x - 1) + x0, -scale * (y + 1) + y0, z0);
}
glEnd();
// isolated dots
for (int x = 0; x < matrix.cols; x++) // down-left
for (int y = 0; y < matrix.rows; y++)
if ((matrix.at<uchar>(y, x) != 0) & (matrix.at<uchar>(y - 1, x - 1) == 0) & (matrix.at<uchar>(y - 1, x) == 0) & (matrix.at<uchar>(y - 1, x + 1) == 0)
& (matrix.at<uchar>(y, x - 1) == 0) & (matrix.at<uchar>(y, x + 1) == 0)
& (matrix.at<uchar>(y + 1, x - 1) == 0) & (matrix.at<uchar>(y + 1, x) == 0) & (matrix.at<uchar>(y + 1, x + 1) == 0)) {
//DrawCircleXY(scale * x + x0, scale * y + y0, z0 , 4, 10, 1, 1, 1, 4);
DrawSphere(3, width / 2.0, scale * x + x0, -scale * y - y0, z0, R, G, B);
}
}
///////////////////////////////////////////////
//// Draw text from string
///////////////////////////////////////////////
int checkCharacter(const int &c, const int &x, const int &y)
{
if ((x < 0) or (y < 0) or (x >= characters_matrix_cols) or (y >= characters_matrix_rows))
return 0;
else
return characters[c].data[x][y];
}
void DrawChar(const uchar ch, const float &x0, const float &y0, const float &z0, const float &scale, const float &R, const float &G, const float &B, const float &width)
{
if ((ch < characters_begin) or (ch > characters_end))
return;
int c = ch - characters_begin;
glColor3f(R,G,B);
glLineWidth(width);
glBegin(GL_LINES);
for (int x = 0; x < characters_matrix_cols; x++) // down
for (int y = 0; y < characters_matrix_rows - 1; y++)
if ((characters[c].data[x][y] != 0) & (checkCharacter(c, x, y + 1) != 0)) {
glVertex3f(scale * x + x0, scale * y + y0, z0);
glVertex3f(scale * x + x0, scale * (y + 1) + y0, z0);
}
for (int x = 0; x < characters_matrix_cols - 1; x++) // right
for (int y = 0; y < characters_matrix_rows; y++)
if ((characters[c].data[x][y] != 0) & (checkCharacter(c, x + 1, y) != 0)) {
glVertex3f(scale * x + x0, scale * y + y0, z0);
glVertex3f(scale * (x + 1) + x0, scale * y + y0, z0);
}
for (int x = 0; x < characters_matrix_cols - 1; x++) // down-right
for (int y = 0; y < characters_matrix_rows - 1; y++)
if ((characters[c].data[x][y] != 0) & (checkCharacter(c, x + 1, y + 1) != 0) & (checkCharacter(c, x + 1, y) == 0) & (checkCharacter(c, x, y + 1) == 0)) {
glVertex3f(scale * x + x0, scale * y + y0, z0);
glVertex3f(scale * (x + 1) + x0, scale * (y + 1) + y0, z0);
}
for (int x = 1; x < characters_matrix_cols; x++) // down-left
for (int y = 0; y < characters_matrix_rows - 1; y++)
if ((characters[c].data[x][y] != 0) & (checkCharacter(c, x - 1, y + 1) != 0) & (checkCharacter(c, x - 1, y) == 0) & (checkCharacter(c, x, y + 1) == 0)) {
glVertex3f(scale * x + x0, scale * y + y0, z0);
glVertex3f(scale * (x - 1) + x0, scale * (y + 1) + y0, z0);
}
glEnd();
// isolated dots
for (int x = 0; x < characters_matrix_cols; x++) // down-left
for (int y = 0; y < characters_matrix_rows; y++)
if ((characters[c].data[x][y] != 0)
& (checkCharacter(c, x - 1, y - 1) == 0) & (checkCharacter(c, x, y - 1) == 0) & (checkCharacter(c, x + 1, y - 1) == 0)
& (checkCharacter(c, x - 1, y) == 0) & (checkCharacter(c, x + 1, y) == 0)
& (checkCharacter(c, x - 1, y + 1) == 0) & (checkCharacter(c, x, y + 1) == 0) & (checkCharacter(c, x + 1, y + 1) == 0)) {
//DrawCircleXY(scale * x + x0, scale * y + y0, z0 , 4, 10, 1, 1, 1, 4);
DrawSphere(3, width, scale * x + x0, -scale * y - y0, z0, R, G, B);
}
}
void DrawText(const std::string &text, const float &x0, const float &y0, const float &z0, const float &scale, const float &R, const float &G, const float &B, const float &width)
{
float xCurrent = 0;
for (uint n = 0; n < text.length(); n++) {
DrawChar(text[n], x0, y0 + xCurrent, z0, scale, R, G, B, width);
xCurrent += scale * characters_matrix_cols;
}
}