初次提交
This commit is contained in:
297
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/LoadUtil.java
Normal file
297
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/LoadUtil.java
Normal file
@@ -0,0 +1,297 @@
|
||||
package com.bn.Sample15_7;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import android.content.res.Resources;
|
||||
import android.util.Log;
|
||||
|
||||
public class LoadUtil
|
||||
{
|
||||
//求两个向量的叉积
|
||||
public static float[] getCrossProduct(float x1,float y1,float z1,float x2,float y2,float z2)
|
||||
{
|
||||
//求出两个矢量叉积矢量在XYZ轴的分量ABC
|
||||
float A=y1*z2-y2*z1;
|
||||
float B=z1*x2-z2*x1;
|
||||
float C=x1*y2-x2*y1;
|
||||
|
||||
return new float[]{A,B,C};
|
||||
}
|
||||
|
||||
//向量规格化
|
||||
public static float[] vectorNormal(float[] vector)
|
||||
{
|
||||
//求向量的模
|
||||
float module=(float)Math.sqrt(vector[0]*vector[0]+vector[1]*vector[1]+vector[2]*vector[2]);
|
||||
return new float[]{vector[0]/module,vector[1]/module,vector[2]/module};
|
||||
}
|
||||
|
||||
//从obj文件中加载携带顶点信息的物体,并自动计算每个顶点的平均法向量
|
||||
public static LoadedObjectVertexNormalAverage loadFromFileVertexOnlyAverage(String fname, Resources r,MySurfaceView mv)
|
||||
{
|
||||
//加载后物体的引用
|
||||
LoadedObjectVertexNormalAverage lo=null;
|
||||
//原始顶点坐标列表--直接从obj文件中加载
|
||||
ArrayList<Float> alv=new ArrayList<Float>();
|
||||
//顶点组装面索引列表--根据面的信息从文件中加载
|
||||
ArrayList<Integer> alFaceIndex=new ArrayList<Integer>();
|
||||
//结果顶点坐标列表--按面组织好
|
||||
ArrayList<Float> alvResult=new ArrayList<Float>();
|
||||
//平均前各个索引对应的点的法向量集合Map
|
||||
//此HashMap的key为点的索引, value为点所在的各个面的法向量的集合
|
||||
HashMap<Integer,HashSet<Normal>> hmn=new HashMap<Integer,HashSet<Normal>>();
|
||||
|
||||
try
|
||||
{
|
||||
InputStream in=r.getAssets().open(fname);
|
||||
InputStreamReader isr=new InputStreamReader(in);
|
||||
BufferedReader br=new BufferedReader(isr);
|
||||
String temps=null;
|
||||
|
||||
//扫面文件,根据行类型的不同执行不同的处理逻辑
|
||||
while((temps=br.readLine())!=null)
|
||||
{
|
||||
//用空格分割行中的各个组成部分
|
||||
String[] tempsa=temps.split("[ ]+");
|
||||
if(tempsa[0].trim().equals("v"))
|
||||
{//此行为顶点坐标
|
||||
//若为顶点坐标行则提取出此顶点的XYZ坐标添加到原始顶点坐标列表中
|
||||
alv.add(Float.parseFloat(tempsa[1]));
|
||||
alv.add(Float.parseFloat(tempsa[2]));
|
||||
alv.add(Float.parseFloat(tempsa[3]));
|
||||
}
|
||||
else if(tempsa[0].trim().equals("f"))
|
||||
{//此行为三角形面
|
||||
/*
|
||||
*若为三角形面行则根据 组成面的顶点的索引从原始顶点坐标列表中
|
||||
*提取相应的顶点坐标值添加到结果顶点坐标列表中,同时根据三个
|
||||
*顶点的坐标计算出此面的法向量并添加到平均前各个索引对应的点
|
||||
*的法向量集合组成的Map中
|
||||
*/
|
||||
|
||||
int[] index=new int[3];//三个顶点索引值的数组
|
||||
|
||||
//计算第0个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[0]=Integer.parseInt(tempsa[1].split("/")[0])-1;
|
||||
float x0=alv.get(3*index[0]);
|
||||
float y0=alv.get(3*index[0]+1);
|
||||
float z0=alv.get(3*index[0]+2);
|
||||
alvResult.add(x0);
|
||||
alvResult.add(y0);
|
||||
alvResult.add(z0);
|
||||
|
||||
//计算第1个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[1]=Integer.parseInt(tempsa[2].split("/")[0])-1;
|
||||
float x1=alv.get(3*index[1]);
|
||||
float y1=alv.get(3*index[1]+1);
|
||||
float z1=alv.get(3*index[1]+2);
|
||||
alvResult.add(x1);
|
||||
alvResult.add(y1);
|
||||
alvResult.add(z1);
|
||||
|
||||
//计算第2个顶点的索引,并获取此顶点的XYZ三个坐标
|
||||
index[2]=Integer.parseInt(tempsa[3].split("/")[0])-1;
|
||||
float x2=alv.get(3*index[2]);
|
||||
float y2=alv.get(3*index[2]+1);
|
||||
float z2=alv.get(3*index[2]+2);
|
||||
alvResult.add(x2);
|
||||
alvResult.add(y2);
|
||||
alvResult.add(z2);
|
||||
|
||||
//记录此面的顶点索引
|
||||
alFaceIndex.add(index[0]);
|
||||
alFaceIndex.add(index[1]);
|
||||
alFaceIndex.add(index[2]);
|
||||
|
||||
//通过三角形面两个边向量0-1,0-2求叉积得到此面的法向量
|
||||
//求0号点到1号点的向量
|
||||
float vxa=x1-x0;
|
||||
float vya=y1-y0;
|
||||
float vza=z1-z0;
|
||||
//求0号点到2号点的向量
|
||||
float vxb=x2-x0;
|
||||
float vyb=y2-y0;
|
||||
float vzb=z2-z0;
|
||||
//通过求两个向量的叉积计算法向量
|
||||
float[] vNormal=vectorNormal(getCrossProduct
|
||||
(
|
||||
vxa,vya,vza,vxb,vyb,vzb
|
||||
));
|
||||
|
||||
for(int tempInxex:index)
|
||||
{//记录每个索引点的法向量到平均前各个索引对应的点的法向量集合组成的Map中
|
||||
//获取当前索引对应点的法向量集合
|
||||
HashSet<Normal> hsn=hmn.get(tempInxex);
|
||||
if(hsn==null)
|
||||
{//若集合不存在则创建
|
||||
hsn=new HashSet<Normal>();
|
||||
}
|
||||
//将此点的法向量添加到集合中
|
||||
//由于Normal类重写了equals方法,因此同样的法向量不会重复出现在此点
|
||||
//对应的法向量集合中
|
||||
hsn.add(new Normal(vNormal[0],vNormal[1],vNormal[2]));
|
||||
//将集合放进HsahMap中
|
||||
hmn.put(tempInxex, hsn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//生成顶点数组
|
||||
int size=alvResult.size();
|
||||
float[] vXYZ=new float[size];
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
vXYZ[i]=alvResult.get(i);
|
||||
}
|
||||
|
||||
//生成法向量数组
|
||||
float[] nXYZ=new float[alFaceIndex.size()*3];
|
||||
int c=0;
|
||||
for(Integer i:alFaceIndex)
|
||||
{
|
||||
//根据当前点的索引从Map中取出一个法向量的集合
|
||||
HashSet<Normal> hsn=hmn.get(i);
|
||||
//求出平均法向量
|
||||
float[] tn=Normal.getAverage(hsn);
|
||||
//将计算出的平均法向量存放到法向量数组中
|
||||
nXYZ[c++]=tn[0];
|
||||
nXYZ[c++]=tn[1];
|
||||
nXYZ[c++]=tn[2];
|
||||
}
|
||||
//创建3D物体对象
|
||||
lo=new LoadedObjectVertexNormalAverage(mv,vXYZ,nXYZ);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Log.d("load error", "load error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
//从obj文件中加载仅携带顶点信息的物体
|
||||
//首先加载顶点信息,再根据顶点组成三角形面的情况自动计算出每个面的法向量
|
||||
//然后将这个面的法向量分配给这个面上的顶点
|
||||
public static LoadedObjectVertexNormalFace loadFromFileVertexOnlyFace(String fname, Resources r,MySurfaceView mv)
|
||||
{
|
||||
//加载后3D对象的引用
|
||||
LoadedObjectVertexNormalFace lo=null;
|
||||
//原始顶点坐标列表--按顺序从obj文件中加载的
|
||||
ArrayList<Float> alv=new ArrayList<Float>();
|
||||
//结果顶点坐标列表 --根据组成面的情况组织好的
|
||||
ArrayList<Float> alvResult=new ArrayList<Float>();
|
||||
//结果法向量列表--根据组成面的情况组织好的
|
||||
ArrayList<Float> alnResult=new ArrayList<Float>();
|
||||
|
||||
try
|
||||
{
|
||||
InputStream in=r.getAssets().open(fname);
|
||||
InputStreamReader isr=new InputStreamReader(in);
|
||||
BufferedReader br=new BufferedReader(isr);
|
||||
String temps=null;
|
||||
|
||||
//循环不断从文件中读取行,根据行类型的不同执行
|
||||
//不同的处理逻辑
|
||||
while((temps=br.readLine())!=null)
|
||||
{
|
||||
String[] tempsa=temps.split("[ ]+");
|
||||
if(tempsa[0].trim().equals("v"))
|
||||
{//此行为顶点坐标
|
||||
//若为顶点坐标行则提取出此顶点的XYZ坐标添加到原始顶点坐标列表中
|
||||
alv.add(Float.parseFloat(tempsa[1]));
|
||||
alv.add(Float.parseFloat(tempsa[2]));
|
||||
alv.add(Float.parseFloat(tempsa[3]));
|
||||
}
|
||||
else if(tempsa[0].trim().equals("f"))
|
||||
{//此行为三角形面
|
||||
/*
|
||||
*若为三角形面行则根据 组成面的顶点的索引从原始顶点坐标列表中
|
||||
*提取相应的顶点坐标值添加到结果顶点坐标列表中,同时根据三个
|
||||
*顶点的坐标计算出法向量并添加到结果法向量列表中
|
||||
*/
|
||||
|
||||
//提取三角形第一个顶点的坐标
|
||||
int index=Integer.parseInt(tempsa[1].split("/")[0])-1;
|
||||
float x0=alv.get(3*index);
|
||||
float y0=alv.get(3*index+1);
|
||||
float z0=alv.get(3*index+2);
|
||||
alvResult.add(x0);
|
||||
alvResult.add(y0);
|
||||
alvResult.add(z0);
|
||||
|
||||
//提取三角形第二个顶点的坐标
|
||||
index=Integer.parseInt(tempsa[2].split("/")[0])-1;
|
||||
float x1=alv.get(3*index);
|
||||
float y1=alv.get(3*index+1);
|
||||
float z1=alv.get(3*index+2);
|
||||
alvResult.add(x1);
|
||||
alvResult.add(y1);
|
||||
alvResult.add(z1);
|
||||
|
||||
//提取三角形第三个顶点的坐标
|
||||
index=Integer.parseInt(tempsa[3].split("/")[0])-1;
|
||||
float x2=alv.get(3*index);
|
||||
float y2=alv.get(3*index+1);
|
||||
float z2=alv.get(3*index+2);
|
||||
alvResult.add(x2);
|
||||
alvResult.add(y2);
|
||||
alvResult.add(z2);
|
||||
|
||||
//通过三角形面两个边向量0-1,0-2求叉积得到此面的法向量
|
||||
//求0号点到1号点的向量
|
||||
float vxa=x1-x0;
|
||||
float vya=y1-y0;
|
||||
float vza=z1-z0;
|
||||
//求0号点到2号点的向量
|
||||
float vxb=x2-x0;
|
||||
float vyb=y2-y0;
|
||||
float vzb=z2-z0;
|
||||
|
||||
//通过球两个向量的叉积计算法向量
|
||||
float[] vNormal=vectorNormal
|
||||
(
|
||||
getCrossProduct
|
||||
(
|
||||
vxa,vya,vza,vxb,vyb,vzb
|
||||
)
|
||||
);
|
||||
//将计算出的法向量添加到结果法向量列表中
|
||||
for(int i=0;i<3;i++)
|
||||
{
|
||||
alnResult.add(vNormal[0]);
|
||||
alnResult.add(vNormal[1]);
|
||||
alnResult.add(vNormal[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//obj文件读取结束后生成顶点数组及生成法向量数组
|
||||
//生成顶点数组
|
||||
int size=alvResult.size();
|
||||
float[] vXYZ=new float[size];
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
vXYZ[i]=alvResult.get(i);
|
||||
}
|
||||
|
||||
//生成法向量数组
|
||||
size=alnResult.size();
|
||||
float[] nXYZ=new float[size];
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
nXYZ[i]=alnResult.get(i);
|
||||
}
|
||||
|
||||
//创建3D对象
|
||||
lo=new LoadedObjectVertexNormalFace(mv,vXYZ,nXYZ);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Log.d("load error", "load error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.bn.Sample15_7;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//加载后的物体——携带顶点信息,自动计算面平均法向量
|
||||
public class LoadedObjectVertexNormalAverage
|
||||
{
|
||||
int mProgram;//自定义渲染管线着色器程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maPositionHandle; //顶点位置属性引用
|
||||
int maNormalHandle; //顶点法向量属性引用
|
||||
int maLightLocationHandle;//光源位置属性引用
|
||||
int maCameraHandle; //摄像机位置属性引用
|
||||
int muIsShadow;//是否绘制阴影属性引用
|
||||
int muProjCameraMatrixHandle;//投影、摄像机组合矩阵引用
|
||||
|
||||
String mVertexShader;//顶点着色器代码脚本
|
||||
String mFragmentShader;//片元着色器代码脚本
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mNormalBuffer;//顶点法向量数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public LoadedObjectVertexNormalAverage(MySurfaceView mv,float[] vertices,float[] normals)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(vertices,normals);
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与着色数据的方法
|
||||
public void initVertexData(float[] vertices,float[] normals)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=vertices.length/3;
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
|
||||
//顶点法向量数据的初始化================begin============================
|
||||
ByteBuffer cbb = ByteBuffer.allocateDirect(normals.length*4);
|
||||
cbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mNormalBuffer = cbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mNormalBuffer.put(normals);//向缓冲区中放入顶点法向量数据
|
||||
mNormalBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点着色数据的初始化================end============================
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_shadow.sh", mv.getResources());
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_shadow.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点颜色属性引用
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中总变换矩阵引用
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中光源位置引用
|
||||
maLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocation");
|
||||
//获取程序中摄像机位置引用
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
//获取程序中是否绘制阴影属性引用
|
||||
muIsShadow=GLES20.glGetUniformLocation(mProgram, "isShadow");
|
||||
//获取程序中投影、摄像机组合矩阵引用
|
||||
muProjCameraMatrixHandle=GLES20.glGetUniformLocation(mProgram, "uMProjCameraMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf(int isShadow)
|
||||
{
|
||||
//制定使用某套着色器程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将光源位置传入着色器程序
|
||||
GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB);
|
||||
//将摄像机位置传入着色器程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
//将是否绘制阴影属性传入着色器程序
|
||||
GLES20.glUniform1i(muIsShadow, isShadow);
|
||||
//将投影、摄像机组合矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muProjCameraMatrixHandle, 1, false, MatrixState.getViewProjMatrix(), 0);
|
||||
//将顶点位置数据传入渲染管线
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//将顶点法向量数据传入渲染管线
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maNormalHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mNormalBuffer
|
||||
);
|
||||
//启用顶点位置、法向量数据
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绘制加载的物体
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package com.bn.Sample15_7;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//加载后的物体——携带顶点信息,自动计算面法向量
|
||||
public class LoadedObjectVertexNormalFace
|
||||
{
|
||||
int mProgram;//自定义渲染管线着色器程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maPositionHandle; //顶点位置属性引用
|
||||
int maNormalHandle; //顶点法向量属性引用
|
||||
int maLightLocationHandle;//光源位置属性引用
|
||||
int maCameraHandle; //摄像机位置属性引用
|
||||
int muIsShadow;//是否绘制阴影属性引用
|
||||
int muProjCameraMatrixHandle;//投影矩阵引用
|
||||
|
||||
String mVertexShader;//顶点着色器代码脚本
|
||||
String mFragmentShader;//片元着色器代码脚本
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mNormalBuffer;//顶点法向量数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public LoadedObjectVertexNormalFace(MySurfaceView mv,float[] vertices,float[] normals)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(vertices,normals);
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与着色数据的方法
|
||||
public void initVertexData(float[] vertices,float[] normals)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
vCount=vertices.length/3;
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点坐标数据的初始化================end============================
|
||||
|
||||
//顶点法向量数据的初始化================begin============================
|
||||
ByteBuffer cbb = ByteBuffer.allocateDirect(normals.length*4);
|
||||
cbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mNormalBuffer = cbb.asFloatBuffer();//转换为Float型缓冲
|
||||
mNormalBuffer.put(normals);//向缓冲区中放入顶点法向量数据
|
||||
mNormalBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
//顶点着色数据的初始化================end============================
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_shadow.sh", mv.getResources());
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_shadow.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点颜色属性引用
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中总变换矩阵引用
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取位置、旋转变换矩阵引用
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
//获取程序中光源位置引用
|
||||
maLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocation");
|
||||
//获取程序中摄像机位置引用
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
//获取程序中是否绘制阴影属性引用
|
||||
muIsShadow=GLES20.glGetUniformLocation(mProgram, "isShadow");
|
||||
//获取程序中投影、摄像机组合矩阵引用
|
||||
muProjCameraMatrixHandle=GLES20.glGetUniformLocation(mProgram, "uMProjCameraMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf(int isShadow)
|
||||
{
|
||||
//制定使用某套着色器程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将光源位置传入着色器程序
|
||||
GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB);
|
||||
//将摄像机位置传入着色器程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
//将是否绘制阴影属性传入着色器程序
|
||||
GLES20.glUniform1i(muIsShadow, isShadow);
|
||||
//将投影、摄像机组合矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muProjCameraMatrixHandle, 1, false, MatrixState.getViewProjMatrix(), 0);
|
||||
|
||||
//将顶点位置数据传入渲染管线
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//将顶点法向量数据传入渲染管线
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maNormalHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mNormalBuffer
|
||||
);
|
||||
//启用顶点位置、法向量数据
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绘制被加载的物体
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
}
|
||||
156
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/MatrixState.java
Normal file
156
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/MatrixState.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package com.bn.Sample15_7;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.*;
|
||||
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;//当前变换矩阵
|
||||
public static float[] lightLocation=new float[]{0,0,0};//定位光光源位置
|
||||
public static FloatBuffer cameraFB;
|
||||
public static FloatBuffer lightPositionFB;
|
||||
|
||||
public static Stack<float[]> mStack=new Stack<float[]>();//保护变换矩阵的栈
|
||||
|
||||
public static void setInitStack()//获取不变换初始矩阵
|
||||
{
|
||||
currMatrix=new float[16];
|
||||
Matrix.setRotateM(currMatrix, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
public static void pushMatrix()//保护变换矩阵
|
||||
{
|
||||
mStack.push(currMatrix.clone());
|
||||
}
|
||||
|
||||
public static void popMatrix()//恢复变换矩阵
|
||||
{
|
||||
currMatrix=mStack.pop();
|
||||
}
|
||||
|
||||
public static void translate(float x,float y,float z)//设置沿xyz轴移动
|
||||
{
|
||||
Matrix.translateM(currMatrix, 0, x, y, z);
|
||||
}
|
||||
|
||||
public static void rotate(float angle,float x,float y,float z)//设置绕xyz轴移动
|
||||
{
|
||||
Matrix.rotateM(currMatrix,0,angle,x,y,z);
|
||||
}
|
||||
|
||||
public static void scale(float x,float y,float z)
|
||||
{
|
||||
Matrix.scaleM(currMatrix,0, x, y, z);
|
||||
}
|
||||
|
||||
|
||||
//设置摄像机
|
||||
public static void 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 float[] getMMatrix()
|
||||
{
|
||||
return currMatrix;
|
||||
}
|
||||
|
||||
//设置灯光位置的方法
|
||||
public static void setLightLocation(float x,float y,float z)
|
||||
{
|
||||
lightLocation[0]=x;
|
||||
lightLocation[1]=y;
|
||||
lightLocation[2]=z;
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(3*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
lightPositionFB=llbb.asFloatBuffer();
|
||||
lightPositionFB.put(lightLocation);
|
||||
lightPositionFB.position(0);
|
||||
}
|
||||
|
||||
public static float[] getViewProjMatrix()
|
||||
{
|
||||
float[] mMVPMatrix=new float[16];
|
||||
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
|
||||
return mMVPMatrix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package com.bn.Sample15_7;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.opengl.GLES20;
|
||||
import android.view.MotionEvent;
|
||||
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坐标
|
||||
//关于摄像机的变量
|
||||
float cx=0;//摄像机x位置
|
||||
float cy=0;//摄像机y位置
|
||||
float cz=60;//摄像机z位置
|
||||
|
||||
float tx=0;//目标点x位置
|
||||
float ty=0;//目标点y位置
|
||||
float tz=0;//目标点z位置
|
||||
public float currSightDis=60;//摄像机和目标的距离
|
||||
float angdegElevation=30;//仰角
|
||||
public float angdegAzimuth=180;//方位角
|
||||
|
||||
//关于灯光的变量
|
||||
float lx=0;//x位置
|
||||
float ly=0;//y位置
|
||||
float lz=0;//z位置
|
||||
float lightDis=100;
|
||||
float lightElevation=40;//灯光仰角
|
||||
public float lightAzimuth=180;//灯光的方位角
|
||||
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位移
|
||||
angdegAzimuth += dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度
|
||||
angdegElevation += dy * TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
|
||||
//将仰角限制在5~90度范围内
|
||||
angdegElevation = Math.max(angdegElevation, 5);
|
||||
angdegElevation = Math.min(angdegElevation, 90);
|
||||
//设置摄像机的位置
|
||||
setCameraPostion();
|
||||
}
|
||||
mPreviousY = y;//记录触控笔位置
|
||||
mPreviousX = x;//记录触控笔位置
|
||||
return true;
|
||||
}
|
||||
// 设置摄像机位置的方法
|
||||
public void setCameraPostion() {
|
||||
//计算摄像机的位置
|
||||
double angradElevation = Math.toRadians(angdegElevation);// 仰角(弧度)
|
||||
double angradAzimuth = Math.toRadians(angdegAzimuth);// 方位角
|
||||
cx = (float) (tx - currSightDis * Math.cos(angradElevation) * Math.sin(angradAzimuth));
|
||||
cy = (float) (ty + currSightDis * Math.sin(angradElevation));
|
||||
cz = (float) (tz - currSightDis * Math.cos(angradElevation) * Math.cos(angradAzimuth));
|
||||
}
|
||||
// 位置灯光位置的方法
|
||||
public void setLightPostion() {
|
||||
//计算灯光的位置
|
||||
double angradElevation = Math.toRadians(lightElevation);// 仰角(弧度)
|
||||
double angradAzimuth = Math.toRadians(lightAzimuth);// 方位角
|
||||
lx = (float) (- lightDis * Math.cos(angradElevation) * Math.sin(angradAzimuth));
|
||||
ly = (float) (+ lightDis * Math.sin(angradElevation));
|
||||
lz = (float) (- lightDis * Math.cos(angradElevation) * Math.cos(angradAzimuth));
|
||||
}
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
//从指定的obj文件中加载对象
|
||||
LoadedObjectVertexNormalFace pm;
|
||||
LoadedObjectVertexNormalFace cft;
|
||||
LoadedObjectVertexNormalAverage qt;
|
||||
LoadedObjectVertexNormalAverage yh;
|
||||
LoadedObjectVertexNormalAverage ch;
|
||||
|
||||
public void onDrawFrame(GL10 gl)
|
||||
{
|
||||
//清除深度缓冲与颜色缓冲
|
||||
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
||||
//设置camera位置
|
||||
MatrixState.setCamera
|
||||
(
|
||||
cx, //人眼位置的X
|
||||
cy, //人眼位置的Y
|
||||
cz, //人眼位置的Z
|
||||
tx, //人眼球看的点X
|
||||
ty, //人眼球看的点Y
|
||||
tz, //人眼球看的点Z
|
||||
0, //up位置
|
||||
1,
|
||||
0
|
||||
);
|
||||
//初始化光源位置
|
||||
MatrixState.setLightLocation(lx, ly, lz);
|
||||
//若加载的物体部位空则绘制物体
|
||||
pm.drawSelf(0);//平面
|
||||
drawObject(1);
|
||||
|
||||
drawObject(0);
|
||||
|
||||
}
|
||||
|
||||
public void drawObject(int situ)
|
||||
{
|
||||
//绘制长方体
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.scale(1.5f, 1.5f, 1.5f);
|
||||
MatrixState.translate(-10f, 0f, 0);
|
||||
cft.drawSelf(situ);
|
||||
MatrixState.popMatrix();
|
||||
//绘制球体
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.scale(1.5f, 1.5f, 1.5f);
|
||||
MatrixState.translate(10f, 0f, 0);
|
||||
qt.drawSelf(situ);
|
||||
MatrixState.popMatrix();
|
||||
//绘制圆环
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.scale(1.5f, 1.5f, 1.5f);
|
||||
MatrixState.translate(0, 0, -10f);
|
||||
yh.drawSelf(situ);
|
||||
MatrixState.popMatrix();
|
||||
//绘制茶壶
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.scale(1.5f, 1.5f, 1.5f);
|
||||
MatrixState.translate(0, 0, 10f);
|
||||
ch.drawSelf(situ);
|
||||
MatrixState.popMatrix();
|
||||
}
|
||||
|
||||
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, 2, 100);
|
||||
//计算摄像机的位置
|
||||
setCameraPostion();
|
||||
//计算灯光的位置
|
||||
setLightPostion();
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run(){
|
||||
while(true){
|
||||
lightAzimuth +=1;
|
||||
lightAzimuth %= 360;
|
||||
//计算灯光的位置
|
||||
setLightPostion();
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//设置屏幕背景色RGBA
|
||||
GLES20.glClearColor(0.3f,0.3f,0.3f,1.0f);
|
||||
//打开深度检测
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
//关闭背面剪裁
|
||||
GLES20.glDisable(GLES20.GL_CULL_FACE);
|
||||
//初始化变换矩阵
|
||||
MatrixState.setInitStack();
|
||||
//加载要绘制的物体
|
||||
ch=LoadUtil.loadFromFileVertexOnlyAverage("ch.obj", MySurfaceView.this.getResources(),MySurfaceView.this);
|
||||
pm=LoadUtil.loadFromFileVertexOnlyFace("pm.obj", MySurfaceView.this.getResources(),MySurfaceView.this);;
|
||||
cft=LoadUtil.loadFromFileVertexOnlyFace("cft.obj", MySurfaceView.this.getResources(),MySurfaceView.this);;
|
||||
qt=LoadUtil.loadFromFileVertexOnlyAverage("qt.obj", MySurfaceView.this.getResources(),MySurfaceView.this);;
|
||||
yh=LoadUtil.loadFromFileVertexOnlyAverage("yh.obj", MySurfaceView.this.getResources(),MySurfaceView.this);;
|
||||
}
|
||||
}
|
||||
}
|
||||
66
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/Normal.java
Normal file
66
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/Normal.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.bn.Sample15_7;
|
||||
|
||||
import java.util.Set;
|
||||
//表示法向量的类,此类的一个对象表示一个法向量
|
||||
public class Normal
|
||||
{
|
||||
public static final float DIFF=0.0000001f;//判断两个法向量是否相同的阈值
|
||||
//法向量在XYZ轴上的分量
|
||||
float nx;
|
||||
float ny;
|
||||
float nz;
|
||||
|
||||
public Normal(float nx,float ny,float nz)
|
||||
{
|
||||
this.nx=nx;
|
||||
this.ny=ny;
|
||||
this.nz=nz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if(o instanceof Normal)
|
||||
{//若两个法向量XYZ三个分量的差都小于指定的阈值则认为这两个法向量相等
|
||||
Normal tn=(Normal)o;
|
||||
if(Math.abs(nx-tn.nx)<DIFF&&
|
||||
Math.abs(ny-tn.ny)<DIFF&&
|
||||
Math.abs(ny-tn.ny)<DIFF
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//由于要用到HashSet,因此一定要重写hashCode方法
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
//求法向量平均值的工具方法
|
||||
public static float[] getAverage(Set<Normal> sn)
|
||||
{
|
||||
//存放法向量和的数组
|
||||
float[] result=new float[3];
|
||||
//把集合中所有的法向量求和
|
||||
for(Normal n:sn)
|
||||
{
|
||||
result[0]+=n.nx;
|
||||
result[1]+=n.ny;
|
||||
result[2]+=n.nz;
|
||||
}
|
||||
//将求和后的法向量规格化
|
||||
return LoadUtil.vectorNormal(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.bn.Sample15_7;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample15_7_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);
|
||||
|
||||
//初始化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
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/ShaderUtil.java
Normal file
126
第15章 真实光学环境的模拟/Sample15_7/src/com/bn/Sample15_7/ShaderUtil.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package com.bn.Sample15_7;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user