-
Notifications
You must be signed in to change notification settings - Fork 2
/
project.cpp
1673 lines (1529 loc) · 76.6 KB
/
project.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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 <iostream>
#include <fstream>
#include <sstream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL/SOIL.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
//Reading the obj file
//int v_num=0; //点
//int vn_num=0;//法线
//int f_num=0; //面
//int vt_num=0;//纹理坐标
void getLineNum(const std::string & sFileName, int& v_num, int& vn_num, int& vt_num, int& f_num)
{
//int v_num;
//int vn_num;
//int f_num;
std::ifstream infile(sFileName.c_str());
std::string sline;
v_num=vn_num=f_num=vt_num=0;
getline(infile,sline);
//while(getline(infile,sline))
while(sline.size()!=0)
{//
if(sline[0]=='v')
{
if(sline[1]=='n')
vn_num++;
else if(sline[1]!='t')//==' '
v_num++;
else if(sline[1]=='t')
vt_num++;
}
else if(sline[0]=='f')
f_num++;
getline(infile,sline);
}
infile.close();
std::cout<<v_num<<std::endl;
std::cout<<vn_num<<std::endl;
std::cout<<vt_num<<std::endl;
std::cout<<f_num<<std::endl;
//return f_num;
}
void readObjFile(std::string addrstr, GLfloat* Arr_read, int v_num, int vn_num, int vt_num, int f_num)
{
std::string s1;
GLfloat f2,f3,f4;
GLfloat **vArr; //点
GLfloat **vnArr;//法线
GLfloat **vtArr;//纹理坐标
//GLfloat Arr_read[f_num*18];
vArr=new GLfloat*[v_num];
for (int i=0;i<v_num;i++)
{
vArr[i]=new GLfloat[3];
}
vnArr=new GLfloat*[vn_num];
for (int i=0;i<vn_num;i++)
{
vnArr[i]=new GLfloat[3];
}
vtArr=new GLfloat*[vt_num];
for (int i=0;i<vt_num;i++)
{
vtArr[i]=new GLfloat[2];
}
std::ifstream infile(addrstr.c_str());
std::string sline;//
int ii=0,jj=0,zz=0,kk=0;
while(getline(infile,sline))
{
if(sline[0]=='v')
{
if(sline[1]=='n')//vn
{
std::istringstream sin(sline);
sin>>s1>>f2>>f3>>f4;
vnArr[ii][0]=f2;
vnArr[ii][1]=f3;
vnArr[ii][2]=f4;
ii++;
}
else if(sline[1]!='t')//v
{
std::istringstream sin(sline);
sin>>s1>>f2>>f3>>f4;
vArr[jj][0]=f2;
vArr[jj][1]=f3;
vArr[jj][2]=f4;
jj++;
}
else if(sline[1]=='t')//vt
{
std::istringstream sin(sline);
sin>>s1>>f2>>f3;
vtArr[zz][0]=f2;
vtArr[zz][1]=f3;
zz++;
}
}
if (sline[0]=='f') //面
{
std::istringstream in(sline);
GLuint a;
in>>s1;//去掉f
int i,k;
for(i=0;i<3;i++)
{
in>>s1;
//std::cout<<s1<<std::endl;
//
a=0;
for(k=0;s1[k]!='/';k++)
{
//std::cout<<k<<std::endl;
a=a*10+(s1[k]-48);
}
//std::cout<<"ok"<<std::endl;
Arr_read[24*kk+8*i+0]=vArr[a-1][0];
Arr_read[24*kk+8*i+1]=vArr[a-1][1];
Arr_read[24*kk+8*i+2]=vArr[a-1][2];
a=0;
//std::cout<<"ok"<<std::endl;
//std::cout<<"k is "<<k<<std::endl;
for(k=k+1;s1[k]!='/';k++)
{
a=a*10+(s1[k]-48);
//std::cout<<k<<std::endl;
}
Arr_read[24*kk+8*i+6]=vtArr[a-1][0];
Arr_read[24*kk+8*i+7]=vtArr[a-1][1];
a=0;
//std::cout<<"and k is "<<k<<std::endl;
for(k=k+1;s1[k];k++)
{
a=a*10+(s1[k]-48);
//std::cout<<"a : "<<a<<std::endl;
}
Arr_read[24*kk+8*i+3]=vnArr[a-1][0];
Arr_read[24*kk+8*i+4]=vnArr[a-1][1];
Arr_read[24*kk+8*i+5]=vnArr[a-1][2];
//std::cout<<"ok"<<std::endl;
}
kk++;
}
}
//return Arr_read;
}
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void do_movement();
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
// Window dimensions
const GLuint WIDTH = 3328, HEIGHT = 1872;
GLfloat mixValue = 0.2f;
bool keys[1024];
glm::mat4 view = glm::mat4(1.0f);
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2;
bool firstMouse;
GLfloat yaw = -90.0f;
GLfloat pitch = 0.0f;
GLfloat aspect = 45.0f;
const char *vs_shadow = "#version 450 core\n"
"layout (location=0) in vec3 aPos;\n"
"uniform mat4 lightSpace;\n"
"uniform mat4 model;\n"
"void main()\n"
"{\n"
" gl_Position = lightSpace * model * vec4(aPos, 1.0);\n"
"}\0";
const char *fs_shadow = "#version 450 core\n"
"void main()\n"
"{\n"
"}\0";
const char *vs_light = "#version 450 core\n"
"layout (location = 0) in vec3 aPos;\n"
//"out vec3 FragPos;\n"
//out vec3 Normal;
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
//" FragPos = vec3(model * vec4(aPos, 1.0));\n"
//Normal = aNormal;
" gl_Position = projection * view * model * vec4(aPos, 1.0);\n"
"}\0";
//片段着色器 light.frag
const char *fs_light = "#version 450 core\n"
"out vec4 FragColor;\n"
//uniform vec3 lightColor;
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.9f, 0.8f, 1.0);\n"
"}\0";
const char *vs_phone ="#version 450 core\n"
"layout (location = 0) in vec3 vposition;\n"
"layout (location = 1) in vec3 vnormal;\n"
//"layout (location = 2) in vec3 vcolor;\n"
"layout (location = 2) in vec2 texCoord;\n"
"#define NUM_POINT_LIGHTS 2\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"uniform mat4 lightSpace[NUM_POINT_LIGHTS];\n" //added
"out vec2 TexCoord;\n"
"out vec3 position;\n"
"out vec3 Normal;\n"
"out vec4 Pos_lightspace[NUM_POINT_LIGHTS];\n" //added
//"out vec4 fscolor;\n"
"void main()\n"
"{\n"
" vec4 v1 = model * vec4(vposition, 1.0f);\n"
" vec4 v2 = vec4(v1.xyz/v1.w, 1.0);\n"
" gl_Position = projection * view * v1;\n"
//" gl_Position = projection * view * model * vec4(position, 1.0f);\n"
" position = vec3(v2.xyz);\n"
" Normal = mat3(transpose(inverse(model)))*vnormal;\n"
" TexCoord = vec2(texCoord.x , 1.0 - texCoord.y);\n"//1.0 - texCoord.y);\n"
" for(int i=0;i<NUM_POINT_LIGHTS;++i)\n"
" {\n"
" Pos_lightspace[i] = lightSpace[i] * v2;\n" //added
" }\n"
//" Pos_lightspace = lightSpace * v2;\n" //added
"}\0";
const char *fs_phone = "#version 450 core\n"
"struct Material{\n"
" sampler2D diffuse_texture;\n"
" sampler2D specular_texture;\n"
" float shininess;\n"
"};\n"
"struct DirLight{\n"
" vec3 direction;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"struct PointLight{\n"
" vec3 position;\n"
" float constant;\n"
" float linear;\n"
" float quadratic;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"struct SpotLight{\n"
" vec3 position;\n"
" vec3 direction;\n"
" float cutoff;\n"
" float outercutoff;\n"
" float constant;\n"
" float linear;\n"
" float quadratic;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"#define NUM_POINT_LIGHTS 2\n"
"#define NUM_TRANS_OBJ 1\n" //add for trans
"in vec3 position;\n"
"in vec3 Normal;\n"
"in vec2 TexCoord;\n"
"in vec4 Pos_lightspace[NUM_POINT_LIGHTS];\n" //added
"out vec4 color;\n"
"uniform vec3 viewPos;\n"
"uniform DirLight dirLight;\n"
"uniform PointLight pointLights[NUM_POINT_LIGHTS];\n"
"uniform SpotLight spotLight;\n"
"uniform Material material;\n"
"uniform sampler2D depthMap[NUM_POINT_LIGHTS+NUM_TRANS_OBJ];\n" //added
"uniform float transparency;\n" //added for trans
//"uniform float trans_a;\n"
"uniform vec3 trans_color;\n" //add for trans
//"// Function prototypes
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);\n"
"vec3 CalcPointLight(int i, PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
"vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
"float ShadowCalc(int i, vec4 Pos_lightspace, vec3 norm_, vec3 lifgtdir_)\n"
"{\n"
" vec3 projcoord = Pos_lightspace.xyz/Pos_lightspace.w;\n"
" projcoord = projcoord*0.5+0.5;\n"
" float current = projcoord.z;\n"
" float bias = max(0.00001, 0.0004*pow(1.0-dot(norm_, lifgtdir_ ), 0.1) );\n"
" float shadow = 0.0;\n"
" vec2 texelsize = 1.0/textureSize(depthMap[i],0);\n"
" for(int x =-1;x<=1;++x)\n"
" {\n"
" for(int y = -1; y <= 1; ++y)\n"
" {\n"
" float pcfDepth = texture(depthMap[i], projcoord.xy + vec2(x, y) * texelsize).r;\n"
" shadow += current - bias > pcfDepth ? 1.0 : 0.0;\n"
" }\n"
" }\n"
" shadow /= 9.0;\n"
" return shadow;\n"
"}\n"
"void main()\n"
"{\n"
//" // Properties
" vec3 norm = normalize(Normal);\n"
" vec3 viewDir = normalize(viewPos - position);\n"
//" // Phase 1: Directional lighting
" vec3 result = CalcDirLight(dirLight, norm, viewDir);\n"
//" // Phase 2: Point lights
" for(int i = 0; i < NUM_POINT_LIGHTS; i++)\n"
" result += CalcPointLight(i, pointLights[i], norm, position, viewDir);\n"
//" // Phase 3: Spot light
" result += CalcSpotLight(spotLight, norm, position, viewDir);\n"
" color = vec4(result, 1.0);\n"
"}\n"
//"// Calculates the color when using a directional light.
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(-light.direction);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Combine results
" vec3 ambient = light.ambient * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 specular = light.specular * spec * vec3(texture(material.specular_texture, TexCoord));\n"
" return (ambient + diffuse + specular);\n"
"}\n"
//"// Calculates the color when using a point light.
"vec3 CalcPointLight(int i, PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(light.position - fragPos);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Attenuation
" float distance = length(light.position - fragPos);\n"
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n"
//" // Combine results
//" vec3 ambient = light.ambient * vec3(texture(material.diffuse_texture, TexCoord));\n"
//" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse_texture, TexCoord));\n"
//" vec3 specular = light.specular * spec * vec3(texture(material.specular_texture, TexCoord));\n"
" float shadow;\n"
" shadow = ShadowCalc(i, Pos_lightspace[i], normal, lightDir);\n"
" float shadow_trans;\n"
" vec3 thiscolor;\n"
" if (i==NUM_TRANS_OBJ)\n"
" {\n"
" shadow_trans = ShadowCalc(i+NUM_TRANS_OBJ, Pos_lightspace[i], normal, lightDir);\n"
//" //if (shadow_trans>0.001 && shadow<0.999)\n"
//{
" vec3 amb_thiscolor=(1.0-transparency)*(light.ambient*(1.0-transparency)+transparency*trans_color);\n"
" vec3 diff_thiscolor=(1.0-transparency)*(light.diffuse*(1.0-transparency)+transparency*trans_color);\n"
" vec3 spec_thiscolor=(1.0-transparency)*(light.specular*(1.0-transparency)+transparency*trans_color);\n"
" vec3 ambient = amb_thiscolor * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 diffuse = diff_thiscolor * diff * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 specular = spec_thiscolor * spec * vec3(texture(material.specular_texture, TexCoord));\n"
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
" thiscolor=ambient + diffuse + specular;\n"
//}
" }\n"
//else thiscolor=thecolor;
//if (i==NUM_TRANS_OBJ) vec3 thiscolor=(1.0-transparency)*(thecolor*(1.0-transparency)+transparency*trans_color);
" vec3 ambient = light.ambient * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 specular = light.specular * spec * vec3(texture(material.specular_texture, TexCoord));\n"
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
//" float shadow;\n"
//" shadow = ShadowCalc(i, Pos_lightspace[i], normal, lightDir);\n"
//float shadow_trans;
//if (i==NUM_TRANS_OBJ) shadow_trans = ShadowCalc(i+NUM_TRANS_OBJ, Pos_lightspace[i], normal, lightDir);
" if (i==NUM_TRANS_OBJ)\n"
" return ( thiscolor*shadow_trans*(1.0-shadow) + (1.0-shadow_trans)*(1.0-shadow)*(ambient + diffuse + specular) + shadow*0.25*ambient );\n"
" else\n"
" return ( (1.0-shadow*0.75) * ambient + (1.0-shadow)*(diffuse + specular));\n"
//else if(shadow<1.0)
//return
"}\n"
"vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(light.position - fragPos);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Attenuation
" float distance = length(light.position - fragPos);\n"
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n"
//" // Spotlight intensity
" float theta = dot(lightDir, normalize(-light.direction));\n"
" float epsilon = light.cutoff - light.outercutoff;\n"
" float intensity = clamp((theta - light.outercutoff) / epsilon, 0.0, 1.0);\n"
//" // Combine results
" vec3 ambient = light.ambient * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse_texture, TexCoord));\n"
" vec3 specular = light.specular * spec * vec3(texture(material.specular_texture, TexCoord));\n"
" ambient *= attenuation * intensity;\n"
" diffuse *= attenuation * intensity;\n"
" specular *= attenuation * intensity;\n"
" return (ambient + diffuse + specular);\n"
"}\n";
//phone_shading_no_texture----------------------------------------------------
const char *vs_phone_no_text ="#version 450 core\n"
"layout (location = 0) in vec3 vposition;\n"
"layout (location = 1) in vec3 vnormal;\n"
//"layout (location = 2) in vec3 vcolor;\n"
//"layout (location = 2) in vec2 texCoord;\n"
"#define NUM_POINT_LIGHTS 2\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"uniform mat4 lightSpace[NUM_POINT_LIGHTS];\n" //added
//"out vec2 TexCoord;\n"
"out vec3 position;\n"
"out vec3 Normal;\n"
//"out vec4 fscolor;\n"
"out vec4 Pos_lightspace[NUM_POINT_LIGHTS];\n" //added
"void main()\n"
"{\n"
" vec4 v1 = model * vec4(vposition, 1.0f);\n"
" vec4 v2 = vec4(v1.xyz/v1.w, 1.0);\n"
" gl_Position = projection * view * v1;\n"
//" gl_Position = projection * view * model * vec4(position, 1.0f);\n"
" position = vec3(v2.xyz);\n"
" Normal = mat3(transpose(inverse(model)))*vnormal;\n"
//" TexCoord = vec2(texCoord.x , 1.0 - texCoord.y);\n"//1.0 - texCoord.y);\n"
" for(int i=0;i<NUM_POINT_LIGHTS;++i)\n"
" {\n"
" Pos_lightspace[i] = lightSpace[i] * v2;\n" //added
" }\n"
//" Pos_lightspace = lightSpace * v2;\n" //added
"}\0";
const char *fs_phone_no_text = "#version 450 core\n"
"struct Material{\n"
//" sampler2D diffuse_texture;\n"
//" sampler2D specular_texture;\n"
" float shininess;\n"
"};\n"
"struct DirLight{\n"
" vec3 direction;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"struct PointLight{\n"
" vec3 position;\n"
" float constant;\n"
" float linear;\n"
" float quadratic;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"struct SpotLight{\n"
" vec3 position;\n"
" vec3 direction;\n"
" float cutoff;\n"
" float outercutoff;\n"
" float constant;\n"
" float linear;\n"
" float quadratic;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"#define NUM_POINT_LIGHTS 2\n"
"#define NUM_TRANS_OBJ 1\n" //add for trans
"in vec3 position;\n"
"in vec3 Normal;\n"
//"in vec2 TexCoord;\n"
"in vec4 Pos_lightspace[NUM_POINT_LIGHTS];\n" //added
"out vec4 color;\n"
"uniform vec3 viewPos;\n"
"uniform DirLight dirLight;\n"
"uniform PointLight pointLights[NUM_POINT_LIGHTS];\n"
"uniform SpotLight spotLight;\n"
"uniform Material material;\n"
"uniform vec3 thecolor;\n"
"uniform sampler2D depthMap[NUM_POINT_LIGHTS+NUM_TRANS_OBJ];\n" //added
"uniform float transparency;\n" //added for trans
"uniform float trans_a;\n"
"uniform vec3 trans_color;\n" //add for trans
"float ShadowCalc(int i, vec4 Pos_lightspace, vec3 norm_, vec3 lifgtdir_)\n"
"{\n"
" vec3 projcoord = Pos_lightspace.xyz/Pos_lightspace.w;\n"
" projcoord = projcoord*0.5+0.5;\n"
" float current = projcoord.z;\n"
" float bias = max(0.00001, 0.0004*pow(1.0-dot(norm_, lifgtdir_ ), 0.1) );\n"
" float shadow = 0.0;\n"
" vec2 texelsize = 1.0/textureSize(depthMap[i],0);\n"
" for(int x =-1;x<=1;++x)\n"
" {\n"
" for(int y = -1; y <= 1; ++y)\n"
" {\n"
" float pcfDepth = texture(depthMap[i], projcoord.xy + vec2(x, y) * texelsize).r;\n"
//float pcf_tran = texture(depthMap[i], projcoord.xy + vec2(x, y) * texelsize).r;
" shadow += current - bias > pcfDepth ? 1.0 : 0.0;\n"
" }\n"
" }\n"
" shadow /= 9.0;\n"
" return shadow;\n"
"}\n"
//"// Function prototypes
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);\n"
"vec3 CalcPointLight(int i, PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
"vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
//"vec3 thecolor = vec3(255, 227, 132)/255;\n"
"void main()\n"
"{\n"
//" // Properties
" vec3 norm = normalize(Normal);\n"
" vec3 viewDir = normalize(viewPos - position);\n"
//" if (transparency < 0.99)"
//" // Phase 1: Directional lighting
" vec3 result = CalcDirLight(dirLight, norm, viewDir);\n"
//" // Phase 2: Point lights
" for(int i = 0; i < NUM_POINT_LIGHTS; i++)\n"
" result += CalcPointLight(i, pointLights[i], norm, position, viewDir);\n"
//" // Phase 3: Spot light
" result += CalcSpotLight(spotLight, norm, position, viewDir);\n"
" color = vec4(result, trans_a);\n"
"}\n"
//"// Calculates the color when using a directional light.
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(-light.direction);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Combine results
" vec3 ambient = light.ambient * thecolor;\n"
" vec3 diffuse = light.diffuse * diff * thecolor;\n"
" vec3 specular = light.specular * spec * thecolor;\n"
" return (ambient + diffuse + specular);\n"
"}\n"
//"// Calculates the color when using a point light.
"vec3 CalcPointLight(int i, PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(light.position - fragPos);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Attenuation
" float distance = length(light.position - fragPos);\n"
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n"
//" // Combine results
" float shadow;\n"
" shadow = ShadowCalc(i, Pos_lightspace[i], normal, lightDir);\n"
" float shadow_trans;\n"
" vec3 thiscolor;\n"
" if (i==NUM_TRANS_OBJ)\n"
" {\n"
" shadow_trans = ShadowCalc(i+NUM_TRANS_OBJ, Pos_lightspace[i], normal, lightDir);\n"
//" //if (shadow_trans>0.001 && shadow<0.999)\n"
//{
" vec3 amb_thiscolor=(1.0-transparency)*(light.ambient*(1.0-transparency)+transparency*trans_color);\n"
" vec3 diff_thiscolor=(1.0-transparency)*(light.diffuse*(1.0-transparency)+transparency*trans_color);\n"
" vec3 spec_thiscolor=(1.0-transparency)*(light.specular*(1.0-transparency)+transparency*trans_color);\n"
" vec3 ambient = amb_thiscolor * thecolor;\n"
" vec3 diffuse = diff_thiscolor * diff * thecolor;\n"
" vec3 specular = spec_thiscolor * spec * thecolor;\n"
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
" thiscolor=ambient + diffuse + specular;\n"
//}
" }\n"
//else thiscolor=thecolor;
//if (i==NUM_TRANS_OBJ) vec3 thiscolor=(1.0-transparency)*(thecolor*(1.0-transparency)+transparency*trans_color);
" vec3 ambient = light.ambient * thecolor;\n"
" vec3 diffuse = light.diffuse * diff * thecolor;\n"
" vec3 specular = light.specular * spec * thecolor;\n"
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
//" float shadow;\n"
//" shadow = ShadowCalc(i, Pos_lightspace[i], normal, lightDir);\n"
//float shadow_trans;
//if (i==NUM_TRANS_OBJ) shadow_trans = ShadowCalc(i+NUM_TRANS_OBJ, Pos_lightspace[i], normal, lightDir);
" if (i==NUM_TRANS_OBJ)\n"
" return ( thiscolor*shadow_trans*(1.0-shadow) + (1.0-shadow_trans)*(1.0-shadow)*(ambient + diffuse + specular) + shadow*0.25*ambient );\n"
" else\n"
" return ( (1.0-shadow*0.75) * ambient + (1.0-shadow)*(diffuse + specular));\n"
//else if(shadow<1.0)
//return
"}\n"
"vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(light.position - fragPos);\n"
//" // Diffuse shading
" float diff = max(dot(normal, lightDir), 0.0);\n"
//" // Specular shading
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
//" // Attenuation
" float distance = length(light.position - fragPos);\n"
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n"
//" // Spotlight intensity
" float theta = dot(lightDir, normalize(-light.direction));\n"
" float epsilon = light.cutoff - light.outercutoff;\n"
" float intensity = clamp((theta - light.outercutoff) / epsilon, 0.0, 1.0);\n"
//" // Combine results
" vec3 ambient = light.ambient * thecolor;\n"
" vec3 diffuse = light.diffuse * diff * thecolor;\n"
" vec3 specular = light.specular * spec * thecolor;\n"
" ambient *= attenuation * intensity;\n"
" diffuse *= attenuation * intensity;\n"
" specular *= attenuation * intensity;\n"
" return (ambient + diffuse + specular);\n"
"}\n";
//----------------------------------------------------------------------------
// The MAIN function, from here start
int main()
{
int num_obj=3;
int v_num[num_obj]={0}; //点的数量
int vn_num[num_obj]={0};//法线的数量
int f_num[num_obj]={0}; //面的数量
int vt_num[num_obj]={0};//纹理的数量
//int f_total=0;
getLineNum("ball.obj", v_num[0], vn_num[0], vt_num[0], f_num[0]);
getLineNum("zhui.obj", v_num[1], vn_num[1], vt_num[1], f_num[1]);
getLineNum("zhu.obj", v_num[2], vn_num[2], vt_num[2], f_num[2]);
//std::cout<<"--------------------"<<std::endl;
//std::cout<<f_num[0]<<f_num[1]<<f_num[2]<<std::endl;
int f_total=f_num[0]+f_num[1]+f_num[2];
//GLuint f_num_ball=f_num;
GLfloat verticesObj[f_total*24];
readObjFile("ball.obj", verticesObj, v_num[0], vn_num[0], vt_num[0], f_num[0]);
//for(int I=0;I<1000;I++) std::cout<<verticesObj[I]<<std::endl;
//getLineNum("zhui.obj");
//GLuint f_num_zhui=f_num;
//GLfloat verticeszhui[f_num_zhui*24];
readObjFile("zhui.obj", &verticesObj[24*f_num[0]], v_num[1], vn_num[1], vt_num[1], f_num[1]);
//getLineNum("zhu.obj");
//GLuint f_num_zhu=f_num;
//GLfloat verticeszhu[f_num_zhu*24];
readObjFile("zhu.obj", &verticesObj[24*(f_num[0]+f_num[1])], v_num[2], vn_num[2], vt_num[2], f_num[2]);
//std::cout<<"------------------------------------"<<std::endl;
//for(int I=0;I<1000;I++) std::cout<<verticesObj[I]<<std::endl;
//GLfloat verticesObj[(f_num_ball+f_num_zhui+f_num_zhu)*24];
//for(int I=0;I<1000;I++) std::cout<<verticesball[I]<<std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4);
glEnable(GL_MULTISAMPLE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "CG_rendering", nullptr, nullptr);
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
glewInit();
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// 开启深度测试
glEnable(GL_DEPTH_TEST);
// Build and compile shader programs
GLuint vertex_sha, fragment_sha;
GLint success_flag;
GLchar infoLog_char[512];
vertex_sha=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_sha, 1, &vs_phone, NULL);
glCompileShader(vertex_sha);
glGetShaderiv(vertex_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(vertex_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
fragment_sha = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_sha, 1, &fs_phone, NULL);
glCompileShader(fragment_sha);
glGetShaderiv(fragment_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(fragment_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
GLuint shaderProgram_int3 = glCreateProgram();
glAttachShader(shaderProgram_int3, vertex_sha);
glAttachShader(shaderProgram_int3, fragment_sha);
glLinkProgram(shaderProgram_int3);
glGetProgramiv(shaderProgram_int3, GL_LINK_STATUS, &success_flag);
if (!success_flag) {
glGetProgramInfoLog(shaderProgram_int3, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog_char << std::endl;
}
glDeleteShader(vertex_sha);
glDeleteShader(fragment_sha);
//---------------------------------------Phone_Shader
//-------------------------shadow shader-------------
vertex_sha=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_sha, 1, &vs_shadow, NULL);
glCompileShader(vertex_sha);
glGetShaderiv(vertex_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(vertex_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
fragment_sha = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_sha, 1, &fs_shadow, NULL);
glCompileShader(fragment_sha);
glGetShaderiv(fragment_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(fragment_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
GLuint shaderProgram_shadow = glCreateProgram();
glAttachShader(shaderProgram_shadow, vertex_sha);
glAttachShader(shaderProgram_shadow, fragment_sha);
glLinkProgram(shaderProgram_shadow);
glGetProgramiv(shaderProgram_shadow, GL_LINK_STATUS, &success_flag);
if (!success_flag) {
glGetProgramInfoLog(shaderProgram_shadow, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog_char << std::endl;
}
glDeleteShader(vertex_sha);
glDeleteShader(fragment_sha);
//---------------------------------------------------
//Then use the light shader:
vertex_sha=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_sha, 1, &vs_light, NULL);
glCompileShader(vertex_sha);
glGetShaderiv(vertex_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(vertex_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
fragment_sha = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_sha, 1, &fs_light, NULL);
glCompileShader(fragment_sha);
glGetShaderiv(fragment_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(fragment_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
GLuint shaderProgram_intL = glCreateProgram();
glAttachShader(shaderProgram_intL, vertex_sha);
glAttachShader(shaderProgram_intL, fragment_sha);
glLinkProgram(shaderProgram_intL);
glGetProgramiv(shaderProgram_intL, GL_LINK_STATUS, &success_flag);
if (!success_flag) {
glGetProgramInfoLog(shaderProgram_intL, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog_char << std::endl;
}
glDeleteShader(vertex_sha);
glDeleteShader(fragment_sha);
//-------------------------------
//Then use the phone_no_text shader:
vertex_sha=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_sha, 1, &vs_phone_no_text, NULL);
glCompileShader(vertex_sha);
glGetShaderiv(vertex_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(vertex_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
fragment_sha = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_sha, 1, &fs_phone_no_text, NULL);
glCompileShader(fragment_sha);
glGetShaderiv(fragment_sha, GL_COMPILE_STATUS, &success_flag);
if (!success_flag)
{
glGetShaderInfoLog(fragment_sha, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog_char << std::endl;
}
GLuint shaderProgram_notext = glCreateProgram();
glAttachShader(shaderProgram_notext, vertex_sha);
glAttachShader(shaderProgram_notext, fragment_sha);
glLinkProgram(shaderProgram_notext);
glGetProgramiv(shaderProgram_notext, GL_LINK_STATUS, &success_flag);
if (!success_flag) {
glGetProgramInfoLog(shaderProgram_notext, 512, NULL, infoLog_char);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog_char << std::endl;
}
glDeleteShader(vertex_sha);
glDeleteShader(fragment_sha);
//-------------------------------
// 立方体顶点数据(位置坐标+法现+纹理坐标)
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.57735027f, -0.57735027f, -0.57735027f, 0.0f, 0.1339746f,
0.5f, -0.5f, 0.5f, 0.57735027f, -0.57735027f, -0.57735027f, 1.0f, 0.1339746f,
0.5f, 0.5f, -0.5f, 0.57735027f, -0.57735027f, -0.57735027f, 0.5f, 1.0f,
0.5f, 0.5f, -0.5f, -0.57735027f, 0.57735027f, -0.57735027f, 1.0f, 0.8660254f,
-0.5f, 0.5f, 0.5f, -0.57735027f, 0.57735027f, -0.57735027f, 0.0f, 0.8660254f,
-0.5f, -0.5f, -0.5f, -0.57735027f, 0.57735027f, -0.57735027f, 0.5f, 0.0f,
0.5f, 0.5f, -0.5f, 0.57735027f, 0.57735027f, 0.57735027f, 1.0f, 0.8660254f,
-0.5f, 0.5f, 0.5f, 0.57735027f, 0.57735027f, 0.57735027f, 0.0f, 0.8660254f,
0.5f, -0.5f, 0.5f, 0.57735027f, 0.57735027f, 0.57735027f, 0.5f, 0.0f,
-0.5f, -0.5f, -0.5f, -0.57735027f, -0.57735027f, 0.57735027f, 0.0f, 0.1339746f,
0.5f, -0.5f, 0.5f, -0.57735027f, -0.57735027f, 0.57735027f, 1.0f, 0.1339746f,
-0.5f, 0.5f, 0.5f, -0.57735027f, -0.57735027f, 0.57735027f, 0.5f, 1.0f
};
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.8f, 0.8f, -1.2f),
glm::vec3(-0.8f, -1.2f, -0.8f),
glm::vec3(-1.8f, 1.8f, -0.8f),
glm::vec3(2.4f, 0.4f, -0.1f),
glm::vec3(-1.7f, 0.6f, 0.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//
GLuint VBO, VAO;
GLuint VBO2, VAO2; //Suppose I have a different shape
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 一次3个FLOAT 跨8个FLOAT再读 从0号位置开读取
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// 一次3个FLOAT 跨8个FLAOT再读 从3个FLOAT值位置后开读
//glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
//glEnableVertexAttribArray(1);
//Normal
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// 一次2个FLOAT 跨8个FLAOT再读 从6个FLOAT值位置后开读
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0); // Unbind VAO
//Now for the other
//----------------------------------------------------------------------------------------------------
glGenVertexArrays(1, &VAO2);
glGenBuffers(1, &VBO2);
glBindVertexArray(VAO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesObj), verticesObj, GL_STATIC_DRAW);
//
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0); // Unbind VAO
//----------------------------------------------------------------------------------------------------
//Configure the depth map FBO----------------------------------------------
//const GLuint shadow_width = 2*WIDTH, shadow_height = 2*HEIGHT;
const GLuint shadow_width = 2048, shadow_height = 2048;
GLuint depthMapFBO;
glGenFramebuffers(1, &depthMapFBO);
GLuint number_of_lights = 2;
GLuint num_trans_obj = 1;
GLuint depthMap[number_of_lights+num_trans_obj];
glGenTextures(number_of_lights+num_trans_obj, depthMap);
//The full version should be +num_light*num_transobj, but I know the first light does not hit the trans onto ground or any obj
for(int i_=0;i_<number_of_lights + num_trans_obj;++i_)
{
glBindTexture(GL_TEXTURE_2D, depthMap[i_]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Set texture filtering