-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsr_graph.h
976 lines (828 loc) · 37.9 KB
/
sr_graph.h
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* sr_graph - v1.0 - public domain plot tracer ;
no warranties implied, use at your own risk.
Do this:
#define SRG_IMPLEMENTATION_SR_GRAPH
before you include this file in *one* C++ file to create the implementation.
You will need to include OpenGL headers (or glew, gl3w,...) before including
this one. In other files you should simply include "sr_graph.h" without using
the above define.
// i.e. it should look like this:
#include <gl.h>// or glew header, gl3w header,...
#include ...
#include ...
#define SRG_IMPLEMENTATION_SR_GRAPH
#include "srg_graph.h"
In the file where the implementation is activated, SR_GRAPH_LOGGING can be
defined to log errors in OpenGL setup (stdio.h will be included).
Usage:
Make sure that an OpenGL context has been setup before calling any sr_graph
functions. All colors are defined by their red, green, blue components
taking values in [0.0,1.0].
Start by creating a graph using:
setup(min x, max x, min y, max y, ratio, margin, red, green, blue);
You specify the bounds of the graph (minimal and maximal values on both
axes), the screen ratio, the margin (between 0 - no margin - and 1 - no
graph -), and the background color. This will return an integer ID that
you will use as a reference to the graph for later calls.
The graph can be customized by adding axes with arrows (automatically
oriented toward the increasing values on both axes). You can specify their
width (in [0,1]) and set their color. By default the axes will pass through
the (0,0) point or stick to the sides of the graph the closest to it. You
can also force them to stick on the side by setting 'side axis' to true.
add_axes(graphID, width, red, green, blue, side axis);
A regular grid can also be added. Specify the spacing between grid lines on
both axis, the width (in [0,1]) and color. The grid can either start from
the left and bottom side of the graph, or be centered on the (0,0) point by
setting 'zero align' to true.
add_grid(graphID, step x, step y, width, red, green, blue, zero align);
You can plot continuous curves by specifying lists of X and Y coordinates,
along with the width ([0,1]) and color of the lines. Points clouds can be
added in a similar manner, by specifying X and Y coordinates, a point size
and color. Finally you can also add histograms, by specifying a set of
values and a number of bins ; spacing between bars and their color should
also be specified.
add_curve(graphID, x values, y values, line width, red, green, blue);
add_points(graphID, x values, y values, point size, red, green, blue);
add_hist(graphID, number of bins, values, spacing, red, green, blue);
These three functions return integers IDs that can then be used to update a
given plot on a given graph at a later time by calling respectively:
update_points(graphID, points cloud id, new x values, new y values);
update_curve(graphID, curve id, new x values, new y values);
update_hist(graphID, histogram id, new values);
All settings (width, size, bins, colors) are preserved. The IDs are managed
separately for each graph and each type of plot.
Call:
draw(graphID, screen ratio);
to draw a graph in the current viewport. If a screen ratio is specified,
the graph will be rescaled to avoid any deformation ; else, nothing is done
to prevent deformations when resizing the window.
You can delete a graph and free its resources, or free all graphs and
resources (including OpenGL buffers and programs) by calling respectively:
free(graphID);
free();
See below the exact list of types and functions expected from OpenGL headers.
GLuint, GLsizei, Glint
GL_DEPTH_TEST, GL_TRUE, GL_FALSE, GL_CULL_FACE, GL_BLEND, GL_FRONT_FACE,
GL_CULL_FACE_MODE, GL_BLEND_SRC, GL_BLEND_DST, GL_POLYGON_MODE, GL_CCW,
GL_BACK, GL_FRONT, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_FRONT_AND_BACK,
GL_FILL, GL_TRIANGLES, GL_ARRAY_BUFFER, GL_STATIC_DRAW, GL_FLOAT,
GL_COMPILE_STATUS, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_LINK_STATUS
glDeleteVertexArrays, glDeleteBuffers, glIsEnabled, glBlendFunc, glDisable,
glEnable, glFrontFace, glCullFace, glGetIntegerv, glPolygonMode,
glUseProgram, glUniform1f, glUniform1i, glUniform3f, glBindVertexArray,
glDrawArrays, glDeleteProgram, glGenVertexArrays, glGenBuffers,
glBindBuffer, glBufferData, glEnableVertexAttribArray, glCreateShader,
glShaderSource, glVertexAttribPointer, glCompileShader, glGetShaderiv,
glCreateProgram, glAttachShader, glLinkProgram, glGetProgramiv,
glDetachShader, glDeleteShader, glGetUniformLocation
Revision history
1.0 First public version! Support for curves, bar charts, points, axes with
arrows, grid, real-time updates.
License:
See end of file.
*/
#ifndef SRG_INCLUDE_SR_GRAPH_H
#define SRG_INCLUDE_SR_GRAPH_H
#include <vector>
#ifdef SR_GRAPH_STATIC
#define SRG_EXTERN static
#else
#define SRG_EXTERN extern
#endif
namespace sr_graph {
SRG_EXTERN int setup(const float minx, const float maxx, const float miny, const float maxy, const float ratio, const float margins, const float bg_r, const float bg_g, const float bg_b);
SRG_EXTERN void add_axes(const int graph_id, const float width, const float axis_r, const float axis_g, const float axis_b, const bool axisOnSide);
SRG_EXTERN void add_grid(const int graph_id, const float stepx, const float stepy, const float width, const float lines_r, const float lines_g, const float lines_b, const bool fromZero);
SRG_EXTERN int add_curve(const int graph_id, const std::vector<float> & xs, const std::vector<float> & ys, const float width, const float color_r, const float color_g, const float color_b);
SRG_EXTERN void update_curve(const int graph_id, const int curve_id, const std::vector<float> & xs, const std::vector<float> & ys);
SRG_EXTERN int add_hist(const int graph_id, const unsigned int bins, const std::vector<float> & ys, const float spacing, const float color_r, const float color_g, const float color_b);
SRG_EXTERN void update_hist(const int graph_id, const int hist_id, const std::vector<float> & ys);
SRG_EXTERN int add_points(const int graph_id, const std::vector<float> & xs, const std::vector<float> & ys, const float size, const float color_r, const float color_g, const float color_b);
SRG_EXTERN void update_points(const int graph_id, const int points_id, const std::vector<float> & xs, const std::vector<float> & ys);
SRG_EXTERN void draw(const int graph_id, const float ratio = 0.0f);
SRG_EXTERN void free(const int graph_id);
SRG_EXTERN void free();
}
#endif
#ifdef SRG_IMPLEMENTATION_SR_GRAPH
#ifdef SR_GRAPH_LOGGING
#include <stdio.h>
#endif
#include <math.h>
#include <vector>
namespace sr_graph {
/// Internal structs.
typedef struct {
float r;
float g;
float b;
} _srg_Color;
typedef struct {
GLuint id;
GLuint bid;
GLsizei count;
} _srg_Buffers;
typedef struct {
_srg_Buffers buffer;
_srg_Color color;
float param0;
unsigned int param1;
} _srg_Curve;
typedef struct {
_srg_Color color;
float minx;
float maxx;
float miny;
float maxy;
float ratio;
float margin;
bool freed;
_srg_Buffers bufferAxes;
_srg_Color colorAxes;
_srg_Buffers bufferGrid;
_srg_Color colorGrid;
std::vector<_srg_Curve> curves;
std::vector<_srg_Curve> curvespoints;
std::vector<_srg_Curve> points;
std::vector<_srg_Curve> hists;
} _srg_Graph;
typedef struct {
_srg_Buffers bufferQuad;
GLuint pid;
GLuint ppid;
GLuint lpid;
GLuint cid;
GLuint pcid;
GLuint lcid;
GLuint rid;
GLuint prid;
GLuint lrid;
GLuint psid;
} _srg_InternalState ;
enum _srg_Orientation {
VERTICAL, HORIZONTAL
};
/// Internal variables.
static bool _srg_isInit = false;
static _srg_InternalState _srg_state;
static std::vector<_srg_Graph> _srg_graphs;
/// Foreward declarations.
static void _srg_internalSetup();
static _srg_Buffers _srg_setDataBuffer(const float * data, const unsigned int count);
static void _srg_getLine(const float p0x, const float p0y, const float p1x, const float p1y, const float w, const float ratio, std::vector<float> & points);
static void _srg_getRectangle(const float p0x, const float p0y, const float p1x, const float p1y, const float w, std::vector<float> & points);
static void _srg_getPoint(const float p0x, const float p0y, const float radius, const float ratio, std::vector<float> & points);
static void _srg_generateAxis(const _srg_Orientation orientation, const float margin, const float ratio, const float width, const float mini, const float maxi, const bool axisOnSide, const bool reverse, std::vector<float> & axisData);
static void _srg_generateCurve(const _srg_Graph & graph, const std::vector<float> & xs, const std::vector<float> & ys, _srg_Curve & curve);
static void _srg_generatePoints(const _srg_Graph & graph, const std::vector<float> & xs, const std::vector<float> & ys, _srg_Curve & curve);
static void _srg_generateHist(const _srg_Graph & graph, const std::vector<float> & ys, _srg_Curve & curve);
/// Exposed functions.
SRG_EXTERN int setup(const float minx, const float maxx, const float miny, const float maxy, const float ratio, const float margins, const float bg_r, const float bg_g, const float bg_b){
// If we haven't initialized our GL programs, do it.
if (!_srg_isInit) {
_srg_internalSetup();
}
// Create a graph with the given infos.
_srg_Graph graph;
graph.color = {bg_r, bg_g, bg_b };
graph.minx = minx;
graph.maxx = maxx;
graph.miny = miny;
graph.maxy = maxy;
graph.ratio = fabs(ratio);
graph.margin = fmin(1.0f, fmax(0.0f, fabs(margins*2.0f)));
graph.freed = false;
graph.bufferAxes = { 0, 0, 0 };
graph.colorAxes = graph.color;
graph.bufferGrid = { 0, 0, 0 };
graph.colorGrid = graph.color;
graph.curves.clear();
graph.curvespoints.clear();
graph.points.clear();
graph.hists.clear();
// Store it.
_srg_graphs.push_back(graph);
return (int)_srg_graphs.size() - 1;
}
SRG_EXTERN void add_axes(const int graph_id, const float width, const float axis_r, const float axis_g, const float axis_b, const bool axisOnSide){
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
/// Generate data for axis.
std::vector<float> axisData;
_srg_generateAxis(HORIZONTAL, graph.margin, graph.ratio, width, graph.miny, graph.maxy, axisOnSide, graph.minx > graph.maxx, axisData);
_srg_generateAxis(VERTICAL, graph.margin, graph.ratio, width, graph.minx, graph.maxx, axisOnSide, graph.miny > graph.maxy, axisData);
graph.colorAxes = { axis_r, axis_g, axis_b };
graph.bufferAxes = _srg_setDataBuffer(&axisData[0], (unsigned int)axisData.size() / 2);
}
SRG_EXTERN void add_grid(const int graph_id, const float stepx, const float stepy, const float width, const float lines_r, const float lines_g, const float lines_b, const bool fromZero) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
std::vector<float> gridData;
const float ax = (2.0f*(1.0f-graph.margin))/(graph.maxx - graph.minx);
const float bx = -1.0f + graph.margin - ax * graph.minx;
const float ay = (2.0f*(1.0f-graph.margin))/(graph.maxy - graph.miny);
const float by = -1.0f + graph.margin - ay * graph.miny;
if(stepx != 0.0f && graph.maxx != graph.minx){
float shiftH = fabs(ax)*fabs(stepx);
if(fromZero){
float xZero = bx;
while(xZero < -1.0f + graph.margin){
xZero += shiftH;
}
while(xZero > 1.0f - graph.margin){
xZero -= shiftH;
}
for(float xi = xZero; xi >= -1.0f+graph.margin-0.0001f; xi -= shiftH){
_srg_getLine(xi, -1.0f+graph.margin, xi, 1.0f-graph.margin, width, graph.ratio, gridData);
}
for(float xi = xZero+shiftH; xi <= 1.0f-graph.margin+0.0001f; xi += shiftH){
_srg_getLine(xi, -1.0f+graph.margin, xi, 1.0f-graph.margin, width, graph.ratio, gridData);
}
} else {
for(float x = -1.0f+graph.margin; x <= 1.0f-graph.margin; x += shiftH){
_srg_getLine(x, -1.0f+graph.margin, x, 1.0f-graph.margin, width, graph.ratio, gridData);
}
_srg_getLine(1.0f - graph.margin, -1.0f+graph.margin, 1.0f - graph.margin, 1.0f-graph.margin, width, graph.ratio, gridData);
}
}
if(stepy != 0.0f && graph.maxy != graph.miny){
float shiftV = fabs(ay)*fabs(stepy);
if(fromZero){
float yZero = by;
while(yZero < -1.0f + graph.margin){
yZero += shiftV;
}
while(yZero > 1.0f - graph.margin){
yZero -= shiftV;
}
for(float yi = yZero; yi >= -1.0f+graph.margin - 0.0001f; yi -= shiftV){
_srg_getLine(-1.0f+graph.margin, yi, 1.0f-graph.margin, yi, width, graph.ratio, gridData);
}
for(float yi = yZero+shiftV; yi <= 1.0f-graph.margin + 0.0001f; yi += shiftV){
_srg_getLine(-1.0f+graph.margin, yi, 1.0f-graph.margin, yi, width, graph.ratio, gridData);
}
} else {
for(float y = -1.0f+graph.margin; y < 1.0f-graph.margin; y += shiftV){
_srg_getLine(-1.0f+graph.margin, y, 1.0f-graph.margin, y, width, graph.ratio, gridData);
}
_srg_getLine(-1.0f+graph.margin, 1.0f - graph.margin, 1.0f-graph.margin, 1.0f - graph.margin, width, graph.ratio, gridData);
}
}
graph.colorGrid = { lines_r, lines_g, lines_b };
graph.bufferGrid = _srg_setDataBuffer(&gridData[0], (unsigned int)gridData.size() / 2);
}
SRG_EXTERN int add_curve(const int graph_id, const std::vector<float> & xs, const std::vector<float> & ys, const float width, const float color_r, const float color_g, const float color_b){
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return -1;
}
_srg_Graph & graph = _srg_graphs[graph_id];
// Generate the lines.
_srg_Curve curve;
curve.color = {color_r, color_g, color_b};
curve.param0 = width;
_srg_generateCurve(graph, xs, ys, curve);
graph.curves.push_back(curve);
// Generate the points junctions.
_srg_Curve curvepoints;
curvepoints.color = { color_r, color_g, color_b };
curvepoints.param0 = width;
_srg_generatePoints(graph, xs, ys, curvepoints);
graph.curvespoints.push_back(curvepoints);
return (int)graph.curves.size()-1;
}
SRG_EXTERN void update_curve(const int graph_id, const int curve_id, const std::vector<float> & xs, const std::vector<float> & ys) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
if(curve_id < 0 || curve_id >= graph.curves.size()){
return;
}
// Update the lines.
_srg_Curve & curve = graph.curves[curve_id];
glDeleteVertexArrays(1, &(curve.buffer.id));
glDeleteBuffers(1, &(curve.buffer.bid));
_srg_generateCurve(graph, xs, ys, curve);
// Update the points junctions.
_srg_Curve & curvepoints = graph.curvespoints[curve_id];
glDeleteVertexArrays(1, &(curvepoints.buffer.id));
glDeleteBuffers(1, &(curvepoints.buffer.bid));
_srg_generatePoints(graph, xs, ys, curvepoints);
}
SRG_EXTERN int add_points(const int graph_id, const std::vector<float> & xs, const std::vector<float> & ys, const float size, const float color_r, const float color_g, const float color_b) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return -1;
}
_srg_Graph & graph = _srg_graphs[graph_id];
_srg_Curve curve;
curve.color = {color_r, color_g, color_b};
curve.param0 = size;
_srg_generatePoints(graph, xs, ys, curve);
graph.points.push_back(curve);
return (int)graph.points.size()-1;
}
SRG_EXTERN void update_points(const int graph_id, const int curve_id, const std::vector<float> & xs, const std::vector<float> & ys) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
if(curve_id < 0 || curve_id >= graph.points.size()){
return;
}
_srg_Curve & curve = graph.points[curve_id];
glDeleteVertexArrays(1, &(curve.buffer.id));
glDeleteBuffers(1, &(curve.buffer.bid));
_srg_generatePoints(graph, xs, ys, curve);
}
SRG_EXTERN int add_hist(const int graph_id, const unsigned int bins, const std::vector<float> & ys, const float spacing, const float color_r, const float color_g, const float color_b) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return -1;
}
_srg_Graph & graph = _srg_graphs[graph_id];
_srg_Curve curve;
curve.color = {color_r, color_g, color_b};
curve.param0 = spacing;
curve.param1 = bins;
_srg_generateHist(graph, ys, curve);
graph.hists.push_back(curve);
return (int)graph.hists.size()-1;
}
SRG_EXTERN void update_hist(const int graph_id, const int curve_id, const std::vector<float> & ys) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
if(curve_id < 0 || curve_id >= graph.hists.size()){
return;
}
_srg_Curve & curve = graph.hists[curve_id];
glDeleteVertexArrays(1, &(curve.buffer.id));
glDeleteBuffers(1, &(curve.buffer.bid));
_srg_generateHist(graph, ys, curve);
}
SRG_EXTERN void draw(const int graph_id, float ratio) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
// Copy OpenGL states.
bool depthState = glIsEnabled(GL_DEPTH_TEST) == GL_TRUE;
bool cullState = glIsEnabled(GL_CULL_FACE) == GL_TRUE;
bool blendState = glIsEnabled(GL_BLEND) == GL_TRUE;
GLint faceMode, cullFaceMode, blendSrcMode, blendDstMode;
glGetIntegerv(GL_FRONT_FACE, &faceMode);
glGetIntegerv(GL_CULL_FACE_MODE, &cullFaceMode);
glGetIntegerv(GL_BLEND_SRC, &blendSrcMode);
glGetIntegerv(GL_BLEND_DST, &blendDstMode);
GLint polygonModes[2];
glGetIntegerv(GL_POLYGON_MODE, polygonModes);
// Set states.
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) ;
// Set ratios.
const _srg_Graph & graph = _srg_graphs[graph_id];
const float finalRatio = (ratio == 0.0) ? 1.0f : ratio/graph.ratio;
glUseProgram(_srg_state.pid);
glUniform1f(_srg_state.rid, finalRatio);
glUseProgram(_srg_state.lpid);
glUniform1f(_srg_state.lrid, finalRatio);
glUseProgram(_srg_state.ppid);
glUniform1f(_srg_state.prid, finalRatio);
glUniform1i(_srg_state.psid, 1);
// Draw quad to clear.
glUseProgram(_srg_state.pid);
glUniform3f(_srg_state.cid, graph.color.r, graph.color.g, graph.color.b);
glBindVertexArray(_srg_state.bufferQuad.id);
glDrawArrays(GL_TRIANGLES, 0, _srg_state.bufferQuad.count);
glBindVertexArray(0);
// Draw grid.
glUseProgram(_srg_state.lpid);
glUniform3f(_srg_state.lcid, graph.colorGrid.r, graph.colorGrid.g, graph.colorGrid.b);
glBindVertexArray(graph.bufferGrid.id);
glDrawArrays(GL_TRIANGLES, 0, graph.bufferGrid.count);
glBindVertexArray(0);
// Draw histograms.
glUseProgram(_srg_state.pid);
for(unsigned int i = 0; i < graph.hists.size(); ++i){
glUniform3f(_srg_state.cid, graph.hists[i].color.r, graph.hists[i].color.g, graph.hists[i].color.b);
glBindVertexArray(graph.hists[i].buffer.id);
glDrawArrays(GL_TRIANGLES, 0, graph.hists[i].buffer.count);
}
// Draw curves.
for(unsigned int i = 0; i < graph.curves.size(); ++i){
// Draw the lines.
glUseProgram(_srg_state.lpid);
glUniform3f(_srg_state.lcid, graph.curves[i].color.r, graph.curves[i].color.g, graph.curves[i].color.b);
glBindVertexArray(graph.curves[i].buffer.id);
glDrawArrays(GL_TRIANGLES, 0, graph.curves[i].buffer.count);
// Draw the points junctions (soft point smoothing enabled). // @IMPROVEMENT: enhance line junctions again.
glUseProgram(_srg_state.ppid);
glUniform3f(_srg_state.pcid, graph.curvespoints[i].color.r, graph.curvespoints[i].color.g, graph.curvespoints[i].color.b);
glBindVertexArray(graph.curvespoints[i].buffer.id);
glDrawArrays(GL_TRIANGLES, 0, graph.curvespoints[i].buffer.count);
}
// Draw axes.
glUseProgram(_srg_state.lpid);
glUniform3f(_srg_state.lcid, graph.colorAxes.r, graph.colorAxes.g, graph.colorAxes.b);
glBindVertexArray(graph.bufferAxes.id);
glDrawArrays(GL_TRIANGLES, 0, graph.bufferAxes.count);
// Draw points (crisp point smoothing enabled).
glUseProgram(_srg_state.ppid);
glUniform1i(_srg_state.psid, 0);
for(unsigned int i = 0; i < graph.points.size(); ++i){
glUniform3f(_srg_state.pcid, graph.points[i].color.r, graph.points[i].color.g, graph.points[i].color.b);
glBindVertexArray(graph.points[i].buffer.id);
glDrawArrays(GL_TRIANGLES, 0, graph.points[i].buffer.count);
}
// Restore OpenGL state.
glBindVertexArray(0);
glUseProgram(0);
if(blendState){
glEnable(GL_BLEND);
} else {
glDisable(GL_BLEND);
}
if(cullState){
glEnable(GL_CULL_FACE);
} else {
glDisable(GL_CULL_FACE);
}
if(depthState){
glEnable(GL_DEPTH_TEST);
} else {
glDisable(GL_DEPTH_TEST);
}
glFrontFace(faceMode);
glCullFace(cullFaceMode);
glBlendFunc(blendSrcMode, blendDstMode);
glPolygonMode(GL_FRONT, polygonModes[0] );
glPolygonMode(GL_BACK, polygonModes[1] );
}
SRG_EXTERN void free(const int graph_id) {
if(graph_id < 0 || graph_id >= _srg_graphs.size() || _srg_graphs[graph_id].freed){
return;
}
_srg_Graph & graph = _srg_graphs[graph_id];
// Free vertex arrays.
glDeleteVertexArrays(1, &graph.bufferGrid.id);
glDeleteVertexArrays(1, &graph.bufferAxes.id);
glDeleteBuffers(1, &(graph.bufferGrid.bid));
glDeleteBuffers(1, &(graph.bufferAxes.bid));
for(unsigned int i = 0; i < graph.curves.size(); ++i){
glDeleteVertexArrays(1, &(graph.curves[i].buffer.id));
glDeleteBuffers(1, &(graph.curves[i].buffer.bid));
}
for (unsigned int i = 0; i < graph.curvespoints.size(); ++i) {
glDeleteVertexArrays(1, &(graph.curvespoints[i].buffer.id));
glDeleteBuffers(1, &(graph.curvespoints[i].buffer.bid));
}
for(unsigned int i = 0; i < graph.hists.size(); ++i){
glDeleteVertexArrays(1, &(graph.hists[i].buffer.id));
glDeleteBuffers(1, &(graph.hists[i].buffer.bid));
}
for(unsigned int i = 0; i < graph.points.size(); ++i){
glDeleteVertexArrays(1, &(graph.points[i].buffer.id));
glDeleteBuffers(1, &(graph.points[i].buffer.bid));
}
graph.freed = true;
}
SRG_EXTERN void free(){
// Free each graph and then clear the map.
for(unsigned int i = 0; i < _srg_graphs.size(); ++i){
free(i);
}
_srg_graphs.clear();
// Delete the programs and quad data.
glDeleteProgram(_srg_state.pid);
glDeleteProgram(_srg_state.lpid);
glDeleteProgram(_srg_state.ppid);
glDeleteVertexArrays(1, &_srg_state.bufferQuad.id);
glDeleteBuffers(1, &_srg_state.bufferQuad.bid);
}
/// Internal functions.
static void _srg_getLine(const float p0x, const float p0y, const float p1x, const float p1y, const float w, const float ratio, std::vector<float> & points) {
// Compute normal vector.
float dirx = p1x - p0x; float diry = p1y - p0y;
const float dirNorm = sqrtf(dirx*dirx+diry*diry);
if(dirNorm != 0.0f){
dirx /= dirNorm; diry /= dirNorm;
}
const float norx = -diry; const float nory = dirx;
const float sdx = w; const float sdy = ratio * w;
const float dNx = sdx * norx; const float dNy = sdy * nory;
const float ax = p0x - dNx; const float ay = p0y - dNy;
const float bx = p1x - dNx; const float by = p1y - dNy;
const float cx = p1x + dNx; const float cy = p1y + dNy;
const float dx = p0x + dNx; const float dy = p0y + dNy;
points.push_back(ax); points.push_back(ay);
points.push_back(bx); points.push_back(by);
points.push_back(cx); points.push_back(cy);
points.push_back(ax); points.push_back(ay);
points.push_back(cx); points.push_back(cy);
points.push_back(dx); points.push_back(dy);
}
static void _srg_getRectangle(const float p0x, const float p0y, const float p1x, const float p1y, const float w, std::vector<float> & points) {
const float wx = w * 0.5f;
const float ax = p0x - wx; const float ay = p0y;
const float bx = p0x + wx; const float by = p0y;
const float cx = p1x + wx; const float cy = p1y;
const float dx = p1x - wx; const float dy = p1y;
points.push_back(ax); points.push_back(ay);
points.push_back(bx); points.push_back(by);
points.push_back(cx); points.push_back(cy);
points.push_back(ax); points.push_back(ay);
points.push_back(cx); points.push_back(cy);
points.push_back(dx); points.push_back(dy);
}
static void _srg_getPoint(const float p0x, const float p0y, const float radius, const float ratio, std::vector<float> & points) {
const float wx = radius; const float wy = radius * ratio;
const float ax = p0x - wx; const float ay = p0y - wy;
const float bx = p0x + wx; const float by = p0y - wy;
const float cx = p0x + wx; const float cy = p0y + wy;
const float dx = p0x - wx; const float dy = p0y + wy;
points.push_back(ax); points.push_back(ay);
points.push_back(bx); points.push_back(by);
points.push_back(cx); points.push_back(cy);
points.push_back(ax); points.push_back(ay);
points.push_back(cx); points.push_back(cy);
points.push_back(dx); points.push_back(dy);
}
static void _srg_generateAxis(const _srg_Orientation orientation, const float margin, const float ratio, const float width, const float mini, const float maxi, const bool axisOnSide, const bool reverse, std::vector<float> & axisData){
float hy;
// Three positions.
if(axisOnSide || 0.0 <= fmin(mini, maxi)){
// Axis on the bottom
hy = -1.0f+margin;
if(!axisOnSide && maxi < mini){
hy *= -1.0f;
}
} else if (0 >= fmax(maxi, mini)){
// Axis on the top
hy = 1.0f-margin;
if(maxi < mini){
hy *= -1.0f;
}
} else {
// Need to find 0 y coord.
hy = -2.0f*(1.0f - margin)*(mini/(maxi-mini))+margin-1.0f;
}
const float ld = fmin(0.03f, 0.3f*margin);
const float rv = orientation == VERTICAL ? ratio : 1.0f;
const float rh = orientation == HORIZONTAL ? ratio : 1.0f;
const int sh = orientation == VERTICAL ? 0 : 1;
const float hx0 = -1.0f + margin - (reverse ? 1.5f*ld*rv : 0.75f*width*rv);
const float hx1 = 1.0f - margin + (reverse ? 0.75f*width*rv : 1.5f*ld*rv);
const float ord = reverse ? hx0 : hx1;
const float sn = reverse ? -1.0f : 1.0f;
const float s0 = sn * (1.0f + sqrtf(2.0)*rv)*width;
const float s1 = sn * ld * rv;
const float s2 = (-sh*2.0f+1.0f) * sn * (ld - sqrt(2.0f)*width)*rh;
const float s3 = (-sh*2.0f+1.0f) * sn * (ld + sqrt(2.0f)*width)*rh;
if (orientation == VERTICAL) {
_srg_getLine(hy, hx0, hy, hx1, width, ratio, axisData);
_srg_getLine(hy, ord + sn*width, hy + sn*ld, ord - sn*ld*ratio, width, ratio, axisData);
} else {
_srg_getLine(hx0, hy, hx1, hy, width, ratio, axisData);
_srg_getLine(ord + sn * width, hy, ord - sn * ld, hy - sn * ld*ratio, width, ratio, axisData);
}
// Correct vertices positions to join the point of the arrow and the tail angles.
axisData[axisData.size() - 2 + sh] = hy;
axisData[axisData.size() - 1 - sh] = ord + s0;
axisData[axisData.size() - 10 + sh] = hy + s2;
axisData[axisData.size() - 8 + sh] = hy + s3;
axisData[axisData.size() - 4 + sh] = hy + s3;
axisData[axisData.size() - 9 - sh] = ord - s1;
axisData[axisData.size() - 7 - sh] = ord - s1;
axisData[axisData.size() - 3 - sh] = ord - s1;
if (orientation == VERTICAL) {
_srg_getLine(hy, ord + sn * width, hy - sn * ld, ord - sn * ld * ratio, width, ratio, axisData);
} else {
_srg_getLine(ord + sn*width, hy, ord - sn*ld, hy + sn * ld*ratio, width, ratio, axisData);
}
// Correct vertices positions to join the point of the arrow and the tail angles.
axisData[axisData.size() - 6 + sh] = hy;
axisData[axisData.size() - 12 + sh] = hy;
axisData[axisData.size() - 5 - sh] = ord + s0;
axisData[axisData.size() - 11 - sh] = ord + s0;
axisData[axisData.size() - 10 + sh] = hy - s3;
axisData[axisData.size() - 8 + sh] = hy - s2;
axisData[axisData.size() - 4 + sh] = hy - s2;
axisData[axisData.size() - 9 - sh] = ord - s1;
axisData[axisData.size() - 7 - sh] = ord - s1;
axisData[axisData.size() - 3 - sh] = ord - s1;
}
static void _srg_generateCurve(const _srg_Graph & graph, const std::vector<float> & xs, const std::vector<float> & ys, _srg_Curve & curve) {
if(xs.size() != ys.size() || xs.size() == 0){
return;
}
const float ax = (2.0f*(1.0f-graph.margin))/(graph.maxx - graph.minx);
const float bx = -1.0f + graph.margin - ax * graph.minx;
const float ay = (2.0f*(1.0f-graph.margin))/(graph.maxy - graph.miny);
const float by = -1.0f + graph.margin - ay * graph.miny;
std::vector<float> curveData;
float x0 = ax*xs[0]+bx;
float y0 = ay*ys[0]+by;
for(unsigned int i = 1; i < xs.size(); ++i){
const float x1 = ax*xs[i]+bx;
const float y1 = ay*ys[i]+by;
_srg_getLine(x0, y0, x1, y1, curve.param0, graph.ratio, curveData);
x0 = x1;
y0 = y1;
}
curve.buffer = _srg_setDataBuffer(&curveData[0], (unsigned int)curveData.size() / 2);
}
static void _srg_generatePoints(const _srg_Graph & graph, const std::vector<float> & xs, const std::vector<float> & ys, _srg_Curve & curve){
if(xs.size() != ys.size() || xs.size() == 0){
return;
}
const float ax = (2.0f*(1.0f-graph.margin))/(graph.maxx - graph.minx);
const float bx = -1.0f + graph.margin - ax * graph.minx;
const float ay = (2.0f*(1.0f-graph.margin))/(graph.maxy - graph.miny);
const float by = -1.0f + graph.margin - ay * graph.miny;
std::vector<float> curveData;
for(unsigned int i = 0; i < xs.size(); ++i){
const float x0 = ax*xs[i]+bx;
const float y0 = ay*ys[i]+by;
_srg_getPoint(x0, y0, curve.param0, graph.ratio, curveData);
}
curve.buffer = _srg_setDataBuffer(&curveData[0], (unsigned int)curveData.size() / 2);
}
static void _srg_generateHist(const _srg_Graph & graph, const std::vector<float> & ys, _srg_Curve & curve){
if(ys.size() == 0){
return;
}
const float binSize = (graph.maxx - graph.minx)/(float)curve.param1;
std::vector<int> binCounts(curve.param1, 0);
for(unsigned int i = 0; i < ys.size(); ++i){
const int j = (unsigned int)floor((ys[i] - graph.minx)/binSize);
if(j < 0 || j >= binCounts.size()){
continue;
}
binCounts[j] += 1;
}
std::vector<float> histData;
const float ax = (2.0f*(1.0f-graph.margin))/(graph.maxx - graph.minx);
const float bx = -1.0f + graph.margin - ax * graph.minx;
const float ay = (2.0f*(1.0f-graph.margin))/(graph.maxy - graph.miny);
const float by = -1.0f + graph.margin - ay * graph.miny;
const float binWidth = fmax(0.0f, 2.0f*(1.0f - graph.margin)/(float)curve.param1 - curve.param0);
for(unsigned int i = 0; i < curve.param1; ++i){
if(binCounts[i] == 0){
continue;
}
float x0 = ax*(graph.minx+(float)(i+0.5f)*binSize) + bx;
float y0 = by;
float y1 = ay * (float)binCounts[i] + by;
_srg_getRectangle(x0, y0, x0, y1, binWidth, histData);
}
curve.buffer = _srg_setDataBuffer(&histData[0], (unsigned int)histData.size() / 2);
}
/// Shaders strings.
static const char * _srg_vstr =
"#version 330\nlayout(location = 0) in vec2 v;\nuniform float ratio;\nout vec2 coords;\nvoid main(){\nvec2 finalRatio = ratio < 1.0 ? vec2(1.0, ratio) : vec2(1.0/ratio, 1.0);\ngl_Position.xy = v * finalRatio;\ngl_Position.zw = vec2(1.0);\nint id = int(mod(gl_VertexID, 6));\nbool uzero = (id == 0) || (id == 3) || (id == 5);\nbool vzero = (id == 0) || (id == 3) || (id == 1);\ncoords = vec2(uzero ? -1.0 : 1.0, vzero ? -1.0 : 1.0);\n}\n";
static const char * _srg_fstr = "#version 330\nin vec2 coords;\nuniform vec3 color;\nout vec4 frag_Color;\nvoid main(){\nfrag_Color.rgb = color;\nfrag_Color.a = 1.0;\n}\n";
static const char * _srg_lfstr = "#version 330\nin vec2 coords;\nuniform vec3 color;\nout vec4 frag_Color;\nvoid main(){\nfrag_Color.rgb = color;\nfrag_Color.a = 1.0-smoothstep(0.5, 1.0, abs(coords.y));\n}\n";
static const char * _srg_pfstr = "#version 330\nin vec2 coords;\nuniform vec3 color;\nuniform bool smoothing;\nout vec4 fragColor;\nvoid main(){\nfragColor = vec4(color, 1.0-smoothstep(smoothing ? 0.5 : 0.9, 1.0, length(coords)));\n}\n";
/// OpenGL helpers.
static _srg_Buffers _srg_setDataBuffer(const float * data, const unsigned int count){
// Create a vertex array and a vertex buffer.
GLuint vaId;
glGenVertexArrays(1, &vaId);
glBindVertexArray(vaId);
GLuint bufferPos;
glGenBuffers(1, &bufferPos);
glBindBuffer(GL_ARRAY_BUFFER, bufferPos);
// Populate the buffer with the 2D vertices.
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * count, data, GL_STATIC_DRAW);
// Setup the corresponding shader attribute at location 0.
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, bufferPos);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glBindVertexArray(0);
return { vaId, bufferPos, (GLsizei)count };
}
static GLuint _srg_loadShader(const char * prog, GLuint type) {
// Create shader, setup string as source and compile it.
GLuint id = glCreateShader(type);
glShaderSource(id, 1, &prog, (const GLint*)NULL);
glCompileShader(id);
// Check shader compilation status.
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
// If compilation failed, get information and display it.
if (success != GL_TRUE) {
#ifdef SR_GRAPH_LOGGING
printf("[sr_graph] Failed OpenGL shader setup.\n");
#endif
// glCreateShader returns 0 if an error happened, so we can too.
return 0;
}
return id;
}
static GLuint _srg_createGLProgram(const char* vertexString, const char * fragmentString) {
GLuint id = glCreateProgram();
// Load and attach shaders, then link them w/ the program.
GLuint vp = _srg_loadShader(vertexString, GL_VERTEX_SHADER);
GLuint fp = _srg_loadShader(fragmentString, GL_FRAGMENT_SHADER);
if(vp==0 || fp == 0){ return 0; }
glAttachShader(id, vp);
glAttachShader(id, fp);
glLinkProgram(id);
//Check linking status.
GLint success = GL_FALSE;
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
#ifdef SR_GRAPH_LOGGING
printf("[sr_graph] Failed OpenGL program setup.\n");
#endif
// glCreateProgram returns 0 if an error happened, so we can too.
return 0;
}
// Clean the shaders objects, detach and delete them.
glDetachShader(id, vp);
glDetachShader(id, fp);
glDeleteShader(vp);
glDeleteShader(fp);
return id;
}
static void _srg_internalSetup() {
// Quads program.
_srg_state.pid = _srg_createGLProgram(_srg_vstr, _srg_fstr);
glUseProgram(_srg_state.pid);
_srg_state.cid = glGetUniformLocation(_srg_state.pid, "color");
_srg_state.rid = glGetUniformLocation(_srg_state.pid, "ratio");
// Lines program.
_srg_state.lpid = _srg_createGLProgram(_srg_vstr, _srg_lfstr);
glUseProgram(_srg_state.lpid);
_srg_state.lcid = glGetUniformLocation(_srg_state.lpid, "color");
_srg_state.lrid = glGetUniformLocation(_srg_state.lpid, "ratio");
// Points program.
_srg_state.ppid = _srg_createGLProgram(_srg_vstr, _srg_pfstr);
glUseProgram(_srg_state.ppid);
_srg_state.pcid = glGetUniformLocation(_srg_state.ppid, "color");
_srg_state.prid = glGetUniformLocation(_srg_state.ppid, "ratio");
_srg_state.psid = glGetUniformLocation(_srg_state.ppid, "smoothing");
glUseProgram(0);
// Quad data for full viewport clearing.
const float quadData[12] = { -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f };
_srg_state.bufferQuad = _srg_setDataBuffer(quadData, 6);
// Check all programs are ok before considering initialization complete.
if(_srg_state.pid != 0 && _srg_state.lpid != 0 && _srg_state.ppid != 0){
_srg_isInit = true;
} else {
#ifdef SR_GRAPH_LOGGING
printf("[sr_graph] Failed to init.\n");
#endif
}
}
}
#endif
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Simon Rodriguez
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/