mirror of
https://gitcode.com/gh_mirrors/le/LearnOpenGLProject.git
synced 2025-05-17 15:07:29 +08:00
4.10实例化
This commit is contained in:
13
LearnOpenGL/assest/model/planet/planet.mtl
Normal file
13
LearnOpenGL/assest/model/planet/planet.mtl
Normal file
@ -0,0 +1,13 @@
|
||||
# Blender MTL
|
||||
# Material Count: 1
|
||||
|
||||
newmtl Material.001
|
||||
Ns 96.078431
|
||||
Ka 0.000000 0.000000 0.000000
|
||||
Kd 0.640000 0.640000 0.640000
|
||||
Ks 0.087302 0.087302 0.087302
|
||||
Ni 1.000000
|
||||
d 1.000000
|
||||
illum 2
|
||||
map_Kd planet_Quom1200.png
|
||||
map_Bump planet_Quom1200.png
|
20854
LearnOpenGL/assest/model/planet/planet.obj
Normal file
20854
LearnOpenGL/assest/model/planet/planet.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
LearnOpenGL/assest/model/planet/planet_Quom1200.png
Normal file
BIN
LearnOpenGL/assest/model/planet/planet_Quom1200.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 MiB |
BIN
LearnOpenGL/assest/model/rock/Rock-Texture-Surface.jpg
Normal file
BIN
LearnOpenGL/assest/model/rock/Rock-Texture-Surface.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.8 MiB |
13
LearnOpenGL/assest/model/rock/rock.mtl
Normal file
13
LearnOpenGL/assest/model/rock/rock.mtl
Normal file
@ -0,0 +1,13 @@
|
||||
# Blender MTL File: 'Rock1.blend'
|
||||
# Material Count: 1
|
||||
|
||||
newmtl Material
|
||||
Ns 13.725490
|
||||
Ka 0.000000 0.000000 0.000000
|
||||
Kd 0.640000 0.640000 0.640000
|
||||
Ks 0.007937 0.007937 0.007937
|
||||
Ni 1.000000
|
||||
d 1.000000
|
||||
illum 2
|
||||
map_Bump Rock-Texture-Surface.jpg
|
||||
map_Kd Rock-Texture-Surface.jpg
|
559
LearnOpenGL/assest/model/rock/rock.obj
Normal file
559
LearnOpenGL/assest/model/rock/rock.obj
Normal file
@ -0,0 +1,559 @@
|
||||
# Blender v2.71 (sub 0) OBJ File: 'Rock1.blend'
|
||||
# www.blender.org
|
||||
mtllib rock.mtl
|
||||
o Cube
|
||||
v 0.896930 -0.116701 -1.078061
|
||||
v 0.736314 -0.076033 1.066762
|
||||
v -1.052088 -0.064600 0.954513
|
||||
v -0.692441 -0.114299 -0.835835
|
||||
v 1.303753 1.083753 -1.023542
|
||||
v 0.444005 1.032712 1.513821
|
||||
v -1.019775 1.174950 1.105961
|
||||
v -0.681862 1.180458 -1.481573
|
||||
v 1.045407 -0.157597 -0.147996
|
||||
v 0.067265 -0.072713 -0.783522
|
||||
v 1.200361 0.538245 -1.221685
|
||||
v -0.154563 -0.114500 1.228987
|
||||
v 1.155268 0.478039 1.381585
|
||||
v -1.094633 -0.254018 -0.118459
|
||||
v -1.648060 0.452009 0.853586
|
||||
v -1.185727 0.481559 -1.133555
|
||||
v 0.975431 1.246558 0.047895
|
||||
v 0.105258 1.126820 -1.585067
|
||||
v -0.344848 1.152380 1.730000
|
||||
v -1.177093 1.303243 0.009798
|
||||
v -0.015789 -0.312706 -0.060242
|
||||
v 0.046107 1.351565 -0.253730
|
||||
v 1.175182 0.692469 0.169032
|
||||
v 0.260077 0.452640 1.365451
|
||||
v -1.648881 0.429819 -0.064031
|
||||
v -0.199824 0.336820 -1.536424
|
||||
v 1.008866 -0.126459 0.470257
|
||||
v -0.321458 -0.060305 -0.702046
|
||||
v 1.385825 0.807576 -1.181814
|
||||
v -0.537873 -0.057997 1.154378
|
||||
v 0.936321 0.677546 1.426494
|
||||
v -0.862364 -0.219203 -0.537108
|
||||
v -1.281588 0.883991 1.218077
|
||||
v -0.974527 0.926074 -1.431442
|
||||
v 0.808457 1.346412 0.902078
|
||||
v -0.174992 1.147173 -1.801163
|
||||
v -0.610285 1.147948 1.327881
|
||||
v -1.356023 1.245036 -0.716849
|
||||
v 1.042022 -0.190494 -0.637866
|
||||
v 0.468949 -0.142859 -1.026806
|
||||
v 1.081272 0.175115 -1.287556
|
||||
v 0.243586 -0.141476 1.191072
|
||||
v 1.014587 0.183491 1.312948
|
||||
v -1.155731 -0.217847 0.459469
|
||||
v -1.571341 0.110279 0.770709
|
||||
v -1.063276 0.074521 -1.015607
|
||||
v 1.231954 1.216866 -0.594471
|
||||
v 0.770454 1.122192 -1.299703
|
||||
v -0.065279 1.102928 1.796044
|
||||
v -1.176430 1.281231 0.646380
|
||||
v -0.476225 -0.274043 -0.037390
|
||||
v 0.480423 -0.312115 -0.111060
|
||||
v 0.172793 -0.188473 -0.532460
|
||||
v -0.106274 -0.274351 0.595963
|
||||
v -0.198010 1.360380 0.630818
|
||||
v 0.062998 1.276304 -0.994314
|
||||
v 0.611501 1.352344 -0.290780
|
||||
v -0.603343 1.343584 -0.011276
|
||||
v 1.267461 0.630730 1.008356
|
||||
v 1.267107 0.600208 -0.689727
|
||||
v 1.268899 0.241091 -0.063884
|
||||
v 1.050912 1.068502 0.197512
|
||||
v -1.043570 0.402928 1.289593
|
||||
v 0.667029 0.439932 1.448684
|
||||
v -0.135120 0.156677 1.505020
|
||||
v -0.066870 0.652060 1.602193
|
||||
v -1.759897 0.319614 -0.724614
|
||||
v -1.646483 0.462612 0.459024
|
||||
v -1.548443 -0.153801 -0.255587
|
||||
v -1.483055 1.068517 0.045330
|
||||
v -0.775458 0.497559 -1.402503
|
||||
v 0.556317 0.449266 -1.496503
|
||||
v 0.153226 0.839249 -1.832230
|
||||
v -0.180515 0.060946 -1.233899
|
||||
v -0.630920 -0.226328 0.592326
|
||||
v -0.677921 1.329703 0.698183
|
||||
v 1.115930 1.067893 0.980967
|
||||
v -0.944509 0.743625 1.392342
|
||||
v -1.719440 0.916506 -0.699776
|
||||
v -0.751048 0.149059 -1.123006
|
||||
v -0.359688 -0.219676 -0.596748
|
||||
v 0.548058 -0.284113 -0.657622
|
||||
v 0.383454 -0.298149 0.533170
|
||||
v 0.342781 1.370886 0.863692
|
||||
v 0.801564 1.333314 -0.716665
|
||||
v -0.544178 1.290048 -0.889409
|
||||
v 1.347667 0.189763 0.746821
|
||||
v 1.323792 0.127379 -0.856060
|
||||
v 1.423543 0.897206 -0.707035
|
||||
v -0.706694 0.190636 1.346937
|
||||
v 0.494274 0.153436 1.399456
|
||||
v 0.524189 0.691410 1.410453
|
||||
v -1.346437 -0.078697 -0.808084
|
||||
v -1.550380 0.033513 0.487642
|
||||
v -1.434319 1.017100 0.746778
|
||||
v -0.263198 0.916780 -1.716774
|
||||
v 0.802037 0.850318 -1.528586
|
||||
v 0.512712 0.114694 -1.198383
|
||||
vt 0.058285 0.569867
|
||||
vt 0.082613 0.506661
|
||||
vt 0.152906 0.498683
|
||||
vt 0.206316 0.378186
|
||||
vt 0.155630 0.402329
|
||||
vt 0.099783 0.387655
|
||||
vt 0.854473 0.349296
|
||||
vt 0.794976 0.248970
|
||||
vt 0.870663 0.174892
|
||||
vt 0.944083 0.694636
|
||||
vt 0.905653 0.682602
|
||||
vt 0.916733 0.604517
|
||||
vt 0.336809 0.082005
|
||||
vt 0.369246 0.000842
|
||||
vt 0.489191 0.000000
|
||||
vt 0.512673 0.310590
|
||||
vt 0.481603 0.323059
|
||||
vt 0.447322 0.276247
|
||||
vt 0.276846 0.583529
|
||||
vt 0.215363 0.578539
|
||||
vt 0.223692 0.502410
|
||||
vt 0.288525 0.695621
|
||||
vt 0.226456 0.694739
|
||||
vt 0.220811 0.635843
|
||||
vt 0.149628 0.688747
|
||||
vt 0.060049 0.668982
|
||||
vt 0.053708 0.620010
|
||||
vt 0.115533 0.299946
|
||||
vt 0.042023 0.402498
|
||||
vt 0.011393 0.383384
|
||||
vt 0.133867 0.094672
|
||||
vt 0.226136 0.127633
|
||||
vt 0.165692 0.199396
|
||||
vt 0.273683 0.187557
|
||||
vt 0.335575 0.267841
|
||||
vt 0.258354 0.321530
|
||||
vt 0.422948 0.654063
|
||||
vt 0.520455 0.684734
|
||||
vt 0.477461 0.726331
|
||||
vt 0.422729 0.459207
|
||||
vt 0.529375 0.479105
|
||||
vt 0.539575 0.580737
|
||||
vt 0.744126 0.814485
|
||||
vt 0.672331 0.787452
|
||||
vt 0.683844 0.711964
|
||||
vt 0.016271 0.536299
|
||||
vt 0.006598 0.482318
|
||||
vt 0.054401 0.402498
|
||||
vt 0.855242 0.125920
|
||||
vt 0.818683 0.137473
|
||||
vt 0.786060 0.070157
|
||||
vt 0.829103 0.182611
|
||||
vt 0.720806 0.251159
|
||||
vt 0.693981 0.268226
|
||||
vt 0.348488 0.225548
|
||||
vt 0.431026 0.176751
|
||||
vt 0.821728 0.494375
|
||||
vt 0.758928 0.488146
|
||||
vt 0.747834 0.361119
|
||||
vt 0.853441 0.628842
|
||||
vt 0.844101 0.700647
|
||||
vt 0.766741 0.707183
|
||||
vt 0.554998 0.057463
|
||||
vt 0.491069 0.166304
|
||||
vt 0.661203 0.071636
|
||||
vt 0.642437 0.169965
|
||||
vt 0.565426 0.199969
|
||||
vt 0.619708 0.321108
|
||||
vt 0.560004 0.310321
|
||||
vt 0.556413 0.271543
|
||||
vt 0.137636 0.563411
|
||||
vt 0.140662 0.629249
|
||||
vt 0.155028 0.344089
|
||||
vt 0.206193 0.274302
|
||||
vt 0.896175 0.282885
|
||||
vt 0.907061 0.133559
|
||||
vt 0.622627 0.581780
|
||||
vt 0.617335 0.678426
|
||||
vt 0.662917 0.562066
|
||||
vt 0.672331 0.661064
|
||||
vt 0.949240 0.587705
|
||||
vt 0.869574 0.490922
|
||||
vt 0.791582 0.866218
|
||||
vt 0.744126 0.821215
|
||||
vt 0.752056 0.707183
|
||||
vt 0.999879 0.706758
|
||||
vt 0.460672 0.063535
|
||||
vt 0.766456 0.640000
|
||||
vt 0.672331 0.596657
|
||||
vt 0.677773 0.459778
|
||||
vt 0.674914 0.687648
|
||||
vt 0.485748 0.254952
|
||||
vt 0.336809 0.833560
|
||||
vt 0.336920 0.785916
|
||||
vt 0.384519 0.726331
|
||||
vt 0.506205 0.350977
|
||||
vt 0.448079 0.354288
|
||||
vt 0.559475 0.342029
|
||||
vt 0.266533 0.646047
|
||||
vt 0.609960 0.361119
|
||||
vt 0.278588 0.749240
|
||||
vt 0.320949 0.673747
|
||||
vt 0.218381 0.752553
|
||||
vt 0.143231 0.752483
|
||||
vt 0.069266 0.722735
|
||||
vt 0.808347 0.361119
|
||||
vt 0.116225 0.149680
|
||||
vt 0.046020 0.277128
|
||||
vt 0.000000 0.241327
|
||||
vt 0.056221 0.145884
|
||||
vt 0.187902 0.035191
|
||||
vt 0.085645 0.065345
|
||||
vt 0.273048 0.064123
|
||||
vt 0.316518 0.068429
|
||||
vt 0.336809 0.140426
|
||||
vt 0.340137 0.610197
|
||||
vt 0.404599 0.713241
|
||||
vt 0.339988 0.536299
|
||||
vt 0.439840 0.553878
|
||||
vt 0.429528 0.400325
|
||||
vt 0.336833 0.476818
|
||||
vt 0.517650 0.412671
|
||||
vt 0.604166 0.482239
|
||||
vt 0.586459 0.423461
|
||||
vt 0.646477 0.441109
|
||||
vt 0.670954 0.491413
|
||||
vt 0.090851 0.431859
|
||||
vt 0.000000 0.608449
|
||||
vt 0.694133 0.143704
|
||||
vt 0.012308 0.682984
|
||||
vt 0.019832 0.742597
|
||||
vt 0.857058 0.070133
|
||||
vt 0.758736 0.168010
|
||||
vt 0.408067 0.318081
|
||||
vt 0.232244 0.440506
|
||||
vt 0.291247 0.454530
|
||||
vt 0.270084 0.523882
|
||||
vt 0.685316 0.388716
|
||||
vt 0.130148 0.436240
|
||||
vt 0.834672 0.401576
|
||||
vt 0.563922 0.000091
|
||||
vt 0.582099 0.007382
|
||||
vt 0.608937 0.072162
|
||||
vt 0.207942 0.000000
|
||||
vt 0.293309 0.025246
|
||||
vt 0.579472 0.365264
|
||||
vt 0.477201 0.361119
|
||||
vt 0.628288 0.255903
|
||||
vt 0.693981 0.150197
|
||||
vt 0.693822 0.237100
|
||||
vt 0.668575 0.311882
|
||||
vt 0.731678 0.707183
|
||||
vt 0.934356 0.187628
|
||||
vt 0.939564 0.506329
|
||||
vt 0.791426 0.711743
|
||||
vt 1.000000 0.606532
|
||||
vt 0.387737 0.801509
|
||||
vt 0.324683 0.722893
|
||||
vt 0.116277 0.014773
|
||||
vt 0.336809 0.674909
|
||||
vt 0.353162 0.419686
|
||||
vt 0.736508 0.092307
|
||||
vt 0.913859 0.076969
|
||||
vt 0.867108 0.417288
|
||||
vt 0.639569 0.392860
|
||||
vn -0.233500 -0.819800 0.522800
|
||||
vn -0.341800 -0.625700 0.701200
|
||||
vn -0.276700 -0.937700 0.209800
|
||||
vn -0.424000 0.890700 0.163900
|
||||
vn -0.339200 0.818400 0.463900
|
||||
vn -0.460600 0.772900 0.436500
|
||||
vn 0.378500 0.884900 0.271500
|
||||
vn 0.471200 0.579600 0.664800
|
||||
vn 0.320600 0.158900 0.933800
|
||||
vn -0.646800 0.288800 0.705800
|
||||
vn -0.900700 0.371900 -0.224300
|
||||
vn -0.458400 0.862700 -0.213600
|
||||
vn -0.301000 0.819900 -0.486800
|
||||
vn 0.010700 -0.806900 -0.590500
|
||||
vn 0.230000 -0.694400 -0.681800
|
||||
vn -0.231200 -0.595000 -0.769700
|
||||
vn 0.016800 -0.846500 -0.532100
|
||||
vn -0.040300 -0.998600 -0.032900
|
||||
vn -0.122100 -0.991600 -0.041200
|
||||
vn -0.012200 -0.971900 -0.235000
|
||||
vn 0.122300 -0.992400 0.015000
|
||||
vn -0.071000 -0.993000 -0.094000
|
||||
vn 0.122200 -0.981400 0.148100
|
||||
vn 0.080500 -0.849300 0.521600
|
||||
vn -0.086200 -0.849400 0.520600
|
||||
vn -0.046700 0.996400 0.070500
|
||||
vn -0.217700 0.494400 0.841500
|
||||
vn -0.086400 0.146500 0.985400
|
||||
vn 0.178300 0.970700 -0.161300
|
||||
vn 0.007300 0.987200 -0.159100
|
||||
vn -0.027300 0.998100 -0.054800
|
||||
vn -0.044300 0.991500 -0.121900
|
||||
vn -0.344700 0.938400 0.022500
|
||||
vn 0.921600 -0.360400 0.143800
|
||||
vn 0.959300 0.222600 0.173800
|
||||
vn 0.648500 -0.032100 0.760500
|
||||
vn 0.919900 -0.291200 -0.262500
|
||||
vn 0.985400 -0.162400 0.050100
|
||||
vn 0.963400 0.267200 -0.018400
|
||||
vn 0.993300 0.035300 0.109500
|
||||
vn 0.638100 0.769000 0.038600
|
||||
vn 0.596700 0.799100 0.073100
|
||||
vn -0.326300 -0.473800 0.818000
|
||||
vn -0.434100 -0.329200 0.838600
|
||||
vn -0.913600 0.089800 0.396600
|
||||
vn 0.126700 -0.032600 0.991400
|
||||
vn -0.189100 -0.186900 0.964000
|
||||
vn -0.073600 -0.184200 0.980100
|
||||
vn 0.114700 -0.242200 0.963400
|
||||
vn -0.899000 -0.340400 -0.275600
|
||||
vn -0.479500 -0.331900 -0.812300
|
||||
vn -0.993700 0.107100 0.034300
|
||||
vn -0.999200 0.000100 0.039900
|
||||
vn -0.739400 -0.654700 -0.156500
|
||||
vn -0.824700 0.505000 0.254600
|
||||
vn -0.218100 -0.181700 -0.958800
|
||||
vn -0.432200 -0.296800 -0.851500
|
||||
vn 0.426900 0.149400 -0.891800
|
||||
vn 0.225900 -0.430300 -0.873900
|
||||
vn -0.117000 -0.587100 -0.801000
|
||||
vn -0.076900 -0.842600 -0.532900
|
||||
vn -0.211900 -0.938300 -0.273300
|
||||
vn -0.131600 -0.849400 -0.511000
|
||||
vn -0.085500 -0.979800 0.180600
|
||||
vn -0.055900 -0.987800 0.145500
|
||||
vn -0.093600 0.986700 0.133100
|
||||
vn -0.043400 0.998800 -0.021100
|
||||
vn 0.752400 0.571000 0.328300
|
||||
vn 0.940500 0.338300 0.030400
|
||||
vn -0.364600 -0.020200 0.930900
|
||||
vn -0.006300 -0.522900 0.852400
|
||||
vn -0.549100 0.086400 -0.831300
|
||||
vn -0.818900 0.564900 0.101300
|
||||
vn -0.109100 -0.775000 -0.622500
|
||||
vn -0.043500 -0.970400 -0.237300
|
||||
vn -0.216400 -0.916000 -0.337700
|
||||
vn 0.483000 -0.867200 -0.120600
|
||||
vn 0.586200 -0.808400 0.052800
|
||||
vn 0.453300 -0.878500 0.150900
|
||||
vn 0.302100 -0.888100 0.346500
|
||||
vn -0.011700 0.990200 0.139300
|
||||
vn 0.104700 0.994500 -0.006000
|
||||
vn 0.287400 0.799500 -0.527400
|
||||
vn 0.226500 0.797300 -0.559400
|
||||
vn 0.166900 0.398200 -0.901900
|
||||
vn 0.449900 -0.591100 0.669400
|
||||
vn 0.991200 -0.131900 -0.006400
|
||||
vn 0.421700 -0.247600 -0.872200
|
||||
vn 0.719900 -0.329000 -0.611100
|
||||
vn 0.873300 -0.027000 -0.486400
|
||||
vn 0.695900 0.658800 -0.285700
|
||||
vn -0.735800 -0.552500 0.391500
|
||||
vn 0.149300 -0.476900 0.866200
|
||||
vn -0.467200 -0.714600 -0.520600
|
||||
vn -0.791300 -0.591900 0.152900
|
||||
vn 0.150800 0.022200 -0.988300
|
||||
vn 0.072000 -0.564800 -0.822100
|
||||
vn 0.246700 -0.812300 -0.528500
|
||||
usemtl Material
|
||||
s 1
|
||||
f 30/1/1 3/2/2 44/3/3
|
||||
f 50/4/4 7/5/5 37/6/6
|
||||
f 35/7/7 6/8/8 31/9/9
|
||||
f 37/10/6 7/11/5 33/12/10
|
||||
f 79/13/11 38/14/12 8/15/13
|
||||
f 28/16/14 4/17/15 46/18/16
|
||||
f 81/19/17 51/20/18 14/21/19
|
||||
f 82/22/20 52/23/21 21/24/22
|
||||
f 83/25/23 42/26/24 12/27/25
|
||||
f 55/28/26 19/29/27 49/30/28
|
||||
f 85/31/29 56/32/30 22/33/31
|
||||
f 86/34/32 38/35/12 20/36/33
|
||||
f 87/37/34 59/38/35 13/39/36
|
||||
f 88/40/37 60/41/38 23/42/39
|
||||
f 89/43/40 47/44/41 17/45/42
|
||||
f 90/46/43 63/47/44 15/48/45
|
||||
f 64/49/46 24/50/47 65/51/48
|
||||
f 92/52/49 49/53/28 19/54/27
|
||||
f 67/55/50 16/56/51 46/18/16
|
||||
f 68/57/52 25/58/53 69/59/54
|
||||
f 95/60/55 50/61/4 20/62/33
|
||||
f 96/63/56 71/64/57 16/56/51
|
||||
f 97/65/58 72/66/59 26/67/60
|
||||
f 40/68/61 10/69/62 74/70/63
|
||||
f 75/71/64 44/3/3 14/21/19
|
||||
f 21/24/22 54/72/65 75/71/64
|
||||
f 12/27/25 30/1/1 75/71/64
|
||||
f 76/73/66 37/6/6 19/29/27
|
||||
f 22/33/31 58/74/67 76/73/66
|
||||
f 58/74/67 20/36/33 50/4/4
|
||||
f 77/75/68 31/9/9 13/76/36
|
||||
f 62/77/69 77/78/68 59/38/35
|
||||
f 17/79/42 35/80/7 77/78/68
|
||||
f 78/81/70 33/12/10 15/82/45
|
||||
f 24/83/47 66/84/71 78/85/70
|
||||
f 19/86/27 37/10/6 78/81/70
|
||||
f 67/55/50 79/13/11 34/87/72
|
||||
f 70/88/73 79/89/11 67/90/50
|
||||
f 20/62/33 38/91/12 79/89/11
|
||||
f 71/64/57 80/92/74 46/18/16
|
||||
f 74/70/63 80/92/74 71/64/57
|
||||
f 10/93/62 28/94/14 80/95/74
|
||||
f 28/16/14 81/96/17 32/97/75
|
||||
f 53/98/76 81/96/17 28/16/14
|
||||
f 53/99/76 21/24/22 51/20/18
|
||||
f 40/68/61 82/100/20 53/98/76
|
||||
f 39/101/77 82/22/20 40/102/61
|
||||
f 39/101/77 9/103/78 52/23/21
|
||||
f 52/23/21 83/25/23 54/72/65
|
||||
f 9/103/78 27/104/79 83/25/23
|
||||
f 27/104/79 2/105/80 42/26/24
|
||||
f 35/7/7 84/106/81 49/53/28
|
||||
f 57/107/82 84/108/81 35/109/7
|
||||
f 57/107/82 22/33/31 55/28/26
|
||||
f 85/31/29 57/107/82 17/110/42
|
||||
f 48/111/83 85/31/29 47/112/41
|
||||
f 18/113/84 56/32/30 85/31/29
|
||||
f 56/32/30 86/34/32 58/74/67
|
||||
f 36/114/85 86/34/32 56/32/30
|
||||
f 36/114/85 8/115/13 38/35/12
|
||||
f 27/116/79 87/37/34 43/117/86
|
||||
f 9/118/78 61/119/87 87/37/34
|
||||
f 61/119/87 23/42/39 59/38/35
|
||||
f 88/40/37 61/119/87 9/118/78
|
||||
f 41/120/88 88/40/37 39/121/77
|
||||
f 41/120/88 11/122/89 60/41/38
|
||||
f 60/41/38 89/123/40 62/77/69
|
||||
f 11/122/89 29/124/90 89/123/40
|
||||
f 29/124/90 5/125/91 47/126/41
|
||||
f 30/1/1 90/46/43 45/127/92
|
||||
f 12/27/25 65/128/48 90/46/43
|
||||
f 65/51/48 24/50/47 63/129/44
|
||||
f 91/130/93 65/128/48 12/27/25
|
||||
f 43/131/86 91/130/93 42/26/24
|
||||
f 13/76/36 64/49/46 91/132/93
|
||||
f 64/49/46 92/52/49 66/133/71
|
||||
f 13/76/36 31/9/9 92/52/49
|
||||
f 31/9/9 6/8/8 49/53/28
|
||||
f 93/134/94 46/18/16 4/17/15
|
||||
f 69/135/54 93/136/94 32/137/75
|
||||
f 25/58/53 67/90/50 93/138/94
|
||||
f 44/3/3 94/139/95 69/135/54
|
||||
f 3/2/2 45/127/92 94/139/95
|
||||
f 15/82/45 68/57/52 94/140/95
|
||||
f 95/60/55 70/88/73 25/58/53
|
||||
f 33/12/10 95/60/55 68/57/52
|
||||
f 33/12/10 7/11/5 50/61/4
|
||||
f 36/141/85 96/63/56 34/87/72
|
||||
f 18/142/84 73/143/96 96/63/56
|
||||
f 73/143/96 26/67/60 71/64/57
|
||||
f 48/111/83 97/144/58 73/145/96
|
||||
f 5/125/91 29/124/90 97/146/58
|
||||
f 29/124/90 11/122/89 72/147/59
|
||||
f 72/66/59 98/148/97 74/70/63
|
||||
f 11/149/89 41/150/88 98/148/97
|
||||
f 41/150/88 1/151/98 40/68/61
|
||||
f 75/71/64 30/1/1 44/3/3
|
||||
f 76/73/66 50/4/4 37/6/6
|
||||
f 77/75/68 35/7/7 31/9/9
|
||||
f 78/81/70 37/10/6 33/12/10
|
||||
f 34/87/72 79/13/11 8/15/13
|
||||
f 80/92/74 28/16/14 46/18/16
|
||||
f 32/137/75 81/19/17 14/21/19
|
||||
f 53/99/76 82/22/20 21/24/22
|
||||
f 54/72/65 83/25/23 12/27/25
|
||||
f 84/108/81 55/28/26 49/30/28
|
||||
f 57/107/82 85/31/29 22/33/31
|
||||
f 58/74/67 86/34/32 20/36/33
|
||||
f 43/117/86 87/37/34 13/39/36
|
||||
f 61/119/87 88/40/37 23/42/39
|
||||
f 62/152/69 89/43/40 17/45/42
|
||||
f 45/127/92 90/46/43 15/48/45
|
||||
f 91/132/93 64/49/46 65/51/48
|
||||
f 66/133/71 92/52/49 19/54/27
|
||||
f 93/134/94 67/55/50 46/18/16
|
||||
f 94/140/95 68/57/52 69/59/54
|
||||
f 70/88/73 95/60/55 20/62/33
|
||||
f 34/87/72 96/63/56 16/56/51
|
||||
f 73/143/96 97/65/58 26/67/60
|
||||
f 98/148/97 40/68/61 74/70/63
|
||||
f 51/20/18 75/71/64 14/21/19
|
||||
f 51/20/18 21/24/22 75/71/64
|
||||
f 54/72/65 12/27/25 75/71/64
|
||||
f 55/28/26 76/73/66 19/29/27
|
||||
f 55/28/26 22/33/31 76/73/66
|
||||
f 76/73/66 58/74/67 50/4/4
|
||||
f 59/153/35 77/75/68 13/76/36
|
||||
f 23/42/39 62/77/69 59/38/35
|
||||
f 62/77/69 17/79/42 77/78/68
|
||||
f 63/154/44 78/81/70 15/82/45
|
||||
f 63/155/44 24/83/47 78/85/70
|
||||
f 66/156/71 19/86/27 78/81/70
|
||||
f 16/56/51 67/55/50 34/87/72
|
||||
f 25/58/53 70/88/73 67/90/50
|
||||
f 70/88/73 20/62/33 79/89/11
|
||||
f 16/56/51 71/64/57 46/18/16
|
||||
f 26/67/60 74/70/63 71/64/57
|
||||
f 74/157/63 10/93/62 80/95/74
|
||||
f 4/17/15 28/16/14 32/97/75
|
||||
f 10/69/62 53/98/76 28/16/14
|
||||
f 81/19/17 53/99/76 51/20/18
|
||||
f 10/69/62 40/68/61 53/98/76
|
||||
f 1/158/98 39/101/77 40/102/61
|
||||
f 82/22/20 39/101/77 52/23/21
|
||||
f 21/24/22 52/23/21 54/72/65
|
||||
f 52/23/21 9/103/78 83/25/23
|
||||
f 83/25/23 27/104/79 42/26/24
|
||||
f 6/8/8 35/7/7 49/53/28
|
||||
f 17/110/42 57/107/82 35/109/7
|
||||
f 84/108/81 57/107/82 55/28/26
|
||||
f 47/112/41 85/31/29 17/110/42
|
||||
f 5/159/91 48/111/83 47/112/41
|
||||
f 48/111/83 18/113/84 85/31/29
|
||||
f 22/33/31 56/32/30 58/74/67
|
||||
f 18/113/84 36/114/85 56/32/30
|
||||
f 86/34/32 36/114/85 38/35/12
|
||||
f 2/160/80 27/116/79 43/117/86
|
||||
f 27/116/79 9/118/78 87/37/34
|
||||
f 87/37/34 61/119/87 59/38/35
|
||||
f 39/121/77 88/40/37 9/118/78
|
||||
f 1/161/98 41/120/88 39/121/77
|
||||
f 88/40/37 41/120/88 60/41/38
|
||||
f 23/42/39 60/41/38 62/77/69
|
||||
f 60/41/38 11/122/89 89/123/40
|
||||
f 89/123/40 29/124/90 47/126/41
|
||||
f 3/2/2 30/1/1 45/127/92
|
||||
f 30/1/1 12/27/25 90/46/43
|
||||
f 90/162/43 65/51/48 63/129/44
|
||||
f 42/26/24 91/130/93 12/27/25
|
||||
f 2/105/80 43/131/86 42/26/24
|
||||
f 43/163/86 13/76/36 91/132/93
|
||||
f 24/50/47 64/49/46 66/133/71
|
||||
f 64/49/46 13/76/36 92/52/49
|
||||
f 92/52/49 31/9/9 49/53/28
|
||||
f 32/97/75 93/134/94 4/17/15
|
||||
f 14/21/19 69/135/54 32/137/75
|
||||
f 69/59/54 25/58/53 93/138/94
|
||||
f 14/21/19 44/3/3 69/135/54
|
||||
f 44/3/3 3/2/2 94/139/95
|
||||
f 45/164/92 15/82/45 94/140/95
|
||||
f 68/57/52 95/60/55 25/58/53
|
||||
f 15/82/45 33/12/10 68/57/52
|
||||
f 95/60/55 33/12/10 50/61/4
|
||||
f 8/15/13 36/141/85 34/87/72
|
||||
f 36/141/85 18/142/84 96/63/56
|
||||
f 96/63/56 73/143/96 71/64/57
|
||||
f 18/113/84 48/111/83 73/145/96
|
||||
f 48/165/83 5/125/91 97/146/58
|
||||
f 97/146/58 29/124/90 72/147/59
|
||||
f 26/67/60 72/66/59 74/70/63
|
||||
f 72/66/59 11/149/89 98/148/97
|
||||
f 98/148/97 41/150/88 40/68/61
|
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 fColor;
|
||||
|
||||
void main(){
|
||||
FragColor = vec4(fColor, 1.0);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec2 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec3 fColor;
|
||||
|
||||
uniform vec2 offsets[100];
|
||||
|
||||
void main()
|
||||
{
|
||||
// gl_InstanceID<49><44>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>
|
||||
vec2 offset = offsets[gl_InstanceID];
|
||||
gl_Position = vec4(aPos + offset, 0.0, 1.0);
|
||||
fColor = aColor;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 fColor;
|
||||
|
||||
void main(){
|
||||
FragColor = vec4(fColor, 1.0);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec2 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aOffset;
|
||||
|
||||
out vec3 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// quad<61>Ĵ<EFBFBD>С<EFBFBD><EFBFBD><F0BDA5B1><EFBFBD>0.01<EFBFBD><EFBFBD>1
|
||||
vec2 pos = aPos * (gl_InstanceID / 100.0);
|
||||
gl_Position = vec4(pos + aOffset, 0.0, 1.0);
|
||||
fColor = aColor;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
|
||||
void main(){
|
||||
FragColor = texture(texture_diffuse1, TexCoords);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
TexCoords = aTexCoords;
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
|
||||
void main(){
|
||||
FragColor = texture(texture_diffuse1, TexCoords);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
layout (location = 3) in mat4 instanceMatrix;
|
||||
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
TexCoords = aTexCoords;
|
||||
gl_Position = projection * view * instanceMatrix* vec4(aPos, 1.0);
|
||||
}
|
BIN
LearnOpenGL/assest/textures/planet.rar
Normal file
BIN
LearnOpenGL/assest/textures/planet.rar
Normal file
Binary file not shown.
BIN
LearnOpenGL/assest/textures/rock.rar
Normal file
BIN
LearnOpenGL/assest/textures/rock.rar
Normal file
Binary file not shown.
@ -17,7 +17,8 @@ enum Camera_Movement {
|
||||
// Default camera values
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
//const float SPEED = 2.5f;
|
||||
const float SPEED = 20.5f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
|
@ -121,6 +121,16 @@ public:
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec2(const std::string& name, float x, float y) const
|
||||
{
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec2(const std::string& name, glm::vec2& vec) const
|
||||
{
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), vec.x, vec.y);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec3(const std::string& name, float x, float y, float z) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
|
@ -99,11 +99,16 @@ int main()
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
//float vertices[] = {
|
||||
//-0.5f, -0.5f, 0.0f, // left
|
||||
// 0.5f, -0.5f, 0.0f, // right
|
||||
// 0.0f, 0.5f, 0.0f // top
|
||||
//};
|
||||
float vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
};
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first Triangle
|
||||
@ -157,7 +162,7 @@ int main()
|
||||
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
// glBindVertexArray(0); // no need to unbind it every time
|
||||
//glBindVertexArray(0); // no need to unbind it every time
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "Shader/Shader.h"
|
||||
#include "Core/Shader/Shader.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
256
LearnOpenGL/src/Main/4高级OpenGL/4.10.1渲染大量物体-quad-实例化.cpp
Normal file
256
LearnOpenGL/src/Main/4高级OpenGL/4.10.1渲染大量物体-quad-实例化.cpp
Normal file
@ -0,0 +1,256 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Core/Shader/Shader.h"
|
||||
#include "Core/Camera/Camera.h"
|
||||
#include "Core/LoadModel/Model.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "MyFileSystem.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow* window);
|
||||
unsigned int loadTexture(const char* path);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 1280;
|
||||
const unsigned int SCR_HEIGHT = 720;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
int main()
|
||||
{
|
||||
// glfw: initialize and configure
|
||||
// ------------------------------
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// tell GLFW to capture our mouse
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure global opengl state
|
||||
// -----------------------------
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
Shader shader("assest/shader/4<><EFBFBD>OpenGL/6.10.1.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-quad-ʵ<><CAB5><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.1.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-quad-ʵ<><CAB5><EFBFBD><EFBFBD>.fs");
|
||||
float quadVertices[] = {
|
||||
// λ<><CEBB> // <20><>ɫ
|
||||
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
|
||||
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
|
||||
-0.05f, -0.05f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
|
||||
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
|
||||
0.05f, 0.05f, 0.0f, 1.0f, 1.0f
|
||||
};
|
||||
// quad VAO
|
||||
unsigned int quadVAO, quadVBO;
|
||||
glGenVertexArrays(1, &quadVAO);
|
||||
glGenBuffers(1, &quadVBO);
|
||||
glBindVertexArray(quadVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glm::vec2 translations[100];
|
||||
int index = 0;
|
||||
float offset = 0.1f;
|
||||
for (int y = -10; y < 10; y += 2)
|
||||
{
|
||||
for (int x = -10; x < 10; x += 2)
|
||||
{
|
||||
glm::vec2 translation;
|
||||
translation.x = (float)x / 10.0f + offset;
|
||||
translation.y = (float)y / 10.0f + offset;
|
||||
translations[index++] = translation;
|
||||
}
|
||||
}
|
||||
// <20>ϴ<EFBFBD>λ<EFBFBD>ø<EFBFBD>glsl<73><6C>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
||||
shader.use();
|
||||
for (unsigned int i = 0; i < 100; i++)
|
||||
{
|
||||
shader.setVec2(("offsets[" + to_string(i) + "]").c_str(), translations[i]);
|
||||
}
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// input
|
||||
// -----
|
||||
processInput(window);
|
||||
|
||||
// render
|
||||
// ------
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// quad
|
||||
glBindVertexArray(quadVAO);
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Է<EFBFBD><D4B7><EFBFBD>GPU<50><55>Ȼ<EFBFBD><C8BB>ʹ<EFBFBD><CAB9>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD><EFBFBD><EFBFBD>OpenGL<47><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD>ݻ<EFBFBD><DDBB>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100);
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
glDeleteVertexArrays(1, &quadVAO);
|
||||
glDeleteBuffers(1, &quadVBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
// make sure the viewport matches the new window dimensions; note that width and
|
||||
// height will be significantly larger than specified on retina displays.
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const* path)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
276
LearnOpenGL/src/Main/4高级OpenGL/4.10.2渲染大量物体-quad-实例化数组.cpp
Normal file
276
LearnOpenGL/src/Main/4高级OpenGL/4.10.2渲染大量物体-quad-实例化数组.cpp
Normal file
@ -0,0 +1,276 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Core/Shader/Shader.h"
|
||||
#include "Core/Camera/Camera.h"
|
||||
#include "Core/LoadModel/Model.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "MyFileSystem.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow* window);
|
||||
unsigned int loadTexture(const char* path);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 1280;
|
||||
const unsigned int SCR_HEIGHT = 720;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
int main()
|
||||
{
|
||||
// glfw: initialize and configure
|
||||
// ------------------------------
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// tell GLFW to capture our mouse
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure global opengl state
|
||||
// -----------------------------
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
Shader shader("assest/shader/4<><EFBFBD>OpenGL/6.10.2.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-quad-ʵ<><CAB5><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.2.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-quad-ʵ<><CAB5><EFBFBD><EFBFBD>.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
// uniform<72><6D><EFBFBD>飬ƫ<E9A3AC><C6AB>λ<EFBFBD><CEBB>
|
||||
glm::vec2 translations[100];
|
||||
int index = 0;
|
||||
float offset = 0.1f;
|
||||
for (int y = -10; y < 10; y += 2) {
|
||||
for (int x = -10; x < 10; x += 2) {
|
||||
glm::vec2 translation;
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1<><31>1֮<31><D6AE>
|
||||
translation.x = (float)x / 10.0f + offset;
|
||||
translation.y = (float)y / 10.0f + offset;
|
||||
translations[index++] = translation;
|
||||
}
|
||||
}
|
||||
// <20>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>黺<EFBFBD><E9BBBA><EFBFBD>洢
|
||||
unsigned int instanceVBO;
|
||||
glGenBuffers(1, &instanceVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 100, &translations[0], GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
float quadVertices[] = {
|
||||
// λ<><CEBB> // <20><>ɫ
|
||||
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
|
||||
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
|
||||
-0.05f, -0.05f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
|
||||
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
|
||||
0.05f, 0.05f, 0.0f, 1.0f, 1.0f
|
||||
};
|
||||
|
||||
// quad VAO
|
||||
unsigned int quadVAO, quadVBO;
|
||||
glGenVertexArrays(1, &quadVAO);
|
||||
glGenBuffers(1, &quadVBO);
|
||||
glBindVertexArray(quadVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>layout=2<><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
// <20><><EFBFBD><EFBFBD>layout=2<><32><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>ÿ2<C3BF><32>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glVertexAttribDivisor(2, 1);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// <20>ǵð<C7B5><C3B0><EFBFBD>shader<65><72><EFBFBD><EFBFBD>ʹû<CAB9><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD>uniform
|
||||
shader.use();
|
||||
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// input
|
||||
// -----
|
||||
processInput(window);
|
||||
|
||||
// render
|
||||
// ------
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// quad
|
||||
glBindVertexArray(quadVAO);
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Է<EFBFBD><D4B7><EFBFBD>GPU<50><55>Ȼ<EFBFBD><C8BB>ʹ<EFBFBD><CAB9>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD><EFBFBD><EFBFBD>OpenGL<47><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD>ݻ<EFBFBD><DDBB>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
glDeleteVertexArrays(1, &quadVAO);
|
||||
glDeleteBuffers(1, &quadVBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
// make sure the viewport matches the new window dimensions; note that width and
|
||||
// height will be significantly larger than specified on retina displays.
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const* path)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
268
LearnOpenGL/src/Main/4高级OpenGL/4.10.3渲染大量物体-行星带-无实例化.cpp
Normal file
268
LearnOpenGL/src/Main/4高级OpenGL/4.10.3渲染大量物体-行星带-无实例化.cpp
Normal file
@ -0,0 +1,268 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Core/Shader/Shader.h"
|
||||
#include "Core/Camera/Camera.h"
|
||||
#include "Core/LoadModel/Model.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "MyFileSystem.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow* window);
|
||||
unsigned int loadTexture(const char* path);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 1280;
|
||||
const unsigned int SCR_HEIGHT = 720;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
int main()
|
||||
{
|
||||
// glfw: initialize and configure
|
||||
// ------------------------------
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// tell GLFW to capture our mouse
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure global opengl state
|
||||
// -----------------------------
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
Shader shader("assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
|
||||
Model rock(FileSystem::getPath("assest/model/rock/rock.obj"));
|
||||
Model planet(FileSystem::getPath("assest/model/planet/planet.obj"));
|
||||
|
||||
// model<65><6C><EFBFBD>飬ʯͷ<CAAF><CDB7>ƫ<EFBFBD><C6AB>λ<EFBFBD><CEBB>
|
||||
unsigned int amount = 1000;
|
||||
glm::mat4* modelMatrices;
|
||||
modelMatrices = new glm::mat4[amount];
|
||||
srand(glfwGetTime());// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
float radius = 50.0f;
|
||||
float offset = 2.5f;
|
||||
for (unsigned int i = 0; i < amount; i++) {
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
// <20>Ƕȣ<C7B6>0-360<36><30>
|
||||
float angle = (float)i / (float)amount * 360.0f;
|
||||
// 1. λ<>ƣ<EFBFBD><C6A3>ֲ<EFBFBD><D6B2>ڰ뾶Ϊ radius <20><>Բ<EFBFBD><D4B2><EFBFBD>ϣ<EFBFBD>ƫ<EFBFBD>Ʒ<EFBFBD>Χ<EFBFBD><CEA7>[-0ffset, offset]
|
||||
// rand()<29><>ΧΪ0~RAND_MAX, 700 % 500 = 200 / 100 = 2 - 2.5 = -0.5
|
||||
float displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float x = sin(angle) * radius + displacement;
|
||||
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float y = displacement * 0.4f;
|
||||
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float z = cos(angle) * radius + displacement;
|
||||
model = glm::translate(model, glm::vec3(x, y, z));
|
||||
|
||||
// 2.<2E><><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD>0.05<EFBFBD><EFBFBD>0.25f֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
float scale = (rand() % 20) / 100.0f + 0.05;
|
||||
model = glm::scale(model, glm::vec3(scale));
|
||||
|
||||
// 3.<2E><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>(<28><>)<29><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
||||
float rotAngle = (rand() % 360);
|
||||
model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));
|
||||
|
||||
// 4. <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
modelMatrices[i] = model;
|
||||
}
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// input
|
||||
// -----
|
||||
processInput(window);
|
||||
|
||||
// render
|
||||
// ------
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
shader.use();
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setMat4("view", view);
|
||||
|
||||
// <20>滭<EFBFBD><E6BBAD><EFBFBD><EFBFBD>
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, -3.0f, 0.0f));
|
||||
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f));
|
||||
shader.setMat4("model", model);
|
||||
planet.Draw(shader);
|
||||
|
||||
// <20>滭ʯͷ
|
||||
for (unsigned int i = 0; i < amount; i++) {
|
||||
// <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB>
|
||||
shader.setMat4("model", modelMatrices[i]);
|
||||
rock.Draw(shader);
|
||||
}
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
// make sure the viewport matches the new window dimensions; note that width and
|
||||
// height will be significantly larger than specified on retina displays.
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const* path)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
304
LearnOpenGL/src/Main/4高级OpenGL/4.10.4渲染大量物体-行星带-实例化数组.cpp
Normal file
304
LearnOpenGL/src/Main/4高级OpenGL/4.10.4渲染大量物体-行星带-实例化数组.cpp
Normal file
@ -0,0 +1,304 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Core/Shader/Shader.h"
|
||||
#include "Core/Camera/Camera.h"
|
||||
#include "Core/LoadModel/Model.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "MyFileSystem.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow* window);
|
||||
unsigned int loadTexture(const char* path);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 1280;
|
||||
const unsigned int SCR_HEIGHT = 720;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 155.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
int main()
|
||||
{
|
||||
// glfw: initialize and configure
|
||||
// ------------------------------
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// tell GLFW to capture our mouse
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure global opengl state
|
||||
// -----------------------------
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
Shader planetshader("assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.fs");
|
||||
Shader rockshader("assest/shader/4<><EFBFBD>OpenGL/6.10.4.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.4.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
|
||||
Model planet(FileSystem::getPath("assest/model/planet/planet.obj"));
|
||||
Model rock(FileSystem::getPath("assest/model/rock/rock.obj"));
|
||||
|
||||
// model<65><6C><EFBFBD>飬ʯͷ<CAAF><CDB7>ƫ<EFBFBD><C6AB>λ<EFBFBD><CEBB>
|
||||
unsigned int amount = 100000;
|
||||
glm::mat4* modelMatrices;
|
||||
modelMatrices = new glm::mat4[amount];
|
||||
srand(static_cast<unsigned int>(glfwGetTime()));// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
float radius = 150.0f;
|
||||
float offset = 25.0f;
|
||||
for (unsigned int i = 0; i < amount; i++) {
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
// <20>Ƕȣ<C7B6>0-360<36><30>
|
||||
float angle = (float)i / (float)amount * 360.0f;
|
||||
// 1. λ<>ƣ<EFBFBD><C6A3>ֲ<EFBFBD><D6B2>ڰ뾶Ϊ radius <20><>Բ<EFBFBD><D4B2><EFBFBD>ϣ<EFBFBD>ƫ<EFBFBD>Ʒ<EFBFBD>Χ<EFBFBD><CEA7>[-0ffset, offset]
|
||||
// rand()<29><>ΧΪ0~RAND_MAX, 700 % 500 = 200 / 100 = 2 - 2.5 = -0.5
|
||||
float displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float x = sin(angle) * radius + displacement;
|
||||
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float y = displacement * 0.4f;
|
||||
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float z = cos(angle) * radius + displacement;
|
||||
model = glm::translate(model, glm::vec3(x, y, z));
|
||||
|
||||
// 2.<2E><><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD>0.05<EFBFBD><EFBFBD>0.25f֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
float scale = (rand() % 20) / 100.0f + 0.05;
|
||||
model = glm::scale(model, glm::vec3(scale));
|
||||
|
||||
// 3.<2E><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>(<28><>)<29><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
||||
float rotAngle = (rand() % 360);
|
||||
model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));
|
||||
|
||||
// 4. <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
modelMatrices[i] = model;
|
||||
}
|
||||
// <20><><EFBFBD>ø<EFBFBD>rock<63><6B>model
|
||||
unsigned int buffer;
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW);
|
||||
for (unsigned int i = 0; i < rock.meshes.size(); i++) {// rock.meshes.size() = 1
|
||||
unsigned int VAO = rock.meshes[i].VAO;
|
||||
glBindVertexArray(VAO);
|
||||
// <20><><EFBFBD><EFBFBD>mat4<74>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(glm::vec4)));
|
||||
glEnableVertexAttribArray(5);
|
||||
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(2 * sizeof(glm::vec4)));
|
||||
glEnableVertexAttribArray(6);
|
||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(3 * sizeof(glm::vec4)));
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>ÿ1<C3BF><31>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glVertexAttribDivisor(3, 1);
|
||||
glVertexAttribDivisor(4, 1);
|
||||
glVertexAttribDivisor(5, 1);
|
||||
glVertexAttribDivisor(6, 1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// input
|
||||
// -----
|
||||
processInput(window);
|
||||
|
||||
// render
|
||||
// ------
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
rockshader.use();
|
||||
rockshader.setMat4("projection", projection);
|
||||
rockshader.setMat4("view", view);
|
||||
planetshader.use();
|
||||
planetshader.setMat4("projection", projection);
|
||||
planetshader.setMat4("view", view);
|
||||
// <20>滭<EFBFBD><E6BBAD><EFBFBD><EFBFBD>
|
||||
planetshader.use();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, -3.0f, 0.0f));
|
||||
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f));
|
||||
planetshader.setMat4("model", model);
|
||||
planet.Draw(planetshader);
|
||||
|
||||
// <20>滭ʯͷ
|
||||
rockshader.use();
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ
|
||||
rockshader.setInt("texture_diffuse1", 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, rock.textures_loaded[0].id);;
|
||||
for (unsigned int i = 0; i < rock.meshes.size(); i++) {
|
||||
glBindVertexArray(rock.meshes[i].VAO);
|
||||
// ע<><D7A2><EFBFBD><EFBFBD>5<EFBFBD><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
glDrawElementsInstanced(GL_TRIANGLES, rock.meshes[i].indices.size(), GL_UNSIGNED_INT, 0, amount);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
// make sure the viewport matches the new window dimensions; note that width and
|
||||
// height will be significantly larger than specified on retina displays.
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const* path)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
@ -12,20 +12,17 @@
|
||||
|
||||
#include <iostream>
|
||||
#include "MyFileSystem.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow* window);
|
||||
unsigned int loadTexture(const char* path);
|
||||
unsigned int loadCubemap(vector<std::string> faces);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 155.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
@ -78,153 +75,79 @@ int main()
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
//Shader shader("6.2.cubemaps.vs", "6.2.cubemaps.fs");
|
||||
//Shader skyboxShader("6.2.skybox.vs", "6.2.skybox.fs");
|
||||
Shader shader("assest/shader/4<><EFBFBD>OpenGL/6.2.1.cube-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>պ<EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.2.1.cube-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>պ<EFBFBD>.fs");
|
||||
Shader skyboxShader("assest/shader/4<><EFBFBD>OpenGL/6.1.1.<2E><><EFBFBD>պ<EFBFBD>-<2D><>ͨЧ<CDA8><D0A7>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.1.1.<2E><><EFBFBD>պ<EFBFBD>-<2D><>ͨЧ<CDA8><D0A7>.fs");
|
||||
Shader asteroidShader("assest/shader/4<><EFBFBD>OpenGL/6.10.4.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.4.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.fs");
|
||||
Shader planetShader("assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.vs", "assest/shader/4<><EFBFBD>OpenGL/6.10.3.<2E><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD>Ǵ<EFBFBD>-<2D><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// load models
|
||||
// -----------
|
||||
Model rock(FileSystem::getPath("assest/model/rock/rock.obj"));
|
||||
Model planet(FileSystem::getPath("assest/model/planet/planet.obj"));
|
||||
|
||||
// generate a large list of semi-random model transformation matrices
|
||||
// ------------------------------------------------------------------
|
||||
float cubeVertices[] = {
|
||||
// positions // normals
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
unsigned int amount = 100000;
|
||||
glm::mat4* modelMatrices;
|
||||
modelMatrices = new glm::mat4[amount];
|
||||
srand(static_cast<unsigned int>(glfwGetTime())); // initialize random seed
|
||||
float radius = 150.0;
|
||||
float offset = 25.0f;
|
||||
for (unsigned int i = 0; i < amount; i++)
|
||||
{
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
// 1. translation: displace along circle with 'radius' in range [-offset, offset]
|
||||
float angle = (float)i / (float)amount * 360.0f;
|
||||
float displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float x = sin(angle) * radius + displacement;
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float y = displacement * 0.4f; // keep height of asteroid field smaller compared to width of x and z
|
||||
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
|
||||
float z = cos(angle) * radius + displacement;
|
||||
model = glm::translate(model, glm::vec3(x, y, z));
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
// 2. scale: Scale between 0.05 and 0.25f
|
||||
float scale = static_cast<float>((rand() % 20) / 100.0 + 0.05);
|
||||
model = glm::scale(model, glm::vec3(scale));
|
||||
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
// 3. rotation: add random rotation around a (semi)randomly picked rotation axis vector
|
||||
float rotAngle = static_cast<float>((rand() % 360));
|
||||
model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
// 4. now add to list of matrices
|
||||
modelMatrices[i] = model;
|
||||
}
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
// configure instanced array
|
||||
// -------------------------
|
||||
unsigned int buffer;
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW);
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
float skyboxVertices[] = {
|
||||
// positions
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
// set transformation matrices as an instance vertex attribute (with divisor 1)
|
||||
// note: we're cheating a little by taking the, now publicly declared, VAO of the model's mesh(es) and adding new vertexAttribPointers
|
||||
// normally you'd want to do this in a more organized fashion, but for learning purposes this will do.
|
||||
// -----------------------------------------------------------------------------------------------------------------------------------
|
||||
for (unsigned int i = 0; i < rock.meshes.size(); i++)
|
||||
{
|
||||
unsigned int VAO = rock.meshes[i].VAO;
|
||||
glBindVertexArray(VAO);
|
||||
// set attribute pointers for matrix (4 times vec4)
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(glm::vec4)));
|
||||
glEnableVertexAttribArray(5);
|
||||
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(2 * sizeof(glm::vec4)));
|
||||
glEnableVertexAttribArray(6);
|
||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(3 * sizeof(glm::vec4)));
|
||||
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
glVertexAttribDivisor(3, 1);
|
||||
glVertexAttribDivisor(4, 1);
|
||||
glVertexAttribDivisor(5, 1);
|
||||
glVertexAttribDivisor(6, 1);
|
||||
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f
|
||||
};
|
||||
|
||||
// cube VAO
|
||||
unsigned int cubeVAO, cubeVBO;
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &cubeVBO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
// skybox VAO
|
||||
unsigned int skyboxVAO, skyboxVBO;
|
||||
glGenVertexArrays(1, &skyboxVAO);
|
||||
glGenBuffers(1, &skyboxVBO);
|
||||
glBindVertexArray(skyboxVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
|
||||
// load textures
|
||||
// -------------
|
||||
//vector<std::string> faces
|
||||
//{
|
||||
// FileSystem::getPath("resources/textures/skybox/right.jpg"),
|
||||
// FileSystem::getPath("resources/textures/skybox/left.jpg"),
|
||||
// FileSystem::getPath("resources/textures/skybox/top.jpg"),
|
||||
// FileSystem::getPath("resources/textures/skybox/bottom.jpg"),
|
||||
// FileSystem::getPath("resources/textures/skybox/front.jpg"),
|
||||
// FileSystem::getPath("resources/textures/skybox/back.jpg"),
|
||||
//};
|
||||
//unsigned int cubemapTexture = loadCubemap(faces);
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>պ<EFBFBD>
|
||||
vector<std::string> faces{
|
||||
FileSystem::getPath("assest/textures/skybox/right.jpg"),
|
||||
FileSystem::getPath("assest/textures/skybox/left.jpg"),
|
||||
FileSystem::getPath("assest/textures/skybox/top.jpg"),
|
||||
FileSystem::getPath("assest/textures/skybox/bottom.jpg"),
|
||||
FileSystem::getPath("assest/textures/skybox/front.jpg"),
|
||||
FileSystem::getPath("assest/textures/skybox/back.jpg")
|
||||
};
|
||||
unsigned int cubemapTexture = loadCubemap(faces);
|
||||
|
||||
// shader configuration
|
||||
// --------------------
|
||||
shader.use();
|
||||
shader.setInt("skybox", 0);
|
||||
|
||||
skyboxShader.use();
|
||||
skyboxShader.setInt("skybox", 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// render loop
|
||||
// -----------
|
||||
@ -245,35 +168,34 @@ int main()
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// draw scene as normal
|
||||
shader.use();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
// configure transformation matrices
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
shader.setMat4("model", model);
|
||||
shader.setMat4("view", view);
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setVec3("cameraPos", camera.Position);
|
||||
// cubes
|
||||
glBindVertexArray(cubeVAO);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glBindVertexArray(0);
|
||||
asteroidShader.use();
|
||||
asteroidShader.setMat4("projection", projection);
|
||||
asteroidShader.setMat4("view", view);
|
||||
planetShader.use();
|
||||
planetShader.setMat4("projection", projection);
|
||||
planetShader.setMat4("view", view);
|
||||
|
||||
// draw skybox as last
|
||||
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
|
||||
skyboxShader.use();
|
||||
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
|
||||
skyboxShader.setMat4("view", view);
|
||||
skyboxShader.setMat4("projection", projection);
|
||||
// skybox cube
|
||||
glBindVertexArray(skyboxVAO);
|
||||
// draw planet
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, -3.0f, 0.0f));
|
||||
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f));
|
||||
planetShader.setMat4("model", model);
|
||||
planet.Draw(planetShader);
|
||||
|
||||
// draw meteorites
|
||||
asteroidShader.use();
|
||||
asteroidShader.setInt("texture_diffuse1", 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glBindVertexArray(0);
|
||||
glDepthFunc(GL_LESS); // set depth function back to default
|
||||
glBindTexture(GL_TEXTURE_2D, rock.textures_loaded[0].id); // note: we also made the textures_loaded vector public (instead of private) from the model class.
|
||||
for (unsigned int i = 0; i < rock.meshes.size(); i++)
|
||||
{
|
||||
glBindVertexArray(rock.meshes[i].VAO);
|
||||
glDrawElementsInstanced(GL_TRIANGLES, static_cast<unsigned int>(rock.meshes[i].indices.size()), GL_UNSIGNED_INT, 0, amount);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
@ -281,13 +203,6 @@ int main()
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteVertexArrays(1, &skyboxVAO);
|
||||
glDeleteBuffers(1, &cubeVBO);
|
||||
glDeleteBuffers(1, &skyboxVBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
@ -324,6 +239,7 @@ void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
@ -345,82 +261,4 @@ void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const* path)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
// loads a cubemap texture from 6 individual texture faces
|
||||
// order:
|
||||
// +X (right)
|
||||
// -X (left)
|
||||
// +Y (top)
|
||||
// -Y (bottom)
|
||||
// +Z (front)
|
||||
// -Z (back)
|
||||
// -------------------------------------------------------
|
||||
unsigned int loadCubemap(vector<std::string> faces)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
for (unsigned int i = 0; i < faces.size(); i++)
|
||||
{
|
||||
unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
}
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
return textureID;
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user