-
Notifications
You must be signed in to change notification settings - Fork 75
/
graph_dev_opengl.c
4566 lines (3869 loc) · 161 KB
/
graph_dev_opengl.c
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <math.h>
#include <assert.h>
#include <sys/stat.h>
#include <limits.h>
#include "shader.h"
#include "vertex.h"
#include "triangle.h"
#include "mtwist.h"
#include "mathutils.h"
#include "matrix.h"
#include "quat.h"
#include "mesh.h"
#include "vec4.h"
#include "snis_graph.h"
#include "graph_dev.h"
#include "material.h"
#include "entity.h"
#include "entity_private.h"
#include "snis_typeface.h"
#include "opengl_cap.h"
#include "png_utils.h"
#define OPENGL_VERSION_STRING "#version 120\n"
#define UNIVERSAL_SHADER_HEADER \
OPENGL_VERSION_STRING
/*
* Filmic tonemapping cribbed from oolite:
*
* gamma correction
* using Jim Hejl's filmic tonemapping and gamma correction approximation.
* Normally this would require HDR, but I think it works extremely well in Oolite.
* Formula taken from https://www.gdcvault.com/play/1012351/Uncharted-2-HDR
* jump to 27:40 in the video. Note the pow 1.0/2.2 is baked into these numbers
*
* Perhaps that it normally requires HDR is the reason it doesn't seem to look so
* great in SNIS.
*/
#define FILMIC_TONEMAPPING \
"uniform float u_FilmicTonemapping;\n" \
"uniform float u_TonemappingGain;\n" \
"vec4 filmic_tonemap(vec4 color) {\n" \
" float dont_tonemap = 1.0 - u_FilmicTonemapping;\n" \
" vec3 x = max(vec3(0.0), color.rgb - 0.004);\n" \
" x = u_TonemappingGain * (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06);\n" \
" return dont_tonemap * color + vec4(u_FilmicTonemapping * x, color.a);\n" \
"}\n\n"
#define TEX_RELOAD_DELAY 1.0
#define CUBEMAP_TEX_RELOAD_DELAY 1.0
#define MAX_LOADED_TEXTURES 80
struct loaded_texture {
GLuint texture_id;
char *filename;
time_t mtime;
double last_mtime_change;
int expired;
int use_mipmaps;
int linear_colorspace;
};
static int nloaded_textures = 0;
static struct loaded_texture loaded_textures[MAX_LOADED_TEXTURES];
static char *error_texture_file = NULL;
static int no_texture_mode = 0;
#define NCUBEMAP_TEXTURES 6
#define MAX_LOADED_CUBEMAP_TEXTURES 40
struct loaded_cubemap_texture {
GLuint texture_id;
int is_inside;
char *filename[NCUBEMAP_TEXTURES];
time_t mtime;
double last_mtime_change;
int expired;
int linear_colorspace;
};
static int nloaded_cubemap_textures = 0;
static struct loaded_cubemap_texture loaded_cubemap_textures[MAX_LOADED_CUBEMAP_TEXTURES];
static int draw_normal_lines = 0;
static int draw_billboard_wireframe = 0;
static int draw_polygon_as_lines = 0;
static int draw_msaa_samples = 0;
static int draw_render_to_texture = 0;
static int draw_smaa = 0;
static int draw_smaa_edge = 0;
static int draw_smaa_blend = 0;
static int draw_atmospheres = 1;
static int filmic_tonemapping = 1;
static float tonemapping_gain = 1.18;
int graph_dev_planet_specularity = 1;
int graph_dev_atmosphere_ring_shadows = 1;
static const char *default_shader_directory = "share/snis/shader";
static char *shader_directory = NULL;
struct mesh_gl_info {
/* common buffer to hold vertex positions */
GLuint vertex_buffer;
int ntriangles;
/* uses vertex_buffer for data */
GLuint triangle_vertex_buffer;
GLuint triangle_normal_lines_buffer;
GLuint triangle_tangent_lines_buffer;
GLuint triangle_bitangent_lines_buffer;
int nwireframe_lines;
GLuint wireframe_lines_vertex_buffer;
int npoints;
/* uses vertex_buffer for data */
int nlines;
/* uses vertex_buffer for data */
GLuint line_vertex_buffer;
int nparticles;
GLuint particle_vertex_buffer;
GLuint particle_index_buffer;
};
struct vertex_buffer_data {
union vec3 position;
};
struct vertex_triangle_buffer_data {
union vec3 normal;
union vec3 tvertex0;
union vec3 tvertex1;
union vec3 tvertex2;
union vec3 wireframe_edge_mask;
union vec2 texture_coord;
union vec3 tangent;
union vec3 bitangent;
};
struct vertex_wireframe_line_buffer_data {
union vec3 position;
union vec3 normal;
};
struct vertex_line_buffer_data {
GLubyte multi_one[4];
union vec3 line_vertex0;
union vec3 line_vertex1;
};
struct vertex_color_buffer_data {
GLfloat position[2];
GLubyte color[4];
};
struct vertex_particle_buffer_data {
GLubyte multi_one[4];
union vec3 start_position;
GLubyte start_tint_color[3];
GLubyte start_apm[2];
union vec3 end_position;
GLubyte end_tint_color[3];
GLubyte end_apm[2];
};
void mesh_graph_dev_cleanup(struct mesh *m)
{
if (m->graph_ptr) {
struct mesh_gl_info *ptr = m->graph_ptr;
glDeleteBuffers(1, &ptr->vertex_buffer);
glDeleteBuffers(1, &ptr->triangle_vertex_buffer);
glDeleteBuffers(1, &ptr->wireframe_lines_vertex_buffer);
glDeleteBuffers(1, &ptr->triangle_normal_lines_buffer);
glDeleteBuffers(1, &ptr->triangle_tangent_lines_buffer);
glDeleteBuffers(1, &ptr->triangle_bitangent_lines_buffer);
glDeleteBuffers(1, &ptr->line_vertex_buffer);
glDeleteBuffers(1, &ptr->particle_vertex_buffer);
glDeleteBuffers(1, &ptr->particle_index_buffer);
free(ptr);
m->graph_ptr = 0;
}
}
/* load/reload an array buffer using stream draw if it is being overwritten */
#define LOAD_BUFFER(buffer_type, buffer_id, buffer_size, buffer_data) \
do { \
GLenum usage; \
if ((buffer_id) == 0) { \
usage = GL_STATIC_DRAW; \
glGenBuffers(1, &(buffer_id)); \
} else \
usage = GL_STREAM_DRAW; \
glBindBuffer((buffer_type), (buffer_id)); \
glBufferData((buffer_type), (buffer_size), (buffer_data), usage); \
} while (0)
void mesh_graph_dev_init(struct mesh *m)
{
struct mesh_gl_info *ptr = m->graph_ptr;
if (!ptr) {
ptr = malloc(sizeof(struct mesh_gl_info));
memset(ptr, 0, sizeof(*ptr));
m->graph_ptr = ptr;
}
if (m->geometry_mode == MESH_GEOMETRY_TRIANGLES) {
/* setup the triangle mesh buffers */
int i;
size_t v_size = sizeof(struct vertex_buffer_data) * m->ntriangles * 3;
size_t vt_size = sizeof(struct vertex_triangle_buffer_data) * m->ntriangles * 3;
struct vertex_buffer_data *g_v_buffer_data = malloc(v_size);
struct vertex_triangle_buffer_data *g_vt_buffer_data = malloc(vt_size);
float normal_line_length = m->radius / 20.0;
size_t nl_size = sizeof(struct vertex_buffer_data) * m->ntriangles * 3 * 2;
struct vertex_buffer_data *g_nl_buffer_data = malloc(nl_size * 3);
memset(g_nl_buffer_data, 0, nl_size * 3);
struct vertex_buffer_data *g_tl_buffer_data = &g_nl_buffer_data[m->ntriangles * 3 * 2];
struct vertex_buffer_data *g_bl_buffer_data = &g_nl_buffer_data[m->ntriangles * 3 * 2 * 2];
ptr->ntriangles = m->ntriangles;
ptr->npoints = m->ntriangles * 3; /* can be rendered as a point cloud too */
for (i = 0; i < m->ntriangles; i++) {
int j = 0;
for (j = 0; j < 3; j++) {
int v_index = i * 3 + j;
g_v_buffer_data[v_index].position.v.x = m->t[i].v[j]->x;
g_v_buffer_data[v_index].position.v.y = m->t[i].v[j]->y;
g_v_buffer_data[v_index].position.v.z = m->t[i].v[j]->z;
g_vt_buffer_data[v_index].normal.v.x = m->t[i].vnormal[j].x;
g_vt_buffer_data[v_index].normal.v.y = m->t[i].vnormal[j].y;
g_vt_buffer_data[v_index].normal.v.z = m->t[i].vnormal[j].z;
g_vt_buffer_data[v_index].tvertex0.v.x = m->t[i].v[0]->x;
g_vt_buffer_data[v_index].tvertex0.v.y = m->t[i].v[0]->y;
g_vt_buffer_data[v_index].tvertex0.v.z = m->t[i].v[0]->z;
g_vt_buffer_data[v_index].tvertex1.v.x = m->t[i].v[1]->x;
g_vt_buffer_data[v_index].tvertex1.v.y = m->t[i].v[1]->y;
g_vt_buffer_data[v_index].tvertex1.v.z = m->t[i].v[1]->z;
g_vt_buffer_data[v_index].tvertex2.v.x = m->t[i].v[2]->x;
g_vt_buffer_data[v_index].tvertex2.v.y = m->t[i].v[2]->y;
g_vt_buffer_data[v_index].tvertex2.v.z = m->t[i].v[2]->z;
g_vt_buffer_data[v_index].tangent.v.x = m->t[i].vtangent[j].x;
g_vt_buffer_data[v_index].tangent.v.y = m->t[i].vtangent[j].y;
g_vt_buffer_data[v_index].tangent.v.z = m->t[i].vtangent[j].z;
g_vt_buffer_data[v_index].bitangent.v.x = m->t[i].vbitangent[j].x;
g_vt_buffer_data[v_index].bitangent.v.y = m->t[i].vbitangent[j].y;
g_vt_buffer_data[v_index].bitangent.v.z = m->t[i].vbitangent[j].z;
/* bias the edge distance to make the coplanar edges not draw */
if ((j == 1 || j == 2) && (m->t[i].flag & TRIANGLE_1_2_COPLANAR))
g_vt_buffer_data[v_index].wireframe_edge_mask.v.x = 1000;
else
g_vt_buffer_data[v_index].wireframe_edge_mask.v.x = 0;
if ((j == 0 || j == 2) && (m->t[i].flag & TRIANGLE_0_2_COPLANAR))
g_vt_buffer_data[v_index].wireframe_edge_mask.v.y = 1000;
else
g_vt_buffer_data[v_index].wireframe_edge_mask.v.y = 0;
if ((j == 0 || j == 1) && (m->t[i].flag & TRIANGLE_0_1_COPLANAR))
g_vt_buffer_data[v_index].wireframe_edge_mask.v.z = 1000;
else
g_vt_buffer_data[v_index].wireframe_edge_mask.v.z = 0;
if (m->tex) {
g_vt_buffer_data[v_index].texture_coord.v.x = m->tex[v_index].u;
g_vt_buffer_data[v_index].texture_coord.v.y = m->tex[v_index].v;
} else {
g_vt_buffer_data[v_index].texture_coord.v.x = 0;
g_vt_buffer_data[v_index].texture_coord.v.y = 0;
}
/* draw a line for each vertex normal, tangent, and bitangent */
int nl_index = i * 6 + j * 2;
/* normal */
g_nl_buffer_data[nl_index].position.v.x = m->t[i].v[j]->x;
g_nl_buffer_data[nl_index].position.v.y = m->t[i].v[j]->y;
g_nl_buffer_data[nl_index].position.v.z = m->t[i].v[j]->z;
g_nl_buffer_data[nl_index + 1].position.v.x =
m->t[i].v[j]->x + normal_line_length * m->t[i].vnormal[j].x;
g_nl_buffer_data[nl_index + 1].position.v.y =
m->t[i].v[j]->y + normal_line_length * m->t[i].vnormal[j].y;
g_nl_buffer_data[nl_index + 1].position.v.z =
m->t[i].v[j]->z + normal_line_length * m->t[i].vnormal[j].z;
/* tangent */
g_tl_buffer_data[nl_index].position.v.x = m->t[i].v[j]->x;
g_tl_buffer_data[nl_index].position.v.y = m->t[i].v[j]->y;
g_tl_buffer_data[nl_index].position.v.z = m->t[i].v[j]->z;
g_tl_buffer_data[nl_index + 1].position.v.x =
m->t[i].v[j]->x + normal_line_length * m->t[i].vtangent[j].x;
g_tl_buffer_data[nl_index + 1].position.v.y =
m->t[i].v[j]->y + normal_line_length * m->t[i].vtangent[j].y;
g_tl_buffer_data[nl_index + 1].position.v.z =
m->t[i].v[j]->z + normal_line_length * m->t[i].vtangent[j].z;
/* bitangent */
g_bl_buffer_data[nl_index].position.v.x = m->t[i].v[j]->x;
g_bl_buffer_data[nl_index].position.v.y = m->t[i].v[j]->y;
g_bl_buffer_data[nl_index].position.v.z = m->t[i].v[j]->z;
g_bl_buffer_data[nl_index + 1].position.v.x =
m->t[i].v[j]->x + normal_line_length * m->t[i].vbitangent[j].x;
g_bl_buffer_data[nl_index + 1].position.v.y =
m->t[i].v[j]->y + normal_line_length * m->t[i].vbitangent[j].y;
g_bl_buffer_data[nl_index + 1].position.v.z =
m->t[i].v[j]->z + normal_line_length * m->t[i].vbitangent[j].z;
}
}
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->vertex_buffer, v_size, g_v_buffer_data);
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->triangle_vertex_buffer, vt_size, g_vt_buffer_data);
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->triangle_normal_lines_buffer, nl_size, g_nl_buffer_data);
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->triangle_tangent_lines_buffer, nl_size, g_tl_buffer_data);
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->triangle_bitangent_lines_buffer, nl_size, g_bl_buffer_data);
free(g_v_buffer_data);
free(g_vt_buffer_data);
free(g_nl_buffer_data);
/* setup the line buffers used for wireframe */
size_t wfl_size = sizeof(struct vertex_wireframe_line_buffer_data) * m->ntriangles * 3 * 2;
struct vertex_wireframe_line_buffer_data *g_wfl_buffer_data = malloc(wfl_size);
/* map the edge combinatinos to the triangle coplanar flag */
static const int tri_coplaner_flags[3][3] = {
{0, TRIANGLE_0_1_COPLANAR, TRIANGLE_0_2_COPLANAR},
{TRIANGLE_0_1_COPLANAR, 0, TRIANGLE_1_2_COPLANAR},
{TRIANGLE_0_2_COPLANAR, TRIANGLE_1_2_COPLANAR, 0} };
ptr->nwireframe_lines = 0;
for (i = 0; i < m->ntriangles; i++) {
int j0 = 0;
for (j0 = 0; j0 < 3; j0++) {
int j1 = (j0 + 1) % 3;
if (!(m->t[i].flag & tri_coplaner_flags[j0][j1])) {
int index = 2 * ptr->nwireframe_lines;
/* add the line from vertex j0 to j1 */
g_wfl_buffer_data[index].position.v.x = m->t[i].v[j0]->x;
g_wfl_buffer_data[index].position.v.y = m->t[i].v[j0]->y;
g_wfl_buffer_data[index].position.v.z = m->t[i].v[j0]->z;
g_wfl_buffer_data[index + 1].position.v.x = m->t[i].v[j1]->x;
g_wfl_buffer_data[index + 1].position.v.y = m->t[i].v[j1]->y;
g_wfl_buffer_data[index + 1].position.v.z = m->t[i].v[j1]->z;
/* the line normal is the same as the triangle */
g_wfl_buffer_data[index].normal.v.x = m->t[i].n.x;
g_wfl_buffer_data[index].normal.v.y = m->t[i].n.y;
g_wfl_buffer_data[index].normal.v.z = m->t[i].n.z;
g_wfl_buffer_data[index + 1].normal.v.x = m->t[i].n.x;
g_wfl_buffer_data[index + 1].normal.v.y = m->t[i].n.y;
g_wfl_buffer_data[index + 1].normal.v.z = m->t[i].n.z;
ptr->nwireframe_lines++;
}
}
}
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->wireframe_lines_vertex_buffer,
sizeof(struct vertex_wireframe_line_buffer_data) * ptr->nwireframe_lines * 2,
g_wfl_buffer_data);
free(g_wfl_buffer_data);
}
if (m->geometry_mode == MESH_GEOMETRY_LINES || m->geometry_mode == MESH_GEOMETRY_PARTICLE_ANIMATION) {
/* setup the line buffers */
int i;
size_t v_size = sizeof(struct vertex_buffer_data) * m->nvertices * 2;
struct vertex_buffer_data *g_v_buffer_data = malloc(v_size);
size_t vl_size = sizeof(struct vertex_line_buffer_data) * m->nvertices * 2;
struct vertex_line_buffer_data *g_vl_buffer_data = malloc(vl_size);
ptr->nlines = 0;
for (i = 0; i < m->nlines; i++) {
struct vertex *vstart = m->l[i].start;
struct vertex *vend = m->l[i].end;
if (m->l[i].flag & MESH_LINE_STRIP) {
struct vertex *vcurr = vstart;
struct vertex *v1;
while (vcurr <= vend) {
struct vertex *v2 = vcurr;
if (v2 != vstart) {
int index = ptr->nlines * 2;
g_v_buffer_data[index].position.v.x = v1->x;
g_v_buffer_data[index].position.v.y = v1->y;
g_v_buffer_data[index].position.v.z = v1->z;
g_v_buffer_data[index + 1].position.v.x = v2->x;
g_v_buffer_data[index + 1].position.v.y = v2->y;
g_v_buffer_data[index + 1].position.v.z = v2->z;
g_vl_buffer_data[index].multi_one[0] =
g_vl_buffer_data[index + 1].multi_one[0] = 0; /* is dotted */
g_vl_buffer_data[index].line_vertex0.v.x =
g_vl_buffer_data[index + 1].line_vertex0.v.x = v1->x;
g_vl_buffer_data[index].line_vertex0.v.y =
g_vl_buffer_data[index + 1].line_vertex0.v.y = v1->y;
g_vl_buffer_data[index].line_vertex0.v.z =
g_vl_buffer_data[index + 1].line_vertex0.v.z = v1->z;
g_vl_buffer_data[index].line_vertex1.v.x =
g_vl_buffer_data[index + 1].line_vertex1.v.x = v2->x;
g_vl_buffer_data[index].line_vertex1.v.y =
g_vl_buffer_data[index + 1].line_vertex1.v.y = v2->y;
g_vl_buffer_data[index].line_vertex1.v.z =
g_vl_buffer_data[index + 1].line_vertex1.v.z = v2->z;
ptr->nlines++;
}
v1 = v2;
++vcurr;
}
} else {
int is_dotted = m->l[i].flag & MESH_LINE_DOTTED;
int index = ptr->nlines * 2;
g_v_buffer_data[index].position.v.x = vstart->x;
g_v_buffer_data[index].position.v.y = vstart->y;
g_v_buffer_data[index].position.v.z = vstart->z;
g_v_buffer_data[index + 1].position.v.x = vend->x;
g_v_buffer_data[index + 1].position.v.y = vend->y;
g_v_buffer_data[index + 1].position.v.z = vend->z;
g_vl_buffer_data[index].multi_one[0] =
g_vl_buffer_data[index + 1].multi_one[0] = is_dotted ? 255 : 0;
g_vl_buffer_data[index].line_vertex0.v.x =
g_vl_buffer_data[index + 1].line_vertex0.v.x = vstart->x;
g_vl_buffer_data[index].line_vertex0.v.y =
g_vl_buffer_data[index + 1].line_vertex0.v.y = vstart->y;
g_vl_buffer_data[index].line_vertex0.v.z =
g_vl_buffer_data[index + 1].line_vertex0.v.z = vstart->z;
g_vl_buffer_data[index].line_vertex1.v.x =
g_vl_buffer_data[index + 1].line_vertex1.v.x = vend->x;
g_vl_buffer_data[index].line_vertex1.v.y =
g_vl_buffer_data[index + 1].line_vertex1.v.y = vend->y;
g_vl_buffer_data[index].line_vertex1.v.z =
g_vl_buffer_data[index + 1].line_vertex1.v.z = vend->z;
ptr->nlines++;
}
}
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->vertex_buffer, sizeof(struct vertex_buffer_data) * 2 * ptr->nlines,
g_v_buffer_data);
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->line_vertex_buffer,
sizeof(struct vertex_line_buffer_data) * 2 * ptr->nlines, g_vl_buffer_data);
ptr->npoints = ptr->nlines * 2; /* can be rendered as a point cloud too */
free(g_v_buffer_data);
free(g_vl_buffer_data);
}
if (m->geometry_mode == MESH_GEOMETRY_POINTS) {
/* setup the point buffers */
size_t v_size = sizeof(struct vertex_buffer_data) * m->nvertices;
struct vertex_buffer_data *g_v_buffer_data = malloc(v_size);
ptr->npoints = m->nvertices;
int i;
for (i = 0; i < m->nvertices; i++) {
g_v_buffer_data[i].position.v.x = m->v[i].x;
g_v_buffer_data[i].position.v.y = m->v[i].y;
g_v_buffer_data[i].position.v.z = m->v[i].z;
}
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->vertex_buffer, v_size, g_v_buffer_data);
free(g_v_buffer_data);
}
if (m->geometry_mode == MESH_GEOMETRY_PARTICLE_ANIMATION) {
ptr->nparticles = m->nvertices / 2;
size_t v_size = sizeof(struct vertex_particle_buffer_data) * ptr->nparticles * 4;
struct vertex_particle_buffer_data *g_v_buffer_data = malloc(v_size);
size_t i_size = sizeof(GLushort) * ptr->nparticles * 6;
GLushort *g_i_buffer_data = malloc(i_size);
/* two triangles from four vertices
V3 (0,1) +---+ V2 (1,1)
+\ +
+ \ +
+ \+
V0 (0,0) +---+ V1 (1,0)
*/
int i;
for (i = 0; i < m->nvertices; i += 2) {
int v_index = i * 2;
GLubyte tint_red = (int)(m->l[i / 2].tint_color.red * 255) & 255;
GLubyte tint_green = (int)(m->l[i / 2].tint_color.green * 255) & 255;
GLubyte tint_blue = (int)(m->l[i / 2].tint_color.blue * 255) & 255;
GLubyte additivity = (int)(m->l[i / 2].additivity * 255) & 255;
GLubyte opacity = (int)(m->l[i / 2].opacity * 255) & 255;
GLubyte time_offset = (int)(m->l[i / 2].time_offset * 255) & 255;
/* texture coord is different for all four vertices */
g_v_buffer_data[v_index + 0].multi_one[0] = 0;
g_v_buffer_data[v_index + 0].multi_one[1] = 0;
g_v_buffer_data[v_index + 1].multi_one[0] = 255;
g_v_buffer_data[v_index + 1].multi_one[1] = 0;
g_v_buffer_data[v_index + 2].multi_one[0] = 255;
g_v_buffer_data[v_index + 2].multi_one[1] = 255;
g_v_buffer_data[v_index + 3].multi_one[0] = 0;
g_v_buffer_data[v_index + 3].multi_one[1] = 255;
g_v_buffer_data[v_index + 0].multi_one[2] =
g_v_buffer_data[v_index + 1].multi_one[2] =
g_v_buffer_data[v_index + 2].multi_one[2] =
g_v_buffer_data[v_index + 3].multi_one[2] = time_offset;
/* the rest of the attributes are the same for all four */
g_v_buffer_data[v_index + 0].start_position.v.x =
g_v_buffer_data[v_index + 1].start_position.v.x =
g_v_buffer_data[v_index + 2].start_position.v.x =
g_v_buffer_data[v_index + 3].start_position.v.x = m->v[i].x;
g_v_buffer_data[v_index + 0].start_position.v.y =
g_v_buffer_data[v_index + 1].start_position.v.y =
g_v_buffer_data[v_index + 2].start_position.v.y =
g_v_buffer_data[v_index + 3].start_position.v.y = m->v[i].y;
g_v_buffer_data[v_index + 0].start_position.v.z =
g_v_buffer_data[v_index + 1].start_position.v.z =
g_v_buffer_data[v_index + 2].start_position.v.z =
g_v_buffer_data[v_index + 3].start_position.v.z = m->v[i].z;
g_v_buffer_data[v_index + 0].start_tint_color[0] =
g_v_buffer_data[v_index + 1].start_tint_color[0] =
g_v_buffer_data[v_index + 2].start_tint_color[0] =
g_v_buffer_data[v_index + 3].start_tint_color[0] = tint_red;
g_v_buffer_data[v_index + 0].start_tint_color[1] =
g_v_buffer_data[v_index + 1].start_tint_color[1] =
g_v_buffer_data[v_index + 2].start_tint_color[1] =
g_v_buffer_data[v_index + 3].start_tint_color[1] = tint_green;
g_v_buffer_data[v_index + 0].start_tint_color[2] =
g_v_buffer_data[v_index + 1].start_tint_color[2] =
g_v_buffer_data[v_index + 2].start_tint_color[2] =
g_v_buffer_data[v_index + 3].start_tint_color[2] = tint_blue;
g_v_buffer_data[v_index + 0].start_apm[0] =
g_v_buffer_data[v_index + 1].start_apm[0] =
g_v_buffer_data[v_index + 2].start_apm[0] =
g_v_buffer_data[v_index + 3].start_apm[0] = additivity;
g_v_buffer_data[v_index + 0].start_apm[1] =
g_v_buffer_data[v_index + 1].start_apm[1] =
g_v_buffer_data[v_index + 2].start_apm[1] =
g_v_buffer_data[v_index + 3].start_apm[1] = opacity;
g_v_buffer_data[v_index + 0].end_position.v.x =
g_v_buffer_data[v_index + 1].end_position.v.x =
g_v_buffer_data[v_index + 2].end_position.v.x =
g_v_buffer_data[v_index + 3].end_position.v.x = m->v[i + 1].x;
g_v_buffer_data[v_index + 0].end_position.v.y =
g_v_buffer_data[v_index + 1].end_position.v.y =
g_v_buffer_data[v_index + 2].end_position.v.y =
g_v_buffer_data[v_index + 3].end_position.v.y = m->v[i + 1].y;
g_v_buffer_data[v_index + 0].end_position.v.z =
g_v_buffer_data[v_index + 1].end_position.v.z =
g_v_buffer_data[v_index + 2].end_position.v.z =
g_v_buffer_data[v_index + 3].end_position.v.z = m->v[i + 1].z;
g_v_buffer_data[v_index + 0].end_tint_color[0] =
g_v_buffer_data[v_index + 1].end_tint_color[0] =
g_v_buffer_data[v_index + 2].end_tint_color[0] =
g_v_buffer_data[v_index + 3].end_tint_color[0] = tint_red;
g_v_buffer_data[v_index + 0].end_tint_color[1] =
g_v_buffer_data[v_index + 1].end_tint_color[1] =
g_v_buffer_data[v_index + 2].end_tint_color[1] =
g_v_buffer_data[v_index + 3].end_tint_color[1] = tint_green;
g_v_buffer_data[v_index + 0].end_tint_color[2] =
g_v_buffer_data[v_index + 1].end_tint_color[2] =
g_v_buffer_data[v_index + 2].end_tint_color[2] =
g_v_buffer_data[v_index + 3].end_tint_color[2] = tint_blue;
g_v_buffer_data[v_index + 0].end_apm[0] =
g_v_buffer_data[v_index + 1].end_apm[0] =
g_v_buffer_data[v_index + 2].end_apm[0] =
g_v_buffer_data[v_index + 3].end_apm[0] = additivity;
g_v_buffer_data[v_index + 0].end_apm[1] =
g_v_buffer_data[v_index + 1].end_apm[1] =
g_v_buffer_data[v_index + 2].end_apm[1] =
g_v_buffer_data[v_index + 3].end_apm[1] = opacity;
/* setup six indices for our two triangles */
int i_index = i * 3;
g_i_buffer_data[i_index + 0] = v_index + 0;
g_i_buffer_data[i_index + 1] = v_index + 1;
g_i_buffer_data[i_index + 2] = v_index + 3;
g_i_buffer_data[i_index + 3] = v_index + 1;
g_i_buffer_data[i_index + 4] = v_index + 2;
g_i_buffer_data[i_index + 5] = v_index + 3;
}
LOAD_BUFFER(GL_ARRAY_BUFFER, ptr->particle_vertex_buffer, v_size, g_v_buffer_data);
LOAD_BUFFER(GL_ELEMENT_ARRAY_BUFFER, ptr->particle_index_buffer, i_size, g_i_buffer_data);
free(g_v_buffer_data);
free(g_i_buffer_data);
}
}
struct graph_dev_gl_shader_metadata {
GLuint *program_id;
};
static void maybe_unload_shader(struct graph_dev_gl_shader_metadata *meta, GLuint *program_id)
{
if (meta->program_id && *meta->program_id != (GLuint) -1) /* Shader is currently loaded? */
glDeleteProgram(*meta->program_id); /* Unload shader */
meta->program_id = program_id;
*meta->program_id = -1;
}
struct graph_dev_gl_vertex_color_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint vertex_position_id;
GLint vertex_color_id;
};
struct graph_dev_gl_single_color_lit_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint mv_matrix_id;
GLint normal_matrix_id;
GLint vertex_position_id;
GLint vertex_normal_id;
GLint light_pos_id;
GLint color_id;
GLint in_shade_id;
GLint ambient_id;
GLint filmic_tonemapping_id;
GLint tonemapping_gain_id;
};
struct graph_dev_gl_atmosphere_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint mv_matrix_id;
GLint normal_matrix_id;
GLint vertex_position_id;
GLint vertex_normal_id;
GLint light_pos_id;
GLint color_id;
GLfloat alpha;
GLint shadow_annulus_texture_id;
GLint shadow_annulus_center_id;
GLint shadow_annulus_normal_id;
GLint shadow_annulus_radius_id;
GLint shadow_annulus_tint_color_id;
GLint ring_texture_v_id;
GLint atmosphere_brightness_id;
GLint filmic_tonemapping_id;
GLint tonemapping_gain_id;
};
struct graph_dev_gl_filled_wireframe_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint viewport_id;
GLint mvp_matrix_id;
GLint position_id;
GLint tvertex0_id;
GLint tvertex1_id;
GLint tvertex2_id;
GLint edge_mask_id;
GLint line_color_id;
GLint triangle_color_id;
};
struct graph_dev_gl_trans_wireframe_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint mv_matrix_id;
GLint normal_matrix_id;
GLint vertex_position_id;
GLint vertex_normal_id;
GLint color_id;
GLint clip_sphere_id;
GLint clip_sphere_radius_fade_id;
};
struct graph_dev_gl_single_color_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint vertex_position_id;
GLint color_id;
};
struct graph_dev_gl_line_single_color_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint viewport_id;
GLint multi_one_id;
GLint vertex_position_id;
GLint line_vertex0_id;
GLint line_vertex1_id;
GLint dot_size_id;
GLint dot_pitch_id;
GLint line_color_id;
};
struct graph_dev_gl_point_cloud_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint vertex_position_id;
GLint point_size_id;
GLint color_id;
GLint time_id;
};
struct graph_dev_gl_skybox_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_id;
GLint vertex_id;
GLint texture_id;
GLuint cube_texture_id;
GLint filmic_tonemapping_id;
GLint tonemapping_gain_id;
};
struct graph_dev_gl_color_by_w_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_id;
GLint position_id;
GLint near_color_id;
GLint near_w_id;
GLint center_color_id;
GLint center_w_id;
GLint far_color_id;
GLint far_w_id;
};
struct graph_dev_gl_textured_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint mv_matrix_id;
GLint normal_matrix_id;
GLint vertex_position_id;
GLint vertex_normal_id;
GLint vertex_tangent_id;
GLint vertex_bitangent_id;
GLint tint_color_id;
GLint texture_coord_id;
GLint texture_2d_id;
GLint emit_texture_2d_id;
GLint emit_intensity_id;
GLint texture_cubemap_id;
GLint normalmap_cubemap_id;
GLint normalmap_id;
GLint light_pos_id;
GLint specular_power_id;
GLint specular_intensity_id;
GLint ambient_id;
GLint filmic_tonemapping_id;
GLint tonemapping_gain_id;
GLint shadow_sphere_id;
GLint shadow_annulus_texture_id;
GLint shadow_annulus_center_id;
GLint shadow_annulus_normal_id;
GLint shadow_annulus_radius_id;
GLint shadow_annulus_tint_color_id;
GLint ring_texture_v_id;
GLint ring_inner_radius_id;
GLint ring_outer_radius_id;
GLint invert; /* used by alpha_by_normal shader */
GLint in_shade;
GLint water_color; /* Used for specular calculations by planet shader */
GLint sun_color; /* Used for specular calculations by planet shader */
GLint u1v1; /* Used by planetary lightning shader */
GLint texture_width; /* Used by planetary lightning shader */
};
struct clip_sphere_data {
union vec3 eye_pos;
float r;
float radius_fade;
};
struct shadow_sphere_data {
union vec3 eye_pos;
float r;
};
struct shadow_annulus_data {
GLuint texture_id;
union vec3 eye_pos;
float r1, r2;
struct sng_color tint_color;
float alpha;
};
struct graph_dev_gl_textured_particle_shader {
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint camera_up_vec_id;
GLint camera_right_vec_id;
GLint time_id;
GLint radius_id;
GLint multi_one_id;
GLint start_position_id;
GLint start_tint_color_id;
GLint start_apm_id;
GLint end_position_id;
GLint end_tint_color_id;
GLint end_apm_id;
GLint texture_id; /* param to vertex shader */
GLint filmic_tonemapping_id;
GLint tonemapping_gain_id;
};
struct graph_dev_gl_fs_effect_shader { /* For full screen effect shaders */
struct graph_dev_gl_shader_metadata meta;
GLuint program_id;
GLint mvp_matrix_id;
GLint vertex_position_id;
GLint texture_coord_id;
GLint tint_color_id;
GLint viewport_id;
GLint texture0_id;
GLint texture1_id;
GLint texture2_id;
};
struct fbo_target {
GLuint fbo;
GLuint color0_texture;
GLuint color0_buffer;
GLuint depth_buffer;
int samples;
int width;
int height;
};
struct graph_dev_smaa_effect {
struct fbo_target edge_target;
struct fbo_target blend_target;
struct graph_dev_gl_fs_effect_shader edge_shader;
struct graph_dev_gl_fs_effect_shader blend_shader;
struct graph_dev_gl_fs_effect_shader neighborhood_shader;
GLuint area_tex;
GLuint search_tex;
};
/* store all the shader parameters */
static struct graph_dev_gl_single_color_lit_shader single_color_lit_shader;
static struct graph_dev_gl_atmosphere_shader atmosphere_shader;
static struct graph_dev_gl_atmosphere_shader atmosphere_with_annulus_shadow_shader;
static struct graph_dev_gl_trans_wireframe_shader trans_wireframe_shader;
static struct graph_dev_gl_trans_wireframe_shader trans_wireframe_with_clip_sphere_shader;
static struct graph_dev_gl_filled_wireframe_shader filled_wireframe_shader;
static struct graph_dev_gl_single_color_shader single_color_shader;
static struct graph_dev_gl_line_single_color_shader line_single_color_shader;
static struct graph_dev_gl_vertex_color_shader vertex_color_shader;
static struct graph_dev_gl_point_cloud_shader point_cloud_shader;
static struct graph_dev_gl_skybox_shader skybox_shader;
static struct graph_dev_gl_color_by_w_shader color_by_w_shader;
static struct graph_dev_gl_textured_shader textured_shader;
static struct graph_dev_gl_textured_shader planetary_lightning_shader;
static struct graph_dev_gl_textured_shader warp_gate_effect_shader;
static struct graph_dev_gl_textured_shader textured_with_sphere_shadow_shader;
static struct graph_dev_gl_textured_shader textured_lit_shader;
static struct graph_dev_gl_textured_shader textured_lit_emit_shader;
static struct graph_dev_gl_textured_shader textured_lit_emit_normal_shader;
static struct graph_dev_gl_textured_shader textured_lit_normal_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_lit_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_lit_normal_map_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_shield_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_lit_with_annulus_shadow_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_normal_mapped_lit_with_annulus_shadow_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_normal_mapped_lit_with_annulus_shadow_specular_shader;
static struct graph_dev_gl_textured_shader textured_cubemap_normal_mapped_lit_specular_shader;
static struct graph_dev_gl_textured_particle_shader textured_particle_shader;
static struct graph_dev_gl_textured_shader alpha_by_normal_shader;
static struct graph_dev_gl_textured_shader textured_alpha_by_normal_shader;
static struct graph_dev_gl_fs_effect_shader fs_copy_shader;
static struct graph_dev_smaa_effect smaa_effect;
static struct fbo_target msaa = { 0 };
static struct fbo_target post_target0 = { 0 };
static struct fbo_target post_target1 = { 0 };
static struct fbo_target render_target_2d = { 0 };
struct graph_dev_primitive {
int nvertices;
GLuint vertex_buffer;
GLuint triangle_vertex_buffer;
};
static struct graph_dev_primitive cubemap_cube;
static struct graph_dev_primitive textured_unit_quad;
#define BUFFERED_VERTICES_2D 2000
#define VERTEX_BUFFER_2D_SIZE (BUFFERED_VERTICES_2D*sizeof(struct vertex_color_buffer_data))
static struct graph_dev_gl_context {
int screen_x, screen_y;
float x_scale, y_scale;
struct graph_dev_color *hue; /* current color */
int alpha_blend;
float alpha;
GLuint fbo_current;
int active_vp; /* 0=none, 1=2d, 2=3d */
int vp_x_3d, vp_y_3d, vp_width_3d, vp_height_3d;
GLuint fbo_2d;
struct mat44 ortho_2d_mvp;
int nvertex_2d;
GLbyte vertex_type_2d[BUFFERED_VERTICES_2D];
struct vertex_color_buffer_data vertex_data_2d[BUFFERED_VERTICES_2D];
GLuint vertex_buffer_2d;
struct mesh_gl_info gl_info_3d_line;
GLuint fbo_3d;
int texture_unit_active;
GLuint texture_unit_bind[4];
GLenum src_blend_func;
GLenum dest_blend_func;
GLint vp_x, vp_y;
GLsizei vp_width, vp_height;
} sgc;
#define BIND_TEXTURE(tex_unit, tex_type, tex_id) \
do { \
int tex_offset = tex_unit - GL_TEXTURE0; \
if (sgc.texture_unit_active != tex_offset) { \
glActiveTexture(tex_unit); \
sgc.texture_unit_active = tex_offset; \
} \
if (sgc.texture_unit_bind[tex_offset] != tex_id) { \
glBindTexture(tex_type, tex_id); \
sgc.texture_unit_bind[tex_offset] = tex_id; \
} \
} while (0)
#define BLEND_FUNC(src_blend, dest_blend) \
do { \
if (sgc.src_blend_func != src_blend || sgc.dest_blend_func != dest_blend) { \
glBlendFunc(src_blend, dest_blend); \
sgc.src_blend_func = src_blend; \
sgc.dest_blend_func = dest_blend; \
} \
} while (0)
#define VIEWPORT(x, y, width, height) \
do { \
if (sgc.vp_x != x || sgc.vp_y != y || sgc.vp_width != width || sgc.vp_height != height) { \
glViewport(x, y, width, height); \
sgc.vp_x = x; \
sgc.vp_y = y; \