初次提交
7
第5章 投影及各种变换/Sample5_1/.classpath
Normal file
@@ -0,0 +1,7 @@
|
||||
<?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="output" path="bin"/>
|
||||
</classpath>
|
||||
33
第5章 投影及各种变换/Sample5_1/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample5_1</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>
|
||||
12
第5章 投影及各种变换/Sample5_1/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,12 @@
|
||||
#Wed Jan 05 21:56:15 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
15
第5章 投影及各种变换/Sample5_1/AndroidManifest.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" package="com.bn.Sample5_1">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name="com.bn.Sample5_1.Sample5_1_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:targetSdkVersion="8"></uses-sdk>
|
||||
</manifest>
|
||||
6
第5章 投影及各种变换/Sample5_1/assets/frag.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
precision mediump float;
|
||||
varying vec4 aaColor; //接收从顶点着色器过来的参数
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = aaColor;//给此片元颜色值
|
||||
}
|
||||
9
第5章 投影及各种变换/Sample5_1/assets/vertex.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec4 aColor; //顶点颜色
|
||||
varying vec4 aaColor; //用于传递给片元着色器的变量
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
aaColor = aColor;//将接收的颜色传递给片元着色器
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_1/bin/Sample5_1.apk
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/classes.dex
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/MatrixState.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/MySurfaceView.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/R$attr.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/R$drawable.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/R$string.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/R.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/ShaderUtil.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/com/bn/Sample5_1/SixPointedStar.class
Normal file
BIN
第5章 投影及各种变换/Sample5_1/bin/resources.ap_
Normal file
13
第5章 投影及各种变换/Sample5_1/default.properties
Normal file
@@ -0,0 +1,13 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Indicates whether an apk should be generated for each density.
|
||||
split.density=false
|
||||
# Project target.
|
||||
target=android-8
|
||||
19
第5章 投影及各种变换/Sample5_1/gen/com/bn/Sample5_1/R.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/* 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.Sample5_1;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int icon=0x7f020000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f030000;
|
||||
}
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_1/res/drawable-hdpi/icon.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第5章 投影及各种变换/Sample5_1/res/drawable-ldpi/icon.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第5章 投影及各种变换/Sample5_1/res/drawable-mdpi/icon.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
4
第5章 投影及各种变换/Sample5_1/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample5_1</string>
|
||||
</resources>
|
||||
64
第5章 投影及各种变换/Sample5_1/src/com/bn/Sample5_1/MatrixState.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.bn.Sample5_1;
|
||||
|
||||
import android.opengl.Matrix;
|
||||
|
||||
//存储系统矩阵状态的类
|
||||
public class MatrixState
|
||||
{
|
||||
private static float[] mProjMatrix = new float[16];//4x4矩阵 投影用
|
||||
private static float[] mVMatrix = new float[16];//摄像机位置朝向9参数矩阵
|
||||
private static float[] mMVPMatrix;//最后起作用的总变换矩阵
|
||||
|
||||
//设置摄像机
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
//设置正交投影参数
|
||||
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[] spec)
|
||||
{
|
||||
mMVPMatrix=new float[16];
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, spec, 0);
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
|
||||
return mMVPMatrix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.bn.Sample5_1;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.view.MotionEvent;
|
||||
import android.opengl.GLES20;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import android.content.Context;
|
||||
|
||||
class MySurfaceView extends GLSurfaceView
|
||||
{
|
||||
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
|
||||
private SceneRenderer mRenderer;//场景渲染器
|
||||
|
||||
private float mPreviousY;//上次的触控位置Y坐标
|
||||
private float mPreviousX;//上次的触控位置X坐标
|
||||
|
||||
public MySurfaceView(Context context) {
|
||||
super(context);
|
||||
this.setEGLContextClientVersion(2); //设置使用OPENGL ES2.0
|
||||
mRenderer = new SceneRenderer(); //创建场景渲染器
|
||||
setRenderer(mRenderer); //设置渲染器
|
||||
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
|
||||
}
|
||||
|
||||
//触摸事件回调方法
|
||||
@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位移
|
||||
for(SixPointedStar h:mRenderer.ha)
|
||||
{
|
||||
h.yAngle += dx * TOUCH_SCALE_FACTOR;//设置六角星数组中的各个六角星绕y轴旋转角度
|
||||
h.xAngle+= dy * TOUCH_SCALE_FACTOR;//设置六角星数组中的各个六角星绕x轴旋转角度
|
||||
}
|
||||
}
|
||||
mPreviousY = y;//记录触控笔位置
|
||||
mPreviousX = x;//记录触控笔位置
|
||||
return true;
|
||||
}
|
||||
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
SixPointedStar[] ha=new SixPointedStar[6];//六角星数组
|
||||
|
||||
public void onDrawFrame(GL10 gl)
|
||||
{
|
||||
//清除深度缓冲与颜色缓冲
|
||||
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
||||
//绘制六角星数组中的各个六角星
|
||||
for(SixPointedStar h:ha)
|
||||
{
|
||||
h.drawSelf();
|
||||
}
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算GLSurfaceView的宽高比
|
||||
float ratio= (float) width / height;
|
||||
//设置平行投影
|
||||
MatrixState.setProjectOrtho(-ratio, ratio, -1, 1, 1, 10);
|
||||
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(
|
||||
0, 0, 3f,
|
||||
0, 0, 0f,
|
||||
0f, 1.0f, 0.0f
|
||||
);
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//设置屏幕背景色RGBA
|
||||
GLES20.glClearColor(0.5f,0.5f,0.5f, 1.0f);
|
||||
//创建六角星数组中的各个对象
|
||||
for(int i=0;i<ha.length;i++)
|
||||
{
|
||||
ha[i]=new SixPointedStar(MySurfaceView.this,0.2f,0.5f,-0.3f*i);
|
||||
}
|
||||
//打开深度检测
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.bn.Sample5_1;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample5_1_Activity extends Activity {
|
||||
private MySurfaceView mGLSurfaceView;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
//设置为全屏
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
//设置为横屏模式
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
|
||||
//初始化GLSurfaceView
|
||||
mGLSurfaceView = new MySurfaceView(this);
|
||||
|
||||
//切换到主界面
|
||||
setContentView(mGLSurfaceView);
|
||||
|
||||
mGLSurfaceView.requestFocus();//获取焦点
|
||||
mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mGLSurfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mGLSurfaceView.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
126
第5章 投影及各种变换/Sample5_1/src/com/bn/Sample5_1/ShaderUtil.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package com.bn.Sample5_1;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
176
第5章 投影及各种变换/Sample5_1/src/com/bn/Sample5_1/SixPointedStar.java
Normal file
@@ -0,0 +1,176 @@
|
||||
package com.bn.Sample5_1;
|
||||
import static com.bn.Sample5_1.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.Matrix;
|
||||
|
||||
//六角星
|
||||
public class SixPointedStar
|
||||
{
|
||||
int mProgram;//自定义渲染管线着色器程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用
|
||||
int maPositionHandle; //顶点位置属性引用
|
||||
int maColorHandle; //顶点颜色属性引用
|
||||
String mVertexShader; //顶点着色器代码脚本
|
||||
String mFragmentShader; //片元着色器代码脚本
|
||||
static float[] mMMatrix = new float[16]; //具体物体的3D变换矩阵,包括旋转、平移、缩放
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mColorBuffer;//顶点着色数据缓冲
|
||||
int vCount=0;
|
||||
float yAngle=0;//绕y轴旋转的角度
|
||||
float xAngle=0;//绕z轴旋转的角度
|
||||
final float UNIT_SIZE=1;
|
||||
|
||||
public SixPointedStar(MySurfaceView mv,float r,float R,float z)
|
||||
{
|
||||
//调用初始化顶点数据的initVertexData方法
|
||||
initVertexData(R,r,z);
|
||||
//调用初始化着色器的intShader方法
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//自定义初始化顶点数据的initVertexData方法
|
||||
public void initVertexData(float R,float r,float z)
|
||||
{
|
||||
List<Float> flist=new ArrayList<Float>();
|
||||
float tempAngle=360/6;
|
||||
for(float angle=0;angle<360;angle+=tempAngle)
|
||||
{
|
||||
//第一个三角形
|
||||
//第一个中心点
|
||||
flist.add(0f);
|
||||
flist.add(0f);
|
||||
flist.add(z);
|
||||
//第二个点
|
||||
flist.add((float) (R*UNIT_SIZE*Math.cos(Math.toRadians(angle))));
|
||||
flist.add((float) (R*UNIT_SIZE*Math.sin(Math.toRadians(angle))));
|
||||
flist.add(z);
|
||||
//第三个点
|
||||
flist.add((float) (r*UNIT_SIZE*Math.cos(Math.toRadians(angle+tempAngle/2))));
|
||||
flist.add((float) (r*UNIT_SIZE*Math.sin(Math.toRadians(angle+tempAngle/2))));
|
||||
flist.add(z);
|
||||
|
||||
//第二个三角形
|
||||
//第一个中心点
|
||||
flist.add(0f);
|
||||
flist.add(0f);
|
||||
flist.add(z);
|
||||
//第二个点
|
||||
flist.add((float) (r*UNIT_SIZE*Math.cos(Math.toRadians(angle+tempAngle/2))));
|
||||
flist.add((float) (r*UNIT_SIZE*Math.sin(Math.toRadians(angle+tempAngle/2))));
|
||||
flist.add(z);
|
||||
//第三个点
|
||||
flist.add((float) (R*UNIT_SIZE*Math.cos(Math.toRadians(angle+tempAngle))));
|
||||
flist.add((float) (R*UNIT_SIZE*Math.sin(Math.toRadians(angle+tempAngle))));
|
||||
flist.add(z);
|
||||
}
|
||||
vCount=flist.size()/3;
|
||||
float[] vertexArray=new float[flist.size()];
|
||||
for(int i=0;i<vCount;i++)
|
||||
{
|
||||
vertexArray[i*3]=flist.get(i*3);
|
||||
vertexArray[i*3+1]=flist.get(i*3+1);
|
||||
vertexArray[i*3+2]=flist.get(i*3+2);
|
||||
}
|
||||
ByteBuffer vbb=ByteBuffer.allocateDirect(vertexArray.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder()); //设置字节顺序为本地操作系统顺序
|
||||
mVertexBuffer=vbb.asFloatBuffer();
|
||||
mVertexBuffer.put(vertexArray);
|
||||
mVertexBuffer.position(0);
|
||||
|
||||
|
||||
|
||||
|
||||
//顶点着色数据的初始化================begin============================
|
||||
float[] colorArray=new float[vCount*4];
|
||||
for(int i=0;i<vCount;i++)
|
||||
{
|
||||
if(i%3==0){//中心点为白色
|
||||
colorArray[i*4]=1;
|
||||
colorArray[i*4+1]=1;
|
||||
colorArray[i*4+2]=1;
|
||||
colorArray[i*4+3]=0;
|
||||
}
|
||||
else{//边上的点为淡蓝色
|
||||
colorArray[i*4]=0.45f;
|
||||
colorArray[i*4+1]=0.75f;
|
||||
colorArray[i*4+2]=0.75f;
|
||||
colorArray[i*4+3]=0;
|
||||
}
|
||||
}
|
||||
ByteBuffer cbb=ByteBuffer.allocateDirect(colorArray.length*4);
|
||||
cbb.order(ByteOrder.nativeOrder()); //设置字节顺序为本地操作系统顺序
|
||||
mColorBuffer=cbb.asFloatBuffer();
|
||||
mColorBuffer.put(colorArray);
|
||||
mColorBuffer.position(0);
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点着色数据的初始化================end============================
|
||||
|
||||
}
|
||||
|
||||
//自定义初始化着色器的intShader方法
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex.sh", mv.getResources());
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
mProgram = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点颜色属性引用id
|
||||
maColorHandle= GLES20.glGetAttribLocation(mProgram, "aColor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf()
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//初始化变换矩阵
|
||||
Matrix.setRotateM(mMMatrix,0,0,0,1,0);
|
||||
//设置沿Z轴正向位移1
|
||||
Matrix.translateM(mMMatrix,0,0,0,1);
|
||||
//设置绕y轴旋转
|
||||
Matrix.rotateM(mMMatrix,0,yAngle,0,1,0);
|
||||
//设置绕z轴旋转
|
||||
Matrix.rotateM(mMMatrix,0,xAngle,1,0,0);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(mMMatrix), 0);
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点着色数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maColorHandle,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
4*4,
|
||||
mColorBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maColorHandle);
|
||||
//绘制六角星
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
7
第5章 投影及各种变换/Sample5_10/.classpath
Normal file
@@ -0,0 +1,7 @@
|
||||
<?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="output" path="bin"/>
|
||||
</classpath>
|
||||
33
第5章 投影及各种变换/Sample5_10/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample5_3</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>
|
||||
12
第5章 投影及各种变换/Sample5_10/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,12 @@
|
||||
#Wed Jan 05 21:56:15 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
14
第5章 投影及各种变换/Sample5_10/AndroidManifest.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" package="com.bn.Sample5_10">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:label="@string/app_name" android:name=".Sample5_10_Activity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
<uses-sdk android:targetSdkVersion="8"></uses-sdk>
|
||||
</manifest>
|
||||
14
第5章 投影及各种变换/Sample5_10/assets/frag.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
precision mediump float;
|
||||
varying vec4 vColor; //接收从顶点着色器过来的参数
|
||||
varying vec3 vPosition;//接收从顶点着色器过来的顶点位置
|
||||
void main() {
|
||||
vec4 finalColor=vColor;
|
||||
//绕z轴转20度的矩阵
|
||||
mat4 mm=mat4(0.9396926,-0.34202012,0.0,0.0, 0.34202012,0.9396926,0.0,0.0,
|
||||
0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0);
|
||||
vec4 tPosition=mm*vec4(vPosition,1);//将顶点坐标绕z轴转20度
|
||||
if(mod(tPosition.x+100.0,0.4)>0.3) {//计算X方向在不在红光色带范围内
|
||||
finalColor=vec4(0.4,0.0,0.0,1.0)+finalColor;//若在给最终颜色加上淡红色
|
||||
}
|
||||
gl_FragColor = finalColor;//给此片元颜色值
|
||||
}
|
||||
11
第5章 投影及各种变换/Sample5_10/assets/vertex.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec4 aColor; //顶点颜色
|
||||
varying vec4 vColor; //用于传递给片元着色器的变量
|
||||
varying vec3 vPosition;//用于传递给片元着色器的顶点位置
|
||||
void main() {
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
vColor = aColor;//将接收的颜色传递给片元着色器
|
||||
vPosition=(uMMatrix * vec4(aPosition,1)).xyz;
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_10/bin/Sample5_10.apk
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/classes.dex
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/ColorRect.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/Constant.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/Cube.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/MatrixState.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/MySurfaceView.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R$attr.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R$drawable.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R$id.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R$layout.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R$string.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/R.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/com/bn/Sample5_10/ShaderUtil.class
Normal file
BIN
第5章 投影及各种变换/Sample5_10/bin/resources.ap_
Normal file
13
第5章 投影及各种变换/Sample5_10/default.properties
Normal file
@@ -0,0 +1,13 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Indicates whether an apk should be generated for each density.
|
||||
split.density=false
|
||||
# Project target.
|
||||
target=android-8
|
||||
26
第5章 投影及各种变换/Sample5_10/gen/com/bn/Sample5_10/R.java
Normal file
@@ -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.Sample5_10;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int icon=0x7f020000;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int ToggleButton01=0x7f050001;
|
||||
public static final int main_liner=0x7f050000;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int main=0x7f030000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f040000;
|
||||
}
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_10/res/drawable-hdpi/icon.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第5章 投影及各种变换/Sample5_10/res/drawable-ldpi/icon.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第5章 投影及各种变换/Sample5_10/res/drawable-mdpi/icon.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
16
第5章 投影及各种变换/Sample5_10/res/layout/main.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?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"
|
||||
android:id="@+id/main_liner"
|
||||
>
|
||||
<ToggleButton
|
||||
android:textOff="合理视角"
|
||||
android:textOn="不合理视角"
|
||||
android:checked="true"
|
||||
android:id="@+id/ToggleButton01"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</ToggleButton>
|
||||
</LinearLayout>
|
||||
4
第5章 投影及各种变换/Sample5_10/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample5_10</string>
|
||||
</resources>
|
||||
133
第5章 投影及各种变换/Sample5_10/src/com/bn/Sample5_10/ColorRect.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.bn.Sample5_10;
|
||||
import static com.bn.Sample5_10.Constant.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//颜色矩形
|
||||
public class ColorRect
|
||||
{
|
||||
int mProgram;//自定义渲染管线着色器程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵引用
|
||||
int maPositionHandle; //顶点位置属性引用
|
||||
int maColorHandle; //顶点颜色属性引用
|
||||
String mVertexShader;//顶点着色器代码脚本
|
||||
String mFragmentShader;//片元着色器代码脚本
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mColorBuffer;//顶点着色数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public ColorRect(MySurfaceView mv)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData();
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与着色数据的方法
|
||||
public void initVertexData()
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=6;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
0,0,0,
|
||||
UNIT_SIZE,UNIT_SIZE,0,
|
||||
-UNIT_SIZE,UNIT_SIZE,0,
|
||||
-UNIT_SIZE,-UNIT_SIZE,0,
|
||||
UNIT_SIZE,-UNIT_SIZE,0,
|
||||
UNIT_SIZE,UNIT_SIZE,0
|
||||
};
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
|
||||
//顶点着色数据的初始化================begin============================
|
||||
float colors[]=new float[]//顶点颜色值数组,每个顶点4个色彩值RGBA
|
||||
{
|
||||
1,1,1,0,
|
||||
0,0,1,0,
|
||||
0,0,1,0,
|
||||
0,0,1,0,
|
||||
0,0,1,0,
|
||||
0,0,1,0,
|
||||
};
|
||||
//创建顶点着色数据缓冲
|
||||
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
|
||||
cbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mColorBuffer = cbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mColorBuffer.put(colors);//向缓冲区中放入顶点着色数据
|
||||
mColorBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点着色数据的初始化================end============================
|
||||
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex.sh", mv.getResources());
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点颜色属性引用id
|
||||
maColorHandle= GLES20.glGetAttribLocation(mProgram, "aColor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf()
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点着色数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maColorHandle,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
4*4,
|
||||
mColorBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maColorHandle);
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.bn.Sample5_10;
|
||||
|
||||
public class Constant
|
||||
{
|
||||
//单位尺寸
|
||||
public static final float UNIT_SIZE=1f;
|
||||
//计算GLSurfaceView的宽高比
|
||||
public static float ratio;
|
||||
}
|
||||
72
第5章 投影及各种变换/Sample5_10/src/com/bn/Sample5_10/Cube.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.bn.Sample5_10;
|
||||
import static com.bn.Sample5_10.Constant.*;
|
||||
|
||||
//立方体
|
||||
public class Cube
|
||||
{
|
||||
//用于绘制各个面的颜色矩形
|
||||
ColorRect cr;
|
||||
|
||||
public Cube(MySurfaceView mv)
|
||||
{
|
||||
//创建用于绘制各个面的颜色矩形
|
||||
cr=new ColorRect(mv);
|
||||
}
|
||||
|
||||
public void drawSelf()
|
||||
{
|
||||
//总绘制思想:通过把一个颜色矩形旋转移位到立方体每个面的位置
|
||||
//绘制立方体的每个面
|
||||
|
||||
//保护现场
|
||||
MatrixState.pushMatrix();
|
||||
|
||||
//绘制前小面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, UNIT_SIZE);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制后小面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0, 0, -UNIT_SIZE);
|
||||
MatrixState.rotate(180, 0, 1, 0);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制上大面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0,UNIT_SIZE,0);
|
||||
MatrixState.rotate(-90, 1, 0, 0);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制下大面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(0,-UNIT_SIZE,0);
|
||||
MatrixState.rotate(90, 1, 0, 0);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制左大面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(UNIT_SIZE,0,0);
|
||||
MatrixState.rotate(-90, 1, 0, 0);
|
||||
MatrixState.rotate(90, 0, 1, 0);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制右大面
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(-UNIT_SIZE,0,0);
|
||||
MatrixState.rotate(90, 1, 0, 0);
|
||||
MatrixState.rotate(-90, 0, 1, 0);
|
||||
cr.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//恢复现场
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
125
第5章 投影及各种变换/Sample5_10/src/com/bn/Sample5_10/MatrixState.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.bn.Sample5_10;
|
||||
import java.nio.ByteBuffer;
|
||||
import android.opengl.Matrix;
|
||||
|
||||
//存储系统矩阵状态的类
|
||||
public class MatrixState
|
||||
{
|
||||
private static float[] mProjMatrix = new float[16];//4x4矩阵 投影用
|
||||
private static float[] mVMatrix = new float[16];//摄像机位置朝向9参数矩阵
|
||||
private static float[] currMatrix;//当前变换矩阵
|
||||
|
||||
//保护变换矩阵的栈
|
||||
static float[][] mStack=new float[10][16];
|
||||
static int stackTop=-1;
|
||||
|
||||
public static void setInitStack()//获取不变换初始矩阵
|
||||
{
|
||||
currMatrix=new float[16];
|
||||
Matrix.setRotateM(currMatrix, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
public static void pushMatrix()//保护变换矩阵
|
||||
{
|
||||
stackTop++;
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
mStack[stackTop][i]=currMatrix[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void popMatrix()//恢复变换矩阵
|
||||
{
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
currMatrix[i]=mStack[stackTop][i];
|
||||
}
|
||||
stackTop--;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
//设置摄像机
|
||||
static ByteBuffer llbb= ByteBuffer.allocateDirect(3*4);
|
||||
static float[] cameraLocation=new float[3];//摄像机位置
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
//设置透视投影参数
|
||||
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);
|
||||
}
|
||||
|
||||
//获取具体物体的总变换矩阵
|
||||
static float[] mMVPMatrix=new float[16];
|
||||
public static float[] getFinalMatrix()
|
||||
{
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, currMatrix, 0);
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
|
||||
return mMVPMatrix;
|
||||
}
|
||||
|
||||
//获取具体物体的变换矩阵
|
||||
public static float[] getMMatrix()
|
||||
{
|
||||
return currMatrix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.bn.Sample5_10;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.view.MotionEvent;
|
||||
import android.opengl.GLES20;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import android.content.Context;
|
||||
import static com.bn.Sample5_10.Constant.*;
|
||||
|
||||
class MySurfaceView extends GLSurfaceView
|
||||
{
|
||||
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
|
||||
private SceneRenderer mRenderer;//场景渲染器
|
||||
|
||||
private float mPreviousX;//上次的触控位置X坐标
|
||||
|
||||
float yAngle=0;//总场景绕y轴旋转的角度
|
||||
|
||||
public MySurfaceView(Context context) {
|
||||
super(context);
|
||||
this.setEGLContextClientVersion(2); //设置使用OPENGL ES2.0
|
||||
mRenderer = new SceneRenderer(); //创建场景渲染器
|
||||
setRenderer(mRenderer); //设置渲染器
|
||||
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
|
||||
}
|
||||
|
||||
//触摸事件回调方法
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
float x = e.getX();
|
||||
switch (e.getAction()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
float dx = x - mPreviousX;//计算触控笔X位移
|
||||
yAngle += dx * TOUCH_SCALE_FACTOR;//设置三角形对绕y轴旋转角度
|
||||
}
|
||||
mPreviousX=x;
|
||||
return true;
|
||||
}
|
||||
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
Cube cube;//立方体
|
||||
|
||||
public void onDrawFrame(GL10 gl)
|
||||
{
|
||||
//清除深度缓冲与颜色缓冲
|
||||
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
||||
//保护现场
|
||||
MatrixState.pushMatrix();
|
||||
//绕Y轴旋转
|
||||
MatrixState.rotate(yAngle, 0, 1, 0);
|
||||
|
||||
//绘制左侧立方体
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(-3, 0, 0);
|
||||
MatrixState.rotate(60, 0, 1, 0);
|
||||
cube.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//绘制右侧立方体
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.translate(3, 0, 0);
|
||||
MatrixState.rotate(-60, 0, 1, 0);
|
||||
cube.drawSelf();
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//恢复现场
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算GLSurfaceView的宽高比
|
||||
ratio = (float) width / height;
|
||||
|
||||
//视角不合适导致变形
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio*0.7f, ratio*0.7f, -0.7f, 0.7f, 1, 10);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(0,0.5f,4,0f,0f,0f,0f,1.0f,0.0f);
|
||||
|
||||
//初始化变换矩阵
|
||||
MatrixState.setInitStack();
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//设置屏幕背景色RGBA
|
||||
GLES20.glClearColor(0.5f,0.5f,0.5f, 1.0f);
|
||||
//创建立方体对象
|
||||
cube=new Cube(MySurfaceView.this);
|
||||
//打开深度检测
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
//打开背面剪裁
|
||||
GLES20.glEnable(GLES20.GL_CULL_FACE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.bn.Sample5_10;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ToggleButton;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
import static com.bn.Sample5_10.Constant.*;
|
||||
|
||||
public class Sample5_10_Activity extends Activity {
|
||||
private MySurfaceView mGLSurfaceView;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
//设置为全屏
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
//设置为横屏模式
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
||||
//切换到主界面
|
||||
setContentView(R.layout.main);
|
||||
//初始化GLSurfaceView
|
||||
mGLSurfaceView = new MySurfaceView(this);
|
||||
mGLSurfaceView.requestFocus();//获取焦点
|
||||
mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控
|
||||
//将自定义的GLSurfaceView添加到外层LinearLayout中
|
||||
LinearLayout ll=(LinearLayout)findViewById(R.id.main_liner);
|
||||
ll.addView(mGLSurfaceView);
|
||||
//控制是否打开背面剪裁的ToggleButton
|
||||
ToggleButton tb=(ToggleButton)this.findViewById(R.id.ToggleButton01);
|
||||
tb.setOnCheckedChangeListener(
|
||||
new OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
|
||||
{
|
||||
if(isChecked)
|
||||
{
|
||||
//视角不合适导致变形
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio*0.7f, ratio*0.7f, -0.7f, 0.7f, 1, 10);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(0,0.5f,4,0f,0f,0f,0f,1.0f,0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
//视角合适不变形
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 20, 100);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(0,8f,45,0f,0f,0f,0f,1.0f,0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mGLSurfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mGLSurfaceView.onPause();
|
||||
}
|
||||
}
|
||||
126
第5章 投影及各种变换/Sample5_10/src/com/bn/Sample5_10/ShaderUtil.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package com.bn.Sample5_10;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
7
第5章 投影及各种变换/Sample5_11/.classpath
Normal file
@@ -0,0 +1,7 @@
|
||||
<?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="output" path="bin"/>
|
||||
</classpath>
|
||||
33
第5章 投影及各种变换/Sample5_11/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample5_11</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>
|
||||
12
第5章 投影及各种变换/Sample5_11/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,12 @@
|
||||
#Wed Jan 05 21:56:15 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
14
第5章 投影及各种变换/Sample5_11/AndroidManifest.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" package="com.bn.Sample5_11">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:label="@string/app_name" android:name=".Sample5_11_Activity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
<uses-sdk android:targetSdkVersion="8"></uses-sdk>
|
||||
</manifest>
|
||||
6
第5章 投影及各种变换/Sample5_11/assets/frag.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
precision mediump float;
|
||||
varying vec4 vColor; //接收从顶点着色器过来的参数
|
||||
varying vec3 vPosition;//接收从顶点着色器过来的顶点位置
|
||||
void main() {
|
||||
gl_FragColor = vColor;//给此片元颜色值
|
||||
}
|
||||
8
第5章 投影及各种变换/Sample5_11/assets/vertex.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec4 aColor; //顶点颜色
|
||||
varying vec4 vColor; //用于传递给片元着色器的变量
|
||||
void main() {
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
vColor = aColor;//将接收的颜色传递给片元着色器
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_11/bin/Sample5_11.apk
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/classes.dex
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/Constant.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/MatrixState.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/MySurfaceView.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R$attr.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R$drawable.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R$id.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R$layout.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R$string.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/R.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/ShaderUtil.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/com/bn/Sample5_11/TrianglePair.class
Normal file
BIN
第5章 投影及各种变换/Sample5_11/bin/resources.ap_
Normal file
13
第5章 投影及各种变换/Sample5_11/default.properties
Normal file
@@ -0,0 +1,13 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Indicates whether an apk should be generated for each density.
|
||||
split.density=false
|
||||
# Project target.
|
||||
target=android-8
|
||||
33
第5章 投影及各种变换/Sample5_11/gen/com/bn/Sample5_11/R.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/* 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.Sample5_11;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int icon=0x7f020000;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int LinearLayout01=0x7f050001;
|
||||
public static final int RadioButton01=0x7f050004;
|
||||
public static final int RadioButton02=0x7f050005;
|
||||
public static final int RadioButton03=0x7f050007;
|
||||
public static final int RadioButton04=0x7f050008;
|
||||
public static final int RadioGroup01=0x7f050003;
|
||||
public static final int RadioGroup02=0x7f050006;
|
||||
public static final int main_liner=0x7f050000;
|
||||
public static final int textView1=0x7f050002;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int main=0x7f030000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f040000;
|
||||
}
|
||||
}
|
||||
BIN
第5章 投影及各种变换/Sample5_11/res/drawable-hdpi/icon.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第5章 投影及各种变换/Sample5_11/res/drawable-ldpi/icon.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第5章 投影及各种变换/Sample5_11/res/drawable-mdpi/icon.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
81
第5章 投影及各种变换/Sample5_11/res/layout/main.xml
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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"
|
||||
android:id="@+id/main_liner"
|
||||
>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/LinearLayout01"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
<TextView
|
||||
android:text="卷绕方式"
|
||||
android:id="@+id/textView1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="fill_parent"
|
||||
android:textColor="#FFFFFF"
|
||||
android:textSize="16dip"
|
||||
android:gravity="center_vertical"
|
||||
>
|
||||
</TextView>
|
||||
<RadioGroup
|
||||
android:id="@+id/RadioGroup01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
>
|
||||
<RadioButton
|
||||
android:text="逆时针"
|
||||
android:id="@+id/RadioButton01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
</RadioButton>
|
||||
<RadioButton
|
||||
android:text="顺时针"
|
||||
android:id="@+id/RadioButton02"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
>
|
||||
</RadioButton>
|
||||
</RadioGroup>
|
||||
<TextView
|
||||
android:text="背面剪裁"
|
||||
android:id="@+id/textView1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="fill_parent"
|
||||
android:textColor="#FFFFFF"
|
||||
android:textSize="16dip"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingLeft="25dip"
|
||||
>
|
||||
</TextView>
|
||||
<RadioGroup
|
||||
android:id="@+id/RadioGroup02"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
>
|
||||
<RadioButton
|
||||
android:text="开启"
|
||||
android:id="@+id/RadioButton03"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
</RadioButton>
|
||||
<RadioButton
|
||||
android:text="关闭"
|
||||
android:id="@+id/RadioButton04"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
>
|
||||
</RadioButton>
|
||||
</RadioGroup>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
4
第5章 投影及各种变换/Sample5_11/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample5_11</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.bn.Sample5_11;
|
||||
|
||||
public class Constant
|
||||
{
|
||||
//单位尺寸
|
||||
public static final float UNIT_SIZE=0.25f;
|
||||
//计算GLSurfaceView的宽高比
|
||||
public static float ratio;
|
||||
}
|
||||