初次提交
This commit is contained in:
9
第17章 游戏的心脏——物理引擎/Sample17_6/.classpath
Normal file
9
第17章 游戏的心脏——物理引擎/Sample17_6/.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_6/.project
Normal file
33
第17章 游戏的心脏——物理引擎/Sample17_6/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample17_6</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>
|
||||
18
第17章 游戏的心脏——物理引擎/Sample17_6/AndroidManifest.xml
Normal file
18
第17章 游戏的心脏——物理引擎/Sample17_6/AndroidManifest.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.bn.Sample17_6"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name="com.bn.Sample17_6.Sample17_6_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>
|
||||
<uses-sdk android:minSdkVersion="8" />
|
||||
|
||||
</manifest>
|
||||
8
第17章 游戏的心脏——物理引擎/Sample17_6/assets/frag.sh
Normal file
8
第17章 游戏的心脏——物理引擎/Sample17_6/assets/frag.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元颜色值
|
||||
gl_FragColor = texture2D(sTexture, vTextureCoord);
|
||||
}
|
||||
9
第17章 游戏的心脏——物理引擎/Sample17_6/assets/frag_color.sh
Normal file
9
第17章 游戏的心脏——物理引擎/Sample17_6/assets/frag_color.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
precision mediump float;
|
||||
varying vec4 ambient;
|
||||
varying vec4 diffuse;
|
||||
varying vec4 specular;
|
||||
void main()
|
||||
{
|
||||
vec4 mColor=vec4(0.763,0.657,0.614,0);
|
||||
gl_FragColor = mColor*ambient+mColor*diffuse+mColor*specular;//¸ø´ËƬԪÑÕɫֵ
|
||||
}
|
||||
12
第17章 游戏的心脏——物理引擎/Sample17_6/assets/vertex.sh
Normal file
12
第17章 游戏的心脏——物理引擎/Sample17_6/assets/vertex.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
uniform vec3 uCamera; //摄像机位置
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
varying vec2 vTextureCoord;
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
//将顶点的纹理坐标传给片元着色器
|
||||
vTextureCoord=aTexCoor;
|
||||
}
|
||||
46
第17章 游戏的心脏——物理引擎/Sample17_6/assets/vertex_color.sh
Normal file
46
第17章 游戏的心脏——物理引擎/Sample17_6/assets/vertex_color.sh
Normal file
@@ -0,0 +1,46 @@
|
||||
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.1,0.1,0.1,1.0),vec4(0.7,0.7,0.7,1.0),vec4(0.3,0.3,0.3,1.0));
|
||||
}
|
||||
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/Sample17_6.apk
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/Sample17_6.apk
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/classes.dex
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/classes.dex
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Constant.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Constant.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Cube.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Cube.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_6/bin/com/bn/Sample17_6/R$attr.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R$attr.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R$layout.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R$layout.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R$string.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R$string.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/R.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/SYSUtil.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/SYSUtil.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Stick.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/Stick.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/TexRect.class
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/com/bn/Sample17_6/TexRect.class
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/resources.ap_
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/bin/resources.ap_
Normal file
Binary file not shown.
11
第17章 游戏的心脏——物理引擎/Sample17_6/default.properties
Normal file
11
第17章 游戏的心脏——物理引擎/Sample17_6/default.properties
Normal file
@@ -0,0 +1,11 @@
|
||||
# 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.
|
||||
|
||||
# Project target.
|
||||
target=android-8
|
||||
25
第17章 游戏的心脏——物理引擎/Sample17_6/gen/com/bn/Sample17_6/R.java
Normal file
25
第17章 游戏的心脏——物理引擎/Sample17_6/gen/com/bn/Sample17_6/R.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/* 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_6;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int arrow_small=0x7f020000;
|
||||
public static final int icon=0x7f020001;
|
||||
public static final int wood_bin1=0x7f020002;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int main=0x7f030000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f040001;
|
||||
public static final int hello=0x7f040000;
|
||||
}
|
||||
}
|
||||
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/lib/jbullet.jar
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/lib/jbullet.jar
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/lib/vecmath.jar
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/lib/vecmath.jar
Normal file
Binary file not shown.
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-hdpi/icon.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-hdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-ldpi/icon.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-ldpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/arrow_small.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/arrow_small.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/icon.png
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/wood_bin1.jpg
Normal file
BIN
第17章 游戏的心脏——物理引擎/Sample17_6/res/drawable-mdpi/wood_bin1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
12
第17章 游戏的心脏——物理引擎/Sample17_6/res/layout/main.xml
Normal file
12
第17章 游戏的心脏——物理引擎/Sample17_6/res/layout/main.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
>
|
||||
<TextView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/hello"
|
||||
/>
|
||||
</LinearLayout>
|
||||
5
第17章 游戏的心脏——物理引擎/Sample17_6/res/values/strings.xml
Normal file
5
第17章 游戏的心脏——物理引擎/Sample17_6/res/values/strings.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="hello">Hello World, MainActivity!</string>
|
||||
<string name="app_name">Sample17_6</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.bn.Sample17_6;
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
public class Constant {
|
||||
|
||||
public static final float cubeSize=1f;//长方体的半高度
|
||||
|
||||
public static final float Ball_R=1f;//长方体的半长度
|
||||
public static final float Ball_Height=0f;//长方体的半长度
|
||||
|
||||
public static final float Stick_Length=5f;//长方体的半长度
|
||||
public static final float Stick_R=0.2f;//长方体的半长度
|
||||
public static final float Stick_Height=4f;//长方体的半长度
|
||||
|
||||
public static final float Ceiling_Height=8f;//长方体的半长度
|
||||
public static final float Floor_Height=-2f;//长方体的半长度
|
||||
|
||||
public static final float LEG_MASS=1f;//腿的质量
|
||||
|
||||
public static final Vector3f boxPos = new Vector3f(-3,5,5);
|
||||
|
||||
public static boolean keyFlag=true;
|
||||
|
||||
|
||||
public static boolean isNumber(String str){
|
||||
if(str.equals("NaN") || Float.isInfinite(Float.parseFloat(str))){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
89
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/Cube.java
Normal file
89
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/Cube.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
import static com.bn.Sample17_6.Constant.isNumber;
|
||||
|
||||
import javax.vecmath.Quat4f;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
|
||||
public class Cube {
|
||||
|
||||
TexRect rect;//正方形
|
||||
float halfSize;
|
||||
|
||||
RigidBody body;
|
||||
MySurfaceView mv;
|
||||
|
||||
public Cube
|
||||
(
|
||||
MySurfaceView mv,
|
||||
float halfSize,
|
||||
RigidBody body
|
||||
){
|
||||
this.mv=mv;
|
||||
this.halfSize=halfSize;
|
||||
rect = new TexRect(mv,1,halfSize,halfSize);
|
||||
this.body=body;
|
||||
}
|
||||
|
||||
public void drawSelf(int[] texIds,int index){
|
||||
int texId = texIds[index];
|
||||
|
||||
MatrixState.pushMatrix();
|
||||
MySurfaceView.init=false;
|
||||
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);
|
||||
if(isNumber(fa[0]+"") && isNumber(fa[1]+"") && isNumber(fa[2]+"")){
|
||||
MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//绘制上面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, halfSize, 0);
|
||||
MatrixState.rotate(-90, 1, 0, 0);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制下面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, -halfSize, 0);
|
||||
MatrixState.rotate(90, 1, 0, 0);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制左面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(-halfSize, 0, 0);
|
||||
MatrixState.rotate(-90, 0, 1, 0);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制右面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(halfSize, 0, 0);
|
||||
MatrixState.rotate(90, 0, 1, 0);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制前面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, halfSize);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制后面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, -halfSize);
|
||||
MatrixState.rotate(180, 0, 1, 0);
|
||||
rect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.bn.Sample17_6;
|
||||
import static com.bn.Sample17_6.Constant.*;
|
||||
public class KeyThread extends Thread //监听键盘状态的线程
|
||||
{
|
||||
MySurfaceView mv;
|
||||
public KeyThread(MySurfaceView mv)
|
||||
{
|
||||
this.mv=mv;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
while(keyFlag)
|
||||
{
|
||||
if((MySurfaceView.keyState&0x1)!=0)
|
||||
{//有UP键按下
|
||||
mv.slideFB(1);
|
||||
}
|
||||
else if((MySurfaceView.keyState&0x2)!=0)
|
||||
{//有down键按下
|
||||
mv.slideFB(-1);
|
||||
}
|
||||
if((MySurfaceView.keyState&0x4)!=0)
|
||||
{//有left键按下
|
||||
mv.slideLR(-1);
|
||||
}
|
||||
else if((MySurfaceView.keyState&0x8)!=0)
|
||||
{//有right键按下
|
||||
mv.slideLR(1);
|
||||
}
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
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矩阵 投影用
|
||||
private 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 matrix(float[] self)
|
||||
{
|
||||
float[] result=new float[16];
|
||||
Matrix.multiplyMM(result,0,currMatrix,0,self,0);
|
||||
currMatrix=result;
|
||||
}
|
||||
|
||||
//用于一帧内的摄像机矩阵
|
||||
private static float[] mVMatrixForSpecFrame = new float[16];//摄像机位置朝向9参数矩阵
|
||||
public static void copyMVMatrix()
|
||||
{
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
mVMatrixForSpecFrame[i]=mVMatrix[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,406 @@
|
||||
package com.bn.Sample17_6;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.opengl.GLUtils;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import com.bn.Sample17_6.R;
|
||||
import com.bulletphysics.BulletGlobals;
|
||||
import com.bulletphysics.collision.broadphase.AxisSweep3;
|
||||
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
|
||||
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
|
||||
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
|
||||
import com.bulletphysics.collision.shapes.BoxShape;
|
||||
import com.bulletphysics.collision.shapes.CollisionShape;
|
||||
import com.bulletphysics.collision.shapes.CylinderShape;
|
||||
import com.bulletphysics.collision.shapes.CylinderShapeX;
|
||||
import com.bulletphysics.collision.shapes.CylinderShapeZ;
|
||||
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
|
||||
import com.bulletphysics.dynamics.constraintsolver.SliderConstraint;
|
||||
import com.bulletphysics.linearmath.MatrixUtil;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
|
||||
import static com.bn.Sample17_6.Constant.*;
|
||||
|
||||
public class MySurfaceView extends GLSurfaceView {
|
||||
|
||||
DiscreteDynamicsWorld dynamicsWorld;
|
||||
CollisionShape boxShape;
|
||||
CollisionShape stickShape;
|
||||
CollisionShape stickFBSliderShape;
|
||||
CollisionShape stickLRSliderShape;
|
||||
//刚体
|
||||
RigidBodyHelper cubeBody;
|
||||
RigidBodyHelper stickFBSliderBody;
|
||||
RigidBodyHelper stickLRFSliderBody;
|
||||
RigidBodyHelper stickLRNSliderBody;
|
||||
//添加滑动约束
|
||||
SliderConstraint sliderFB;
|
||||
SliderConstraint sliderLRF;//远端的横向轴
|
||||
SliderConstraint sliderLRN;//近端的横向轴
|
||||
SliderConstraint[] sliders=new SliderConstraint[3];
|
||||
static boolean sliding=false;
|
||||
static boolean init=true;
|
||||
int currIndex;
|
||||
MyRenderer renderer;
|
||||
public static int keyState=0;
|
||||
boolean flag=true;
|
||||
|
||||
float screenWidth;
|
||||
float screenHeight;
|
||||
float buttonPixels;
|
||||
|
||||
public MySurfaceView(Context context) {
|
||||
super(context);
|
||||
this.setEGLContextClientVersion(2);
|
||||
initWorld();
|
||||
renderer = new MyRenderer();
|
||||
this.setRenderer(renderer);
|
||||
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
|
||||
}
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
float y = e.getY();
|
||||
float x = e.getX();
|
||||
switch (e.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
handleArrowDown(x,y);
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
handleArrowUp(x,y);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void handleArrowDown(float x,float y){
|
||||
float buttonCenterX=screenWidth-buttonPixels/2;
|
||||
float buttonCenterY=screenHeight-buttonPixels/2;
|
||||
float baseUnit=buttonPixels/4;
|
||||
|
||||
float upXMax=baseUnit+buttonCenterX;
|
||||
float upXMin=-baseUnit+buttonCenterX;
|
||||
float upYMin=-2*baseUnit+buttonCenterY;
|
||||
float upYMax=-baseUnit+buttonCenterY;
|
||||
|
||||
float downXMax=baseUnit+buttonCenterX;
|
||||
float downXMin=-baseUnit+buttonCenterX;
|
||||
float downYMax=2*baseUnit+buttonCenterY;
|
||||
float downYMin=baseUnit+buttonCenterY;
|
||||
|
||||
float leftXMax=-baseUnit+buttonCenterX;
|
||||
float leftXMin=-2*baseUnit+buttonCenterX;
|
||||
float leftYMax=baseUnit+buttonCenterY;
|
||||
float leftYMin=-baseUnit+buttonCenterY;
|
||||
|
||||
float rightXMax=2*baseUnit+buttonCenterX;
|
||||
float rightXMin=baseUnit+buttonCenterX;
|
||||
float rightYMax=baseUnit+buttonCenterY;
|
||||
float rightYMin=-baseUnit+buttonCenterY;
|
||||
|
||||
if(upXMin<x && x<upXMax && upYMin<y && y<upYMax){
|
||||
keyState=0x1;
|
||||
}else if(rightXMin<x && x<rightXMax && rightYMin<y && y<rightYMax){
|
||||
keyState=0x8;
|
||||
}else if(downXMin<x && x<downXMax && downYMin<y && y<downYMax){
|
||||
keyState=0x2;
|
||||
}else if(leftXMin<x && x<leftXMax && leftYMin<y && y<leftYMax){
|
||||
keyState=0x4;
|
||||
}
|
||||
}
|
||||
public void handleArrowUp(float x,float y){
|
||||
float buttonCenterX=screenWidth-buttonPixels/2;
|
||||
float buttonCenterY=screenHeight-buttonPixels/2;
|
||||
float baseUnit=buttonPixels/4;
|
||||
|
||||
float upXMax=baseUnit+buttonCenterX;
|
||||
float upXMin=-baseUnit+buttonCenterX;
|
||||
float upYMin=-2*baseUnit+buttonCenterY;
|
||||
float upYMax=-baseUnit+buttonCenterY;
|
||||
|
||||
float downXMax=baseUnit+buttonCenterX;
|
||||
float downXMin=-baseUnit+buttonCenterX;
|
||||
float downYMax=2*baseUnit+buttonCenterY;
|
||||
float downYMin=baseUnit+buttonCenterY;
|
||||
|
||||
float leftXMax=-baseUnit+buttonCenterX;
|
||||
float leftXMin=-2*baseUnit+buttonCenterX;
|
||||
float leftYMax=baseUnit+buttonCenterY;
|
||||
float leftYMin=-baseUnit+buttonCenterY;
|
||||
|
||||
float rightXMax=2*baseUnit+buttonCenterX;
|
||||
float rightXMin=baseUnit+buttonCenterX;
|
||||
float rightYMax=baseUnit+buttonCenterY;
|
||||
float rightYMin=-baseUnit+buttonCenterY;
|
||||
|
||||
if(upXMin<x && x<upXMax && upYMin<y && y<upYMax){
|
||||
keyState=0;
|
||||
stopSlide();
|
||||
}else if(rightXMin<x && x<rightXMax && rightYMin<y && y<rightYMax){
|
||||
keyState=0;
|
||||
stopSlide();
|
||||
}else if(downXMin<x && x<downXMax && downYMin<y && y<downYMax){
|
||||
keyState=0;
|
||||
stopSlide();
|
||||
}else if(leftXMin<x && x<leftXMax && leftYMin<y && y<leftYMax){
|
||||
keyState=0;
|
||||
stopSlide();
|
||||
}
|
||||
}
|
||||
public void initWorld(){
|
||||
CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
|
||||
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
|
||||
Vector3f worldAabbMin = new Vector3f(-10000, -10000, -10000);
|
||||
Vector3f worldAabbMax = new Vector3f(10000, 10000, 10000);
|
||||
int maxProxies = 1024;
|
||||
AxisSweep3 overlappingPairCache =new AxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
|
||||
SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
|
||||
dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver,collisionConfiguration);
|
||||
dynamicsWorld.setGravity(new Vector3f(0, -10, 0));
|
||||
boxShape=new BoxShape(new Vector3f(cubeSize,cubeSize,cubeSize));
|
||||
stickShape = new CylinderShape(new Vector3f(Stick_R,Stick_Length,Stick_R));//Y方向圆柱
|
||||
stickFBSliderShape = new CylinderShapeZ(new Vector3f(Stick_R,Stick_R,Stick_Length));//Z方向圆柱
|
||||
stickLRSliderShape = new CylinderShapeX(new Vector3f(Stick_Length,Stick_R,Stick_R));//X方向圆柱
|
||||
initRigidBody();
|
||||
//添加长方体与前后方向的棍之间的约束
|
||||
Vector3f originA = new Vector3f(0, 0, 0);
|
||||
Vector3f originB = new Vector3f(0, 0, 0);
|
||||
addSliderConstraint(0,stickFBSliderBody.body,cubeBody.body,BulletGlobals.SIMD_PI/2,originA,originB,true);//BulletGlobals.SIMD_PI/2
|
||||
//添加前后方向的棍与左右方向远端的棍之间的约束
|
||||
originA = new Vector3f(0,0,0);
|
||||
originB = new Vector3f(0, 0, -Stick_Length);
|
||||
addSliderConstraint(1,stickLRFSliderBody.body,stickFBSliderBody.body,0,originA,originB,true);
|
||||
//添加FB方向的棍与LR方向近端的棍之间的约束
|
||||
originA = new Vector3f(0,0,0);
|
||||
originB = new Vector3f(0, 0, Stick_Length);
|
||||
addSliderConstraint(2,stickLRNSliderBody.body,stickFBSliderBody.body,0,originA,originB,true);
|
||||
}
|
||||
public void initRigidBody(){ //TODO
|
||||
cubeBody = new RigidBodyHelper(boxShape,1f,dynamicsWorld, 0.0f, 0.8f, new Vector3f(0,Ceiling_Height-5,0),true);
|
||||
stickFBSliderBody = new RigidBodyHelper(stickFBSliderShape, 1f, dynamicsWorld, 0.0f, 0.8f, new Vector3f(0,Ceiling_Height-5,0),true);
|
||||
stickLRFSliderBody = new RigidBodyHelper(stickLRSliderShape, 0, dynamicsWorld, 0.0f, 0.8f, new Vector3f(0,Ceiling_Height-5,-Stick_Length),true);
|
||||
stickLRNSliderBody = new RigidBodyHelper(stickLRSliderShape, 0, dynamicsWorld, 0.0f, 0.8f, new Vector3f(0,Ceiling_Height-5,Stick_Length),true);
|
||||
}
|
||||
public void addSliderConstraint(int index,RigidBody ra,RigidBody rb,float angle,Vector3f originA,Vector3f originB,boolean force){
|
||||
Transform localA = new Transform();
|
||||
Transform localB = new Transform();
|
||||
localA.setIdentity();
|
||||
localB.setIdentity();
|
||||
MatrixUtil.setEulerZYX(localA.basis, 0,angle, 0 );
|
||||
localA.origin.set(originA);
|
||||
MatrixUtil.setEulerZYX(localB.basis, 0, angle, 0);
|
||||
localB.origin.set(originB);
|
||||
if(index==0){
|
||||
sliderFB = new SliderConstraint(ra, rb, localA, localB, force);
|
||||
//设置初始的limit
|
||||
sliderFB.setLowerLinLimit(-Stick_Length);//控制滑动的最小距离
|
||||
sliderFB.setUpperLinLimit(Stick_Length);//控制滑动的最大距离
|
||||
sliderFB.setLowerAngLimit(0);
|
||||
sliderFB.setUpperAngLimit(0);
|
||||
sliderFB.setDampingDirLin(0.05f); //设置线性阻尼
|
||||
dynamicsWorld.addConstraint(sliderFB,true);
|
||||
sliders[index]=sliderFB;
|
||||
}
|
||||
if(index==1){
|
||||
sliderLRF = new SliderConstraint(ra, rb, localA, localB, force);
|
||||
//设置初始的limit
|
||||
sliderLRF.setLowerLinLimit(-Stick_Length);//控制滑动的最小距离
|
||||
sliderLRF.setUpperLinLimit(Stick_Length);//控制滑动的最大距离
|
||||
sliderLRF.setLowerAngLimit(0);
|
||||
sliderLRF.setUpperAngLimit(0);
|
||||
sliderLRF.setDampingDirLin(0.5f); //设置线性阻尼
|
||||
dynamicsWorld.addConstraint(sliderLRF,true);
|
||||
sliders[index]=sliderLRF;
|
||||
}
|
||||
if(index==2){
|
||||
sliderLRN = new SliderConstraint(ra, rb, localA, localB, force);
|
||||
//设置初始的limit
|
||||
sliderLRN.setLowerLinLimit(-Stick_Length);//控制滑动的最小距离
|
||||
sliderLRN.setUpperLinLimit(Stick_Length);//控制滑动的最大距离
|
||||
sliderLRN.setLowerAngLimit(0);
|
||||
sliderLRN.setUpperAngLimit(0);
|
||||
sliderLRN.setDampingDirLin(0.5f); //设置线性阻尼
|
||||
dynamicsWorld.addConstraint(sliderLRN,true);
|
||||
sliders[index]=sliderLRN;
|
||||
}
|
||||
}
|
||||
public void slideFB(float mulFactor){
|
||||
sliding=true;
|
||||
sliderFB.getRigidBodyB().activate();
|
||||
currIndex=0;
|
||||
sliderFB.setPoweredLinMotor(true);//设置motor可用
|
||||
sliderFB.setMaxLinMotorForce(1.0f);//设置线性运动力的大小
|
||||
sliderFB.setTargetLinMotorVelocity(5.0f*mulFactor);//设置线性运动的速度
|
||||
}
|
||||
public void slideLR(float mulFactor){
|
||||
sliding=true;
|
||||
sliderLRF.getRigidBodyB().activate();
|
||||
currIndex=1;
|
||||
sliderLRF.setPoweredLinMotor(true);//设置motor可用
|
||||
sliderLRF.setMaxLinMotorForce(5.0f);//设置线性运动力的大小
|
||||
sliderLRF.setTargetLinMotorVelocity(5.0f*mulFactor);//设置线性运动的速度
|
||||
|
||||
sliderLRN.setPoweredLinMotor(true);//设置motor可用
|
||||
sliderLRN.setMaxLinMotorForce(5.0f);//设置线性运动力的大小
|
||||
sliderLRN.setTargetLinMotorVelocity(5.0f*mulFactor);//设置线性运动的速度
|
||||
}
|
||||
public void stopSlide(){
|
||||
sliding=false;
|
||||
sliders[currIndex].setPoweredLinMotor(false);
|
||||
sliders[currIndex].setMaxLinMotorForce(0.0f);//设置线性运动力的大小
|
||||
sliders[currIndex].setTargetLinMotorVelocity(0.0f);//设置线性运动的速度
|
||||
if(currIndex==1){
|
||||
sliders[currIndex+1].setPoweredLinMotor(false);
|
||||
sliders[currIndex+1].setMaxLinMotorForce(0.0f);//设置线性运动力的大小
|
||||
sliders[currIndex+1].setTargetLinMotorVelocity(0.0f);//设置线性运动的速度
|
||||
}
|
||||
}
|
||||
private class MyRenderer implements GLSurfaceView.Renderer{
|
||||
float ratio;
|
||||
Cube cube;
|
||||
Stick stickFBSliderAxis;
|
||||
Stick stickLRFSliderAxis;
|
||||
Stick stickLRNSliderAxis;
|
||||
int activeTexId;
|
||||
int deactiveTexId;
|
||||
int textureArrow;//系统分配的游戏前进虚拟按钮纹理id
|
||||
int[] texIds = new int[2];
|
||||
TexRect button;//虚拟按钮
|
||||
@Override public void onDrawFrame(GL10 gl) {
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 1.5f, 100);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(2,14,2f,0,0,0,0f,0f,-1f);
|
||||
MatrixState.setLightLocationRed(5, 50, 15);
|
||||
|
||||
MatrixState.pushMatrix();
|
||||
cube.drawSelf(texIds, 1);
|
||||
stickFBSliderAxis.drawSelf(90,0,1,0);//绘制纵向轴
|
||||
stickLRFSliderAxis.drawSelf(0,1, 0, 0);//绘制远端横向轴
|
||||
stickLRNSliderAxis.drawSelf(0,1, 0, 0);//绘制近端横向轴
|
||||
MatrixState.popMatrix();
|
||||
//绘制虚拟按钮
|
||||
//开启混合
|
||||
MatrixState.setCamera(0,0,10,0,0,0,0f,1.0f,0.0f);
|
||||
MatrixState.setProjectOrtho(-ratio, ratio, -1, 1, 1.5f, 100);
|
||||
MatrixState.pushMatrix();
|
||||
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
|
||||
GLES20.glEnable(GLES20.GL_BLEND);
|
||||
//设置混合因子
|
||||
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
|
||||
//绘制按钮
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(ratio-0.5f,-(1-0.5f),0);
|
||||
button.drawSelf(textureArrow);
|
||||
MatrixState.popMatrix();
|
||||
//关闭混合
|
||||
GLES20.glDisable(GLES20.GL_BLEND);
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
@Override public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算GLSurfaceView的宽高比
|
||||
ratio= (float) width / height;
|
||||
screenWidth= width;
|
||||
screenHeight=height;
|
||||
buttonPixels=screenHeight/2;
|
||||
}
|
||||
@Override public void onSurfaceCreated(GL10 gl, EGLConfig config) {//TODO
|
||||
//设置屏幕背景色RGBA
|
||||
GLES20.glClearColor(0.0f,0.0f,0.0f, 1.0f);
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
MatrixState.setInitStack();
|
||||
activeTexId = initTexture(R.drawable.wood_bin1);
|
||||
textureArrow = initTexture(R.drawable.arrow_small);
|
||||
texIds[0]=deactiveTexId;
|
||||
texIds[1]=activeTexId;
|
||||
cube = new Cube(MySurfaceView.this,cubeSize,cubeBody.body);
|
||||
stickFBSliderAxis = new Stick(MySurfaceView.this,Stick_Length,Stick_R,11.25f,
|
||||
new float[]{1,0,0,1},
|
||||
stickFBSliderBody.body);
|
||||
stickLRFSliderAxis = new Stick(MySurfaceView.this,Stick_Length,Stick_R,11.25f,
|
||||
new float[]{1,0,0,1},
|
||||
stickLRFSliderBody.body);
|
||||
stickLRNSliderAxis = new Stick(MySurfaceView.this,Stick_Length,Stick_R,11.25f,
|
||||
new float[]{1,0,0,1},
|
||||
stickLRNSliderBody.body);
|
||||
button = new TexRect(MySurfaceView.this, 0.5f,1f,1f);
|
||||
|
||||
new Thread(){
|
||||
public void run(){
|
||||
while(flag){
|
||||
try{
|
||||
//模拟
|
||||
dynamicsWorld.stepSimulation(1f/60.f, 5);
|
||||
Thread.sleep(20);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
new KeyThread(MySurfaceView.this).start();
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
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 RigidBodyHelper {
|
||||
|
||||
RigidBody body;
|
||||
boolean noGravity;
|
||||
|
||||
public RigidBodyHelper(CollisionShape shape,float mass,DiscreteDynamicsWorld dynamicsWorld,
|
||||
float restitution,float friction,Vector3f origin,boolean noGravity){
|
||||
this.noGravity=noGravity;
|
||||
boolean isDynamic = (mass!=0);
|
||||
Vector3f localInertia = new Vector3f(0,0,0);
|
||||
if(isDynamic){
|
||||
shape.calculateLocalInertia(mass, localInertia);
|
||||
}
|
||||
//创建刚体的初始变换对象
|
||||
Transform groundTransform = new Transform();
|
||||
groundTransform.setIdentity();
|
||||
groundTransform.origin.set(origin);
|
||||
//创建刚体的运动状态对象
|
||||
DefaultMotionState myMotionState = new DefaultMotionState(groundTransform);
|
||||
//创建刚体信息对象
|
||||
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
|
||||
//创建刚体
|
||||
body = new RigidBody(rbInfo);
|
||||
//设置反弹系数
|
||||
body.setRestitution(restitution);
|
||||
//设置摩擦系数
|
||||
body.setFriction(friction);
|
||||
|
||||
//将刚体添加进物理世界
|
||||
//bullet是在add刚体的时候为刚体设置其重力的
|
||||
dynamicsWorld.addRigidBody(body);
|
||||
//一定要在add刚体之后再从新设置重力才能将原重力覆盖
|
||||
if(noGravity){
|
||||
body.setGravity(new Vector3f(0,0,0));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
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,50 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
|
||||
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_6_Activity extends Activity {
|
||||
|
||||
MySurfaceView surfaceView;
|
||||
|
||||
@Override
|
||||
public 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);//Ç¿ÖÆºáÆÁ
|
||||
|
||||
surfaceView = new MySurfaceView(this);
|
||||
setContentView(surfaceView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
surfaceView.onPause();
|
||||
surfaceView.flag=false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
surfaceView.onResume();
|
||||
surfaceView.flag=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent e)
|
||||
{
|
||||
if(keyCode==4)
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
251
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/Stick.java
Normal file
251
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/Stick.java
Normal file
@@ -0,0 +1,251 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.vecmath.Quat4f;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
|
||||
import android.opengl.GLES20;
|
||||
import static com.bn.Sample17_6.Constant.*;
|
||||
//默认是横向X圆柱
|
||||
public class Stick {
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maNormalHandle; //顶点法向量属性引用id
|
||||
int maLightLocationHandle;//光源位置属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
static float[] mMMatrix = new float[16];//具体物体的移动旋转矩阵
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mNormalBuffer;//顶点纹理坐标数据缓冲
|
||||
|
||||
int vCount=0;
|
||||
float length=10f;//圆柱长度
|
||||
float circle_radius=2f;//圆截环半径
|
||||
float degreespan=18f; //圆截环每一份的度数大小
|
||||
|
||||
RigidBody body;
|
||||
MySurfaceView mv;
|
||||
|
||||
public Stick(MySurfaceView mv,float length,float circle_radius,float degreespan,float[] colorValue,
|
||||
RigidBody body)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
this.mv=mv;
|
||||
initVertexData( length, circle_radius, degreespan,colorValue);
|
||||
//初始化shader
|
||||
intShader(mv);
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
//初始化顶点坐标与着色数据的方法
|
||||
public void initVertexData(float length,float circle_radius,float degreespan,float[] colorValue)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
ArrayList<Float> val=new ArrayList<Float>();//顶点存放列表
|
||||
ArrayList<Float> ial=new ArrayList<Float>();//法向量存放列表
|
||||
|
||||
this.length = length; //半长
|
||||
this.circle_radius = circle_radius;
|
||||
this.degreespan = degreespan;
|
||||
|
||||
for(float circle_degree=360.0f;circle_degree>0.0f;circle_degree-=degreespan)//循环行
|
||||
{
|
||||
float x1 =(float)(-length);
|
||||
float y1=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
|
||||
float z1=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
|
||||
|
||||
float a1=0;
|
||||
float b1=y1;
|
||||
float c1=z1;
|
||||
float l1=getVectorLength(a1, b1, c1);//模长
|
||||
a1=a1/l1;//法向量规格化
|
||||
b1=b1/l1;
|
||||
c1=c1/l1;
|
||||
|
||||
float x2 =(float)(-length);
|
||||
float y2=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
|
||||
float z2=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
|
||||
|
||||
float a2=0;
|
||||
float b2=y2;
|
||||
float c2=z2;
|
||||
float l2=getVectorLength(a2, b2, c2);//模长
|
||||
a2=a2/l2;//法向量规格化
|
||||
b2=b2/l2;
|
||||
c2=c2/l2;
|
||||
|
||||
float x3 =(float)(length);
|
||||
float y3=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
|
||||
float z3=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
|
||||
|
||||
float a3=0;
|
||||
float b3=y3;
|
||||
float c3=z3;
|
||||
float l3=getVectorLength(a3, b3, c3);//模长
|
||||
a3=a3/l3;//法向量规格化
|
||||
b3=b3/l3;
|
||||
c3=c3/l3;
|
||||
|
||||
float x4 =(float)(length);
|
||||
float y4=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
|
||||
float z4=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
|
||||
|
||||
float a4=0;
|
||||
float b4=y4;
|
||||
float c4=z4;
|
||||
float l4=getVectorLength(a4, b4, c4);//模长
|
||||
a4=a4/l4;//法向量规格化
|
||||
b4=b4/l4;
|
||||
c4=c4/l4;
|
||||
|
||||
val.add(x1);val.add(y1);val.add(z1);//两个三角形,共6个顶点的坐标
|
||||
val.add(x2);val.add(y2);val.add(z2);
|
||||
val.add(x4);val.add(y4);val.add(z4);
|
||||
|
||||
val.add(x2);val.add(y2);val.add(z2);
|
||||
val.add(x3);val.add(y3);val.add(z3);
|
||||
val.add(x4);val.add(y4);val.add(z4);
|
||||
|
||||
ial.add(a1);ial.add(b1);ial.add(c1);//顶点对应的法向量
|
||||
ial.add(a2);ial.add(b2);ial.add(c2);
|
||||
ial.add(a4);ial.add(b4);ial.add(c4);
|
||||
|
||||
ial.add(a2);ial.add(b2);ial.add(c2);
|
||||
ial.add(a3);ial.add(b3);ial.add(c3);
|
||||
ial.add(a4);ial.add(b4);ial.add(c4);
|
||||
}
|
||||
|
||||
vCount=val.size()/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
|
||||
|
||||
//将alVertix中的坐标值转存到一个float数组中
|
||||
float vertices[]=new float[vCount*3];
|
||||
for(int i=0;i<val.size();i++)
|
||||
{
|
||||
vertices[i]=val.get(i);
|
||||
}
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//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(),否则有可能会出问题
|
||||
|
||||
//将alVertix中的坐标值转存到一个float数组中
|
||||
float normals[]=new float[ial.size()];
|
||||
for(int i=0;i<ial.size();i++)
|
||||
{
|
||||
normals[i]=ial.get(i);
|
||||
}
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length*4);
|
||||
nbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mNormalBuffer = nbb.asFloatBuffer();//转换为int型缓冲
|
||||
mNormalBuffer.put(normals);//向缓冲区中放入顶点坐标数据
|
||||
mNormalBuffer.position(0);//设置缓冲区起始位置
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void intShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_color.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_color.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中顶点法向量属性引用
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中光源位置引用
|
||||
maLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocation");
|
||||
//获取程序中摄像机位置引用
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
}
|
||||
|
||||
public void drawSelf(float angle,float x,float y,float z)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
MatrixState.pushMatrix();
|
||||
MySurfaceView.init=false;
|
||||
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);
|
||||
if(isNumber(fa[0]+"") && isNumber(fa[1]+"") && isNumber(fa[2]+"")){
|
||||
|
||||
MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
|
||||
}
|
||||
}
|
||||
|
||||
GLES20.glUseProgram(mProgram);
|
||||
MatrixState.rotate(angle, x, y, z);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.currMatrix, 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,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mNormalBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
|
||||
//法向量规格化,求模长度
|
||||
public float getVectorLength(float x,float y,float z)
|
||||
{
|
||||
float pingfang=x*x+y*y+z*z;
|
||||
float length=(float) Math.sqrt(pingfang);
|
||||
return length;
|
||||
}
|
||||
}
|
||||
139
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/TexRect.java
Normal file
139
第17章 游戏的心脏——物理引擎/Sample17_6/src/com/bn/Sample17_6/TexRect.java
Normal file
@@ -0,0 +1,139 @@
|
||||
package com.bn.Sample17_6;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//表示纹理矩形的类(含法线贴图)
|
||||
public class TexRect
|
||||
{
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
|
||||
int uTexHandle;//外观纹理属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mTextureBuffer;//顶点纹理坐标数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public TexRect(MySurfaceView mv,float size,float width,float height)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(size,width,height);
|
||||
//初始化shader
|
||||
intShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与纹理数据的方法
|
||||
public void initVertexData(float UNIT_SIZE,float width,float height)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=6;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
-width*UNIT_SIZE,height*UNIT_SIZE,0,
|
||||
-width*UNIT_SIZE,-height*UNIT_SIZE,0,
|
||||
width*UNIT_SIZE,height*UNIT_SIZE,0,
|
||||
|
||||
-width*UNIT_SIZE,-height*UNIT_SIZE,0,
|
||||
width*UNIT_SIZE,-height*UNIT_SIZE,0,
|
||||
width*UNIT_SIZE,height*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,0,0,1,1,0,
|
||||
0,1,1,1,1,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(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点经纬度属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
}
|
||||
|
||||
public void drawSelf(int texId)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
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