初次提交
This commit is contained in:
9
第17章 游戏的心脏——物理引擎/Sample17_4/.classpath
Normal file
9
第17章 游戏的心脏——物理引擎/Sample17_4/.classpath
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="lib" path="lib/jbullet.jar"/>
|
||||
<classpathentry kind="lib" path="lib/vecmath.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
33
第17章 游戏的心脏——物理引擎/Sample17_4/.project
Normal file
33
第17章 游戏的心脏——物理引擎/Sample17_4/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample17_4</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
15
第17章 游戏的心脏——物理引擎/Sample17_4/AndroidManifest.xml
Normal file
15
第17章 游戏的心脏——物理引擎/Sample17_4/AndroidManifest.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" package="com.bn.Sample17_4">
|
||||
<uses-sdk android:targetSdkVersion="8" android:minSdkVersion="8"></uses-sdk>
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name="com.bn.Sample17_4.Sample17_4_Activity"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
864
第17章 游戏的心脏——物理引擎/Sample17_4/assets/ch.obj
Normal file
864
第17章 游戏的心脏——物理引擎/Sample17_4/assets/ch.obj
Normal file
@@ -0,0 +1,864 @@
|
||||
# Max2Obj Version 4.0 Mar 10th, 2001
|
||||
#
|
||||
# object (null) to come ...
|
||||
#
|
||||
v 0.527905 0.358035 0.024609
|
||||
v 0.522275 0.389330 0.024609
|
||||
v 0.539495 0.389330 0.024609
|
||||
v 0.563671 0.358035 0.024609
|
||||
v 0.460400 0.358035 0.279051
|
||||
v 0.455529 0.389330 0.276190
|
||||
v 0.470428 0.389330 0.284941
|
||||
v 0.491344 0.358035 0.297225
|
||||
v 0.281623 0.358035 0.457828
|
||||
v 0.278762 0.389330 0.452957
|
||||
v 0.287512 0.389330 0.467856
|
||||
v 0.299797 0.358035 0.488772
|
||||
v 0.027181 0.358035 0.525333
|
||||
v 0.027181 0.389330 0.519703
|
||||
v 0.027181 0.389330 0.536924
|
||||
v 0.027181 0.358035 0.561099
|
||||
v -0.241568 0.358035 0.457828
|
||||
v -0.228639 0.389330 0.452957
|
||||
v -0.233681 0.389330 0.467856
|
||||
v -0.245436 0.358035 0.488772
|
||||
v -0.413191 0.358035 0.279051
|
||||
v -0.403287 0.389330 0.276190
|
||||
v -0.416331 0.389330 0.284941
|
||||
v -0.436983 0.358035 0.297225
|
||||
v -0.473543 0.358035 0.024609
|
||||
v -0.467913 0.389330 0.024609
|
||||
v -0.485134 0.389330 0.024609
|
||||
v -0.509309 0.358035 0.024609
|
||||
v -0.406038 0.358035 -0.229833
|
||||
v -0.401167 0.389330 -0.226972
|
||||
v -0.416067 0.389330 -0.235723
|
||||
v -0.436983 0.358035 -0.248008
|
||||
v -0.227261 0.358035 -0.408610
|
||||
v -0.224400 0.389330 -0.403739
|
||||
v -0.233151 0.389330 -0.418638
|
||||
v -0.245436 0.358035 -0.439554
|
||||
v 0.027181 0.358035 -0.476115
|
||||
v 0.027181 0.389330 -0.470485
|
||||
v 0.027181 0.389330 -0.487706
|
||||
v 0.027181 0.358035 -0.511881
|
||||
v 0.281623 0.358035 -0.408610
|
||||
v 0.278762 0.389330 -0.403739
|
||||
v 0.287513 0.389330 -0.418638
|
||||
v 0.299797 0.358035 -0.439554
|
||||
v 0.460400 0.358035 -0.229833
|
||||
v 0.455529 0.389330 -0.226972
|
||||
v 0.470428 0.389330 -0.235723
|
||||
v 0.491344 0.358035 -0.248008
|
||||
v 0.649774 0.171257 0.024609
|
||||
v 0.716007 -0.009560 0.024609
|
||||
v 0.742501 -0.178455 0.024609
|
||||
v 0.565839 0.171257 0.340979
|
||||
v 0.623143 -0.009560 0.374635
|
||||
v 0.646065 -0.178455 0.388097
|
||||
v 0.343550 0.171257 0.563267
|
||||
v 0.377207 -0.009560 0.620571
|
||||
v 0.390669 -0.178455 0.643493
|
||||
v 0.027181 0.171257 0.647202
|
||||
v 0.027181 -0.009560 0.713436
|
||||
v 0.027181 -0.178455 0.739929
|
||||
v -0.289189 0.171257 0.563267
|
||||
v -0.322845 -0.009560 0.620571
|
||||
v -0.336308 -0.178455 0.643493
|
||||
v -0.511478 0.171257 0.340978
|
||||
v -0.568782 -0.009560 0.374635
|
||||
v -0.591704 -0.178455 0.388097
|
||||
v -0.595413 0.171257 0.024609
|
||||
v -0.661646 -0.009560 0.024609
|
||||
v -0.688139 -0.178455 0.024609
|
||||
v -0.511478 0.171257 -0.291761
|
||||
v -0.568782 -0.009560 -0.325417
|
||||
v -0.591704 -0.178455 -0.338880
|
||||
v -0.289189 0.171257 -0.514050
|
||||
v -0.322845 -0.009560 -0.571354
|
||||
v -0.336308 -0.178455 -0.594275
|
||||
v 0.027181 0.171257 -0.597984
|
||||
v 0.027181 -0.009560 -0.664218
|
||||
v 0.027181 -0.178455 -0.690711
|
||||
v 0.343550 0.171257 -0.514050
|
||||
v 0.377207 -0.009560 -0.571354
|
||||
v 0.390669 -0.178455 -0.594275
|
||||
v 0.565839 0.171257 -0.291761
|
||||
v 0.623143 -0.009560 -0.325417
|
||||
v 0.646065 -0.178455 -0.338880
|
||||
v 0.696137 -0.313571 0.024609
|
||||
v 0.610034 -0.400999 0.024609
|
||||
v 0.563671 -0.446700 0.024609
|
||||
v 0.605952 -0.313571 0.364538
|
||||
v 0.531457 -0.400999 0.320785
|
||||
v 0.491344 -0.446700 0.297225
|
||||
v 0.367110 -0.313571 0.603380
|
||||
v 0.323356 -0.400999 0.528885
|
||||
v 0.299797 -0.446700 0.488772
|
||||
v 0.027181 -0.313571 0.693565
|
||||
v 0.027181 -0.400999 0.607462
|
||||
v 0.027181 -0.446700 0.561099
|
||||
v -0.312748 -0.313571 0.603380
|
||||
v -0.268995 -0.400999 0.528885
|
||||
v -0.245436 -0.446700 0.488772
|
||||
v -0.551591 -0.313571 0.364538
|
||||
v -0.477095 -0.400999 0.320785
|
||||
v -0.436983 -0.446700 0.297225
|
||||
v -0.641776 -0.313571 0.024609
|
||||
v -0.555673 -0.400999 0.024609
|
||||
v -0.509309 -0.446700 0.024609
|
||||
v -0.551591 -0.313571 -0.315320
|
||||
v -0.477095 -0.400999 -0.271567
|
||||
v -0.436983 -0.446700 -0.248008
|
||||
v -0.312748 -0.313571 -0.554163
|
||||
v -0.268995 -0.400999 -0.479667
|
||||
v -0.245436 -0.446700 -0.439554
|
||||
v 0.027181 -0.313571 -0.644348
|
||||
v 0.027181 -0.400999 -0.558244
|
||||
v 0.027181 -0.446700 -0.511881
|
||||
v 0.367110 -0.313571 -0.554163
|
||||
v 0.323357 -0.400999 -0.479667
|
||||
v 0.299797 -0.446700 -0.439554
|
||||
v 0.605952 -0.313571 -0.315320
|
||||
v 0.531457 -0.400999 -0.271567
|
||||
v 0.491344 -0.446700 -0.248008
|
||||
v 0.537840 -0.472531 0.024609
|
||||
v 0.392789 -0.492401 0.024609
|
||||
v 0.027181 -0.500349 0.024609
|
||||
v 0.468995 -0.472531 0.284099
|
||||
v 0.343499 -0.492401 0.210392
|
||||
v 0.286671 -0.472531 0.466423
|
||||
v 0.212964 -0.492401 0.340927
|
||||
v 0.027181 -0.472531 0.535268
|
||||
v 0.027181 -0.492401 0.390217
|
||||
v -0.232310 -0.472531 0.466423
|
||||
v -0.158602 -0.492401 0.340927
|
||||
v -0.414634 -0.472531 0.284099
|
||||
v -0.289138 -0.492401 0.210392
|
||||
v -0.483478 -0.472531 0.024609
|
||||
v -0.338427 -0.492401 0.024609
|
||||
v -0.414634 -0.472531 -0.234882
|
||||
v -0.289138 -0.492401 -0.161174
|
||||
v -0.232310 -0.472531 -0.417206
|
||||
v -0.158602 -0.492401 -0.291710
|
||||
v 0.027181 -0.472531 -0.486050
|
||||
v 0.027181 -0.492401 -0.340999
|
||||
v 0.286671 -0.472531 -0.417206
|
||||
v 0.212964 -0.492401 -0.291710
|
||||
v 0.468995 -0.472531 -0.234882
|
||||
v 0.343499 -0.492401 -0.161174
|
||||
v -0.545075 0.223912 0.024609
|
||||
v -0.758347 0.220932 0.024609
|
||||
v -0.892138 0.200068 0.024609
|
||||
v -0.938501 0.143439 0.024609
|
||||
v -0.535803 0.244776 0.096141
|
||||
v -0.771054 0.241023 0.096141
|
||||
v -0.916522 0.214750 0.096141
|
||||
v -0.966319 0.143439 0.096141
|
||||
v -0.518582 0.283522 0.096141
|
||||
v -0.794652 0.278334 0.096141
|
||||
v -0.961806 0.242016 0.096141
|
||||
v -1.017981 0.143439 0.096141
|
||||
v -0.509309 0.304386 0.024609
|
||||
v -0.807359 0.298425 0.024609
|
||||
v -0.986189 0.256698 0.024609
|
||||
v -1.045799 0.143439 0.024609
|
||||
v -0.518582 0.283522 -0.046923
|
||||
v -0.794652 0.278334 -0.046923
|
||||
v -0.961806 0.242016 -0.046923
|
||||
v -1.017981 0.143439 -0.046923
|
||||
v -0.535803 0.244776 -0.046923
|
||||
v -0.771054 0.241023 -0.046923
|
||||
v -0.916522 0.214750 -0.046923
|
||||
v -0.966319 0.143439 -0.046923
|
||||
v -0.913333 0.042102 0.024609
|
||||
v -0.832528 -0.077118 0.024609
|
||||
v -0.688139 -0.178455 0.024609
|
||||
v -0.936686 0.027935 0.096141
|
||||
v -0.843174 -0.097724 0.096141
|
||||
v -0.678867 -0.206273 0.096141
|
||||
v -0.980057 0.001626 0.096141
|
||||
v -0.862946 -0.135992 0.096141
|
||||
v -0.661646 -0.257935 0.096141
|
||||
v -1.003410 -0.012541 0.024609
|
||||
v -0.873593 -0.156598 0.024609
|
||||
v -0.652373 -0.285753 0.024609
|
||||
v -0.980057 0.001626 -0.046923
|
||||
v -0.862946 -0.135992 -0.046923
|
||||
v -0.661646 -0.257935 -0.046923
|
||||
v -0.936686 0.027935 -0.046923
|
||||
v -0.843174 -0.097724 -0.046923
|
||||
v -0.678867 -0.206273 -0.046923
|
||||
v 0.635203 0.009316 0.024609
|
||||
v 0.839201 0.075881 0.024609
|
||||
v 0.908084 0.219938 0.024609
|
||||
v 0.992863 0.358035 0.024609
|
||||
v 0.635203 -0.067183 0.181979
|
||||
v 0.863928 0.026942 0.156634
|
||||
v 0.938993 0.201651 0.109564
|
||||
v 1.048499 0.358035 0.084219
|
||||
v 0.635203 -0.209254 0.181979
|
||||
v 0.909850 -0.063945 0.156634
|
||||
v 0.996395 0.167688 0.109564
|
||||
v 1.151823 0.358035 0.084219
|
||||
v 0.635203 -0.285753 0.024609
|
||||
v 0.934577 -0.112884 0.024609
|
||||
v 1.027304 0.149400 0.024609
|
||||
v 1.207459 0.358035 0.024609
|
||||
v 0.635203 -0.209254 -0.132762
|
||||
v 0.909850 -0.063945 -0.107416
|
||||
v 0.996395 0.167688 -0.060346
|
||||
v 1.151823 0.358035 -0.035001
|
||||
v 0.635203 -0.067183 -0.132762
|
||||
v 0.863928 0.026942 -0.107416
|
||||
v 0.938993 0.201651 -0.060346
|
||||
v 1.048499 0.358035 -0.035001
|
||||
v 1.025979 0.375918 0.024609
|
||||
v 1.043200 0.375918 0.024609
|
||||
v 1.028629 0.358035 0.024609
|
||||
v 1.085050 0.377463 0.078037
|
||||
v 1.093856 0.377850 0.066557
|
||||
v 1.065719 0.358035 0.060375
|
||||
v 1.194752 0.380333 0.078037
|
||||
v 1.187932 0.381437 0.066557
|
||||
v 1.134602 0.358035 0.060375
|
||||
v 1.253822 0.381879 0.024609
|
||||
v 1.238588 0.383369 0.024609
|
||||
v 1.171693 0.358035 0.024609
|
||||
v 1.194752 0.380333 -0.028819
|
||||
v 1.187932 0.381437 -0.017339
|
||||
v 1.134602 0.358035 -0.011157
|
||||
v 1.085050 0.377463 -0.028819
|
||||
v 1.093856 0.377850 -0.017339
|
||||
v 1.065719 0.358035 -0.011157
|
||||
v 0.027181 0.626280 0.024609
|
||||
v 0.156998 0.596475 0.024609
|
||||
v 0.111959 0.530904 0.024609
|
||||
v 0.098713 0.465333 0.024609
|
||||
v 0.139567 0.596475 0.090717
|
||||
v 0.100565 0.530904 0.067760
|
||||
v 0.089069 0.465333 0.060958
|
||||
v 0.093288 0.596475 0.136996
|
||||
v 0.070331 0.530904 0.097993
|
||||
v 0.063530 0.465333 0.086497
|
||||
v 0.027181 0.596475 0.154426
|
||||
v 0.027181 0.530904 0.109388
|
||||
v 0.027181 0.465333 0.096141
|
||||
v -0.038927 0.596475 0.136996
|
||||
v -0.015970 0.530904 0.097993
|
||||
v -0.009168 0.465333 0.086497
|
||||
v -0.085206 0.596475 0.090717
|
||||
v -0.046204 0.530904 0.067760
|
||||
v -0.034708 0.465333 0.060958
|
||||
v -0.102637 0.596475 0.024609
|
||||
v -0.057598 0.530904 0.024609
|
||||
v -0.044351 0.465333 0.024609
|
||||
v -0.085206 0.596475 -0.041499
|
||||
v -0.046204 0.530904 -0.018542
|
||||
v -0.034708 0.465333 -0.011740
|
||||
v -0.038927 0.596475 -0.087778
|
||||
v -0.015970 0.530904 -0.048776
|
||||
v -0.009168 0.465333 -0.037280
|
||||
v 0.027181 0.596475 -0.105208
|
||||
v 0.027181 0.530904 -0.060170
|
||||
v 0.027181 0.465333 -0.046923
|
||||
v 0.093288 0.596475 -0.087778
|
||||
v 0.070331 0.530904 -0.048776
|
||||
v 0.063530 0.465333 -0.037280
|
||||
v 0.139567 0.596475 -0.041499
|
||||
v 0.100565 0.530904 -0.018542
|
||||
v 0.089069 0.465333 -0.011740
|
||||
v 0.232504 0.425593 0.024609
|
||||
v 0.406035 0.397775 0.024609
|
||||
v 0.492139 0.358035 0.024609
|
||||
v 0.204823 0.425593 0.128944
|
||||
v 0.354960 0.397775 0.217123
|
||||
v 0.429455 0.358035 0.260876
|
||||
v 0.131515 0.425593 0.202252
|
||||
v 0.219695 0.397775 0.352388
|
||||
v 0.263448 0.358035 0.426884
|
||||
v 0.027181 0.425593 0.229932
|
||||
v 0.027181 0.397775 0.403464
|
||||
v 0.027181 0.358035 0.489567
|
||||
v -0.077154 0.425593 0.202252
|
||||
v -0.165334 0.397775 0.352388
|
||||
v -0.209087 0.358035 0.426884
|
||||
v -0.150462 0.425593 0.128944
|
||||
v -0.300599 0.397775 0.217123
|
||||
v -0.375094 0.358035 0.260876
|
||||
v -0.178143 0.425593 0.024609
|
||||
v -0.351674 0.397775 0.024609
|
||||
v -0.437777 0.358035 0.024609
|
||||
v -0.150462 0.425593 -0.079726
|
||||
v -0.300599 0.397775 -0.167905
|
||||
v -0.375094 0.358035 -0.211659
|
||||
v -0.077154 0.425593 -0.153034
|
||||
v -0.165334 0.397775 -0.303171
|
||||
v -0.209087 0.358035 -0.377666
|
||||
v 0.027181 0.425593 -0.180714
|
||||
v 0.027181 0.397775 -0.354246
|
||||
v 0.027181 0.358035 -0.440349
|
||||
v 0.131515 0.425593 -0.153034
|
||||
v 0.219695 0.397775 -0.303171
|
||||
v 0.263448 0.358035 -0.377666
|
||||
v 0.204823 0.425593 -0.079726
|
||||
v 0.354960 0.397775 -0.167905
|
||||
v 0.429455 0.358035 -0.211659
|
||||
# 302 vertices
|
||||
|
||||
g (null)
|
||||
f 1 5 6
|
||||
f 6 2 1
|
||||
f 2 6 7
|
||||
f 7 3 2
|
||||
f 3 7 8
|
||||
f 8 4 3
|
||||
f 5 9 10
|
||||
f 10 6 5
|
||||
f 6 10 11
|
||||
f 11 7 6
|
||||
f 7 11 12
|
||||
f 12 8 7
|
||||
f 9 13 14
|
||||
f 14 10 9
|
||||
f 10 14 15
|
||||
f 15 11 10
|
||||
f 11 15 16
|
||||
f 16 12 11
|
||||
f 13 17 18
|
||||
f 18 14 13
|
||||
f 14 18 19
|
||||
f 19 15 14
|
||||
f 15 19 20
|
||||
f 20 16 15
|
||||
f 17 21 22
|
||||
f 22 18 17
|
||||
f 18 22 23
|
||||
f 23 19 18
|
||||
f 19 23 24
|
||||
f 24 20 19
|
||||
f 21 25 26
|
||||
f 26 22 21
|
||||
f 22 26 27
|
||||
f 27 23 22
|
||||
f 23 27 28
|
||||
f 28 24 23
|
||||
f 25 29 30
|
||||
f 30 26 25
|
||||
f 26 30 31
|
||||
f 31 27 26
|
||||
f 27 31 32
|
||||
f 32 28 27
|
||||
f 29 33 34
|
||||
f 34 30 29
|
||||
f 30 34 35
|
||||
f 35 31 30
|
||||
f 31 35 36
|
||||
f 36 32 31
|
||||
f 33 37 38
|
||||
f 38 34 33
|
||||
f 34 38 39
|
||||
f 39 35 34
|
||||
f 35 39 40
|
||||
f 40 36 35
|
||||
f 37 41 42
|
||||
f 42 38 37
|
||||
f 38 42 43
|
||||
f 43 39 38
|
||||
f 39 43 44
|
||||
f 44 40 39
|
||||
f 41 45 46
|
||||
f 46 42 41
|
||||
f 42 46 47
|
||||
f 47 43 42
|
||||
f 43 47 48
|
||||
f 48 44 43
|
||||
f 45 1 2
|
||||
f 2 46 45
|
||||
f 46 2 3
|
||||
f 3 47 46
|
||||
f 47 3 4
|
||||
f 4 48 47
|
||||
f 4 8 52
|
||||
f 52 49 4
|
||||
f 49 52 53
|
||||
f 53 50 49
|
||||
f 50 53 54
|
||||
f 54 51 50
|
||||
f 8 12 55
|
||||
f 55 52 8
|
||||
f 52 55 56
|
||||
f 56 53 52
|
||||
f 53 56 57
|
||||
f 57 54 53
|
||||
f 12 16 58
|
||||
f 58 55 12
|
||||
f 55 58 59
|
||||
f 59 56 55
|
||||
f 56 59 60
|
||||
f 60 57 56
|
||||
f 16 20 61
|
||||
f 61 58 16
|
||||
f 58 61 62
|
||||
f 62 59 58
|
||||
f 59 62 63
|
||||
f 63 60 59
|
||||
f 20 24 64
|
||||
f 64 61 20
|
||||
f 61 64 65
|
||||
f 65 62 61
|
||||
f 62 65 66
|
||||
f 66 63 62
|
||||
f 24 28 67
|
||||
f 67 64 24
|
||||
f 64 67 68
|
||||
f 68 65 64
|
||||
f 65 68 69
|
||||
f 69 66 65
|
||||
f 28 32 70
|
||||
f 70 67 28
|
||||
f 67 70 71
|
||||
f 71 68 67
|
||||
f 68 71 72
|
||||
f 72 69 68
|
||||
f 32 36 73
|
||||
f 73 70 32
|
||||
f 70 73 74
|
||||
f 74 71 70
|
||||
f 71 74 75
|
||||
f 75 72 71
|
||||
f 36 40 76
|
||||
f 76 73 36
|
||||
f 73 76 77
|
||||
f 77 74 73
|
||||
f 74 77 78
|
||||
f 78 75 74
|
||||
f 40 44 79
|
||||
f 79 76 40
|
||||
f 76 79 80
|
||||
f 80 77 76
|
||||
f 77 80 81
|
||||
f 81 78 77
|
||||
f 44 48 82
|
||||
f 82 79 44
|
||||
f 79 82 83
|
||||
f 83 80 79
|
||||
f 80 83 84
|
||||
f 84 81 80
|
||||
f 48 4 49
|
||||
f 49 82 48
|
||||
f 82 49 50
|
||||
f 50 83 82
|
||||
f 83 50 51
|
||||
f 51 84 83
|
||||
f 51 54 88
|
||||
f 88 85 51
|
||||
f 85 88 89
|
||||
f 89 86 85
|
||||
f 86 89 90
|
||||
f 90 87 86
|
||||
f 54 57 91
|
||||
f 91 88 54
|
||||
f 88 91 92
|
||||
f 92 89 88
|
||||
f 89 92 93
|
||||
f 93 90 89
|
||||
f 57 60 94
|
||||
f 94 91 57
|
||||
f 91 94 95
|
||||
f 95 92 91
|
||||
f 92 95 96
|
||||
f 96 93 92
|
||||
f 60 63 97
|
||||
f 97 94 60
|
||||
f 94 97 98
|
||||
f 98 95 94
|
||||
f 95 98 99
|
||||
f 99 96 95
|
||||
f 63 66 100
|
||||
f 100 97 63
|
||||
f 97 100 101
|
||||
f 101 98 97
|
||||
f 98 101 102
|
||||
f 102 99 98
|
||||
f 66 69 103
|
||||
f 103 100 66
|
||||
f 100 103 104
|
||||
f 104 101 100
|
||||
f 101 104 105
|
||||
f 105 102 101
|
||||
f 69 72 106
|
||||
f 106 103 69
|
||||
f 103 106 107
|
||||
f 107 104 103
|
||||
f 104 107 108
|
||||
f 108 105 104
|
||||
f 72 75 109
|
||||
f 109 106 72
|
||||
f 106 109 110
|
||||
f 110 107 106
|
||||
f 107 110 111
|
||||
f 111 108 107
|
||||
f 75 78 112
|
||||
f 112 109 75
|
||||
f 109 112 113
|
||||
f 113 110 109
|
||||
f 110 113 114
|
||||
f 114 111 110
|
||||
f 78 81 115
|
||||
f 115 112 78
|
||||
f 112 115 116
|
||||
f 116 113 112
|
||||
f 113 116 117
|
||||
f 117 114 113
|
||||
f 81 84 118
|
||||
f 118 115 81
|
||||
f 115 118 119
|
||||
f 119 116 115
|
||||
f 116 119 120
|
||||
f 120 117 116
|
||||
f 84 51 85
|
||||
f 85 118 84
|
||||
f 118 85 86
|
||||
f 86 119 118
|
||||
f 119 86 87
|
||||
f 87 120 119
|
||||
f 87 90 124
|
||||
f 124 121 87
|
||||
f 121 124 125
|
||||
f 125 122 121
|
||||
f 122 125 123
|
||||
f 90 93 126
|
||||
f 126 124 90
|
||||
f 124 126 127
|
||||
f 127 125 124
|
||||
f 125 127 123
|
||||
f 93 96 128
|
||||
f 128 126 93
|
||||
f 126 128 129
|
||||
f 129 127 126
|
||||
f 127 129 123
|
||||
f 96 99 130
|
||||
f 130 128 96
|
||||
f 128 130 131
|
||||
f 131 129 128
|
||||
f 129 131 123
|
||||
f 99 102 132
|
||||
f 132 130 99
|
||||
f 130 132 133
|
||||
f 133 131 130
|
||||
f 131 133 123
|
||||
f 102 105 134
|
||||
f 134 132 102
|
||||
f 132 134 135
|
||||
f 135 133 132
|
||||
f 133 135 123
|
||||
f 105 108 136
|
||||
f 136 134 105
|
||||
f 134 136 137
|
||||
f 137 135 134
|
||||
f 135 137 123
|
||||
f 108 111 138
|
||||
f 138 136 108
|
||||
f 136 138 139
|
||||
f 139 137 136
|
||||
f 137 139 123
|
||||
f 111 114 140
|
||||
f 140 138 111
|
||||
f 138 140 141
|
||||
f 141 139 138
|
||||
f 139 141 123
|
||||
f 114 117 142
|
||||
f 142 140 114
|
||||
f 140 142 143
|
||||
f 143 141 140
|
||||
f 141 143 123
|
||||
f 117 120 144
|
||||
f 144 142 117
|
||||
f 142 144 145
|
||||
f 145 143 142
|
||||
f 143 145 123
|
||||
f 120 87 121
|
||||
f 121 144 120
|
||||
f 144 121 122
|
||||
f 122 145 144
|
||||
f 145 122 123
|
||||
f 146 150 151
|
||||
f 151 147 146
|
||||
f 147 151 152
|
||||
f 152 148 147
|
||||
f 148 152 153
|
||||
f 153 149 148
|
||||
f 150 154 155
|
||||
f 155 151 150
|
||||
f 151 155 156
|
||||
f 156 152 151
|
||||
f 152 156 157
|
||||
f 157 153 152
|
||||
f 154 158 159
|
||||
f 159 155 154
|
||||
f 155 159 160
|
||||
f 160 156 155
|
||||
f 156 160 161
|
||||
f 161 157 156
|
||||
f 158 162 163
|
||||
f 163 159 158
|
||||
f 159 163 164
|
||||
f 164 160 159
|
||||
f 160 164 165
|
||||
f 165 161 160
|
||||
f 162 166 167
|
||||
f 167 163 162
|
||||
f 163 167 168
|
||||
f 168 164 163
|
||||
f 164 168 169
|
||||
f 169 165 164
|
||||
f 166 146 147
|
||||
f 147 167 166
|
||||
f 167 147 148
|
||||
f 148 168 167
|
||||
f 168 148 149
|
||||
f 149 169 168
|
||||
f 149 153 173
|
||||
f 173 170 149
|
||||
f 170 173 174
|
||||
f 174 171 170
|
||||
f 171 174 175
|
||||
f 175 172 171
|
||||
f 153 157 176
|
||||
f 176 173 153
|
||||
f 173 176 177
|
||||
f 177 174 173
|
||||
f 174 177 178
|
||||
f 178 175 174
|
||||
f 157 161 179
|
||||
f 179 176 157
|
||||
f 176 179 180
|
||||
f 180 177 176
|
||||
f 177 180 181
|
||||
f 181 178 177
|
||||
f 161 165 182
|
||||
f 182 179 161
|
||||
f 179 182 183
|
||||
f 183 180 179
|
||||
f 180 183 184
|
||||
f 184 181 180
|
||||
f 165 169 185
|
||||
f 185 182 165
|
||||
f 182 185 186
|
||||
f 186 183 182
|
||||
f 183 186 187
|
||||
f 187 184 183
|
||||
f 169 149 170
|
||||
f 170 185 169
|
||||
f 185 170 171
|
||||
f 171 186 185
|
||||
f 186 171 172
|
||||
f 172 187 186
|
||||
f 188 192 193
|
||||
f 193 189 188
|
||||
f 189 193 194
|
||||
f 194 190 189
|
||||
f 190 194 195
|
||||
f 195 191 190
|
||||
f 192 196 197
|
||||
f 197 193 192
|
||||
f 193 197 198
|
||||
f 198 194 193
|
||||
f 194 198 199
|
||||
f 199 195 194
|
||||
f 196 200 201
|
||||
f 201 197 196
|
||||
f 197 201 202
|
||||
f 202 198 197
|
||||
f 198 202 203
|
||||
f 203 199 198
|
||||
f 200 204 205
|
||||
f 205 201 200
|
||||
f 201 205 206
|
||||
f 206 202 201
|
||||
f 202 206 207
|
||||
f 207 203 202
|
||||
f 204 208 209
|
||||
f 209 205 204
|
||||
f 205 209 210
|
||||
f 210 206 205
|
||||
f 206 210 211
|
||||
f 211 207 206
|
||||
f 208 188 189
|
||||
f 189 209 208
|
||||
f 209 189 190
|
||||
f 190 210 209
|
||||
f 210 190 191
|
||||
f 191 211 210
|
||||
f 191 195 215
|
||||
f 215 212 191
|
||||
f 212 215 216
|
||||
f 216 213 212
|
||||
f 213 216 217
|
||||
f 217 214 213
|
||||
f 195 199 218
|
||||
f 218 215 195
|
||||
f 215 218 219
|
||||
f 219 216 215
|
||||
f 216 219 220
|
||||
f 220 217 216
|
||||
f 199 203 221
|
||||
f 221 218 199
|
||||
f 218 221 222
|
||||
f 222 219 218
|
||||
f 219 222 223
|
||||
f 223 220 219
|
||||
f 203 207 224
|
||||
f 224 221 203
|
||||
f 221 224 225
|
||||
f 225 222 221
|
||||
f 222 225 226
|
||||
f 226 223 222
|
||||
f 207 211 227
|
||||
f 227 224 207
|
||||
f 224 227 228
|
||||
f 228 225 224
|
||||
f 225 228 229
|
||||
f 229 226 225
|
||||
f 211 191 212
|
||||
f 212 227 211
|
||||
f 227 212 213
|
||||
f 213 228 227
|
||||
f 228 213 214
|
||||
f 214 229 228
|
||||
f 234 231 230
|
||||
f 231 234 235
|
||||
f 235 232 231
|
||||
f 232 235 236
|
||||
f 236 233 232
|
||||
f 237 234 230
|
||||
f 234 237 238
|
||||
f 238 235 234
|
||||
f 235 238 239
|
||||
f 239 236 235
|
||||
f 240 237 230
|
||||
f 237 240 241
|
||||
f 241 238 237
|
||||
f 238 241 242
|
||||
f 242 239 238
|
||||
f 243 240 230
|
||||
f 240 243 244
|
||||
f 244 241 240
|
||||
f 241 244 245
|
||||
f 245 242 241
|
||||
f 246 243 230
|
||||
f 243 246 247
|
||||
f 247 244 243
|
||||
f 244 247 248
|
||||
f 248 245 244
|
||||
f 249 246 230
|
||||
f 246 249 250
|
||||
f 250 247 246
|
||||
f 247 250 251
|
||||
f 251 248 247
|
||||
f 252 249 230
|
||||
f 249 252 253
|
||||
f 253 250 249
|
||||
f 250 253 254
|
||||
f 254 251 250
|
||||
f 255 252 230
|
||||
f 252 255 256
|
||||
f 256 253 252
|
||||
f 253 256 257
|
||||
f 257 254 253
|
||||
f 258 255 230
|
||||
f 255 258 259
|
||||
f 259 256 255
|
||||
f 256 259 260
|
||||
f 260 257 256
|
||||
f 261 258 230
|
||||
f 258 261 262
|
||||
f 262 259 258
|
||||
f 259 262 263
|
||||
f 263 260 259
|
||||
f 264 261 230
|
||||
f 261 264 265
|
||||
f 265 262 261
|
||||
f 262 265 266
|
||||
f 266 263 262
|
||||
f 231 264 230
|
||||
f 264 231 232
|
||||
f 232 265 264
|
||||
f 265 232 233
|
||||
f 233 266 265
|
||||
f 233 236 270
|
||||
f 270 267 233
|
||||
f 267 270 271
|
||||
f 271 268 267
|
||||
f 268 271 272
|
||||
f 272 269 268
|
||||
f 236 239 273
|
||||
f 273 270 236
|
||||
f 270 273 274
|
||||
f 274 271 270
|
||||
f 271 274 275
|
||||
f 275 272 271
|
||||
f 239 242 276
|
||||
f 276 273 239
|
||||
f 273 276 277
|
||||
f 277 274 273
|
||||
f 274 277 278
|
||||
f 278 275 274
|
||||
f 242 245 279
|
||||
f 279 276 242
|
||||
f 276 279 280
|
||||
f 280 277 276
|
||||
f 277 280 281
|
||||
f 281 278 277
|
||||
f 245 248 282
|
||||
f 282 279 245
|
||||
f 279 282 283
|
||||
f 283 280 279
|
||||
f 280 283 284
|
||||
f 284 281 280
|
||||
f 248 251 285
|
||||
f 285 282 248
|
||||
f 282 285 286
|
||||
f 286 283 282
|
||||
f 283 286 287
|
||||
f 287 284 283
|
||||
f 251 254 288
|
||||
f 288 285 251
|
||||
f 285 288 289
|
||||
f 289 286 285
|
||||
f 286 289 290
|
||||
f 290 287 286
|
||||
f 254 257 291
|
||||
f 291 288 254
|
||||
f 288 291 292
|
||||
f 292 289 288
|
||||
f 289 292 293
|
||||
f 293 290 289
|
||||
f 257 260 294
|
||||
f 294 291 257
|
||||
f 291 294 295
|
||||
f 295 292 291
|
||||
f 292 295 296
|
||||
f 296 293 292
|
||||
f 260 263 297
|
||||
f 297 294 260
|
||||
f 294 297 298
|
||||
f 298 295 294
|
||||
f 295 298 299
|
||||
f 299 296 295
|
||||
f 263 266 300
|
||||
f 300 297 263
|
||||
f 297 300 301
|
||||
f 301 298 297
|
||||
f 298 301 302
|
||||
f 302 299 298
|
||||
f 266 233 267
|
||||
f 267 300 266
|
||||
f 300 267 268
|
||||
f 268 301 300
|
||||
f 301 268 269
|
||||
f 269 302 301
|
||||
# 552 faces
|
||||
|
||||
g
|
||||
9
第17章 游戏的心脏——物理引擎/Sample17_4/assets/frag.sh
Normal file
9
第17章 游戏的心脏——物理引擎/Sample17_4/assets/frag.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
|
||||
void main()
|
||||
{
|
||||
//给此片元颜色值
|
||||
gl_FragColor = texture2D(sTexture, vTextureCoord);
|
||||
}
|
||||
10
第17章 游戏的心脏——物理引擎/Sample17_4/assets/frag_color.sh
Normal file
10
第17章 游戏的心脏——物理引擎/Sample17_4/assets/frag_color.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
precision mediump float;
|
||||
varying vec4 ambient;
|
||||
varying vec4 diffuse;
|
||||
varying vec4 specular;
|
||||
void main()
|
||||
{
|
||||
vec4 finalColor=vec4(1.0,1.0,1.0,1.0);
|
||||
gl_FragColor = finalColor*ambient+finalColor*specular+finalColor*diffuse;//¸ø´ËƬԪÑÕɫֵ
|
||||
|
||||
}
|
||||
1092
第17章 游戏的心脏——物理引擎/Sample17_4/assets/table.obj
Normal file
1092
第17章 游戏的心脏——物理引擎/Sample17_4/assets/table.obj
Normal file
File diff suppressed because it is too large
Load Diff
11
第17章 游戏的心脏——物理引擎/Sample17_4/assets/vertex.sh
Normal file
11
第17章 游戏的心脏——物理引擎/Sample17_4/assets/vertex.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
varying vec2 vTextureCoord; //传给着色器的纹理坐标
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
vTextureCoord=aTexCoor;
|
||||
}
|
||||
45
第17章 游戏的心脏——物理引擎/Sample17_4/assets/vertex_color.sh
Normal file
45
第17章 游戏的心脏——物理引擎/Sample17_4/assets/vertex_color.sh
Normal file
@@ -0,0 +1,45 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
uniform vec3 uLightLocation; //光源位置
|
||||
uniform vec3 uCamera; //摄像机位置
|
||||
attribute vec3 aNormal; //顶点法向量
|
||||
//用于传递给片元着色器的变量
|
||||
varying vec4 ambient;
|
||||
varying vec4 diffuse;
|
||||
varying vec4 specular;
|
||||
|
||||
//定位光光照计算的方法
|
||||
void pointLight( //定位光光照计算的方法
|
||||
in vec3 normal, //法向量
|
||||
inout vec4 ambient, //环境光最终强度
|
||||
inout vec4 diffuse, //散射光最终强度
|
||||
inout vec4 specular, //镜面光最终强度
|
||||
in vec3 lightLocation, //光源位置
|
||||
in vec4 lightAmbient, //环境光强度
|
||||
in vec4 lightDiffuse, //散射光强度
|
||||
in vec4 lightSpecular //镜面光强度
|
||||
){
|
||||
ambient=lightAmbient; //直接得出环境光的最终强度
|
||||
vec3 normalTarget=aPosition+normal; //计算变换后的法向量
|
||||
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition,1)).xyz;
|
||||
newNormal=normalize(newNormal); //对法向量规格化
|
||||
//计算从表面点到摄像机的向量
|
||||
vec3 eye= normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
//计算从表面点到光源位置的向量vp
|
||||
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
vp=normalize(vp);//格式化vp
|
||||
vec3 halfVector=normalize(vp+eye); //求视线与光线的半向量
|
||||
float shininess=50.0; //粗糙度,越小越光滑
|
||||
float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量与vp的点积与0的最大值
|
||||
diffuse=lightDiffuse*nDotViewPosition; //计算散射光的最终强度
|
||||
float nDotViewHalfVector=dot(newNormal,halfVector); //法线与半向量的点积
|
||||
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess)); //镜面反射光强度因子
|
||||
specular=lightSpecular*powerFactor; //计算镜面光的最终强度
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
pointLight(normalize(aNormal),ambient,diffuse,specular,uLightLocation,vec4(0.3,0.3,0.3,1.0),vec4(0.7,0.7,0.7,1.0),vec4(0.3,0.3,0.3,1.0));
|
||||
}
|
||||
106
第17章 游戏的心脏——物理引擎/Sample17_4/assets/yh.obj
Normal file
106
第17章 游戏的心脏——物理引擎/Sample17_4/assets/yh.obj
Normal file
@@ -0,0 +1,106 @@
|
||||
# Max2Obj Version 4.0 Mar 10th, 2001
|
||||
#
|
||||
# object (null) to come ...
|
||||
#
|
||||
v 0.011881 0.003369 -0.983799
|
||||
v 0.011881 0.203369 -0.783799
|
||||
v 0.011881 0.003369 -0.583799
|
||||
v 0.011881 -0.196631 -0.783799
|
||||
v 0.718988 0.003369 -0.690906
|
||||
v 0.577566 0.203369 -0.549484
|
||||
v 0.436145 0.003369 -0.408063
|
||||
v 0.577566 -0.196631 -0.549485
|
||||
v 1.011881 0.003369 0.016201
|
||||
v 0.811881 0.203369 0.016201
|
||||
v 0.611881 0.003369 0.016201
|
||||
v 0.811881 -0.196631 0.016201
|
||||
v 0.718988 0.003369 0.723308
|
||||
v 0.577566 0.203369 0.581886
|
||||
v 0.436145 0.003369 0.440465
|
||||
v 0.577566 -0.196631 0.581886
|
||||
v 0.011881 0.003369 1.016201
|
||||
v 0.011881 0.203369 0.816201
|
||||
v 0.011881 0.003369 0.616201
|
||||
v 0.011881 -0.196631 0.816201
|
||||
v -0.695226 0.003369 0.723308
|
||||
v -0.553805 0.203369 0.581886
|
||||
v -0.412383 0.003369 0.440465
|
||||
v -0.553805 -0.196631 0.581886
|
||||
v -0.988119 0.003369 0.016201
|
||||
v -0.788119 0.203369 0.016201
|
||||
v -0.588119 0.003369 0.016201
|
||||
v -0.788119 -0.196631 0.016201
|
||||
v -0.695226 0.003369 -0.690906
|
||||
v -0.553804 0.203369 -0.549485
|
||||
v -0.412383 0.003369 -0.408063
|
||||
v -0.553804 -0.196631 -0.549485
|
||||
# 32 vertices
|
||||
|
||||
g (null)
|
||||
f 1 6 5
|
||||
f 1 2 6
|
||||
f 2 7 6
|
||||
f 2 3 7
|
||||
f 3 8 7
|
||||
f 3 4 8
|
||||
f 4 5 8
|
||||
f 4 1 5
|
||||
f 5 10 9
|
||||
f 5 6 10
|
||||
f 6 11 10
|
||||
f 6 7 11
|
||||
f 7 12 11
|
||||
f 7 8 12
|
||||
f 8 9 12
|
||||
f 8 5 9
|
||||
f 9 14 13
|
||||
f 9 10 14
|
||||
f 10 15 14
|
||||
f 10 11 15
|
||||
f 11 16 15
|
||||
f 11 12 16
|
||||
f 12 13 16
|
||||
f 12 9 13
|
||||
f 13 18 17
|
||||
f 13 14 18
|
||||
f 14 19 18
|
||||
f 14 15 19
|
||||
f 15 20 19
|
||||
f 15 16 20
|
||||
f 16 17 20
|
||||
f 16 13 17
|
||||
f 17 22 21
|
||||
f 17 18 22
|
||||
f 18 23 22
|
||||
f 18 19 23
|
||||
f 19 24 23
|
||||
f 19 20 24
|
||||
f 20 21 24
|
||||
f 20 17 21
|
||||
f 21 26 25
|
||||
f 21 22 26
|
||||
f 22 27 26
|
||||
f 22 23 27
|
||||
f 23 28 27
|
||||
f 23 24 28
|
||||
f 24 25 28
|
||||
f 24 21 25
|
||||
f 25 30 29
|
||||
f 25 26 30
|
||||
f 26 31 30
|
||||
f 26 27 31
|
||||
f 27 32 31
|
||||
f 27 28 32
|
||||
f 28 29 32
|
||||
f 28 25 29
|
||||
f 29 2 1
|
||||
f 29 30 2
|
||||
f 30 3 2
|
||||
f 30 31 3
|
||||
f 31 4 3
|
||||
f 31 32 4
|
||||
f 32 1 4
|
||||
f 32 29 1
|
||||
# 64 faces
|
||||
|
||||
g
|
||||
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/Sample17_4.apk
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/Sample17_4.apk
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/classes.dex
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/classes.dex
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/BNThing.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/BNThing.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/Constant.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/Constant.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/LoadUtil.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/LoadUtil.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/Normal.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/Normal.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R$attr.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R$attr.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R$string.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R$string.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/R.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/SYSUtil.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/SYSUtil.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/TexCube.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/TexCube.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/TexFloor.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/com/bn/Sample17_4/TexFloor.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/resources.ap_
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/bin/resources.ap_
Normal file
Binary file not shown.
13
第17章 游戏的心脏——物理引擎/Sample17_4/default.properties
Normal file
13
第17章 游戏的心脏——物理引擎/Sample17_4/default.properties
Normal file
@@ -0,0 +1,13 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Indicates whether an apk should be generated for each density.
|
||||
split.density=false
|
||||
# Project target.
|
||||
target=android-8
|
||||
24
第17章 游戏的心脏——物理引擎/Sample17_4/gen/com/bn/Sample17_4/R.java
Normal file
24
第17章 游戏的心脏——物理引擎/Sample17_4/gen/com/bn/Sample17_4/R.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
||||
*
|
||||
* This class was automatically generated by the
|
||||
* aapt tool from the resource data it found. It
|
||||
* should not be modified by hand.
|
||||
*/
|
||||
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int f6=0x7f020000;
|
||||
public static final int floor=0x7f020001;
|
||||
public static final int icon=0x7f020002;
|
||||
public static final int wood_bin0=0x7f020003;
|
||||
public static final int wood_bin1=0x7f020004;
|
||||
public static final int wood_bin2=0x7f020005;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f030000;
|
||||
}
|
||||
}
|
||||
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/lib/jbullet.jar
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/lib/jbullet.jar
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/lib/vecmath.jar
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/lib/vecmath.jar
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/f6.jpg
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/f6.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/floor.jpg
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/floor.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/icon.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin0.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin0.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin1.jpg
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin2.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_4/res/drawable-mdpi/wood_bin2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
4
第17章 游戏的心脏——物理引擎/Sample17_4/res/values/strings.xml
Normal file
4
第17章 游戏的心脏——物理引擎/Sample17_4/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample17_4</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
public abstract class BNThing
|
||||
{
|
||||
public abstract void drawSelf();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
public class Constant
|
||||
{
|
||||
//»ù±¾³ß´çµ¥Ôª
|
||||
final static float UNIT_SIZE=0.5f;
|
||||
|
||||
final static float CAMERA_X=-4.5f;
|
||||
final static float CAMERA_Y=4;
|
||||
final static float CAMERA_Z=4.5f;
|
||||
|
||||
final static float TARGET_X=0;
|
||||
final static float TARGET_Y=0;
|
||||
final static float TARGET_Z=0;
|
||||
|
||||
static float LIGHT_X;
|
||||
static float LIGHT_Y;
|
||||
static float LIGHT_Z;
|
||||
|
||||
final static float DISTANCE=100;
|
||||
|
||||
public static void calculateLightPosition(){
|
||||
float[] dir = new float[3];
|
||||
dir[0]=CAMERA_X-TARGET_X;
|
||||
dir[1]=CAMERA_Y-TARGET_Y;
|
||||
dir[2]=CAMERA_Z-TARGET_Z;
|
||||
|
||||
LIGHT_X=dir[0]*DISTANCE+CAMERA_X;
|
||||
LIGHT_Y=dir[1]*100+CAMERA_Y;
|
||||
LIGHT_Z=dir[2]*DISTANCE+CAMERA_Z;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.bn.Sample17_4;
|
||||
import javax.vecmath.Quat4f;
|
||||
import javax.vecmath.Vector3f;
|
||||
import com.bulletphysics.collision.shapes.CollisionShape;
|
||||
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
|
||||
import com.bulletphysics.linearmath.DefaultMotionState;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
public class LoadRigidBody extends BNThing
|
||||
{
|
||||
LoadedObjectVertexNormal lovo;
|
||||
RigidBody body;//对应的刚体对象
|
||||
int mProgram;
|
||||
public LoadRigidBody(int mProgram,float mass,LoadedObjectVertexNormal lovo,float cx,float cy,float cz,DiscreteDynamicsWorld dynamicsWorld)
|
||||
{
|
||||
this.lovo=lovo;//保存加载物体对象索引
|
||||
this.mProgram=mProgram;//保存着色器程序索引
|
||||
CollisionShape colShape=lovo.loadShape;//保存碰撞形状
|
||||
boolean isDynamic = (mass != 0f);//物体是否可运动
|
||||
Vector3f localInertia = new Vector3f(0f, 0f, 0f);//创建惯性向量
|
||||
if (isDynamic) {
|
||||
colShape.calculateLocalInertia(mass, localInertia);//计算惯性
|
||||
}
|
||||
Transform startTransform = new Transform();//创建刚体的初始变换对象
|
||||
startTransform.setIdentity();//初始化变换对象
|
||||
startTransform.origin.set(new Vector3f(cx, cy, cz));//设置变换的初始位置
|
||||
//创建刚体的运动状态对象
|
||||
DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
|
||||
//创建刚体信息对象
|
||||
RigidBodyConstructionInfo cInfo = new RigidBodyConstructionInfo
|
||||
(mass, myMotionState, colShape, localInertia);
|
||||
body = new RigidBody(cInfo);//创建刚体
|
||||
body.setRestitution(0.4f);//设置反弹系数
|
||||
body.setFriction(0.8f);//设置摩擦系数
|
||||
dynamicsWorld.addRigidBody(body);//将刚体添加进物理世界
|
||||
}
|
||||
public void drawSelf()
|
||||
{
|
||||
lovo.initShader(mProgram);//初始化着色器
|
||||
MatrixState.pushMatrix();//保存现场
|
||||
//获取这个箱子的变换信息对象
|
||||
Transform trans = body.getMotionState().getWorldTransform(new Transform());
|
||||
//进行移位变换
|
||||
MatrixState.translate(trans.origin.x,trans.origin.y, trans.origin.z);
|
||||
Quat4f ro=trans.getRotation(new Quat4f());//获取旋转的四元数对象
|
||||
if(ro.x!=0||ro.y!=0||ro.z!=0)
|
||||
{
|
||||
float[] fa=SYSUtil.fromSYStoAXYZ(ro);//将四元数转换成AXYZ形式
|
||||
MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);//执行旋转
|
||||
}
|
||||
lovo.drawSelf();//调用加载物体对象的绘制方法
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
}
|
||||
}
|
||||
175
第17章 游戏的心脏——物理引擎/Sample17_4/src/com/bn/Sample17_4/LoadUtil.java
Normal file
175
第17章 游戏的心脏——物理引擎/Sample17_4/src/com/bn/Sample17_4/LoadUtil.java
Normal file
@@ -0,0 +1,175 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import android.content.res.Resources;
|
||||
import android.util.Log;
|
||||
|
||||
public class LoadUtil
|
||||
{
|
||||
//求两个向量的叉积
|
||||
public static float[] getCrossProduct(float x1,float y1,float z1,float x2,float y2,float z2)
|
||||
{
|
||||
//求出两个矢量叉积矢量在XYZ轴的分量ABC
|
||||
float A=y1*z2-y2*z1;
|
||||
float B=z1*x2-z2*x1;
|
||||
float C=x1*y2-x2*y1;
|
||||
|
||||
return new float[]{A,B,C};
|
||||
}
|
||||
|
||||
//向量规格化
|
||||
public static float[] vectorNormal(float[] vector)
|
||||
{
|
||||
//求向量的模
|
||||
float module=(float)Math.sqrt(vector[0]*vector[0]+vector[1]*vector[1]+vector[2]*vector[2]);
|
||||
return new float[]{vector[0]/module,vector[1]/module,vector[2]/module};
|
||||
}
|
||||
|
||||
//从obj文件中加载携带顶点信息的物体,并自动计算每个顶点的平均法向量
|
||||
public static LoadedObjectVertexNormal loadFromFile(String fname, Resources r,MySurfaceView mv)
|
||||
{
|
||||
//加载后物体的引用
|
||||
LoadedObjectVertexNormal lo=null;
|
||||
//原始顶点坐标列表--直接从obj文件中加载
|
||||
ArrayList<Float> alv=new ArrayList<Float>();
|
||||
//顶点组装面索引列表--根据面的信息从文件中加载
|
||||
ArrayList<Integer> alFaceIndex=new ArrayList<Integer>();
|
||||
//结果顶点坐标列表--按面组织好
|
||||
ArrayList<Float> alvResult=new ArrayList<Float>();
|
||||
//平均前各个索引对应的点的法向量集合Map
|
||||
//此HashMap的key为点的索引, value为点所在的各个面的法向量的集合
|
||||
HashMap<Integer,HashSet<Normal>> hmn=new HashMap<Integer,HashSet<Normal>>();
|
||||
|
||||
try
|
||||
{
|
||||
InputStream in=r.getAssets().open(fname);
|
||||
InputStreamReader isr=new InputStreamReader(in);
|
||||
BufferedReader br=new BufferedReader(isr);
|
||||
String temps=null;
|
||||
|
||||
//扫面文件,根据行类型的不同执行不同的处理逻辑
|
||||
while((temps=br.readLine())!=null)
|
||||
{
|
||||
//用空格分割行中的各个组成部分
|
||||
String[] tempsa=temps.split("[ ]+");
|
||||
if(tempsa[0].trim().equals("v"))
|
||||
{//此行为顶点坐标
|
||||
//若为顶点坐标行则提取出此顶点的XYZ坐标添加到原始顶点坐标列表中
|
||||
alv.add(Float.parseFloat(tempsa[1]));
|
||||
alv.add(Float.parseFloat(tempsa[2]));
|
||||
alv.add(Float.parseFloat(tempsa[3]));
|
||||
}
|
||||
else if(tempsa[0].trim().equals("f"))
|
||||
{//此行为三角形面
|
||||
/*
|
||||
*若为三角形面行则根据 组成面的顶点的索引从原始顶点坐标列表中
|
||||
*提取相应的顶点坐标值添加到结果顶点坐标列表中,同时根据三个
|
||||
*顶点的坐标计算出此面的法向量并添加到平均前各个索引对应的点
|
||||
*的法向量集合组成的Map中
|
||||
*/
|
||||
|
||||
int[] index=new int[3];//三个顶点索引值的数组
|
||||
|
||||
//计算第0个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[0]=Integer.parseInt(tempsa[1].split("/")[0])-1;
|
||||
float x0=alv.get(3*index[0]);
|
||||
float y0=alv.get(3*index[0]+1);
|
||||
float z0=alv.get(3*index[0]+2);
|
||||
alvResult.add(x0);
|
||||
alvResult.add(y0);
|
||||
alvResult.add(z0);
|
||||
|
||||
//计算第1个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[1]=Integer.parseInt(tempsa[2].split("/")[0])-1;
|
||||
float x1=alv.get(3*index[1]);
|
||||
float y1=alv.get(3*index[1]+1);
|
||||
float z1=alv.get(3*index[1]+2);
|
||||
alvResult.add(x1);
|
||||
alvResult.add(y1);
|
||||
alvResult.add(z1);
|
||||
|
||||
//计算第2个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[2]=Integer.parseInt(tempsa[3].split("/")[0])-1;
|
||||
float x2=alv.get(3*index[2]);
|
||||
float y2=alv.get(3*index[2]+1);
|
||||
float z2=alv.get(3*index[2]+2);
|
||||
alvResult.add(x2);
|
||||
alvResult.add(y2);
|
||||
alvResult.add(z2);
|
||||
|
||||
//记录此面的顶点索引
|
||||
alFaceIndex.add(index[0]);
|
||||
alFaceIndex.add(index[1]);
|
||||
alFaceIndex.add(index[2]);
|
||||
|
||||
//通过三角形面两个边向量0-1,0-2求叉积得到此面的法向量
|
||||
//求0号点到1号点的向量
|
||||
float vxa=x1-x0;
|
||||
float vya=y1-y0;
|
||||
float vza=z1-z0;
|
||||
//求0号点到2号点的向量
|
||||
float vxb=x2-x0;
|
||||
float vyb=y2-y0;
|
||||
float vzb=z2-z0;
|
||||
//通过求两个向量的叉积计算法向量
|
||||
float[] vNormal=vectorNormal(getCrossProduct
|
||||
(
|
||||
vxa,vya,vza,vxb,vyb,vzb
|
||||
));
|
||||
|
||||
for(int tempInxex:index)
|
||||
{//记录每个索引点的法向量到平均前各个索引对应的点的法向量集合组成的Map中
|
||||
//获取当前索引对应点的法向量集合
|
||||
HashSet<Normal> hsn=hmn.get(tempInxex);
|
||||
if(hsn==null)
|
||||
{//若集合不存在则创建
|
||||
hsn=new HashSet<Normal>();
|
||||
}
|
||||
//将此点的法向量添加到集合中
|
||||
//由于Normal类重写了equals方法,因此同样的法向量不会重复出现在此点
|
||||
//对应的法向量集合中
|
||||
hsn.add(new Normal(vNormal[0],vNormal[1],vNormal[2]));
|
||||
//将集合放进HsahMap中
|
||||
hmn.put(tempInxex, hsn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//生成顶点数组
|
||||
int size=alvResult.size();
|
||||
float[] vXYZ=new float[size];
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
vXYZ[i]=alvResult.get(i);
|
||||
}
|
||||
|
||||
//生成法向量数组
|
||||
float[] nXYZ=new float[alFaceIndex.size()*3];
|
||||
int c=0;
|
||||
for(Integer i:alFaceIndex)
|
||||
{
|
||||
//根据当前点的索引从Map中取出一个法向量的集合
|
||||
HashSet<Normal> hsn=hmn.get(i);
|
||||
//求出平均法向量
|
||||
float[] tn=Normal.getAverage(hsn);
|
||||
//将计算出的平均法向量存放到法向量数组中
|
||||
nXYZ[c++]=tn[0];
|
||||
nXYZ[c++]=tn[1];
|
||||
nXYZ[c++]=tn[2];
|
||||
}
|
||||
//创建3D物体对象
|
||||
lo=new LoadedObjectVertexNormal(vXYZ,nXYZ);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Log.d("load error", "load error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import android.opengl.GLES20;
|
||||
import com.bulletphysics.collision.shapes.CollisionShape;
|
||||
import com.bulletphysics.collision.shapes.TriangleIndexVertexArray;
|
||||
import com.bulletphysics.extras.gimpact.GImpactMeshShape;
|
||||
|
||||
|
||||
public class LoadedObjectVertexNormal
|
||||
{
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maNormalHandle; //顶点法向量属性引用
|
||||
int maLightLocationHandle;//光源位置属性引用
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
private FloatBuffer mNormalBuffer;//顶点法向量数据缓冲
|
||||
int vCount=0;
|
||||
CollisionShape loadShape;
|
||||
float[] vertices;
|
||||
float[] normals;
|
||||
|
||||
public LoadedObjectVertexNormal(float[] vertices,float[] normals)
|
||||
{
|
||||
this.vertices=vertices;
|
||||
this.normals=normals;
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=vertices.length/3;
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
|
||||
//顶点法向量数据的初始化================begin============================
|
||||
ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length*4);
|
||||
nbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mNormalBuffer = nbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mNormalBuffer.put(normals);//向缓冲区中放入顶点法向量数据
|
||||
mNormalBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点着色数据的初始化================end============================
|
||||
|
||||
//为刚体的顶点分配缓冲
|
||||
ByteBuffer gVertices=ByteBuffer.allocateDirect(vCount*3*4).order(ByteOrder.nativeOrder());
|
||||
for(int i=0;i<vertices.length;i++)//将顶点数据添加到缓冲中
|
||||
{
|
||||
gVertices.putFloat(i*4,vertices[i]);
|
||||
}
|
||||
gVertices.position(0);//设置缓冲区起始位置
|
||||
//刚体索引缓冲
|
||||
ByteBuffer gIndices=ByteBuffer.allocateDirect(vCount*4).order(ByteOrder.nativeOrder());
|
||||
for(int i=0;i<vCount;i++)//将索引数据添加到缓冲中
|
||||
{
|
||||
gIndices.putInt(i);
|
||||
}
|
||||
gIndices.position(0);//设置缓冲区起始位置
|
||||
int vertStride = 4*3;//顶点数据间隔
|
||||
int indexStride = 4*3;//索引数据间隔
|
||||
TriangleIndexVertexArray indexVertexArrays= //创建三角形索引顶点数组
|
||||
new TriangleIndexVertexArray
|
||||
(
|
||||
vCount/3,//三角形的个数
|
||||
gIndices, //索引缓冲
|
||||
indexStride,//索引间隔
|
||||
vCount, //顶点个数
|
||||
gVertices, //顶点缓冲
|
||||
vertStride//顶点间隔
|
||||
);
|
||||
//创建曲面形状
|
||||
GImpactMeshShape trimesh = new GImpactMeshShape(indexVertexArrays);
|
||||
trimesh.updateBound();
|
||||
loadShape =trimesh;//保存碰撞形状
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(int mProgram)
|
||||
{
|
||||
this.mProgram=mProgram;
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取程序中顶点法向量属性引用
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取位置、旋转变换矩阵引用
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中光源位置引用
|
||||
maLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocation");
|
||||
//获取程序中摄像机位置引用
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
}
|
||||
|
||||
public void drawSelf()
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将光源位置传入着色器程序
|
||||
GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFBRed);
|
||||
//将摄像机位置传入着色器程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//将顶点法向量数据传入渲染管线
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maNormalHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mNormalBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.Stack;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
|
||||
//存储系统矩阵状态的类
|
||||
public class MatrixState
|
||||
{
|
||||
private static float[] mProjMatrix = new float[16];//4x4矩阵 投影用
|
||||
static float[] mVMatrix = new float[16];//摄像机位置朝向9参数矩阵
|
||||
static float[] mMVPMatrix;//最后起作用的总变换矩阵
|
||||
public static float[] lightLocationRed=new float[]{0,0,0};//红色定位光光源位置
|
||||
public static float[] lightLocationGreenBlue=new float[]{0,0,0};//天蓝色定位光光源位置
|
||||
public static FloatBuffer cameraFB;
|
||||
public static FloatBuffer lightPositionFBRed;
|
||||
public static FloatBuffer lightPositionFBGreenBlue;
|
||||
|
||||
//设置摄像机
|
||||
public static void setCamera
|
||||
(
|
||||
float cx, //摄像机位置x
|
||||
float cy, //摄像机位置y
|
||||
float cz, //摄像机位置z
|
||||
float tx, //摄像机目标点x
|
||||
float ty, //摄像机目标点y
|
||||
float tz, //摄像机目标点z
|
||||
float upx, //摄像机UP向量X分量
|
||||
float upy, //摄像机UP向量Y分量
|
||||
float upz //摄像机UP向量Z分量
|
||||
)
|
||||
{
|
||||
Matrix.setLookAtM
|
||||
(
|
||||
mVMatrix,
|
||||
0,
|
||||
cx,
|
||||
cy,
|
||||
cz,
|
||||
tx,
|
||||
ty,
|
||||
tz,
|
||||
upx,
|
||||
upy,
|
||||
upz
|
||||
);
|
||||
|
||||
float[] cameraLocation=new float[3];//摄像机位置
|
||||
cameraLocation[0]=cx;
|
||||
cameraLocation[1]=cy;
|
||||
cameraLocation[2]=cz;
|
||||
//摄像机位置矩阵
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(3*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
cameraFB=llbb.asFloatBuffer();
|
||||
cameraFB.put(cameraLocation);
|
||||
cameraFB.position(0);
|
||||
}
|
||||
|
||||
//设置透视投影参数
|
||||
public static void setProjectFrustum
|
||||
(
|
||||
float left, //near面的left
|
||||
float right, //near面的right
|
||||
float bottom, //near面的bottom
|
||||
float top, //near面的top
|
||||
float near, //near面距离
|
||||
float far //far面距离
|
||||
)
|
||||
{
|
||||
Matrix.frustumM(mProjMatrix, 0, left, right, bottom, top, near, far);
|
||||
}
|
||||
|
||||
//设置正交投影参数
|
||||
public static void setProjectOrtho
|
||||
(
|
||||
float left, //near面的left
|
||||
float right, //near面的right
|
||||
float bottom, //near面的bottom
|
||||
float top, //near面的top
|
||||
float near, //near面距离
|
||||
float far //far面距离
|
||||
)
|
||||
{
|
||||
Matrix.orthoM(mProjMatrix, 0, left, right, bottom, top, near, far);
|
||||
}
|
||||
|
||||
//获取具体物体的总变换矩阵
|
||||
public static float[] getFinalMatrix()
|
||||
{
|
||||
float[] mMVPMatrix=new float[16];
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, currMatrix, 0);
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
|
||||
return mMVPMatrix;
|
||||
}
|
||||
|
||||
//设置红色灯光位置的方法
|
||||
public static void setLightLocationRed(float x,float y,float z)
|
||||
{
|
||||
lightLocationRed[0]=x;
|
||||
lightLocationRed[1]=y;
|
||||
lightLocationRed[2]=z;
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(3*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
lightPositionFBRed=llbb.asFloatBuffer();
|
||||
lightPositionFBRed.put(lightLocationRed);
|
||||
lightPositionFBRed.position(0);
|
||||
}
|
||||
|
||||
//设置天蓝色灯光位置的方法
|
||||
public static void setLightLocationGreenBlue(float x,float y,float z)
|
||||
{
|
||||
lightLocationGreenBlue[0]=x;
|
||||
lightLocationGreenBlue[1]=y;
|
||||
lightLocationGreenBlue[2]=z;
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(3*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
lightPositionFBGreenBlue=llbb.asFloatBuffer();
|
||||
lightPositionFBGreenBlue.put(lightLocationGreenBlue);
|
||||
lightPositionFBGreenBlue.position(0);
|
||||
}
|
||||
|
||||
public static Stack<float[]> mStack=new Stack<float[]>();//保护变换矩阵的栈
|
||||
static float[] currMatrix;//当前变换矩阵
|
||||
|
||||
public static void setInitStack()//获取不变换初始矩阵
|
||||
{
|
||||
currMatrix=new float[16];
|
||||
Matrix.setRotateM(currMatrix, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
public static void pushMatrix()//保护变换矩阵
|
||||
{
|
||||
mStack.push(currMatrix.clone());
|
||||
}
|
||||
|
||||
public static void popMatrix()//恢复变换矩阵
|
||||
{
|
||||
currMatrix=mStack.pop();
|
||||
}
|
||||
|
||||
public static void translate(float x,float y,float z)//设置沿xyz轴移动
|
||||
{
|
||||
Matrix.translateM(currMatrix, 0, x, y, z);
|
||||
}
|
||||
|
||||
public static void rotate(float angle,float x,float y,float z)//设置绕xyz轴移动
|
||||
{
|
||||
Matrix.rotateM(currMatrix,0,angle,x,y,z);
|
||||
}
|
||||
|
||||
public static void scale(float x,float y,float z){
|
||||
Matrix.scaleM(currMatrix, 0, x, y, z);
|
||||
}
|
||||
|
||||
//插入自带矩阵
|
||||
public static void matrix(float[] self)
|
||||
{
|
||||
float[] result=new float[16];
|
||||
Matrix.multiplyMM(result,0,currMatrix,0,self,0);
|
||||
currMatrix=result;
|
||||
}
|
||||
|
||||
public static float[] getMMatrix() {
|
||||
return currMatrix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
package com.bn.Sample17_4;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.opengl.GLUtils;
|
||||
import android.view.MotionEvent;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
import com.bn.Sample17_4.R;
|
||||
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
|
||||
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
|
||||
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
|
||||
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
|
||||
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
|
||||
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
|
||||
import com.bulletphysics.extras.gimpact.GImpactCollisionAlgorithm;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import com.bulletphysics.collision.shapes.BoxShape;
|
||||
import com.bulletphysics.collision.shapes.CollisionShape;
|
||||
import com.bulletphysics.collision.shapes.StaticPlaneShape;
|
||||
import static com.bn.Sample17_4.Constant.*;
|
||||
|
||||
class MySurfaceView extends GLSurfaceView
|
||||
{
|
||||
private SceneRenderer mRenderer;//场景渲染器
|
||||
DiscreteDynamicsWorld dynamicsWorld;//世界对象
|
||||
ArrayList<BNThing> tca=new ArrayList<BNThing>();
|
||||
ArrayList<BNThing> tcaForAdd=new ArrayList<BNThing>();
|
||||
CollisionDispatcher dispatcher;
|
||||
CollisionShape boxShape;//共用的立方体
|
||||
CollisionShape planeShape;//共用的平面形状
|
||||
//从指定的obj文件中加载对象
|
||||
LoadedObjectVertexNormal[] lovoa=new LoadedObjectVertexNormal[3];
|
||||
int[] cubeTextureId=new int[2];//箱子面纹理
|
||||
Sample17_4_Activity activity;
|
||||
|
||||
public MySurfaceView(Context context)
|
||||
{
|
||||
super(context);
|
||||
activity = (Sample17_4_Activity) context;
|
||||
//初始化物理世界
|
||||
this.setEGLContextClientVersion(2);
|
||||
initWorld();
|
||||
mRenderer = new SceneRenderer(); //创建场景渲染器
|
||||
setRenderer(mRenderer); //设置渲染器
|
||||
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
|
||||
}
|
||||
|
||||
//初始化物理世界的方法
|
||||
public void initWorld()
|
||||
{
|
||||
//创建碰撞检测配置信息对象
|
||||
CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
|
||||
//创建碰撞检测算法分配者对象,其功能为扫描所有的碰撞检测对,并确定适用的检测策略对应的算法
|
||||
dispatcher = new CollisionDispatcher(collisionConfiguration);
|
||||
BroadphaseInterface overlappingPairCache = new DbvtBroadphase();
|
||||
//创建推动约束解决者对象
|
||||
SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
|
||||
//创建物理世界对象
|
||||
dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
|
||||
//设置重力加速度
|
||||
dynamicsWorld.setGravity(new Vector3f(0, -10, 0));
|
||||
|
||||
//创建共用的立方体
|
||||
boxShape=new BoxShape(new Vector3f(Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE));
|
||||
//创建共用的平面形状
|
||||
planeShape=new StaticPlaneShape(new Vector3f(0, 1, 0), 0);
|
||||
}
|
||||
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
int floorTextureId;//地面纹理
|
||||
TexFloor floor;//纹理矩形1
|
||||
|
||||
public void onDrawFrame(GL10 gl) {
|
||||
|
||||
//清除颜色缓存于深度缓存
|
||||
GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
//绘制箱子
|
||||
synchronized(tca)
|
||||
{
|
||||
for(BNThing tc:tca)
|
||||
{
|
||||
MatrixState.pushMatrix();
|
||||
tc.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
}
|
||||
//绘制地板
|
||||
MatrixState.pushMatrix();
|
||||
floor.drawSelf(floorTextureId);
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算透视投影的比例
|
||||
float ratio = (float) width / height;
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);
|
||||
MatrixState.setCamera(
|
||||
CAMERA_X, //人眼位置的X
|
||||
CAMERA_Y, //人眼位置的Y
|
||||
CAMERA_Z, //人眼位置的Z
|
||||
TARGET_X, //人眼球看的点X
|
||||
TARGET_Y, //人眼球看的点Y
|
||||
TARGET_Z, //人眼球看的点Z
|
||||
0,
|
||||
1,
|
||||
0);
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//关闭抗抖动
|
||||
GLES20.glDisable(GL10.GL_DITHER);
|
||||
//设置屏幕背景色黑色RGBA
|
||||
GLES20.glClearColor(0,0,0,0);
|
||||
//启用深度测试
|
||||
GLES20.glEnable(GL10.GL_DEPTH_TEST);
|
||||
//设置为打开背面剪裁
|
||||
GLES20.glEnable(GL10.GL_CULL_FACE);
|
||||
//初始化变换矩阵
|
||||
MatrixState.setInitStack();
|
||||
//初始化光源位置
|
||||
MatrixState.setLightLocationRed(CAMERA_X*2, 50, CAMERA_Z*2);
|
||||
ShaderManager.loadCodeFromFile(activity.getResources());
|
||||
ShaderManager.compileShader();
|
||||
|
||||
lovoa[0]=LoadUtil.loadFromFile("table.obj", MySurfaceView.this.getResources(), MySurfaceView.this);
|
||||
lovoa[1]=LoadUtil.loadFromFile("yh.obj", MySurfaceView.this.getResources(), MySurfaceView.this);
|
||||
lovoa[2]=LoadUtil.loadFromFile("ch.obj", MySurfaceView.this.getResources(), MySurfaceView.this);
|
||||
|
||||
//初始化纹理
|
||||
cubeTextureId[0]=initTexture(R.drawable.wood_bin2);
|
||||
cubeTextureId[1]=initTexture(R.drawable.wood_bin1);
|
||||
floorTextureId=initTextureRepeat(R.drawable.f6);
|
||||
|
||||
//创建地面矩形
|
||||
floor=new TexFloor(ShaderManager.getTextureLightShaderProgram(),80*Constant.UNIT_SIZE,
|
||||
-Constant.UNIT_SIZE,planeShape,dynamicsWorld);
|
||||
|
||||
new Thread()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized(tcaForAdd)
|
||||
{
|
||||
synchronized(tca)
|
||||
{
|
||||
for(BNThing tc:tcaForAdd)
|
||||
{
|
||||
tca.add(tc);
|
||||
}
|
||||
}
|
||||
tcaForAdd.clear();
|
||||
}
|
||||
//模拟
|
||||
dynamicsWorld.stepSimulation(1f/200.f, 5);
|
||||
Thread.sleep(5);
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
//触摸事件回调方法
|
||||
int index=0;
|
||||
@Override public boolean onTouchEvent(MotionEvent e)
|
||||
{
|
||||
switch (e.getAction())
|
||||
{
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
if(index==0)
|
||||
{
|
||||
TexCube tcTemp=new TexCube
|
||||
(
|
||||
MySurfaceView.this,
|
||||
Constant.UNIT_SIZE,
|
||||
boxShape,
|
||||
dynamicsWorld,
|
||||
1,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
cubeTextureId,
|
||||
ShaderManager.getTextureLightShaderProgram()
|
||||
);
|
||||
//设置箱子的初始速度
|
||||
tcTemp.body.setLinearVelocity(new Vector3f(0,2,-6));
|
||||
tcTemp.body.setAngularVelocity(new Vector3f(5,0,0));
|
||||
//将新立方体加入到列表中
|
||||
synchronized(tcaForAdd)
|
||||
{
|
||||
tcaForAdd.add(tcTemp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadRigidBody tcTemp=new LoadRigidBody
|
||||
(
|
||||
ShaderManager.getColorShaderProgram(),
|
||||
1,
|
||||
lovoa[index-1],
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
dynamicsWorld
|
||||
);
|
||||
//设置物体的初始速度
|
||||
tcTemp.body.setLinearVelocity(new Vector3f(0,2,-6));
|
||||
tcTemp.body.setAngularVelocity(new Vector3f(5,0,0));
|
||||
//将新立方体加入到列表中
|
||||
synchronized(tcaForAdd)
|
||||
{
|
||||
tcaForAdd.add(tcTemp);
|
||||
}
|
||||
GImpactCollisionAlgorithm.registerAlgorithm(dispatcher);
|
||||
}
|
||||
index=(index+1)%4;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int initTexture(int drawableId)//textureId
|
||||
{
|
||||
//生成纹理ID
|
||||
int[] textures = new int[1];
|
||||
GLES20.glGenTextures
|
||||
(
|
||||
1, //产生的纹理id的数量
|
||||
textures, //纹理id的数组
|
||||
0 //偏移量
|
||||
);
|
||||
int textureId=textures[0];
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
|
||||
|
||||
//通过输入流加载图片===============begin===================
|
||||
InputStream is = this.getResources().openRawResource(drawableId);
|
||||
Bitmap bitmapTmp;
|
||||
try
|
||||
{
|
||||
bitmapTmp = BitmapFactory.decodeStream(is);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//通过输入流加载图片===============end=====================
|
||||
|
||||
//实际加载纹理
|
||||
GLUtils.texImage2D
|
||||
(
|
||||
GLES20.GL_TEXTURE_2D, //纹理类型,在OpenGL ES中必须为GL10.GL_TEXTURE_2D
|
||||
0, //纹理的层次,0表示基本图像层,可以理解为直接贴图
|
||||
bitmapTmp, //纹理图像
|
||||
0 //纹理边框尺寸
|
||||
);
|
||||
bitmapTmp.recycle(); //纹理加载成功后释放图片
|
||||
|
||||
return textureId;
|
||||
}
|
||||
public int initTextureRepeat(int drawableId)//textureId
|
||||
{
|
||||
//生成纹理ID
|
||||
int[] textures = new int[1];
|
||||
GLES20.glGenTextures
|
||||
(
|
||||
1, //产生的纹理id的数量
|
||||
textures, //纹理id的数组
|
||||
0 //偏移量
|
||||
);
|
||||
int textureId=textures[0];
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_REPEAT);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_REPEAT);
|
||||
|
||||
//通过输入流加载图片===============begin===================
|
||||
InputStream is = this.getResources().openRawResource(drawableId);
|
||||
Bitmap bitmapTmp;
|
||||
try
|
||||
{
|
||||
bitmapTmp = BitmapFactory.decodeStream(is);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//通过输入流加载图片===============end=====================
|
||||
|
||||
//实际加载纹理
|
||||
GLUtils.texImage2D
|
||||
(
|
||||
GLES20.GL_TEXTURE_2D, //纹理类型,在OpenGL ES中必须为GL10.GL_TEXTURE_2D
|
||||
0, //纹理的层次,0表示基本图像层,可以理解为直接贴图
|
||||
bitmapTmp, //纹理图像
|
||||
0 //纹理边框尺寸
|
||||
);
|
||||
bitmapTmp.recycle(); //纹理加载成功后释放图片
|
||||
|
||||
return textureId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import java.util.Set;
|
||||
//表示法向量的类,此类的一个对象表示一个法向量
|
||||
public class Normal
|
||||
{
|
||||
public static final float DIFF=0.0000001f;//判断两个法向量是否相同的阈值
|
||||
//法向量在XYZ轴上的分量
|
||||
float nx;
|
||||
float ny;
|
||||
float nz;
|
||||
|
||||
public Normal(float nx,float ny,float nz)
|
||||
{
|
||||
this.nx=nx;
|
||||
this.ny=ny;
|
||||
this.nz=nz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if(o instanceof Normal)
|
||||
{//若两个法向量XYZ三个分量的差都小于指定的阈值则认为这两个法向量相等
|
||||
Normal tn=(Normal)o;
|
||||
if(Math.abs(nx-tn.nx)<DIFF&&
|
||||
Math.abs(ny-tn.ny)<DIFF&&
|
||||
Math.abs(ny-tn.ny)<DIFF
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//由于要用到HashSet,因此一定要重写hashCode方法
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
//求法向量平均值的工具方法
|
||||
public static float[] getAverage(Set<Normal> sn)
|
||||
{
|
||||
//存放法向量和的数组
|
||||
float[] result=new float[3];
|
||||
//把集合中所有的法向量求和
|
||||
for(Normal n:sn)
|
||||
{
|
||||
result[0]+=n.nx;
|
||||
result[1]+=n.ny;
|
||||
result[2]+=n.nz;
|
||||
}
|
||||
//将求和后的法向量规格化
|
||||
return LoadUtil.vectorNormal(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import javax.vecmath.Quat4f;
|
||||
|
||||
public class SYSUtil
|
||||
{
|
||||
//将四元数转换为角度及转轴向量
|
||||
public static float[] fromSYStoAXYZ(Quat4f q4)
|
||||
{
|
||||
double sitaHalf=Math.acos(q4.w);
|
||||
float nx=(float) (q4.x/Math.sin(sitaHalf));
|
||||
float ny=(float) (q4.y/Math.sin(sitaHalf));
|
||||
float nz=(float) (q4.z/Math.sin(sitaHalf));
|
||||
|
||||
return new float[]{(float) Math.toDegrees(sitaHalf*2),nx,ny,nz};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample17_4_Activity extends Activity {
|
||||
private MySurfaceView mGLSurfaceView;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉标头
|
||||
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制横屏
|
||||
|
||||
mGLSurfaceView = new MySurfaceView(this);
|
||||
mGLSurfaceView.requestFocus();//获取焦点
|
||||
mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控
|
||||
|
||||
setContentView(mGLSurfaceView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mGLSurfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mGLSurfaceView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent e)
|
||||
{
|
||||
if(keyCode==4)
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.bn.Sample17_4;
|
||||
import android.content.res.Resources;
|
||||
|
||||
public class ShaderManager
|
||||
{
|
||||
final static int shaderCount=2;
|
||||
final static String[][] shaderName=
|
||||
{
|
||||
{"vertex.sh","frag.sh"},
|
||||
{"vertex_color.sh","frag_color.sh"}
|
||||
};
|
||||
static String[]mVertexShader=new String[shaderCount];
|
||||
static String[]mFragmentShader=new String[shaderCount];
|
||||
static int[] program=new int[shaderCount];
|
||||
|
||||
public static void loadCodeFromFile(Resources r)
|
||||
{
|
||||
for(int i=0;i<shaderCount;i++)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader[i]=ShaderUtil.loadFromAssetsFile(shaderName[i][0],r);
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader[i]=ShaderUtil.loadFromAssetsFile(shaderName[i][1], r);
|
||||
}
|
||||
}
|
||||
|
||||
//编译3D物体的shader
|
||||
public static void compileShader()
|
||||
{
|
||||
for(int i=0;i<shaderCount;i++)
|
||||
{
|
||||
program[i]=ShaderUtil.createProgram(mVertexShader[i], mFragmentShader[i]);
|
||||
System.out.println("mProgram "+program[i]);
|
||||
}
|
||||
}
|
||||
//这里返回的是纹理带光照的shader程序
|
||||
public static int getTextureLightShaderProgram()
|
||||
{
|
||||
return program[0];
|
||||
}
|
||||
//这里返回的是颜色的shader程序
|
||||
public static int getColorShaderProgram()
|
||||
{
|
||||
return program[1];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.bn.Sample17_4;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.opengl.GLES20;
|
||||
import android.util.Log;
|
||||
|
||||
//加载顶点Shader与片元Shader的工具类
|
||||
public class ShaderUtil
|
||||
{
|
||||
//加载制定shader的方法
|
||||
public static int loadShader
|
||||
(
|
||||
int shaderType, //shader的类型 GLES20.GL_VERTEX_SHADER GLES20.GL_FRAGMENT_SHADER
|
||||
String source //shader的脚本字符串
|
||||
)
|
||||
{
|
||||
//创建一个新shader
|
||||
int shader = GLES20.glCreateShader(shaderType);
|
||||
//若创建成功则加载shader
|
||||
if (shader != 0)
|
||||
{
|
||||
//加载shader的源代码
|
||||
GLES20.glShaderSource(shader, source);
|
||||
//编译shader
|
||||
GLES20.glCompileShader(shader);
|
||||
//存放编译成功shader数量的数组
|
||||
int[] compiled = new int[1];
|
||||
//获取Shader的编译情况
|
||||
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
|
||||
if (compiled[0] == 0)
|
||||
{//若编译失败则显示错误日志并删除此shader
|
||||
Log.e("ES20_ERROR", "Could not compile shader " + shaderType + ":");
|
||||
Log.e("ES20_ERROR", GLES20.glGetShaderInfoLog(shader));
|
||||
GLES20.glDeleteShader(shader);
|
||||
shader = 0;
|
||||
}
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
//创建shader程序的方法
|
||||
public static int createProgram(String vertexSource, String fragmentSource)
|
||||
{
|
||||
//加载顶点着色器
|
||||
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
|
||||
if (vertexShader == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
//加载片元着色器
|
||||
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
|
||||
if (pixelShader == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
//创建程序
|
||||
int program = GLES20.glCreateProgram();
|
||||
//若程序创建成功则向程序中加入顶点着色器与片元着色器
|
||||
if (program != 0)
|
||||
{
|
||||
//向程序中加入顶点着色器
|
||||
GLES20.glAttachShader(program, vertexShader);
|
||||
checkGlError("glAttachShader");
|
||||
//向程序中加入片元着色器
|
||||
GLES20.glAttachShader(program, pixelShader);
|
||||
checkGlError("glAttachShader");
|
||||
//链接程序
|
||||
GLES20.glLinkProgram(program);
|
||||
//存放链接成功program数量的数组
|
||||
int[] linkStatus = new int[1];
|
||||
//获取program的链接情况
|
||||
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
|
||||
//若链接失败则报错并删除程序
|
||||
if (linkStatus[0] != GLES20.GL_TRUE)
|
||||
{
|
||||
Log.e("ES20_ERROR", "Could not link program: ");
|
||||
Log.e("ES20_ERROR", GLES20.glGetProgramInfoLog(program));
|
||||
GLES20.glDeleteProgram(program);
|
||||
program = 0;
|
||||
}
|
||||
}
|
||||
return program;
|
||||
}
|
||||
//检查每一步操作是否有错误的方法
|
||||
public static void checkGlError(String op)
|
||||
{
|
||||
int error;
|
||||
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
|
||||
{
|
||||
Log.e("ES20_ERROR", op + ": glError " + error);
|
||||
throw new RuntimeException(op + ": glError " + error);
|
||||
}
|
||||
}
|
||||
//从sh脚本中加载shader内容的方法
|
||||
public static String loadFromAssetsFile(String fname,Resources r)
|
||||
{
|
||||
String result=null;
|
||||
try
|
||||
{
|
||||
InputStream in=r.getAssets().open(fname);
|
||||
int ch=0;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
while((ch=in.read())!=-1)
|
||||
{
|
||||
baos.write(ch);
|
||||
}
|
||||
byte[] buff=baos.toByteArray();
|
||||
baos.close();
|
||||
in.close();
|
||||
result=new String(buff,"UTF-8");
|
||||
result=result.replaceAll("\\r\\n","\n");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.bn.Sample17_4;
|
||||
import javax.vecmath.Quat4f;
|
||||
import javax.vecmath.Vector3f;
|
||||
import com.bulletphysics.collision.shapes.CollisionShape;
|
||||
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
|
||||
import com.bulletphysics.linearmath.DefaultMotionState;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
public class TexCube extends BNThing
|
||||
{
|
||||
TextureRect tr;//用于绘制各个面的纹理矩形
|
||||
float halfSize;//立方体的半边长
|
||||
RigidBody body;//对应的刚体对象
|
||||
int mProgram;
|
||||
MySurfaceView mv;
|
||||
int[] texIda;
|
||||
public TexCube(MySurfaceView mv,float halfSize,CollisionShape colShape,
|
||||
DiscreteDynamicsWorld dynamicsWorld,float mass,float cx,float cy,float cz,int[] texIda,int mProgram)
|
||||
{
|
||||
this.texIda=texIda;
|
||||
boolean isDynamic = (mass != 0f);//物体是否可以运动
|
||||
Vector3f localInertia = new Vector3f(0, 0, 0);//惯性向量
|
||||
if (isDynamic) //如果物体可以运动
|
||||
{
|
||||
colShape.calculateLocalInertia(mass, localInertia);//计算惯性
|
||||
}
|
||||
Transform startTransform = new Transform();//创建刚体的初始变换对象
|
||||
startTransform.setIdentity();//变换初始化
|
||||
startTransform.origin.set(new Vector3f(cx, cy, cz));//设置初始的位置
|
||||
//创建刚体的运动状态对象
|
||||
DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
|
||||
//创建刚体信息对象
|
||||
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo
|
||||
(mass, myMotionState, colShape, localInertia);
|
||||
body = new RigidBody(rbInfo);//创建刚体
|
||||
body.setRestitution(0.6f);//设置反弹系数
|
||||
body.setFriction(0.8f);//设置摩擦系数
|
||||
dynamicsWorld.addRigidBody(body);//将刚体添加进物理世界
|
||||
this.mv=mv; //保存MySurfaceView引用
|
||||
tr=new TextureRect(halfSize);//创建纹理矩形
|
||||
this.mProgram=mProgram;//保存着色器程序引用
|
||||
this.halfSize=halfSize; //保存半长
|
||||
}
|
||||
public void drawSelf()
|
||||
{
|
||||
tr.intShader(mv, mProgram);//纹理矩形初始化着色器
|
||||
int texId=texIda[0];//物体运动时的纹理id
|
||||
if(!body.isActive()){texId=texIda[1];}//物体静止时的纹理id
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
//获取这个箱子的变换信息对象
|
||||
Transform trans = body.getMotionState().getWorldTransform(new Transform());
|
||||
//进行移位变换
|
||||
MatrixState.translate(trans.origin.x,trans.origin.y, trans.origin.z);
|
||||
Quat4f ro=trans.getRotation(new Quat4f());//获取当前变换的旋转信息
|
||||
if(ro.x!=0||ro.y!=0||ro.z!=0)
|
||||
{
|
||||
float[] fa=SYSUtil.fromSYStoAXYZ(ro);//将四元数转换成AXYZ的形式
|
||||
MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);//执行旋转
|
||||
}
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(0, halfSize, 0);//执行平移
|
||||
MatrixState.rotate(-90, 1, 0, 0);//执行旋转
|
||||
tr.drawSelf( texId);//绘制上面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(0, -halfSize, 0);//执行平移
|
||||
MatrixState.rotate(90, 1, 0, 0);//执行旋转
|
||||
tr.drawSelf( texId);//绘制下面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(-halfSize, 0, 0);//执行平移
|
||||
MatrixState.rotate(-90, 0, 1, 0);//执行旋转
|
||||
tr.drawSelf( texId);//绘制左面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(halfSize, 0, 0);//执行平移
|
||||
MatrixState.rotate(90, 0, 1, 0);//执行旋转
|
||||
tr.drawSelf( texId);//绘制右面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(0, 0, halfSize);//执行平移
|
||||
tr.drawSelf(texId);//绘制前面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
MatrixState.translate(0, 0, -halfSize);//执行平移
|
||||
MatrixState.rotate(180, 0, 1, 0);//执行旋转
|
||||
tr.drawSelf( texId);//绘制后面
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
}
|
||||
}
|
||||
157
第17章 游戏的心脏——物理引擎/Sample17_4/src/com/bn/Sample17_4/TexFloor.java
Normal file
157
第17章 游戏的心脏——物理引擎/Sample17_4/src/com/bn/Sample17_4/TexFloor.java
Normal file
@@ -0,0 +1,157 @@
|
||||
package com.bn.Sample17_4;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
import com.bulletphysics.collision.shapes.*;
|
||||
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
|
||||
import com.bulletphysics.linearmath.DefaultMotionState;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
public class TexFloor {
|
||||
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
int uTexHandle;//外观纹理属性引用id
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
private FloatBuffer mTextureBuffer;//顶点着色数据缓冲
|
||||
|
||||
int vCount;
|
||||
float yOffset;
|
||||
|
||||
public TexFloor(int mProgram,final float UNIT_SIZE, float yOffset,CollisionShape groundShape,DiscreteDynamicsWorld dynamicsWorld)
|
||||
{
|
||||
this.mProgram=mProgram;
|
||||
this.yOffset=yOffset;
|
||||
//创建刚体的初始变换对象
|
||||
Transform groundTransform = new Transform();
|
||||
groundTransform.setIdentity();
|
||||
groundTransform.origin.set(new Vector3f(0.f, yOffset, 0.f));
|
||||
Vector3f localInertia = new Vector3f(0, 0, 0);//惯性
|
||||
//创建刚体的运动状态对象
|
||||
DefaultMotionState myMotionState = new DefaultMotionState(groundTransform);
|
||||
//创建刚体信息对象
|
||||
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0, myMotionState, groundShape, localInertia);
|
||||
//创建刚体
|
||||
RigidBody body = new RigidBody(rbInfo);
|
||||
//设置反弹系数
|
||||
body.setRestitution(0.4f);
|
||||
//设置摩擦系数
|
||||
body.setFriction(0.8f);
|
||||
//将刚体添加进物理世界
|
||||
dynamicsWorld.addRigidBody(body);
|
||||
initVertexData(UNIT_SIZE);
|
||||
intShader(mProgram);
|
||||
}
|
||||
|
||||
public void initVertexData(final float UNIT_SIZE){
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=6;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
|
||||
-1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,
|
||||
-1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
|
||||
|
||||
1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
|
||||
1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,
|
||||
-1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,
|
||||
};
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
//顶点纹理数据的初始化================begin============================
|
||||
float textures[]=new float[]
|
||||
{
|
||||
UNIT_SIZE/2,UNIT_SIZE/2, 0,0, 0,UNIT_SIZE/2,
|
||||
UNIT_SIZE/2,UNIT_SIZE/2, UNIT_SIZE/2,0, 0,0
|
||||
};
|
||||
|
||||
//创建顶点纹理数据缓冲
|
||||
ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length*4);
|
||||
tbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTextureBuffer= tbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mTextureBuffer.put(textures);//向缓冲区中放入顶点着色数据
|
||||
mTextureBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点纹理数据的初始化================end============================
|
||||
}
|
||||
//初始化shader
|
||||
public void intShader(int mProgram)
|
||||
{
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点经纬度属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中摄像机位置引用id
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
uTexHandle=GLES20.glGetUniformLocation(mProgram, "sTexture");
|
||||
}
|
||||
|
||||
public void drawSelf(int texId)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将摄像机位置传入shader程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点纹理坐标数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maTexCoorHandle,
|
||||
2,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
2*4,
|
||||
mTextureBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
GLES20.glUniform1i(uTexHandle, 0);
|
||||
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.bn.Sample17_4;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
public class TextureRect {
|
||||
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int uTexHandle;//外观纹理属性引用id
|
||||
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
private FloatBuffer mTextureBuffer;//顶点着色数据缓冲
|
||||
int vCount;
|
||||
|
||||
public TextureRect(final float UNIT_SIZE)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(UNIT_SIZE);
|
||||
}
|
||||
|
||||
public void initVertexData(final float UNIT_SIZE){
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=6;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
-1*UNIT_SIZE,1*UNIT_SIZE,0,
|
||||
-1*UNIT_SIZE,-1*UNIT_SIZE,0,
|
||||
1*UNIT_SIZE,1*UNIT_SIZE,0,
|
||||
|
||||
-1*UNIT_SIZE,-1*UNIT_SIZE,0,
|
||||
1*UNIT_SIZE,-1*UNIT_SIZE,0,
|
||||
1*UNIT_SIZE,1*UNIT_SIZE,0
|
||||
};
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
//顶点纹理数据的初始化================begin============================
|
||||
float textures[]=new float[]
|
||||
{
|
||||
0,1, 0,0, 1,1,
|
||||
0,0, 1,0, 1,1
|
||||
};
|
||||
//创建顶点纹理数据缓冲
|
||||
ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length*4);
|
||||
tbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTextureBuffer= tbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mTextureBuffer.put(textures);//向缓冲区中放入顶点着色数据
|
||||
mTextureBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点纹理数据的初始化================end============================
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void intShader(MySurfaceView mv,int mProgram)
|
||||
{
|
||||
this.mProgram=mProgram;
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点经纬度属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中摄像机位置引用id
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
uTexHandle=GLES20.glGetUniformLocation(mProgram, "sTexture");
|
||||
}
|
||||
|
||||
public void drawSelf(int texId)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将摄像机位置传入shader程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点纹理坐标数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maTexCoorHandle,
|
||||
2,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
2*4,
|
||||
mTextureBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
GLES20.glUniform1i(uTexHandle, 0);
|
||||
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user