-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesLuma.h
1632 lines (1632 loc) · 54.6 KB
/
esLuma.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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
GLint projection_id;
GLint modelview_id;
GLint position_id;
GLint normal_id;
GLint color_id;
GLint lightpos_id;
GLint ambient_id;
GLint saturate_id;
GLint specular_id;
GLint specpower_id;
GLint hsv_id;
GLint viewdist_id;
GLint opacity_id;
GLint lightness_id;
typedef struct{float x,y,z,w;} vec;
typedef struct {float m[4][4];} mat;
mat projection, view, model, modelview;
#define updateModelView() ; mMul(&modelview,&model,&view);glUniformMatrix4fv(modelview_id,1,GL_FALSE,(float*)&modelview.m[0][0]);
#define toView() ; glUniformMatrix4fv(modelview_id,1,GL_FALSE,(float*)&view.m[0][0]);
#define toModelView() ; glUniformMatrix4fv(modelview_id,1,GL_FALSE,(float*)&modelview.m[0][0]);
typedef struct
{
GLuint vid;
GLuint iid;
GLuint cid;
GLuint nid;
#ifdef MAX_MODELS
GLuint itp;
GLuint ni;
GLuint nv;
GLfloat *vertices, *normals;
GLubyte *colors;
vec pos;
GLfloat rad;
GLfloat rsq;
#endif
} ESModel;
GLuint esRand(const GLuint min, const GLuint max);
GLfloat esRandFloat(const GLfloat min, const GLfloat max);
void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
void shadeFullbrightSolid(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* lightness, GLint* opacity);
void shadeLambertSolid(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* saturate, GLint* opacity);
void shadeFullbright(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* lightness, GLint* opacity);
void shadeLambert(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* saturate, GLint* opacity);
void shadePhong(GLint* position, GLint* iprojection, GLint* imodelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* specular, GLint* specpower, GLint* hsv, GLint* viewdist, GLint* opacity);
GLuint debugShader(GLuint shader_program);
void makeAllShaders();
void makeFullbrightSolid();
void makeLambertSolid();
void makeFullbright();
void makeLambert();
void makePhong();
#define PI 3.14159265359f
#define x2PI 6.28318530718f
#define d2PI 1.57079632679f
#define DEGREE 57.2957795131f
#define RADIAN 0.01745329251f
#define RAD2DEG DEGREE
#define DEG2RAD RADIAN
#define FLOAT_MAX 9223372036854775807.f
static inline float randf();
static inline float randfc();
float randfn();
static inline float fRandFloat(const float min, const float max);
static inline int fRand(const float min, const float max);
int vec_ftoi(float f);
float smoothstep(float x){return x*x*(3.f-2.f*x);}
void vRuv(vec* v);
void vRuvN(vec* v);
void vRuvBT(vec* v);
void vRuvTA(vec* v);
void vRuvTD(vec* v);
void vCross(vec* r, const vec v1, const vec v2);
float vDot(const vec v1, const vec v2);
float vSum(const vec v);
float vSumAbs(const vec v);
void vReflect(vec* r, const vec v, const vec n);
int vEqualTol(const vec a, const vec b, const float tol);
int vEqualInt(const vec a, const vec b);
void vMin(vec* r, const vec v1, const vec v2);
void vMax(vec* r, const vec v1, const vec v2);
void vNorm(vec* v);
float vDist(const vec v1, const vec v2);
float vDistSq(const vec a, const vec b);
float vDistMh(const vec a, const vec b);
float vDistLa(const vec a, const vec b);
float vMod(const vec v);
float vMag(const vec v);
void vInv(vec* v);
void vCopy(vec* r, const vec v);
void vDir(vec* r, const vec v1, const vec v2);
void vRotX(vec* v, const float radians);
void vRotY(vec* v, const float radians);
void vRotZ(vec* v, const float radians);
void vAdd(vec* r, const vec v1, const vec v2);
void vSub(vec* r, const vec v1, const vec v2);
void vDiv(vec* r, const vec numerator, const vec denominator);
void vMul(vec* r, const vec v1, const vec v2);
void vAddS(vec* r, const vec v1, const float v2);
void vSubS(vec* r, const vec v1, const float v2);
void vDivS(vec* r, const vec v1, const float v2);
void vMulS(vec* r, const vec v1, const float v2);
void mIdent(mat *m);
void mCopy(mat *r, const mat *v);
void mMul(mat *r, const mat *a, const mat *b);
void mMulP(vec *r, const mat *a, const float x, const float y, const float z);
void mMulV(vec *r, const mat *a, const vec v);
void mScale(mat *r, const float x, const float y, const float z);
void mScale1(mat *r, const float s);
void mTranslate(mat *r, const float x, const float y, const float z);
void mRotate(mat *r, const float radians, float x, float y, float z);
void mRotX(mat *r, const float radians);
void mRotY(mat *r, const float radians);
void mRotZ(mat *r, const float radians);
void mAngleAxisRotate(mat *r, const mat view, const float xrot, const float yrot, const float zrot);
void mFrustum(mat *r, const float left, const float right, const float bottom, const float top, const float nearZ, const float farZ);
void mPerspective(mat *r, const float fovy, const float aspect, const float nearZ, const float farZ);
void mOrtho(mat *r, const float left, const float right, const float bottom, const float top, const float nearZ, const float farZ);
void mLookAt(mat *r, const vec origin, const vec unit_dir);
void mInvert(float *dst, const float *mat);
void mTranspose(mat *r, const mat *m);
void mSetViewDir(mat *r, const vec dir_norm);
void mGetViewDir(vec *r, const mat matrix);
void mGetViewX(vec *r, const mat matrix);
void mGetViewY(vec *r, const mat matrix);
void mGetViewZ(vec *r, const mat matrix);
void mSetDir(mat *r, const vec dir_norm);
void mGetDirX(vec *r, const mat matrix);
void mGetDirY(vec *r, const mat matrix);
void mGetDirZ(vec *r, const mat matrix);
void mGetPos(vec *r, const mat matrix);
void mSetPos(mat *r, const vec pos);
void mDump(const mat matrix);
float mGetRotX(const mat *m);
float mGetRotY(const mat *m);
float mGetRotZ(const mat *m);
void mSetRotX(mat *r, const float radians);
void mSetRotY(mat *r, const float radians);
void mSetRotZ(mat *r, const float radians);
GLuint esRand(const GLuint min, const GLuint max){return (rand()%(max+1-min))+min;}
GLfloat esRandFloat(const GLfloat min, const GLfloat max)
{
static GLfloat rrndmax = 1.f/(GLfloat)RAND_MAX;
return (((GLfloat)rand()) * rrndmax) * (max-min) + min;
}
void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
{
glGenBuffers(1, buffer);
glBindBuffer(target, *buffer);
glBufferData(target, datalen, data, usage);
}
void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
{
glBindBuffer(target, *buffer);
glBufferData(target, datalen, data, usage);
}
#ifdef GL_DEBUG
void GLAPIENTRY MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
{
printf("GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
type, severity, message );
}
void esDebug(const GLuint state)
{
if(state == 1)
{
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(MessageCallback, 0);
glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_FALSE);
}
else
{
glDisable(GL_DEBUG_OUTPUT);
glDisable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
}
}
#endif
#ifdef MAX_MODELS
ESModel esModelArray[MAX_MODELS];
uint esModelArray_index = 0;
uint esBoundModel = 0;
void esBindModel(const uint id)
{
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].cid);
glVertexAttribPointer(color_id, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, esModelArray[id].iid);
esBoundModel = id;
}
void esBindModelF(const uint id)
{
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].cid);
glVertexAttribPointer(color_id, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, esModelArray[id].iid);
esBoundModel = id;
}
void esRenderModel()
{
glDrawElements(GL_TRIANGLES, esModelArray[esBoundModel].ni, esModelArray[esBoundModel].itp, 0);
}
void esBindRender(const uint id)
{
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].cid);
glVertexAttribPointer(color_id, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, esModelArray[id].iid);
glDrawElements(GL_TRIANGLES, esModelArray[id].ni, esModelArray[id].itp, 0);
}
void esBindRenderS(const uint id)
{
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, esModelArray[id].iid);
glDrawElements(GL_TRIANGLES, esModelArray[id].ni, esModelArray[id].itp, 0);
}
void esBindRenderF(const uint id)
{
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, esModelArray[id].cid);
glVertexAttribPointer(color_id, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, esModelArray[id].iid);
glDrawElements(GL_TRIANGLES, esModelArray[id].ni, esModelArray[id].itp, 0);
}
#define esLoadModel(x) \
esBind(GL_ARRAY_BUFFER, &esModelArray[esModelArray_index].vid, x##_vertices, sizeof(x##_vertices[0]) * x##_numvert * 3, GL_STATIC_DRAW); \
esBind(GL_ARRAY_BUFFER, &esModelArray[esModelArray_index].nid, x##_normals, sizeof(x##_normals[0]) * x##_numvert * 3, GL_STATIC_DRAW); \
esBind(GL_ARRAY_BUFFER, &esModelArray[esModelArray_index].cid, x##_colors, sizeof(x##_colors[0]) * x##_numvert * 3, GL_STATIC_DRAW); \
esBind(GL_ELEMENT_ARRAY_BUFFER, &esModelArray[esModelArray_index].iid, x##_indices, sizeof(x##_indices[0]) * x##_numind * 3, GL_STATIC_DRAW); \
esModelArray[esModelArray_index].itp = x##_GL_TYPE; \
esModelArray[esModelArray_index].ni = x##_numind * 3; \
esModelArray_index++;
#endif
const GLchar* v0 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float lightness;\n"
"uniform vec3 color;\n"
"uniform float opacity;\n"
"attribute vec4 position;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"fragcolor = vec4(color*lightness, opacity);\n"
"gl_Position = projection * modelview * position;\n"
"}\n";
const GLchar* f0 =
"#version 100\n"
"precision highp float;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"gl_FragColor = fragcolor;\n"
"}\n";
const GLchar* v01 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float lightness;\n"
"attribute vec3 color;\n"
"uniform float opacity;\n"
"attribute vec4 position;\n"
"varying vec3 vertCol;\n"
"varying float vertOpa;\n"
"void main()\n"
"{\n"
"vertCol = color*lightness;\n"
"vertOpa = opacity;\n"
"gl_Position = projection * modelview * position;\n"
"}\n";
const GLchar* f01 =
"#version 100\n"
"precision highp float;\n"
"varying vec3 vertCol;\n"
"varying float vertOpa;\n"
"void main()\n"
"{\n"
"gl_FragColor = vec4(vertCol, vertOpa);\n"
"}\n";
#ifdef VERTEX_SHADE
const GLchar* v1 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform vec3 color;\n"
"uniform float ambient;\n"
"uniform float saturate;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vec3 vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
"vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
"fragcolor = vec4((color*ambient) + (color * min(max(dot(lightDir, vertNorm), 0.0), saturate)), opacity);\n"
"gl_Position = projection * vertPos4;\n"
"}\n";
const GLchar* v2 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float ambient;\n"
"uniform float saturate;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"attribute vec3 color;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vec3 vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
"vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
"fragcolor = vec4((color*ambient) + (color * min(max(dot(lightDir, vertNorm), 0.0), saturate)), opacity);\n"
"gl_Position = projection * vertPos4;\n"
"}\n";
const GLchar* f1 =
"#version 100\n"
"precision highp float;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"gl_FragColor = fragcolor;\n"
"}\n";
const GLchar* v3 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float ambient;\n"
"uniform float specular;\n"
"uniform float specpower;\n"
"uniform vec3 hsv;\n"
"uniform float viewdist;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"attribute vec3 color;\n"
"varying vec4 fragcolor;\n"
"vec3 rgb2hsv(vec3 c)\n"
"{\n"
"vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"
"vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
"vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
"float d = q.x - min(q.w, q.y);\n"
"float e = 1.0e-10;\n"
"return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
"}\n"
"vec3 hsv2rgb(vec3 c)\n"
"{\n"
"vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
"vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
"return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
"}\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vec3 vertPos = vertPos4.xyz / vertPos4.w;\n"
"vec3 vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
"vec3 vertCol = rgb2hsv(color);\n"
"vertCol += hsv;\n"
"vertCol = hsv2rgb(vertCol);\n"
"gl_Position = projection * vertPos4;\n"
"vec3 lightDir = normalize(lightpos - vertPos);\n"
"vec3 viewDir = normalize(-vertPos);\n"
#ifndef BLINN_PHONG
"vec3 reflectDir = reflect(-lightDir, vertNorm);\n"
#else
"vec3 halfDir = normalize(viewDir + lightDir);\n"
#endif
"float lumosity = max(dot(lightDir, vertNorm), 0.0);\n"
"vec3 spectotal = vertCol;\n"
#ifndef BLINN_PHONG
"float specAngle = max(dot(reflectDir, viewDir), 0.0);\n"
#else
"float specAngle = max(dot(halfDir, vertNorm), 0.0);\n"
#endif
"spectotal += pow(specAngle, specpower) * (vertCol*specular);\n"
"fragcolor = vec4( ((vertCol*ambient) + (spectotal * lumosity)) * clamp(1.0 - (length(vertPos)*viewdist), 0.0, 1.0), opacity);\n"
"}\n";
const GLchar* f2 =
"#version 100\n"
"precision highp float;\n"
"varying vec4 fragcolor;\n"
"void main()\n"
"{\n"
"gl_FragColor = fragcolor;\n"
"}\n";
#else
const GLchar* v1 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform vec3 color;\n"
"uniform float ambient;\n"
"uniform float saturate;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNorm;\n"
"varying vec3 vertCol;\n"
"varying float vertAmb;\n"
"varying float vertSat;\n"
"varying float vertOpa;\n"
"varying vec3 vlightPos;\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vertPos = vertPos4.xyz / vertPos4.w;\n"
"vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
"vertCol = color;\n"
"vertAmb = ambient;\n"
"vertSat = saturate;\n"
"vertOpa = opacity;\n"
"vlightPos = lightpos;\n"
"gl_Position = projection * vertPos4;\n"
"}\n";
const GLchar* v2 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float ambient;\n"
"uniform float saturate;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"attribute vec3 color;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNorm;\n"
"varying vec3 vertCol;\n"
"varying float vertAmb;\n"
"varying float vertSat;\n"
"varying float vertOpa;\n"
"varying vec3 vlightPos;\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vertPos = vertPos4.xyz / vertPos4.w;\n"
"vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
"vertCol = color;\n"
"vertAmb = ambient;\n"
"vertSat = saturate;\n"
"vertOpa = opacity;\n"
"vlightPos = lightpos;\n"
"gl_Position = projection * vertPos4;\n"
"}\n";
const GLchar* f1 =
"#version 100\n"
"precision highp float;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNorm;\n"
"varying vec3 vertCol;\n"
"varying float vertAmb;\n"
"varying float vertSat;\n"
"varying float vertOpa;\n"
"varying vec3 vlightPos;\n"
"void main()\n"
"{\n"
"vec3 lightDir = normalize(vlightPos - vertPos);\n"
"float lambertian = min(max(dot(lightDir, normalize(vertNorm)), 0.0), vertSat);\n"
"gl_FragColor = vec4((vertCol*vertAmb) + (vertCol*lambertian), vertOpa);\n"
"}\n";
const GLchar* v3 =
"#version 100\n"
"uniform mat4 modelview;\n"
"uniform mat4 projection;\n"
"uniform float ambient;\n"
"uniform float specular;\n"
"uniform float specpower;\n"
"uniform vec3 hsv;\n"
"uniform float viewdist;\n"
"uniform float opacity;\n"
"uniform vec3 lightpos;\n"
"attribute vec4 position;\n"
"attribute vec3 normal;\n"
"attribute vec3 color;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNorm;\n"
"varying vec3 vertCol;\n"
"varying float vertAmb;\n"
"varying float vertSpec;\n"
"varying float vertSpecPow;\n"
"varying float vertViewDist;\n"
"varying float vertOpa;\n"
"varying vec3 vlightPos;\n"
"vec3 rgb2hsv(vec3 c)\n"
"{\n"
"vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"
"vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
"vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
"float d = q.x - min(q.w, q.y);\n"
"float e = 1.0e-10;\n"
"return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
"}\n"
"vec3 hsv2rgb(vec3 c)\n"
"{\n"
"vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
"vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
"return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
"}\n"
"void main()\n"
"{\n"
"vec4 vertPos4 = modelview * position;\n"
"vertPos = vertPos4.xyz / vertPos4.w;\n"
"vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
"vertCol = rgb2hsv(color);\n"
"vertCol += hsv;\n"
"vertCol = hsv2rgb(vertCol);\n"
"vertAmb = ambient;\n"
"vertSpec = specular;\n"
"vertSpecPow = specpower;\n"
"vertViewDist = viewdist;\n"
"vertOpa = opacity;\n"
"vlightPos = lightpos;\n"
"gl_Position = projection * vertPos4;\n"
"}\n";
const GLchar* f2 =
"#version 100\n"
"precision highp float;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNorm;\n"
"varying vec3 vertCol;\n"
"varying float vertAmb;\n"
"varying float vertSpec;\n"
"varying float vertSpecPow;\n"
"varying float vertViewDist;\n"
"varying float vertOpa;\n"
"varying vec3 vlightPos;\n"
"void main()\n"
"{\n"
"vec3 lightDir = normalize(vlightPos - vertPos);\n"
"vec3 viewDir = normalize(-vertPos);\n"
#ifndef BLINN_PHONG
"vec3 reflectDir = reflect(-lightDir, vertNorm);\n"
#else
"vec3 halfDir = normalize(viewDir + lightDir);\n"
#endif
"float lumosity = max(dot(lightDir, vertNorm), 0.0);\n"
"vec3 specular = vertCol;\n"
#ifndef BLINN_PHONG
"float specAngle = max(dot(reflectDir, viewDir), 0.0);\n"
#else
"float specAngle = max(dot(halfDir, vertNorm), 0.0);\n"
#endif
"specular += pow(specAngle, vertSpecPow) * (vertCol*vertSpec);\n"
"gl_FragColor = vec4( ((vertCol*vertAmb) + (specular * lumosity)) * clamp(1.0 - (length(vertPos)*vertViewDist), 0.0, 1.0), vertOpa);\n"
"}\n";
#endif
GLuint shdFullbrightSolid;
GLint shdFullbrightSolid_position;
GLint shdFullbrightSolid_projection;
GLint shdFullbrightSolid_modelview;
GLint shdFullbrightSolid_color;
GLint shdFullbrightSolid_opacity;
GLint shdFullbrightSolid_lightness;
GLuint shdFullbright;
GLint shdFullbright_position;
GLint shdFullbright_projection;
GLint shdFullbright_modelview;
GLint shdFullbright_color;
GLint shdFullbright_opacity;
GLint shdFullbright_lightness;
GLuint shdLambertSolid;
GLint shdLambertSolid_position;
GLint shdLambertSolid_projection;
GLint shdLambertSolid_modelview;
GLint shdLambertSolid_lightpos;
GLint shdLambertSolid_color;
GLint shdLambertSolid_normal;
GLint shdLambertSolid_ambient;
GLint shdLambertSolid_saturate;
GLint shdLambertSolid_opacity;
GLuint shdLambert;
GLint shdLambert_position;
GLint shdLambert_projection;
GLint shdLambert_modelview;
GLint shdLambert_lightpos;
GLint shdLambert_color;
GLint shdLambert_normal;
GLint shdLambert_ambient;
GLint shdLambert_saturate;
GLint shdLambert_opacity;
GLuint shdPhong;
GLint shdPhong_position;
GLint shdPhong_projection;
GLint shdPhong_modelview;
GLint shdPhong_lightpos;
GLint shdPhong_color;
GLint shdPhong_normal;
GLint shdPhong_ambient;
GLint shdPhong_specular;
GLint shdPhong_specpower;
GLint shdPhong_hsv;
GLint shdPhong_viewdist;
GLint shdPhong_opacity;
GLuint debugShader(GLuint shader_program)
{
GLint linked;
glGetProgramiv(shader_program, GL_LINK_STATUS, &linked);
if(linked == GL_FALSE)
{
GLint infoLen = 0;
glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = malloc(sizeof(char) * infoLen);
if(infoLog != NULL)
{
glGetProgramInfoLog(shader_program, infoLen, NULL, infoLog);
printf("!!! error linking shader !!!\n%s\n", infoLog);
free(infoLog);
}
}
else
{
printf("!!! failed to link shader with no returned debug output !!!\n");
}
glDeleteProgram(shader_program);
return linked;
}
return linked;
}
void makeFullbrightSolid()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &v0, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &f0, NULL);
glCompileShader(fragmentShader);
shdFullbrightSolid = glCreateProgram();
glAttachShader(shdFullbrightSolid, vertexShader);
glAttachShader(shdFullbrightSolid, fragmentShader);
glLinkProgram(shdFullbrightSolid);
if(debugShader(shdFullbrightSolid) == GL_FALSE){return;}
shdFullbrightSolid_position = glGetAttribLocation(shdFullbrightSolid, "position");
shdFullbrightSolid_projection = glGetUniformLocation(shdFullbrightSolid, "projection");
shdFullbrightSolid_modelview = glGetUniformLocation(shdFullbrightSolid, "modelview");
shdFullbrightSolid_color = glGetUniformLocation(shdFullbrightSolid, "color");
shdFullbrightSolid_opacity = glGetUniformLocation(shdFullbrightSolid, "opacity");
shdFullbrightSolid_lightness = glGetUniformLocation(shdFullbrightSolid, "lightness");
}
void makeFullbright()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &v01, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &f01, NULL);
glCompileShader(fragmentShader);
shdFullbright = glCreateProgram();
glAttachShader(shdFullbright, vertexShader);
glAttachShader(shdFullbright, fragmentShader);
glLinkProgram(shdFullbright);
if(debugShader(shdFullbright) == GL_FALSE){return;}
shdFullbright_position = glGetAttribLocation(shdFullbright, "position");
shdFullbright_color = glGetAttribLocation(shdFullbright, "color");
shdFullbright_projection = glGetUniformLocation(shdFullbright, "projection");
shdFullbright_modelview = glGetUniformLocation(shdFullbright, "modelview");
shdFullbright_opacity = glGetUniformLocation(shdFullbright, "opacity");
shdFullbright_lightness = glGetUniformLocation(shdFullbright, "lightness");
}
void makeLambertSolid()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &v1, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &f1, NULL);
glCompileShader(fragmentShader);
shdLambertSolid = glCreateProgram();
glAttachShader(shdLambertSolid, vertexShader);
glAttachShader(shdLambertSolid, fragmentShader);
glLinkProgram(shdLambertSolid);
if(debugShader(shdLambertSolid) == GL_FALSE){return;}
shdLambertSolid_position = glGetAttribLocation(shdLambertSolid, "position");
shdLambertSolid_normal = glGetAttribLocation(shdLambertSolid, "normal");
shdLambertSolid_projection = glGetUniformLocation(shdLambertSolid, "projection");
shdLambertSolid_modelview = glGetUniformLocation(shdLambertSolid, "modelview");
shdLambertSolid_lightpos = glGetUniformLocation(shdLambertSolid, "lightpos");
shdLambertSolid_color = glGetUniformLocation(shdLambertSolid, "color");
shdLambertSolid_ambient = glGetUniformLocation(shdLambertSolid, "ambient");
shdLambertSolid_saturate = glGetUniformLocation(shdLambertSolid, "saturate");
shdLambertSolid_opacity = glGetUniformLocation(shdLambertSolid, "opacity");
}
void makeLambert()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &v2, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &f1, NULL);
glCompileShader(fragmentShader);
shdLambert = glCreateProgram();
glAttachShader(shdLambert, vertexShader);
glAttachShader(shdLambert, fragmentShader);
glLinkProgram(shdLambert);
if(debugShader(shdLambert) == GL_FALSE){return;}
shdLambert_position = glGetAttribLocation(shdLambert, "position");
shdLambert_normal = glGetAttribLocation(shdLambert, "normal");
shdLambert_color = glGetAttribLocation(shdLambert, "color");
shdLambert_projection = glGetUniformLocation(shdLambert, "projection");
shdLambert_modelview = glGetUniformLocation(shdLambert, "modelview");
shdLambert_lightpos = glGetUniformLocation(shdLambert, "lightpos");
shdLambert_ambient = glGetUniformLocation(shdLambert, "ambient");
shdLambert_saturate = glGetUniformLocation(shdLambert, "saturate");
shdLambert_opacity = glGetUniformLocation(shdLambert, "opacity");
}
void makePhong()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &v3, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &f2, NULL);
glCompileShader(fragmentShader);
shdPhong = glCreateProgram();
glAttachShader(shdPhong, vertexShader);
glAttachShader(shdPhong, fragmentShader);
glLinkProgram(shdPhong);
if(debugShader(shdPhong) == GL_FALSE){return;}
shdPhong_position = glGetAttribLocation(shdPhong, "position");
shdPhong_normal = glGetAttribLocation(shdPhong, "normal");
shdPhong_color = glGetAttribLocation(shdPhong, "color");
shdPhong_projection = glGetUniformLocation(shdPhong, "projection");
shdPhong_modelview = glGetUniformLocation(shdPhong, "modelview");
shdPhong_lightpos = glGetUniformLocation(shdPhong, "lightpos");
shdPhong_ambient = glGetUniformLocation(shdPhong, "ambient");
shdPhong_specular = glGetUniformLocation(shdPhong, "specular");
shdPhong_specpower = glGetUniformLocation(shdPhong, "specpower");
shdPhong_hsv = glGetUniformLocation(shdPhong, "hsv");
shdPhong_viewdist = glGetUniformLocation(shdPhong, "viewdist");
shdPhong_opacity = glGetUniformLocation(shdPhong, "opacity");
}
void makeAllShaders()
{
makeFullbrightSolid();
makeFullbright();
makeLambertSolid();
makeLambert();
makePhong();
}
void shadeFullbrightSolid(GLint* position, GLint* iprojection, GLint* imodelview, GLint* color, GLint* lightness, GLint* opacity)
{
*position = shdFullbrightSolid_position;
*iprojection = shdFullbrightSolid_projection;
*imodelview = shdFullbrightSolid_modelview;
*color = shdFullbrightSolid_color;
*opacity = shdFullbrightSolid_opacity;
*lightness = shdFullbrightSolid_lightness;
glUseProgram(shdFullbrightSolid);
}
void shadeFullbright(GLint* position, GLint* iprojection, GLint* imodelview, GLint* color, GLint* lightness, GLint* opacity)
{
*position = shdFullbright_position;
*iprojection = shdFullbright_projection;
*imodelview = shdFullbright_modelview;
*color = shdFullbright_color;
*opacity = shdFullbright_opacity;
*lightness = shdFullbright_lightness;
glUseProgram(shdFullbright);
}
void shadeLambertSolid(GLint* position, GLint* iprojection, GLint* imodelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* saturate, GLint* opacity)
{
*position = shdLambertSolid_position;
*iprojection = shdLambertSolid_projection;
*imodelview = shdLambertSolid_modelview;
*lightpos = shdLambertSolid_lightpos;
*color = shdLambertSolid_color;
*normal = shdLambertSolid_normal;
*ambient = shdLambertSolid_ambient;
*saturate = shdLambertSolid_saturate;
*opacity = shdLambertSolid_opacity;
glUseProgram(shdLambertSolid);
}
void shadeLambert(GLint* position, GLint* iprojection, GLint* imodelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* saturate, GLint* opacity)
{
*position = shdLambert_position;
*iprojection = shdLambert_projection;
*imodelview = shdLambert_modelview;
*lightpos = shdLambert_lightpos;
*color = shdLambert_color;
*normal = shdLambert_normal;
*ambient = shdLambert_ambient;
*saturate = shdLambert_saturate;
*opacity = shdLambert_opacity;
glUseProgram(shdLambert);
}
void shadePhong(GLint* position, GLint* iprojection, GLint* imodelview, GLint* lightpos, GLint* normal, GLint* color, GLint* ambient, GLint* specular, GLint* specpower, GLint* hsv, GLint* viewdist, GLint* opacity)
{
*position = shdPhong_position;
*iprojection = shdPhong_projection;
*imodelview = shdPhong_modelview;
*lightpos = shdPhong_lightpos;
*color = shdPhong_color;
*normal = shdPhong_normal;
*ambient = shdPhong_ambient;
*specular = shdPhong_specular;
*specpower = shdPhong_specpower;
*hsv = shdPhong_hsv;
*viewdist = shdPhong_viewdist;
*opacity = shdPhong_opacity;
glUseProgram(shdPhong);
}
#include <math.h>
#include <string.h>
int srandfq = 74235;
static inline void srandf(const int seed){srandfq = seed;}
static inline float randf()
{
srandfq *= 16807;
return (float)(srandfq & 0x7FFFFFFF) * 4.6566129e-010f;
}
static inline float randfc()
{
srandfq *= 16807;
return ((float)(srandfq)) * 4.6566129e-010f;
}
static inline float fRandFloat(const float min, const float max)
{
return min + randf() * (max-min);
}
static inline int fRand(const float min, const float max)
{
return (int)((min + randf() * (max-min))+0.5f);
}
float randfn()
{
float u = randfc();
float v = randfc();
float r = u * u + v * v;
while(r == 0.f || r > 1.f)
{
u = randfc();
v = randfc();
r = u * u + v * v;
}
return u * sqrtf(-2.f * logf(r) / r);
}
void vRuv(vec* v)
{
v->x = randfc();
v->y = randfc();
v->z = randfc();
}
void vRuvN(vec* v)
{
v->x = randfn();
v->y = randfn();
v->z = randfn();
}
void vRuvBT(vec* v)
{
const float y = acosf(randfc()) - d2PI;
const float p = x2PI * randf();
v->x = cosf(y) * cosf(p);
v->y = cosf(y) * sinf(p);
v->z = sinf(y);
}
void vRuvTA(vec* v)
{
while(1)
{
v->x = randfc();
v->y = randfc();
v->z = randfc();
const float len = vMag(*v);
if(len <= 1.0f){return;}
}
}
void vRuvTD(vec* v)
{
v->x = sinf((randf() * x2PI) - PI);
v->y = cosf((randf() * x2PI) - PI);
v->z = randfc();
}
void vCross(vec* r, const vec v1, const vec v2)
{
r->x = (v1.y * v2.z) - (v2.y * v1.z);
r->y = -((v1.x * v2.z) - (v2.x * v1.z));
r->z = (v1.x * v2.y) - (v2.x * v1.y);
}
float vDot(const vec v1, const vec v2)
{
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}
float vSum(const vec v)
{
return v.x + v.y + v.z;
}
float vSumAbs(const vec v)
{
return fabs(v.x) + fabs(v.y) + fabs(v.z);
}
void vInv(vec* v)
{
v->x = -v->x;
v->y = -v->y;
v->z = -v->z;
}
void vNorm(vec* v)
{
const float len = 1.f/sqrtf(v->x*v->x + v->y*v->y + v->z*v->z);
v->x *= len;
v->y *= len;
v->z *= len;
}
float vDist(const vec v1, const vec v2)
{
const float xm = (v1.x - v2.x);
const float ym = (v1.y - v2.y);
const float zm = (v1.z - v2.z);
return sqrtf(xm*xm + ym*ym + zm*zm);
}
float vDistSq(const vec a, const vec b)
{
const float xm = (a.x - b.x);
const float ym = (a.y - b.y);
const float zm = (a.z - b.z);
return xm*xm + ym*ym + zm*zm;
}
float vDistMh(const vec a, const vec b)
{
return (a.x - b.x) + (a.y - b.y) + (a.z - b.z);
}
float vDistLa(const vec v1, const vec v2)
{
const float xm = fabsf(v1.x - v2.x);
const float ym = fabsf(v1.y - v2.y);
const float zm = fabsf(v1.z - v2.z);
float dist = xm;
if(ym > dist)
dist = ym;
if(zm > dist)
dist = zm;
return dist;
}
void vReflect(vec* r, const vec v, const vec n)
{
const float angle = vDot(v, n);
r->x = v.x - (2.f * n.x) * angle;
r->y = v.y - (2.f * n.y) * angle;
r->z = v.z - (2.f * n.z) * angle;
}
int vEqualTol(const vec a, const vec b, const float tol)
{
return a.x >= b.x - tol && a.x <= b.x + tol &&
a.y >= b.y - tol && a.y <= b.y + tol &&
a.z >= b.z - tol && a.z <= b.z + tol;
}
void vMin(vec* r, const vec v1, const vec v2)
{
if(v1.x < v2.x && v1.y < v2.y && v1.z < v2.z)
{
r->x = v1.x;
r->y = v1.y;
r->z = v1.z;
}
r->x = v2.x;
r->y = v2.y;
r->z = v2.z;