初次提交
This commit is contained in:
@@ -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>
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample17_5</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>
|
||||
@@ -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_5"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name="com.bn.Sample17_5.Sample17_5_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>
|
||||
@@ -0,0 +1,8 @@
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元颜色值
|
||||
gl_FragColor = texture2D(sTexture, vTextureCoord);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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
|
||||
@@ -0,0 +1,26 @@
|
||||
/* 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_5;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int floor=0x7f020000;
|
||||
public static final int icon=0x7f020001;
|
||||
public static final int wood_bin0=0x7f020002;
|
||||
public static final int wood_bin1=0x7f020003;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#
|
||||
# An unexpected error has been detected by Java Runtime Environment:
|
||||
#
|
||||
# Internal Error (434C41535326494C453041525345520E4350500B65), pid=6676, tid=5304
|
||||
#
|
||||
# Java VM: Java HotSpot(TM) Client VM (1.6.0_02-b06 mixed mode)
|
||||
# If you would like to submit a bug report, please visit:
|
||||
# http://java.sun.com/webapps/bugreport/crash.jsp
|
||||
#
|
||||
|
||||
--------------- T H R E A D ---------------
|
||||
|
||||
Current thread (0x003f6c00): JavaThread "Unknown thread" [_thread_in_vm, id=5304]
|
||||
|
||||
Stack: [0x008d0000,0x00920000)
|
||||
[error occurred during error reporting, step 110, id 0xc00000fd]
|
||||
|
||||
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
|
||||
C [javaw.exe+0xc4fc]
|
||||
C [javaw.exe+0x81b5]
|
||||
C [kernel32.dll+0xb729]
|
||||
|
||||
|
||||
--------------- P R O C E S S ---------------
|
||||
|
||||
Java Threads: ( => current thread )
|
||||
|
||||
Other Threads:
|
||||
|
||||
VM state:not at safepoint (not fully initilizated)
|
||||
|
||||
VM Mutex/Monitor currently owned by a thread: None
|
||||
|
||||
Heap
|
||||
def new generation total 960K, used 0K [0x029d0000, 0x02ad0000, 0x02eb0000)
|
||||
eden space 896K, 0% used [0x029d0000, 0x029d0000, 0x02ab0000)
|
||||
from space 64K, 0% used [0x02ab0000, 0x02ab0000, 0x02ac0000)
|
||||
to space 64K, 0% used [0x02ac0000, 0x02ac0000, 0x02ad0000)
|
||||
tenured generation total 4096K, used 0K [0x02eb0000, 0x032b0000, 0x069d0000)
|
||||
the space 4096K, 0% used [0x02eb0000, 0x02eb0000, 0x02eb0200, 0x032b0000)
|
||||
compacting perm gen total 12288K, used 99K [0x069d0000, 0x075d0000, 0x0a9d0000)
|
||||
the space 12288K, 0% used [0x069d0000, 0x069e8d40, 0x069e8e00, 0x075d0000)
|
||||
No shared spaces configured.
|
||||
|
||||
Dynamic libraries:
|
||||
0x00400000 - 0x00423000 D:\java\jdk1.6.0_02\bin\javaw.exe
|
||||
0x7c920000 - 0x7c9b6000 C:\WINDOWS\system32\ntdll.dll
|
||||
0x7c800000 - 0x7c91e000 C:\WINDOWS\system32\kernel32.dll
|
||||
0x77da0000 - 0x77e49000 C:\WINDOWS\system32\ADVAPI32.dll
|
||||
0x77e50000 - 0x77ee3000 C:\WINDOWS\system32\RPCRT4.dll
|
||||
0x77fc0000 - 0x77fd1000 C:\WINDOWS\system32\Secur32.dll
|
||||
0x77d10000 - 0x77da0000 C:\WINDOWS\system32\USER32.dll
|
||||
0x77ef0000 - 0x77f39000 C:\WINDOWS\system32\GDI32.dll
|
||||
0x76300000 - 0x7631d000 C:\WINDOWS\system32\IMM32.DLL
|
||||
0x62c20000 - 0x62c29000 C:\WINDOWS\system32\LPK.DLL
|
||||
0x73fa0000 - 0x7400b000 C:\WINDOWS\system32\USP10.dll
|
||||
0x7c340000 - 0x7c396000 D:\java\jdk1.6.0_02\jre\bin\msvcr71.dll
|
||||
0x6d870000 - 0x6dab9000 D:\java\jdk1.6.0_02\jre\bin\client\jvm.dll
|
||||
0x76b10000 - 0x76b3a000 C:\WINDOWS\system32\WINMM.dll
|
||||
0x6d3c0000 - 0x6d3c8000 D:\java\jdk1.6.0_02\jre\bin\hpi.dll
|
||||
0x76bc0000 - 0x76bcb000 C:\WINDOWS\system32\PSAPI.DLL
|
||||
0x6d820000 - 0x6d82c000 D:\java\jdk1.6.0_02\jre\bin\verify.dll
|
||||
0x6d460000 - 0x6d47f000 D:\java\jdk1.6.0_02\jre\bin\java.dll
|
||||
0x6d860000 - 0x6d86f000 D:\java\jdk1.6.0_02\jre\bin\zip.dll
|
||||
|
||||
VM Arguments:
|
||||
jvm_args: -Dfile.encoding=GBK -Xbootclasspath:D:\android-sdk-windows\platforms\android-8\android.jar
|
||||
java_command: com.JointResearch_1.HelloWorld
|
||||
Launcher Type: SUN_STANDARD
|
||||
|
||||
Environment Variables:
|
||||
JAVA_HOME=D:\java\jdk1.6.0_02
|
||||
CLASSPATH=.;D:\android-sdk-windows\tools;D:\java\jdk1.6.0_02\BIN;C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar;C:\oracle\product\10.2.0\db_1\jdbc\lib\nls_charset12.jar;
|
||||
|
||||
PATH=D:/java/jdk1.6.0_02/bin/../jre/bin/client;D:/java/jdk1.6.0_02/bin/../jre/bin;D:/java/jdk1.6.0_02/bin/../jre/lib/i386;D:\android-sdk-windows_2_2Update\android-sdk-windows\tools;D:\java\jdk1.6.0_02\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ASUS Security Center\ASUS Security Protect Manager\bin;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;E:\powerBuilder\win32;E:\Shared\win32;E:\Shared\Sybase Central 4.1;E:\powerBuilder\sybase\PowerDynamo\win32;E:\powerBuilder\Shared\PowerBuilder;E:\powerBuilder\Shared\Web Targets;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;d:\CTEX\UserData\miktex\bin;d:\CTEX\MiKTeX\miktex\bin;d:\CTEX\CTeX\ctex\bin;d:\CTEX\CTeX\cct\bin;d:\CTEX\CTeX\ty\bin;d:\CTEX\Ghostscript\gs9.00\bin;d:\CTEX\GSview\gsview;d:\CTEX\WinEdt;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\backburner 2\;C:\Program Files\Autodesk\Backburner\;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Cesoft\Excel Server2010;C:\Program Files\Cesoft\Excel Server2010;F:\cpp\Tools\WinNT;F:\cpp\MSDev98\Bin;F:\cpp\Tools;F:\VC98\bin
|
||||
USERNAME=WADE
|
||||
OS=Windows_NT
|
||||
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel
|
||||
|
||||
|
||||
|
||||
--------------- S Y S T E M ---------------
|
||||
|
||||
OS: Windows XP Build 2600 Service Pack 3
|
||||
|
||||
CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 15 stepping 13, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3
|
||||
|
||||
Memory: 4k page, physical 2096304k(851556k free), swap 4038148k(2667328k free)
|
||||
|
||||
vm_info: Java HotSpot(TM) Client VM (1.6.0_02-b06) for windows-x86, built on Jul 12 2007 01:16:14 by "java_re" with unknown MS VC++:1310
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
@@ -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>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="hello">Hello World, MainActivity!</string>
|
||||
<string name="app_name">Sample17_5</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.bn.Sample17_5;
|
||||
public class Constant {
|
||||
|
||||
public static final float halfX=0.2f;//长方体的半高度
|
||||
public static final float halfY=6f;//长方体的半宽度
|
||||
public static final float halfZ=4f;//长方体的半长度
|
||||
public static final float groundHeight=-2f;//长方体的半长度
|
||||
|
||||
public static final float LEG_MASS=1f;//腿的质量
|
||||
|
||||
public static float cx=15;//摄像机位置XYZ坐标
|
||||
public static float cy=0;
|
||||
public static float cz=0;
|
||||
public static float yAngle=0;
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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 Cuboid {
|
||||
|
||||
TexRect frontBackRect;//前后的长方形
|
||||
TexRect topBottomRect;//上下的长方形
|
||||
TexRect leftRightRect;//左右的长方形
|
||||
float halfX;
|
||||
float halfY;
|
||||
float halfZ;
|
||||
|
||||
RigidBody body;
|
||||
|
||||
public Cuboid
|
||||
(
|
||||
DiscreteDynamicsWorld dynamicsWorld,CollisionShape boxShape,
|
||||
float mass,
|
||||
float cx,float cy,float cz,
|
||||
MySurfaceView mv,
|
||||
float halfX, //x
|
||||
float halfY, //y
|
||||
float halfZ //z
|
||||
){
|
||||
this.halfX=halfX;
|
||||
this.halfY = halfY;
|
||||
this.halfZ = halfZ;
|
||||
frontBackRect = new TexRect(mv,1,halfX,halfY);
|
||||
topBottomRect = new TexRect(mv,1,halfX,halfZ);
|
||||
leftRightRect = new TexRect(mv,1,halfZ,halfY);
|
||||
boolean isDynamic = (mass!=0);
|
||||
Vector3f localInertia = new Vector3f(0,0,0);
|
||||
if(isDynamic){
|
||||
boxShape.calculateLocalInertia(mass, localInertia);
|
||||
}
|
||||
Transform transform = new Transform();
|
||||
transform.setIdentity();
|
||||
transform.origin.set(new Vector3f(cx,cy,cz));
|
||||
DefaultMotionState myMotionState = new DefaultMotionState(transform);
|
||||
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo
|
||||
(
|
||||
mass, myMotionState, boxShape, localInertia
|
||||
);
|
||||
body = new RigidBody(rbInfo);
|
||||
body.setRestitution(0.6f);
|
||||
body.setFriction(0.8f);
|
||||
dynamicsWorld.addRigidBody(body);
|
||||
}
|
||||
|
||||
public void drawSelf(int texId){
|
||||
|
||||
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);
|
||||
MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
|
||||
}
|
||||
|
||||
//绘制上面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, halfY, 0);
|
||||
MatrixState.rotate(-90, 1, 0, 0);
|
||||
topBottomRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制下面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, -halfY, 0);
|
||||
MatrixState.rotate(90, 1, 0, 0);
|
||||
topBottomRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制左面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(-halfX, 0, 0);
|
||||
MatrixState.rotate(-90, 0, 1, 0);
|
||||
leftRightRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制右面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(halfX, 0, 0);
|
||||
MatrixState.rotate(90, 0, 1, 0);
|
||||
leftRightRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制前面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, halfZ);
|
||||
frontBackRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制后面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, -halfZ);
|
||||
MatrixState.rotate(180, 0, 1, 0);
|
||||
frontBackRect.drawSelf(texId);
|
||||
MatrixState.popMatrix();
|
||||
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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_5.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.dynamics.DiscreteDynamicsWorld;
|
||||
import com.bulletphysics.dynamics.RigidBody;
|
||||
import com.bulletphysics.dynamics.constraintsolver.HingeConstraint;
|
||||
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
|
||||
import com.bulletphysics.linearmath.Clock;
|
||||
import com.bulletphysics.linearmath.MatrixUtil;
|
||||
import com.bulletphysics.linearmath.Transform;
|
||||
import static com.bn.Sample17_5.Constant.*;
|
||||
|
||||
public class MySurfaceView extends GLSurfaceView {
|
||||
|
||||
DiscreteDynamicsWorld dynamicsWorld;//物理世界
|
||||
CollisionShape boxShape;//盒子碰撞形状
|
||||
HingeConstraint constraint;//铰链关节
|
||||
private float time;//时间量
|
||||
private float cyclePeriod; //周期
|
||||
private float muscleStrength;//关节力
|
||||
Clock clock = new Clock();//时钟
|
||||
private float mPreviousY;//上次的触控位置Y坐标
|
||||
private float mPreviousX;//上次的触控位置X坐标
|
||||
boolean flag=true;//循环控制标志
|
||||
MyRenderer renderer; //渲染器
|
||||
|
||||
public MySurfaceView(Context context) {
|
||||
super(context);
|
||||
this.setEGLContextClientVersion(2);
|
||||
renderer = new MyRenderer();//创建渲染器
|
||||
this.setRenderer(renderer);//设置渲染器
|
||||
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式
|
||||
initWorld();
|
||||
time = 0.0f;
|
||||
cyclePeriod = 3000.0f; //设置周期
|
||||
muscleStrength = 5f;//设置关节力
|
||||
}
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
float y = e.getY();
|
||||
float x = e.getX();
|
||||
switch (e.getAction()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
float dy = y - mPreviousY;//计算触控笔Y位移
|
||||
float dx = x - mPreviousX;//计算触控笔X位移
|
||||
if(Math.abs(dx)>10)
|
||||
{
|
||||
yAngle += dx /100;//设置纹理圆球绕y轴旋转的角度
|
||||
cx=(float) (Math.cos(yAngle)*15);
|
||||
cz=(float) (Math.sin(yAngle)*15);
|
||||
}
|
||||
if(Math.abs(dy)>10)
|
||||
{
|
||||
cy=cy+dy/10;
|
||||
if(cy>15)
|
||||
{
|
||||
cy=15;
|
||||
}
|
||||
else if(cy<-15)
|
||||
{
|
||||
cy=-15;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
mPreviousY = y;//记录触控笔位置
|
||||
mPreviousX = x;//记录触控笔位置
|
||||
return true;
|
||||
}
|
||||
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(halfX,halfY,halfZ));
|
||||
}
|
||||
private class MyRenderer implements GLSurfaceView.Renderer{
|
||||
Cuboid door1; //门1
|
||||
Cuboid door2; //门2
|
||||
int activeTexId;//门的纹理id
|
||||
@Override
|
||||
public void onDrawFrame(GL10 gl) {
|
||||
|
||||
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);//清空缓存
|
||||
float ms = getDeltaTimeMicroseconds();//获取两帧的时间间隔
|
||||
time+=ms;//时间递增
|
||||
float preAngle = constraint.getHingeAngle();//获取关节上一时刻的角度
|
||||
//求得公式1中需要的x
|
||||
float x=(((int)(time / 1000) % (int)(cyclePeriod)) / cyclePeriod)*BulletGlobals.SIMD_2_PI;
|
||||
//根据公式1求出y
|
||||
float y= 0.5f * (1.0f + (float)Math.sin(x));
|
||||
//计算出当前旋转角度
|
||||
float currAngle = constraint.getLowerLimit() + y * (constraint.getUpperLimit() - constraint.getLowerLimit());
|
||||
float angleDelta = currAngle - preAngle; //计算角度差
|
||||
float desiredAngularVel = 1000000.f * angleDelta/ms;//计算此帧的角速度
|
||||
constraint.enableAngularMotor(true, desiredAngularVel, muscleStrength);//设置马达
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(cx,cy,cz,0,0,0,0f,1.0f,0.0f);
|
||||
MatrixState.pushMatrix();//保护现场
|
||||
door1.drawSelf(activeTexId);//绘制门1
|
||||
door2.drawSelf(activeTexId);//绘制门2
|
||||
MatrixState.popMatrix();//恢复现场
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算GLSurfaceView的宽高比
|
||||
float ratio= (float) width / height;
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 1.5f, 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//设置屏幕背景色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);
|
||||
door1 = new Cuboid(dynamicsWorld, boxShape, 0, 0, 0, 4, MySurfaceView.this,halfX,halfY,halfZ);
|
||||
door2 = new Cuboid(dynamicsWorld, boxShape, 1, 0, 0, -4, MySurfaceView.this,halfX,halfY,halfZ);
|
||||
addHingeConstraint(door1.body,door2.body);
|
||||
new Thread()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
while(flag)
|
||||
{
|
||||
try
|
||||
{
|
||||
//模拟
|
||||
dynamicsWorld.stepSimulation(1f/60.f, 5);
|
||||
Thread.sleep(20);
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void addHingeConstraint(RigidBody rbA,RigidBody rbB){//添加铰链约束
|
||||
Transform transA = new Transform();//创建变换
|
||||
transA.setIdentity();//初始化变换
|
||||
MatrixUtil.setEulerZYX(transA.basis, BulletGlobals.SIMD_HALF_PI,0,0 );//约束旋转
|
||||
transA.origin.set(new Vector3f(0,0,-halfZ));//约束的位置
|
||||
Transform transB = new Transform();//创建变换
|
||||
transB.setIdentity();//初始化变换
|
||||
MatrixUtil.setEulerZYX(transB.basis, BulletGlobals.SIMD_HALF_PI,0,0 );//约束旋转
|
||||
transB.origin.set(new Vector3f(0,0,halfZ));
|
||||
constraint = new HingeConstraint(rbA,rbB,transA,transB);//创建约束
|
||||
constraint.setLimit(-BulletGlobals.SIMD_HALF_PI/2, BulletGlobals.SIMD_HALF_PI/2);//设置约束转动的范围
|
||||
dynamicsWorld.addConstraint(constraint, 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 float getDeltaTimeMicroseconds() {
|
||||
float dt = clock.getTimeMicroseconds();//获取从时钟创建或上次时间重置到现在的毫秒数
|
||||
clock.reset();//时钟重置
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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,39 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample17_5_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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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,140 @@
|
||||
package com.bn.Sample17_5;
|
||||
|
||||
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