初次提交
This commit is contained in:
7
第7章 纹理映射/Sample7_3/.classpath
Normal file
7
第7章 纹理映射/Sample7_3/.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
第7章 纹理映射/Sample7_3/.project
Normal file
33
第7章 纹理映射/Sample7_3/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample7_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
第7章 纹理映射/Sample7_3/.settings/org.eclipse.jdt.core.prefs
Normal file
12
第7章 纹理映射/Sample7_3/.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
第7章 纹理映射/Sample7_3/AndroidManifest.xml
Normal file
15
第7章 纹理映射/Sample7_3/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.Sample7_3">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name=".MyActivity"
|
||||
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>
|
||||
8
第7章 纹理映射/Sample7_3/assets/frag.sh
Normal file
8
第7章 纹理映射/Sample7_3/assets/frag.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord; //接收从顶点着色器过来的参数
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元从纹理中采样出颜色值
|
||||
gl_FragColor = texture2D(sTexture, vTextureCoord);
|
||||
}
|
||||
9
第7章 纹理映射/Sample7_3/assets/vertex.sh
Normal file
9
第7章 纹理映射/Sample7_3/assets/vertex.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
varying vec2 vTextureCoord; //用于传递给片元着色器的变量
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
vTextureCoord = aTexCoor;//将接收的纹理坐标传递给片元着色器
|
||||
}
|
||||
BIN
第7章 纹理映射/Sample7_3/bin/Sample7_3.apk
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/Sample7_3.apk
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/classes.dex
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/classes.dex
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MatrixState.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MatrixState.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$1.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$1.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$2.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$2.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$3.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$3.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$4.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity$4.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MyActivity.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MySurfaceView.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/MySurfaceView.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$attr.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$attr.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$drawable.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$drawable.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$id.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$id.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$layout.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$layout.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$string.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R$string.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/R.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/ShaderUtil.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/ShaderUtil.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/TextureRect.class
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/com/bn/Sample7_3/TextureRect.class
Normal file
Binary file not shown.
BIN
第7章 纹理映射/Sample7_3/bin/resources.ap_
Normal file
BIN
第7章 纹理映射/Sample7_3/bin/resources.ap_
Normal file
Binary file not shown.
13
第7章 纹理映射/Sample7_3/default.properties
Normal file
13
第7章 纹理映射/Sample7_3/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
第7章 纹理映射/Sample7_3/gen/com/bn/Sample7_3/R.java
Normal file
33
第7章 纹理映射/Sample7_3/gen/com/bn/Sample7_3/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.Sample7_3;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int bw256=0x7f020000;
|
||||
public static final int bw32=0x7f020001;
|
||||
public static final int icon=0x7f020002;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int Radi01=0x7f050003;
|
||||
public static final int Radi02=0x7f050004;
|
||||
public static final int Radi03=0x7f050005;
|
||||
public static final int Radi04=0x7f050006;
|
||||
public static final int RadioGroup01=0x7f050001;
|
||||
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
第7章 纹理映射/Sample7_3/res/drawable-hdpi/bw256.png
Normal file
BIN
第7章 纹理映射/Sample7_3/res/drawable-hdpi/bw256.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 794 B |
BIN
第7章 纹理映射/Sample7_3/res/drawable-hdpi/bw32.png
Normal file
BIN
第7章 纹理映射/Sample7_3/res/drawable-hdpi/bw32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 225 B |
BIN
第7章 纹理映射/Sample7_3/res/drawable-hdpi/icon.png
Normal file
BIN
第7章 纹理映射/Sample7_3/res/drawable-hdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第7章 纹理映射/Sample7_3/res/drawable-ldpi/icon.png
Normal file
BIN
第7章 纹理映射/Sample7_3/res/drawable-ldpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第7章 纹理映射/Sample7_3/res/drawable-mdpi/icon.png
Normal file
BIN
第7章 纹理映射/Sample7_3/res/drawable-mdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
45
第7章 纹理映射/Sample7_3/res/layout/main.xml
Normal file
45
第7章 纹理映射/Sample7_3/res/layout/main.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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"
|
||||
>
|
||||
<RadioGroup
|
||||
android:id="@+id/RadioGroup01"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
<TextView android:text="采样方式:"
|
||||
android:id="@+id/textView1"
|
||||
android:textSize="16dip"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</TextView>
|
||||
<RadioButton
|
||||
android:text="NN"
|
||||
android:id="@+id/Radi01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true">
|
||||
</RadioButton>
|
||||
<RadioButton
|
||||
android:text="LL"
|
||||
android:id="@+id/Radi02"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</RadioButton>
|
||||
<RadioButton
|
||||
android:text="NL"
|
||||
android:id="@+id/Radi03"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</RadioButton>
|
||||
<RadioButton
|
||||
android:text="LN"
|
||||
android:id="@+id/Radi04"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</RadioButton>
|
||||
</RadioGroup>
|
||||
</LinearLayout>
|
||||
4
第7章 纹理映射/Sample7_3/res/values/strings.xml
Normal file
4
第7章 纹理映射/Sample7_3/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample7_3</string>
|
||||
</resources>
|
||||
108
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MatrixState.java
Normal file
108
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MatrixState.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package com.bn.Sample7_3;
|
||||
|
||||
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;//最后起作用的总变换矩阵
|
||||
|
||||
static float[] mMMatrix ;//具体物体的移动旋转矩阵
|
||||
|
||||
//保护变换矩阵的栈
|
||||
static float[][] mStack=new float[10][16];
|
||||
static int stackTop=-1;
|
||||
|
||||
public static void setInitStack()//获取不变换初始矩阵
|
||||
{
|
||||
mMMatrix = new float[16];
|
||||
Matrix.setRotateM(mMMatrix, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
public static void pushMatrix()//保护变换矩阵
|
||||
{
|
||||
stackTop++;
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
mStack[stackTop][i]=mMMatrix[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void popMatrix()//恢复变换矩阵
|
||||
{
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
mMMatrix[i]=mStack[stackTop][i];
|
||||
}
|
||||
stackTop--;
|
||||
}
|
||||
|
||||
public static void transtate(float x,float y,float z)//设置沿xyz轴移动
|
||||
{
|
||||
Matrix.translateM(mMMatrix, 0, x, y, z);
|
||||
}
|
||||
|
||||
public static void rotate(float angle,float x,float y,float z)//设置绕xyz轴转动
|
||||
{
|
||||
Matrix.rotateM(mMMatrix,0,angle,x,y,z);
|
||||
}
|
||||
public static void scale(float x,float y,float z)
|
||||
{
|
||||
Matrix.scaleM(mMMatrix,0, x, y, z);
|
||||
}
|
||||
|
||||
//设置摄像机
|
||||
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 setProject
|
||||
(
|
||||
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 float[] getFinalMatrix()
|
||||
{
|
||||
mMVPMatrix=new float[16];
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
|
||||
return mMVPMatrix;
|
||||
}
|
||||
}
|
||||
114
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MyActivity.java
Normal file
114
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MyActivity.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package com.bn.Sample7_3;
|
||||
|
||||
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.RadioButton;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
|
||||
public class MyActivity 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);
|
||||
//切换到主界面
|
||||
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);
|
||||
|
||||
//为RadioButton添加监听器
|
||||
RadioButton rb=(RadioButton)findViewById(R.id.Radi01);
|
||||
rb.setOnCheckedChangeListener(
|
||||
new OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
|
||||
{
|
||||
if(isChecked)
|
||||
{
|
||||
mGLSurfaceView.currenttexId32 = mGLSurfaceView.texId[0];
|
||||
mGLSurfaceView.currenttexId256 = mGLSurfaceView.texId[4];
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
rb=(RadioButton)findViewById(R.id.Radi02);
|
||||
rb.setOnCheckedChangeListener(
|
||||
new OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
|
||||
{
|
||||
if(isChecked)
|
||||
{
|
||||
mGLSurfaceView.currenttexId32 = mGLSurfaceView.texId[1];
|
||||
mGLSurfaceView.currenttexId256 = mGLSurfaceView.texId[5];
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
rb=(RadioButton)findViewById(R.id.Radi03);
|
||||
rb.setOnCheckedChangeListener(
|
||||
new OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
|
||||
{
|
||||
if(isChecked)
|
||||
{
|
||||
mGLSurfaceView.currenttexId32 = mGLSurfaceView.texId[2];
|
||||
mGLSurfaceView.currenttexId256 = mGLSurfaceView.texId[6];
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
rb=(RadioButton)findViewById(R.id.Radi04);
|
||||
rb.setOnCheckedChangeListener(
|
||||
new OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
|
||||
{
|
||||
if(isChecked)
|
||||
{
|
||||
mGLSurfaceView.currenttexId32 = mGLSurfaceView.texId[3];
|
||||
mGLSurfaceView.currenttexId256 = mGLSurfaceView.texId[7];
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mGLSurfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mGLSurfaceView.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
132
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MySurfaceView.java
Normal file
132
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/MySurfaceView.java
Normal file
@@ -0,0 +1,132 @@
|
||||
package com.bn.Sample7_3;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.opengl.GLUtils;
|
||||
import android.opengl.GLES20;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
class MySurfaceView extends GLSurfaceView
|
||||
{
|
||||
private SceneRenderer mRenderer;//场景渲染器
|
||||
|
||||
int currenttexId32; //当前纹理的id
|
||||
int currenttexId256; //当前纹理的id
|
||||
int[] texId = new int[8]; //系统分配的纹理id
|
||||
|
||||
public MySurfaceView(Context context) {
|
||||
super(context);
|
||||
this.setEGLContextClientVersion(2); //设置使用OPENGL ES2.0
|
||||
mRenderer = new SceneRenderer(); //创建场景渲染器
|
||||
setRenderer(mRenderer); //设置渲染器
|
||||
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
|
||||
}
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
TextureRect texRect; //纹理矩形对象的引用
|
||||
public void onDrawFrame(GL10 gl)
|
||||
{
|
||||
//清除深度缓冲与颜色缓冲
|
||||
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
||||
//绘制小纹理矩形
|
||||
MatrixState.pushMatrix(); //进栈
|
||||
MatrixState.transtate(0, 1.3f, 1); //平移
|
||||
MatrixState.rotate(-20, 0, 0, 1); //旋转
|
||||
MatrixState.scale(0.3f, 0.3f, 0.3f); //缩放
|
||||
texRect.drawSelf(currenttexId256); //绘制小四边形
|
||||
MatrixState.popMatrix();//出栈
|
||||
|
||||
//绘制大纹理矩形
|
||||
MatrixState.pushMatrix(); //进栈
|
||||
MatrixState.transtate(0, -0.6f, 1); //平移
|
||||
MatrixState.rotate(-20, 0, 0, 1); //旋转
|
||||
texRect.drawSelf(currenttexId32); //绘制大四边形
|
||||
MatrixState.popMatrix();//出栈
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
//设置视窗大小及位置
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
//计算GLSurfaceView的宽高比
|
||||
float ratio = (float) width / height;
|
||||
//调用此方法计算产生透视投影矩阵
|
||||
MatrixState.setProject(-ratio, ratio, -1, 1, 1, 10);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(0,0,3,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);
|
||||
//创建纹理矩形对象
|
||||
texRect = new TextureRect(MySurfaceView.this);
|
||||
//打开深度检测
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
//初始化32*32像素的四个纹理
|
||||
texId[0] = initTexture(R.drawable.bw32,GLES20.GL_NEAREST,GLES20.GL_NEAREST);
|
||||
texId[1] = initTexture(R.drawable.bw32,GLES20.GL_LINEAR,GLES20.GL_LINEAR);
|
||||
texId[2] = initTexture(R.drawable.bw32,GLES20.GL_NEAREST,GLES20.GL_LINEAR);
|
||||
texId[3] = initTexture(R.drawable.bw32,GLES20.GL_LINEAR,GLES20.GL_NEAREST);
|
||||
//初始化256*256像素的四个纹理
|
||||
texId[4] = initTexture(R.drawable.bw256,GLES20.GL_NEAREST,GLES20.GL_NEAREST);
|
||||
texId[5] = initTexture(R.drawable.bw256,GLES20.GL_LINEAR,GLES20.GL_LINEAR);
|
||||
texId[6] = initTexture(R.drawable.bw256,GLES20.GL_NEAREST,GLES20.GL_LINEAR);
|
||||
texId[7] = initTexture(R.drawable.bw256,GLES20.GL_LINEAR,GLES20.GL_NEAREST);
|
||||
|
||||
currenttexId32 = texId[0]; //设置默认纹理
|
||||
currenttexId256 = texId[4]; //设置默认纹理
|
||||
//关闭背面剪裁
|
||||
GLES20.glDisable(GLES20.GL_CULL_FACE);
|
||||
}
|
||||
}
|
||||
|
||||
public int initTexture(int drawableId,float sample1,float sample2){
|
||||
//生成纹理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,sample1);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,sample2);
|
||||
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();
|
||||
}
|
||||
}
|
||||
//实际加载纹理
|
||||
GLUtils.texImage2D
|
||||
(
|
||||
GLES20.GL_TEXTURE_2D, //纹理类型,在OpenGL ES中必须为GL10.GL_TEXTURE_2D
|
||||
0, //纹理的层次,0表示基本图像层,可以理解为直接贴图
|
||||
bitmapTmp, //纹理图像
|
||||
0 //纹理边框尺寸
|
||||
);
|
||||
bitmapTmp.recycle(); //纹理加载成功后释放图片
|
||||
|
||||
return textureId;
|
||||
}
|
||||
}
|
||||
126
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/ShaderUtil.java
Normal file
126
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/ShaderUtil.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package com.bn.Sample7_3;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
137
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/TextureRect.java
Normal file
137
第7章 纹理映射/Sample7_3/src/com/bn/Sample7_3/TextureRect.java
Normal file
@@ -0,0 +1,137 @@
|
||||
package com.bn.Sample7_3;
|
||||
import static com.bn.Sample7_3.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//纹理矩形
|
||||
public class TextureRect
|
||||
{
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mTexCoorBuffer;//顶点纹理坐标数据缓冲
|
||||
int vCount=0;
|
||||
float xAngle=0;//绕x轴旋转的角度
|
||||
float yAngle=0;//绕y轴旋转的角度
|
||||
float zAngle=0;//绕z轴旋转的角度
|
||||
|
||||
public TextureRect(MySurfaceView mv)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData();
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与着色数据的方法
|
||||
public void initVertexData()
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=6;
|
||||
final float UNIT_SIZE=0.15f;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
//较大的纹理矩形
|
||||
-6*UNIT_SIZE,6*UNIT_SIZE,0,
|
||||
-6*UNIT_SIZE,-6*UNIT_SIZE,0,
|
||||
6*UNIT_SIZE,-6*UNIT_SIZE,0,
|
||||
|
||||
6*UNIT_SIZE,-6*UNIT_SIZE,0,
|
||||
6*UNIT_SIZE,6*UNIT_SIZE,0,
|
||||
-6*UNIT_SIZE,6*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 texCoor[]=new float[]//顶点颜色值数组,每个顶点4个色彩值RGBA
|
||||
{
|
||||
0,0, 0,1, 1,1,
|
||||
1,1, 1,0, 0,0
|
||||
};
|
||||
//创建顶点纹理坐标数据缓冲
|
||||
ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length*4);
|
||||
cbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTexCoorBuffer = cbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mTexCoorBuffer.put(texCoor);//向缓冲区中放入顶点着色数据
|
||||
mTexCoorBuffer.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 = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点纹理坐标属性引用id
|
||||
maTexCoorHandle= GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
}
|
||||
|
||||
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,
|
||||
mTexCoorBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
|
||||
//绘制纹理矩形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user