-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.rs
937 lines (842 loc) · 25.4 KB
/
shaders.rs
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
use super::*;
#[derive(Clone,Debug,Copy)]
pub enum RenderMode{
Default=0,
Color,
Tex0,
Tex1,
Tex0MulTex1,
Tex0BlendTex1,
Normal,
TexCoord0,
Light,
SphericalHarmonicLight,
FogTex0,
Count
// pub const Count:usize=6;
}
pub const RenderModeCount:usize=RenderMode::Count as usize;
pub static mut g_shader_program:[GLuint;RenderModeCount]=[-1i32 as uint;RenderModeCount];
pub static mut g_shader_uniforms:[UniformTable;RenderModeCount]=[
UniformTable{
mat_proj:-1,
mat_model_view:-1,
mat_model_view_proj:-1,
mat_color:-1,
mat_env_map:-1,
tex0:-1,
tex1:-1,
cam_in_obj:-1,
ambient:-1,
diffuse_dx:-1,
diffuse_dy:-1,
diffuse_dz:-1,
specular_color:-1,
specular_dir:-1,
sky_dir:-1,
test_vec_4:-1,
fog_color:-1,
fog_falloff:-1,
light0_pos_r:-1,
light0_color:-1,
};RenderModeCount
];
pub static mut g_pixel_shader:[GLuint;RenderModeCount]=[-1i32 as uint;RenderModeCount];
pub static mut g_vertex_shader:[GLuint;RenderModeCount]=[-1i32 as uint;RenderModeCount];
#[derive(Copy,Clone,Debug)]
#[repr(u32)]
pub enum VertexAttrIndex {
VAI_pos =0x0000,
VAI_color,
VAI_norm,
VAI_tex0,
VAI_tex1,
VAI_joints,
VAI_weights,
VAI_tangent,
VAI_binormal,
VAI_count
}
impl Into<u32> for VertexAttrIndex{
fn into(self)->u32{ self as u32}
}
pub unsafe fn get_attrib_location(shader_prog:GLuint, name:&str)->GLint {
let r=glGetAttribLocation(shader_prog, c_str(name));
println!("get attrib location {:?}={:?}", name, r);
r
}
pub unsafe fn get_uniform_location(shader_prog:GLuint, name:&str)->GLint {
let r=glGetUniformLocation(shader_prog, c_str(name));
println!("get uniform_location location {:?}={:?}", name, r);
r
}
pub unsafe fn create_and_compile_shader(shader_type:GLenum, source:&str) ->GLuint
{
android_logw_str("create_and_compile_shader\0");
let shader = glCreateShader(shader_type );
android_logw_string(&format!("shader={:?}\0",shader));
let sources_as_c_str:[*const c_char;1]=[c_str(source)];
android_logw_str("set shader source..\0");
glShaderSource(shader, 1 as GLsizei, &sources_as_c_str as *const *const c_char, 0 as *const c_int/*(&length[0])*/);
android_logw_str("compile..\0");
glCompileShader(shader);
let status:c_int=0;
android_logw_str("get status..\0");
glGetShaderiv(shader,GL_COMPILE_STATUS,&status);
android_logw_str("got status\0");
android_logw_string(&format!("status = {:?}\0",status));
if status==GL_FALSE as GLint
{
android_logw_string(&format!("failed, getting log..\0"));
let compile_log:[c_char;512]=[0 as c_char;512]; //int len;
let log_len:c_int=0;
glGetShaderInfoLog(shader, 512,&log_len as *const c_int, &compile_log[0]);
android_logw_string(&format!("Compile Shader Failed: logsize={:?}\0",
log_len));
println!("compile shader {:?} failed: \n{:?}\n", shader, compile_log[0]);
// println!("compile shader {:?} failed: \n{:?}\n", shader,
// CString::new((&compile_log[0]) as *const c_char).as_str());
println!("TODO here 168");
println!("{}",source);
println!("error {:?}", CStr::from_ptr(&compile_log[0]));
// android_logw(
// match c_str::CString::new(&compile_log[0],false).as_str() {
// Some(s)=>c_str(s),
// None=>c_str("couldn't unwrap error lol"),
// }
// );
for i in 0..log_len {
// android_logw_string(&format!("{:?}",compile_log[i]));
}
emscripten::alert(&format!("failed to build {}\0",source));
panic!();
}
else {
println!("create shader{:?} - compile suceeded\n", shader);
android_logw_string(&format!("create shader{:?} - compile suceeded\n\0", shader));
}
android_logw_str("create shader-done\0");
shader
}
#[derive(Clone,Debug,Copy)]
pub struct VertexAttr {
pub pos:GLint,pub color:GLint,pub norm:GLint,pub tex0:GLint,pub tex1:GLint,pub joints:GLint,pub weights:GLint,pub tangent:GLint,pub binormal:GLint,
}
pub static g_vertex_attr_empty:VertexAttr=VertexAttr{
pos:-1,color:-1,norm:-1,tex0:-1,tex1:-1,joints:-1,weights:-1,tangent:-1,binormal:-1
};
pub static mut g_vertex_shader_attrib:[VertexAttr;RenderModeCount]=[VertexAttr{
pos:-1,color:-1,norm:-1,tex0:-1,tex1:-1,joints:-1,weights:-1,tangent:-1,binormal:-1};RenderModeCount];
//g_vertex_attr_empty;
pub static mut g_shader_uniforms_main:UniformTable=UniformTable{
mat_proj:-1,
mat_model_view:-1,
mat_model_view_proj:-1,
mat_color:-1,
mat_env_map:-1,
tex0:-1,
tex1:-1,
cam_in_obj:-1,
ambient:-1,
diffuse_dx:-1,
diffuse_dy:-1,
diffuse_dz:-1,
specular_color:-1,
specular_dir:-1,
sky_dir:-1,
test_vec_4:-1,
fog_color:-1,
fog_falloff:-1,
light0_pos_r:-1,
light0_color:-1,
};
pub static mut g_shader_uniforms_debug:UniformTable=UniformTable{
mat_proj:-1,
mat_model_view:-1,
mat_model_view_proj:-1,
mat_color:-1,
mat_env_map:-1,
tex0:-1,
tex1:-1,
cam_in_obj:-1,
ambient:-1,
diffuse_dx:-1,
diffuse_dy:-1,
diffuse_dz:-1,
specular_color:-1,
specular_dir:-1,
sky_dir:-1,
test_vec_4:-1,
fog_color:-1,
fog_falloff:-1,
light0_pos_r:-1,
light0_color:-1,
};
//g_uniform_table_empty;
// Paired pixel and vertex shaders.
pub type VertexShader=GLuint;
pub type PixelShader=GLuint;
pub type ShaderProgram=GLuint;
extern {pub fn bind_attrib_locations(prog:c_uint);}
pub unsafe fn create_shader_program(
pixelShaderSource:&str,
vertexShaderSource:&str)->(PixelShader,VertexShader,ShaderProgram)
{
android_logw(c_str("create_shader_program\0"));
let pixelShaderOut = create_and_compile_shader(GL_FRAGMENT_SHADER, pixelShaderSource);
let vertexShaderOut = create_and_compile_shader(GL_VERTEX_SHADER, vertexShaderSource);
let prog = glCreateProgram();
assert!(prog>=0);
android_logw(c_str("bind attrib locations\0"));
// assign attribute names before linking
for &x in [
(VertexAttrIndex::VAI_pos, "a_pos\0"),
(VertexAttrIndex::VAI_color, "a_color\0"),
(VertexAttrIndex::VAI_norm, "a_norm\0"),
(VertexAttrIndex::VAI_tex0, "a_tex0\0"),
(VertexAttrIndex::VAI_tex1, "a_tex1\0"),
(VertexAttrIndex::VAI_joints, "a_joints\0"),
(VertexAttrIndex::VAI_weights, "a_weights\0"),
(VertexAttrIndex::VAI_tangent, "a_tangent\0"),
(VertexAttrIndex::VAI_binormal, "a_binormal\0"),
].iter() {glBindAttribLocation(prog, x.0 as GLuint, c_str(x.1));}
glAttachShader(prog, pixelShaderOut);
glAttachShader(prog, vertexShaderOut);
println!("linking verteshader{:?}, pixelshader{:?} to program{:?}\n", vertexShaderOut, pixelShaderOut, prog);
glLinkProgram(prog);
let mut lstatus:GLint=0;
glGetProgramiv(prog,GL_LINK_STATUS,(&lstatus) as *const GLint);
if lstatus==(GL_FALSE as i32)
{
let mut buffer=[0 as GLchar;1024];
let mut len:GLint=0;
glGetProgramInfoLog(prog,1024,&len,&buffer[0]);
println!("link program failed: {:?}",lstatus);
println!("todo\n");
// println!("{:?}",CString::new(&buffer[0]));
panic!();
} else {
assert!(lstatus==(GL_TRUE as i32));
println!("link ok");
}
(pixelShaderOut,vertexShaderOut,prog)
}
//TODO: split into default uniforms, default vertex, default vertex-shader-out
// TODO [cfg OPENGL_ES ..]
static shader_prefix_desktop:&'static str=&"
#version 120 \n\
#define highp \n\
#define mediump \n\
#define lowp \n\
";
static vertex_shader_prefix_gles:&'static str=&"
#version 100 \n\
precision highp float; \n\
";
//#version 100 \n\
static pixel_shader_prefix_gles:&'static str=&"
precision mediump float; \n\
";
static ps_vs_interface0:&'static str=&"
varying highp vec4 v_pos;
varying highp vec4 v_color;
varying highp vec3 v_norm;
varying highp vec2 v_tex0;
varying highp vec3 v_tex1uvw;
varying highp vec4 v_tangent;
varying highp vec4 v_binormal;
";
static ps_vertex_format0:&'static str=&"
attribute vec3 a_pos;
attribute vec2 a_tex0;
attribute vec4 a_color;
attribute vec3 a_norm;
";
static g_VS_Default:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 epos = uMatModelView * pos4;
vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;
vec4 spos=uMatProj * epos;
gl_Position = spos;
v_pos = posw;
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = enorm;
}
";
/// replacement debug vertex shader - dont apply transformations, just view vertices..
static g_VS_PassThru:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 epos = uMatModelView * posw;
vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;
vec4 spos=uMatProj * epos;
gl_Position = vec4(posw.xyz,1.0);
v_pos = epos;
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = enorm;
}
";
static g_vs_uniforms:&'static str="
uniform mat4 uMatProj;
uniform mat4 uMatModelView;
";
static g_VS_PassThruTweak:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 epos = uMatModelView * posw;
vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;
vec4 spos=uMatProj * epos;
gl_Position = spos;
v_pos = posw;
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = enorm;
}
";
/// replacement debug vertex shader - dont apply perspective, just view transformed models
static g_VS_RotTransPers:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 eye_pos = uMatModelView * posw;
vec3 eye_norm = normalize((uMatModelView * vec4(a_norm.xyz,0.0)).xyz);
vec4 screen_pos=uMatProj * eye_pos;
gl_Position = screen_pos;
v_pos = vec4(eye_pos.xyz,1.0);
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = eye_norm;
}
";
/// replacement debug vertex shader - dont apply perspective, just view transformed models
static g_VS_Translate2d:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 epos = uMatModelView * posw;
vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;
vec4 spos=uMatProj * epos;
gl_Position = vec4(posw.xyz,1.0)+uMatModelView[3].xyzw;
v_pos = posw;
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = enorm;
}
";
static g_VS_Persp:&'static str="
void main() {
vec4 posw = vec4(a_pos.xyz,1.0);
vec4 epos = uMatModelView * posw;
vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;
vec4 spos=uMatProj * epos;
gl_Position = spos;
v_pos = posw;
v_color = a_color;
v_tex0 = a_tex0;
v_tex1uvw = a_pos.xyz;
v_norm = enorm;
}
";
/*
cases:
VSO:
static scene
animation,3bone
PS:
2textures
3textures
*/
// sanity check debug, checking that the andoir build does this ok..
static g_PS_ConcatForAndroid:&'static str= &"
precision mediump float;
varying highp vec4 v_pos;
varying highp vec4 v_color;
varying highp vec3 v_norm;
varying highp vec2 v_tex0;
varying highp vec3 v_tex1uvw;\n\
varying highp vec4 v_tangent;\n\
varying highp vec4 v_binormal;\n\
uniform sampler2D uTex0;\n\
uniform sampler2D uTex1;\n\
uniform vec4 uSpecularDir;\n\
uniform float uSpecularPower;\n\
uniform vec4 uSpecularColor;\n\
uniform vec4 uFogColor;\n\
uniform vec4 uFogFalloff;\n\
uniform vec4 uAmbient;\n\
uniform vec4 uDiffuseDX;\n\
uniform vec4 uDiffuseDY;\n\
uniform vec4 uDiffuseDZ;\n\
\n\
uniform vec4 uLightPos;\n\
uniform vec4 uLightColor;\n\
uniform vec4 uLightFalloff;\n\
vec4 applyFog(vec3 pos, vec4 color){\n\
return mix(color,uFogColor, clamp(-uFogFalloff.x-pos.z*uFogFalloff.y,0.0,1.0));\n\
}\n\
vec4 pointlight(vec3 pos, vec3 norm,vec3 lpos, vec4 color, vec4 falloff) {\n\
vec3 dv=lpos-pos;\n\
float d2=sqrt(dot(dv,dv));\n\
float f=clamp( 1.0-(d2/falloff.x),0.0,1.0);\n\
vec3 lv=normalize(dv);\n\
return clamp(dot(lv,norm),0.0,1.0) * f*color;\n\
}\n\
void main() { \n\
float inva=(v_color.w),a=(1.0-v_color.w);\n\
vec4 t0=texture2D(uTex0, v_tex0);\n\
vec4 t1=texture2D(uTex1, v_tex0);\n\
float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;\n\
float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;\n\
float highlight=max(0.0,dot(v_norm,uSpecularDir.xyz));\n\
highlight=(highlight*highlight);highlight=highlight*highlight;\n\
vec4 surfaceColor=mix(t0,t1,v_color.w);\n\
vec4 surfaceSpec=clamp(4.0*(surfaceColor-vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));\n\
vec4 spec=highlight*uSpecularColor*surfaceSpec;\n\
vec4 diff=uAmbient+v_norm.x*uDiffuseDX+v_norm.y*uDiffuseDY+v_norm.z*uDiffuseDZ;\n\
float lx=0.5,ly=0.5;\n\
diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,ly,-1.0), vec4(1.0,0.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));\n\
diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,-ly,-1.0), vec4(0.0,1.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));\n\
diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,-ly,-1.0), vec4(0.0,0.0,1.0,0.0),vec4(1.0,0.0,0.0,0.0));\n\
diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,ly,-1.0), vec4(0.5,0.0,0.5,0.0),vec4(1.0,0.0,0.0,0.0));\n\
// gl_FragColor =applyFog(v_pos.xyz,surfaceColor*diff*vec4(v_color.xyz,0.0)*2.0+spec);\n\
gl_FragColor =vec4(v_norm.xyz,0.0)*0.5+vec4(0.5,0.5,0.5,1.0);\n\
}";
static g_PS_Alpha:&'static str= "
uniform sampler2D uTex0;
uniform sampler2D uTex1;
uniform vec4 uSpecularDir;
uniform float uSpecularPower;
uniform vec4 uSpecularColor;
uniform vec4 uFogColor;
uniform vec4 uFogFalloff;
uniform vec4 uAmbient;
uniform vec4 uDiffuseDX;
uniform vec4 uDiffuseDY;
uniform vec4 uDiffuseDZ;
uniform vec4 uLightPos;
uniform vec4 uLightColor;
uniform vec4 uLightFalloff;
void main() {
float inva=(v_color.w),a=(1.0-v_color.w);
vec4 t0=texture2D(uTex0, v_tex0);
vec4 t1=texture2D(uTex1, v_tex0);
float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;
float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;
float highlight=max(0.0,dot(v_norm,uSpecularDir.xyz));
highlight=(highlight*highlight);highlight=highlight*highlight;
vec4 surfaceColor=mix(t0,t1,v_color.w);
vec4 surfaceSpec=clamp(4.0*(surfaceColor-vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));
vec4 spec=highlight*uSpecularColor*surfaceSpec;
vec4 diff=uAmbient+v_norm.x*uDiffuseDX+v_norm.y*uDiffuseDY+v_norm.z*uDiffuseDZ;
float lx=0.5,ly=0.5;
gl_FragColor =applyFog(v_pos.xyz,surfaceColor*evalAllLight()*vec4(v_color.xyz,0.0)*2.0+spec);
// gl_FragColor =vec4(v_norm.xyz,0.0)*0.5+vec4(0.5,0.5,0.5,1.0)+vec4(v_tex0,0.0,0.0);
}";
// debug shader
static g_PS_Add:&'static str= "
uniform sampler2D s_tex0;
uniform sampler2D s_tex1;
uniform vec4 uSpecularDir;
uniform float uSpecularPower;
uniform vec4 uSpecularColor;
uniform vec4 uAmbient;
uniform vec4 uDiffuseDX;
uniform vec4 uDiffuseDY;
uniform vec4 uDiffuseDZ;
void main() {
float inva=(v_color.w),a=(1.0-v_color.w);
vec4 t0=texture2D(s_tex0, v_tex0);
vec4 t1=texture2D(s_tex1, v_tex0);
float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;
float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;
float highlight=max(0.0,dot(v_norm,uSpecularDir.xyz));
highlight=(highlight*highlight);highlight=highlight*highlight;
vec4 surfaceColor=t0+(t1-vec4(0.5f,0.5f,0.5f,0.0f))*v_color.w;
vec4 surfaceSpec=clamp(4.0*(surfaceColor-Vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));
vec4 spec=highlight*uSpecularColor*surfaceSpec;
vec4 diff=uAmbient+vso_norm.x*uDiffuseDX+vso_norm.y*uDiffuseDY+vso_norm.z*uDiffuseDZ;
gl_FragColor =surfaceColor*diff*vec4(v_color.xyz,0.0)*2.0+spec;
}";
static g_PS_Flat:&'static str="
void main() {
gl_FragColor= mediump vec4(0.0, 1.0, 0.0, 1.0);
}
";
static g_PS_FogTex0:&'static str="
void main() {
gl_FragColor= evalFog(getTex0());
}
";
static g_PS_Light:&'static str="
void main() {
gl_FragColor= evalAllLight();
}
";
static g_PS_SphericalHarmonicLight:&'static str="
void main() {
gl_FragColor= evalAllLight();
}
";
static g_PS_MinimumDebugAndroidCompiler:&'static str= &"
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
";
static g_PS_Tex3_AlphaMul:&'static str=&"
uniform sampler2D s_tex0;
uniform sampler2D s_tex1;
uniform sampler2D s_tex2;
uniform vec4 uSpecularDir;
uniform float uSpecularPower;
uniform vec4 uSpecularColor;
uniform vec4 uAmbient;
uniform vec4 uDiffuseDX;
uniform vec4 uDiffuseDY;
uniform vec4 uDiffuseDZ;
void main() {
float inva=(v_color.w),a=(1.0-v_color.w);
vec4 t0=texture2D(s_tex0, v_tex0);
vec4 t1=texture2D(s_tex1, v_tex0);
float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;
float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;
float highlight=max(0.0,dot(v_norm,uSpecularDir.xyz));
highlight=(highlight*highlight);highlight=highlight*highlight;
vec4 surfaceColor=mix(t0,t1, v_color.w*t1.a);
vec4 surfaceSpec=clamp(4.0*(surfaceColor-Vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));
vec4 spec=highlight*uSpecularColor*surfaceSpec;
vec4 diff=uAmbient+vso_norm.x*uDiffuseDX+vso_norm.y*uDiffuseDY+vso_norm.z*uDiffuseDZ;
gl_FragColor =surfaceColor*diff*Vec4(v_color.xyz,0.0)*2.0+spec;
}
";
static g_PS_DeclUniforms:&'static str="
uniform sampler2D s_tex0;
uniform sampler2D s_tex1;
uniform sampler2D s_tex2;
uniform vec4 uSpecularDir;
uniform float uSpecularPower;
uniform vec4 uSpecularColor;
uniform vec4 uAmbient;
uniform vec4 uDiffuseDX;
uniform vec4 uDiffuseDY;
uniform vec4 uDiffuseDZ;
uniform vec4 uFogFalloff;
uniform vec4 uFogColor;
";
// passthrough various minimal versions
static g_PS_TexCoord0:&'static str="
void main() {
gl_FragColor =vec4(mod(v_tex0.x,1.0),mod(v_tex0.y,1.0),0.0,1.0);
}
";
static g_PS_TexCoord1:&'static str="
void main() {
gl_FragColor =vec4(mod(v_tex1uvw.x,1.0),mod(v_tex1uvw.y,1.0),mod(v_tex1uvw.z,1.0),1.0);
}
";
static g_PS_Normal:&'static str="
void main() {
gl_FragColor =vec4(v_norm*0.5+vec3(0.5,0.5,0.5),1.0);
}
";
static g_PS_Color:&'static str="
void main() {
gl_FragColor =v_color;
}
";
static g_PS_Tex0:&'static str=&"
void main() {
gl_FragColor=texture2D(s_tex0, v_tex0);
}
";
static g_PS_common:&'static str="
vec4 applyFogAt(vec3 pos,vec4 unfogged_color){
return mix(unfogged_color,uFogColor, clamp(-uFogFalloff.x-pos.z*uFogFalloff.y,0.0,1.0));
}
vec4 evalFog(vec4 surface_color){
return applyFogAt(v_pos.xyz, surface_color);
}
vec4 pointlight(vec3 pos, vec3 norm,vec3 lpos, vec4 color, vec4 falloff) {
vec3 dv=lpos-pos;
float d2=sqrt(dot(dv,dv));
float f=clamp( 1.0-(d2/falloff.x),0.0,1.0);
vec3 lv=normalize(dv);
return clamp(dot(lv,norm),0.0,1.0) * f*color;
}
// hardcoded point lights;
// todo: feed aproximation of N lights
// through SH centre and 'most-significant-nearby-pointlight'
vec4 evalPointLights(){
float lx=0.5,ly=0.5;
vec4 acc=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,ly,-1.0), vec4(1.0,0.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));
acc+=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,-ly,-1.0), vec4(0.0,1.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));
acc+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,-ly,-1.0), vec4(0.0,0.0,1.0,0.0),vec4(1.0,0.0,0.0,0.0));
acc+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,ly,-1.0), vec4(0.5,0.0,0.5,0.0),vec4(1.0,0.0,0.0,0.0));
return acc;
}
vec4 evalSphericalHarmonic(){
return uAmbient+v_norm.x*uDiffuseDX+v_norm.y*uDiffuseDY+v_norm.z*uDiffuseDZ;
}
vec4 evalAllLight(){
return evalSphericalHarmonic()+evalPointLights();
}
vec4 getTex1(){
vec3 factors=v_norm*v_norm;
vec3 fnorm=normalize(factors*factors);
return
texture2D(s_tex1, v_tex1uvw.xy)*fnorm.x+
texture2D(s_tex1, v_tex1uvw.xz)*fnorm.y+
texture2D(s_tex1, v_tex1uvw.yz)*fnorm.z;
}
vec4 getTex0(){
return texture2D(s_tex0,v_tex0);
}
";
static g_PS_Tex1Triplanar:&'static str=&"
void main() {
gl_FragColor =getTex1();
}
";
static g_PS_Tex0MulTex1:&'static str=&"
// todo: we need to know the normalization factor,
// not all textures are mid-grey.
void main() {
gl_FragColor =getTex1() * getTex0()*vec4(2.0,2.0,2.0,1.0);
}
";
static g_PS_Tex0AddTex1:&'static str=&"
// todo: we need to know the normalization factor,
// not all textures are mid-grey.
void main() {
gl_FragColor =getTex1() + getTex0()-vec4(0.5, 0.5, 0.5, 0.5);
}
";
// blend the textures with their alpha; vertex alpha controls worldblend
static g_PS_Tex0BlendTex1:&'static str=&"
void main() {
gl_FragColor =mix(getTex0(),getTex1,v_color.w);
}
";
// tex 1 blend added by vertex alpha
static g_PS_Tex0VertexBlendTex1:&'static str=&"
void main() {
vec4 tex0=getTex0;
vec4 tex1=getTex1;
gl_FragColor =mix(getTex1() , getTex0(), clamp(tex1.w+(v_color.w-0.5)*2.0 ,0.0,1.0) );
}
";
#[derive(Clone,Debug,Default,Copy)]
pub struct UniformTable {
pub mat_proj:UniformIndex,
pub mat_model_view:UniformIndex,
pub mat_model_view_proj:UniformIndex,
pub mat_color:UniformIndex,
pub mat_env_map:UniformIndex,
pub tex0:UniformIndex,
pub tex1:UniformIndex,
pub cam_in_obj:UniformIndex,
pub ambient:UniformIndex,
pub diffuse_dx:UniformIndex,
pub diffuse_dy:UniformIndex,
pub diffuse_dz:UniformIndex,
pub specular_color:UniformIndex,
pub specular_dir:UniformIndex,
pub sky_dir:UniformIndex,
pub test_vec_4:UniformIndex,
pub fog_color:UniformIndex,
pub fog_falloff:UniformIndex,
pub light0_pos_r:UniformIndex,
pub light0_color:UniformIndex,
}
pub static g_uniform_table_empty:UniformTable= UniformTable{
mat_proj:-1,
mat_model_view:-1,
mat_model_view_proj:-1,
mat_color:-1,
mat_env_map:-1,
tex0:-1,
tex1:-1,
cam_in_obj:-1,
ambient:-1,
diffuse_dx:-1,
diffuse_dy:-1,
diffuse_dz:-1,
specular_color:-1,
specular_dir:-1,
sky_dir:-1,
test_vec_4:-1,
fog_color:-1,
fog_falloff:-1,
light0_pos_r:-1,
light0_color:-1,
};
//map_shader_params(VertexAttr* vsa,UniformTable* su,int prog)
pub fn map_shader_params(prog:GLuint)->(VertexAttr,UniformTable)
{
// read attrib back from shader
// at the minute we've preset these from VAI indices,
// but leave this path for datadriven approch later
// todo: rustic macro
unsafe {
let mut num_uniforms:GLint=0;
glGetProgramiv( prog, GL_ACTIVE_UNIFORMS, &num_uniforms );
for i in 0..num_uniforms {
let mut uname:[c_char;256]=[0;256];
let mut name_len:GLint=0;
let mut unisize:GLint=0;
let mut utype:GLuint=0;
glGetActiveUniform(prog,i as GLuint, 255, &name_len, &unisize,&utype,&uname as *const c_char);
let mut uindexr=glGetUniformLocation(prog,&uname[0]);
println!("uniform {:?}index={:?}\tsize={:?}\ttype={:?}", uindexr,CStr::from_ptr(&uname[0]), unisize, utype);
}
(
VertexAttr{
pos: get_attrib_location(prog, &"a_pos\0"),
color: get_attrib_location(prog, &"a_color\0"),
norm: get_attrib_location(prog, &"a_norm\0"),
tex0: get_attrib_location(prog, &"a_tex0\0"),
tex1: get_attrib_location(prog, &"a_tex1\0"),
joints: get_attrib_location(prog, &"a_joints\0"),
weights: get_attrib_location(prog, &"a_weights\0"),
tangent: get_attrib_location(prog, &"a_binormal\0"),
binormal: get_attrib_location(prog, &"a_tangent\0")
},
UniformTable{
mat_proj:get_uniform_location(prog,&"uMatProj\0"),
mat_model_view:get_uniform_location(prog,&"uMatModelView\0"),
specular_color:get_uniform_location(prog,&"uSpecularColor\0"),
specular_dir:get_uniform_location(prog,&"uSpecularDir\0"),
ambient:get_uniform_location(prog,&"uAmbient\0"),
diffuse_dx:get_uniform_location(prog,&"uDiffuseDX\0"),
diffuse_dy:get_uniform_location(prog,&"uDiffuseDY\0"),
diffuse_dz:get_uniform_location(prog,&"uDiffuseDZ\0"),
fog_color:get_uniform_location(prog,&"uFogColor\0"),
fog_falloff:get_uniform_location(prog,&"uFogFalloff\0"),
light0_pos_r:get_uniform_location(prog,&"uLight0PosR\0"),
light0_color:get_uniform_location(prog,&"uLight0Color\0"),
..g_uniform_table_empty
}
)
}
}
#[cfg(any(target_os = "android",target_os="emscripten"))]
pub fn get_shader_prefix(st:ShaderType)->&'static str {
use ShaderType::*;
match st{
ShaderType::Pixel=>pixel_shader_prefix_gles,
ShaderType::Vertex=>vertex_shader_prefix_gles,
_=>panic!("can't deal with this case")
}
}
/*
#[cfg(not(target_os = "android"))]
fn get_shader_prefix(is_ps:int)->&'static str {
if is_ps==0 {vertex_shader_prefix_gles} else {pixel_shader_prefix_gles}
}
*/
#[cfg(any(target_os = "macos",target_os="linux"))]
pub fn get_shader_prefix(st:ShaderType)->&'static str {
shader_prefix_desktop
}
pub fn concat_shader(src:&[&str])->String{
let mut ret=String::new();
// todo- insert \n after each statement?
for (i,x) in src.iter().enumerate() {
ret.push_str(format!("//from part {}\n",i).as_str());// todo - pass name thru macro
ret.push_str(x);
ret.push_str("\n");
}
ret.push_str("\0"); // null terminatin
ret
}
pub fn create_shader_sub(mode:RenderMode, ps:&[&str], vs:&[&str]){
println!("CREATE SHADER MODE {:?} \n",mode);
let psconcat=concat_shader(ps);
let vsconcat=concat_shader(vs);
let (vsh,psh,prg)=unsafe{
create_shader_program(&psconcat,&vsconcat)
};
let (vsa,su)=map_shader_params(prg);
println!("vs={:?}",vs);
println!("su={:?}",su);
let modei=mode as usize;
unsafe{
g_shader_program[modei]=prg;
g_vertex_shader[modei]=vsh;
g_pixel_shader[modei]=psh;
g_shader_uniforms[modei]=su;
g_vertex_shader_attrib[modei]=vsa;
}
println!("CREATE SHADER MODE DONE{:?} \n",mode);
}
pub fn create_shader_sub2(mode:RenderMode, ps_main:&str){
create_shader_sub(
mode,
&[ get_shader_prefix(ShaderType::Pixel),
g_PS_DeclUniforms,
ps_vs_interface0,
g_PS_common,
ps_main],
&[get_shader_prefix(ShaderType::Vertex),
g_vs_uniforms,
ps_vertex_format0,
ps_vs_interface0,
g_VS_RotTransPers]);
}
pub fn create_shaders()
{
println!("create shaders");
//todo: vs, ps, interface, permute all with same interface
//or allow shader(vertexmode,texmode,lightmode)
create_shader_sub(
RenderMode::Default,
&[ get_shader_prefix(ShaderType::Pixel),
g_PS_DeclUniforms,
ps_vs_interface0,
g_PS_common,
g_PS_Tex0/*g_PS_Alpha*/
],
&[ get_shader_prefix(ShaderType::Vertex),
g_vs_uniforms,
ps_vertex_format0,
ps_vs_interface0,
g_VS_PassThruTweak
]);
create_shader_sub2(
RenderMode::Tex0,
g_PS_Tex1Triplanar);
create_shader_sub2(
RenderMode::Tex1,
g_PS_Tex1Triplanar);
create_shader_sub2(
RenderMode::Tex0MulTex1,
g_PS_Tex0MulTex1);
create_shader_sub2(
RenderMode::Tex0BlendTex1,
g_PS_Tex0MulTex1);
create_shader_sub2(
RenderMode::Light,
g_PS_Light);
create_shader_sub2(
RenderMode::SphericalHarmonicLight,
g_PS_SphericalHarmonicLight);
create_shader_sub2(
RenderMode::FogTex0,
g_PS_FogTex0);
create_shader_sub2(
RenderMode::Color,
g_PS_Color);
create_shader_sub2(
RenderMode::Normal,
g_PS_Normal);
create_shader_sub2(
RenderMode::TexCoord0,
g_PS_TexCoord0);
}