初次提交
This commit is contained in:
77
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Celestial.java
Normal file
77
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Celestial.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package com.bn.Sample10_2;
|
||||
import static com.bn.Sample10_2.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import android.opengl.GLES20;
|
||||
public class Celestial { //表示星空天球的类
|
||||
final float UNIT_SIZE=10.0f;//天球半径
|
||||
private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
int vCount=0;//星星数量
|
||||
float yAngle;//天球绕Y轴旋转的角度
|
||||
float scale;//星星尺寸
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int uPointSizeHandle;//顶点尺寸参数引用
|
||||
public Celestial(float scale,float yAngle,int vCount,MySurfaceView mv){
|
||||
this.yAngle=yAngle;
|
||||
this.scale=scale;
|
||||
this.vCount=vCount;
|
||||
initVertexData();
|
||||
intShader(mv);
|
||||
}
|
||||
public void initVertexData(){ //初始化顶点坐标的方法
|
||||
//顶点坐标数据的初始化
|
||||
float vertices[]=new float[vCount*3];
|
||||
for(int i=0;i<vCount;i++){
|
||||
//随机产生每个星星的xyz坐标
|
||||
double angleTempJD=Math.PI*2*Math.random();
|
||||
double angleTempWD=Math.PI*(Math.random()-0.5f);
|
||||
vertices[i*3]=(float)(UNIT_SIZE*Math.cos(angleTempWD)*Math.sin(angleTempJD));
|
||||
vertices[i*3+1]=(float)(UNIT_SIZE*Math.sin(angleTempWD));
|
||||
vertices[i*3+2]=(float)(UNIT_SIZE*Math.cos(angleTempWD)*Math.cos(angleTempJD));
|
||||
}
|
||||
//创建顶点坐标数据缓冲
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
}
|
||||
public void intShader(MySurfaceView mv){ //初始化着色器
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_xk.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_xk.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取顶点尺寸参数引用
|
||||
uPointSizeHandle = GLES20.glGetUniformLocation(mProgram, "uPointSize");
|
||||
}
|
||||
public void drawSelf(){
|
||||
GLES20.glUseProgram(mProgram); //制定使用某套着色器程序
|
||||
//将最终变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
GLES20.glUniform1f(uPointSizeHandle, scale); //将顶点尺寸传入着色器程序
|
||||
GLES20.glVertexAttribPointer( //为画笔指定顶点位置数据
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vCount); //绘制星星点
|
||||
}}
|
||||
228
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Cloud.java
Normal file
228
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Cloud.java
Normal file
@@ -0,0 +1,228 @@
|
||||
package com.bn.Sample10_2;
|
||||
import static com.bn.Sample10_2.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//表示月球的类,为普通纹理球,未采用多重纹理
|
||||
public class Cloud
|
||||
{
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maNormalHandle; //顶点法向量属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
int maSunLightLocationHandle;//光源位置属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mTexCoorBuffer;//顶点纹理坐标数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public Cloud(MySurfaceView mv,float r)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(r);
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与纹理数据的方法
|
||||
public void initVertexData(float r)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
final float UNIT_SIZE=0.5f;
|
||||
ArrayList<Float> alVertix=new ArrayList<Float>();//存放顶点坐标的ArrayList
|
||||
final float angleSpan=10f;//将球进行单位切分的角度
|
||||
for(float vAngle=90;vAngle>-90;vAngle=vAngle-angleSpan)//垂直方向angleSpan度一份
|
||||
{
|
||||
for(float hAngle=360;hAngle>0;hAngle=hAngle-angleSpan)//水平方向angleSpan度一份
|
||||
{//纵向横向各到一个角度后计算对应的此点在球面上的坐标
|
||||
double xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x1=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z1=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y1=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x2=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z2=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y2=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x3=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z3=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y3=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x4=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z4=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y4=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
|
||||
//构建第一三角形
|
||||
alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
//构建第二三角形
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x3);alVertix.add(y3);alVertix.add(z3);
|
||||
}
|
||||
}
|
||||
vCount=alVertix.size()/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
|
||||
|
||||
//将alVertix中的坐标值转存到一个float数组中
|
||||
float vertices[]=new float[vCount*3];
|
||||
for(int i=0;i<alVertix.size();i++)
|
||||
{
|
||||
vertices[i]=alVertix.get(i);
|
||||
}
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
|
||||
//将alTexCoor中的纹理坐标值转存到一个float数组中
|
||||
float[] texCoor=generateTexCoor//获取切分整图的纹理数组
|
||||
(
|
||||
(int)(360/angleSpan), //纹理图切分的列数
|
||||
(int)(180/angleSpan) //纹理图切分的行数
|
||||
);
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTexCoorBuffer=llbb.asFloatBuffer();
|
||||
mTexCoorBuffer.put(texCoor);
|
||||
mTexCoorBuffer.position(0);
|
||||
//顶点坐标数据的初始化================end============================
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_cloud.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_cloud.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点经纬度属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中顶点法向量属性引用id
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取程序中摄像机位置引用id
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
//获取程序中光源位置引用id
|
||||
maSunLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocationSun");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf(int texId)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将摄像机位置传入shader程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
//将光源位置传入shader程序
|
||||
GLES20.glUniform3fv(maSunLightLocationHandle, 1, MatrixState.lightPositionFBSun);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点经纬度数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maTexCoorHandle,
|
||||
2,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
2*4,
|
||||
mTexCoorBuffer
|
||||
);
|
||||
//为画笔指定顶点法向量数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maNormalHandle,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
|
||||
//自动切分纹理产生纹理数组的方法
|
||||
public float[] generateTexCoor(int bw,int bh)
|
||||
{
|
||||
float[] result=new float[bw*bh*6*2];
|
||||
float sizew=1.0f/bw;//列数
|
||||
float sizeh=1.0f/bh;//行数
|
||||
int c=0;
|
||||
for(int i=0;i<bh;i++)
|
||||
{
|
||||
for(int j=0;j<bw;j++)
|
||||
{
|
||||
//每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
|
||||
float s=j*sizew;
|
||||
float t=i*sizeh;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t+sizeh;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
9
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Constant.java
Normal file
9
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Constant.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.bn.Sample10_2;
|
||||
|
||||
public class Constant
|
||||
{
|
||||
//GLSurfaceView的宽高比
|
||||
public static float ratio;
|
||||
//旋转地月系线程的工作标志位
|
||||
public static boolean threadFlag=true;
|
||||
}
|
||||
191
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Earth.java
Normal file
191
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Earth.java
Normal file
@@ -0,0 +1,191 @@
|
||||
package com.bn.Sample10_2;
|
||||
import static com.bn.Sample10_2.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import android.opengl.GLES20;
|
||||
//表示地球的类,采用多重纹理
|
||||
public class Earth {
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maNormalHandle; //顶点法向量属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
int maSunLightLocationHandle;//光源位置属性引用id
|
||||
int uDayTexHandle;//白天纹理属性引用id
|
||||
int uNightTexHandle;//黑夜纹理属性引用id
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mTexCoorBuffer;//顶点纹理坐标数据缓冲
|
||||
int vCount=0;
|
||||
public Earth(MySurfaceView mv,float r){
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(r);
|
||||
//初始化着色器
|
||||
initShader(mv);
|
||||
}
|
||||
//初始化顶点坐标与纹理数据的方法
|
||||
public void initVertexData(float r){
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
final float UNIT_SIZE=0.5f;
|
||||
ArrayList<Float> alVertix=new ArrayList<Float>();//存放顶点坐标的ArrayList
|
||||
final float angleSpan=10f;//将球进行单位切分的角度
|
||||
for(float vAngle=90;vAngle>-90;vAngle=vAngle-angleSpan){//垂直方向angleSpan度一份
|
||||
for(float hAngle=360;hAngle>0;hAngle=hAngle-angleSpan){//水平方向angleSpan度一份
|
||||
//纵向横向各到一个角度后计算对应的此点在球面上的坐标
|
||||
double xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x1=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z1=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y1=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x2=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z2=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y2=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x3=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z3=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y3=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x4=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z4=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y4=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
//构建第一三角形
|
||||
alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
//构建第二三角形
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x3);alVertix.add(y3);alVertix.add(z3);
|
||||
}}
|
||||
vCount=alVertix.size()/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
|
||||
//将alVertix中的坐标值转存到一个float数组中
|
||||
float vertices[]=new float[vCount*3];
|
||||
for(int i=0;i<alVertix.size();i++){
|
||||
vertices[i]=alVertix.get(i);
|
||||
}
|
||||
//创建顶点坐标数据缓冲
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//将alTexCoor中的纹理坐标值转存到一个float数组中
|
||||
float[] texCoor=generateTexCoor(//获取切分整图的纹理数组
|
||||
(int)(360/angleSpan), //纹理图切分的列数
|
||||
(int)(180/angleSpan) //纹理图切分的行数
|
||||
);
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTexCoorBuffer=llbb.asFloatBuffer();
|
||||
mTexCoorBuffer.put(texCoor);
|
||||
mTexCoorBuffer.position(0);
|
||||
}
|
||||
public void initShader(MySurfaceView mv){ //初始化着色器
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_earth.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_earth.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点纹理属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中顶点法向量属性引用id
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取程序中摄像机位置引用id
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
//获取程序中光源位置引用id
|
||||
maSunLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocationSun");
|
||||
//获取白天、黑夜两个纹理引用
|
||||
uDayTexHandle=GLES20.glGetUniformLocation(mProgram, "sTextureDay");
|
||||
uNightTexHandle=GLES20.glGetUniformLocation(mProgram, "sTextureNight");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
}
|
||||
public void drawSelf(int texId,int texIdNight) {
|
||||
//制定使用某套着色器程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入着色器程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将摄像机位置传入着色器程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
//将光源位置传入着色器程序
|
||||
GLES20.glUniform3fv(maSunLightLocationHandle, 1, MatrixState.lightPositionFBSun);
|
||||
GLES20.glVertexAttribPointer(//为画笔指定顶点位置数据
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
GLES20.glVertexAttribPointer( //为画笔指定顶点纹理数据
|
||||
maTexCoorHandle,
|
||||
2,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
2*4,
|
||||
mTexCoorBuffer
|
||||
);
|
||||
GLES20.glVertexAttribPointer //为画笔指定顶点法向量数据
|
||||
(
|
||||
maNormalHandle,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIdNight);
|
||||
GLES20.glUniform1i(uDayTexHandle, 0);
|
||||
GLES20.glUniform1i(uNightTexHandle, 1);
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
//自动切分纹理产生纹理数组的方法
|
||||
public float[] generateTexCoor(int bw,int bh){
|
||||
float[] result=new float[bw*bh*6*2];
|
||||
float sizew=1.0f/bw;//列数
|
||||
float sizeh=1.0f/bh;//行数
|
||||
int c=0;
|
||||
for(int i=0;i<bh;i++){
|
||||
for(int j=0;j<bw;j++){
|
||||
//每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
|
||||
float s=j*sizew;
|
||||
float t=i*sizeh;
|
||||
result[c++]=s;
|
||||
result[c++]=t;
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t+sizeh;
|
||||
}}
|
||||
return result;
|
||||
}}
|
||||
145
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/MatrixState.java
Normal file
145
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/MatrixState.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.bn.Sample10_2;
|
||||
|
||||
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[] lightLocationSun=new float[]{0,0,0};//太阳定位光光源位置
|
||||
public static FloatBuffer cameraFB;
|
||||
public static FloatBuffer lightPositionFBSun;
|
||||
|
||||
|
||||
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 transtate(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 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 setLightLocationSun(float x,float y,float z)
|
||||
{
|
||||
lightLocationSun[0]=x;
|
||||
lightLocationSun[1]=y;
|
||||
lightLocationSun[2]=z;
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(3*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
lightPositionFBSun=llbb.asFloatBuffer();
|
||||
lightPositionFBSun.put(lightLocationSun);
|
||||
lightPositionFBSun.position(0);
|
||||
}
|
||||
}
|
||||
228
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Moon.java
Normal file
228
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/Moon.java
Normal file
@@ -0,0 +1,228 @@
|
||||
package com.bn.Sample10_2;
|
||||
import static com.bn.Sample10_2.ShaderUtil.createProgram;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import android.opengl.GLES20;
|
||||
|
||||
//表示月球的类,为普通纹理球,未采用多重纹理
|
||||
public class Moon
|
||||
{
|
||||
int mProgram;//自定义渲染管线程序id
|
||||
int muMVPMatrixHandle;//总变换矩阵引用id
|
||||
int muMMatrixHandle;//位置、旋转变换矩阵
|
||||
int maCameraHandle; //摄像机位置属性引用id
|
||||
int maPositionHandle; //顶点位置属性引用id
|
||||
int maNormalHandle; //顶点法向量属性引用id
|
||||
int maTexCoorHandle; //顶点纹理坐标属性引用id
|
||||
int maSunLightLocationHandle;//光源位置属性引用id
|
||||
|
||||
String mVertexShader;//顶点着色器
|
||||
String mFragmentShader;//片元着色器
|
||||
|
||||
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
|
||||
FloatBuffer mTexCoorBuffer;//顶点纹理坐标数据缓冲
|
||||
int vCount=0;
|
||||
|
||||
public Moon(MySurfaceView mv,float r)
|
||||
{
|
||||
//初始化顶点坐标与着色数据
|
||||
initVertexData(r);
|
||||
//初始化shader
|
||||
initShader(mv);
|
||||
}
|
||||
|
||||
//初始化顶点坐标与纹理数据的方法
|
||||
public void initVertexData(float r)
|
||||
{
|
||||
//顶点坐标数据的初始化================begin============================
|
||||
final float UNIT_SIZE=0.5f;
|
||||
ArrayList<Float> alVertix=new ArrayList<Float>();//存放顶点坐标的ArrayList
|
||||
final float angleSpan=10f;//将球进行单位切分的角度
|
||||
for(float vAngle=90;vAngle>-90;vAngle=vAngle-angleSpan)//垂直方向angleSpan度一份
|
||||
{
|
||||
for(float hAngle=360;hAngle>0;hAngle=hAngle-angleSpan)//水平方向angleSpan度一份
|
||||
{//纵向横向各到一个角度后计算对应的此点在球面上的坐标
|
||||
double xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x1=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z1=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y1=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x2=(float)(xozLength*Math.cos(Math.toRadians(hAngle)));
|
||||
float z2=(float)(xozLength*Math.sin(Math.toRadians(hAngle)));
|
||||
float y2=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle-angleSpan));
|
||||
float x3=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z3=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y3=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle-angleSpan)));
|
||||
|
||||
xozLength=r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
|
||||
float x4=(float)(xozLength*Math.cos(Math.toRadians(hAngle-angleSpan)));
|
||||
float z4=(float)(xozLength*Math.sin(Math.toRadians(hAngle-angleSpan)));
|
||||
float y4=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
|
||||
|
||||
//构建第一三角形
|
||||
alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
//构建第二三角形
|
||||
alVertix.add(x4);alVertix.add(y4);alVertix.add(z4);
|
||||
alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
|
||||
alVertix.add(x3);alVertix.add(y3);alVertix.add(z3);
|
||||
}
|
||||
}
|
||||
vCount=alVertix.size()/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
|
||||
|
||||
//将alVertix中的坐标值转存到一个float数组中
|
||||
float vertices[]=new float[vCount*3];
|
||||
for(int i=0;i<alVertix.size();i++)
|
||||
{
|
||||
vertices[i]=alVertix.get(i);
|
||||
}
|
||||
|
||||
//创建顶点坐标数据缓冲
|
||||
//vertices.length*4是因为一个整数四个字节
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
vbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mVertexBuffer = vbb.asFloatBuffer();//转换为int型缓冲
|
||||
mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
|
||||
mVertexBuffer.position(0);//设置缓冲区起始位置
|
||||
//特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
|
||||
//转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
|
||||
|
||||
//将alTexCoor中的纹理坐标值转存到一个float数组中
|
||||
float[] texCoor=generateTexCoor//获取切分整图的纹理数组
|
||||
(
|
||||
(int)(360/angleSpan), //纹理图切分的列数
|
||||
(int)(180/angleSpan) //纹理图切分的行数
|
||||
);
|
||||
ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length*4);
|
||||
llbb.order(ByteOrder.nativeOrder());//设置字节顺序
|
||||
mTexCoorBuffer=llbb.asFloatBuffer();
|
||||
mTexCoorBuffer.put(texCoor);
|
||||
mTexCoorBuffer.position(0);
|
||||
//顶点坐标数据的初始化================end============================
|
||||
}
|
||||
|
||||
//初始化shader
|
||||
public void initShader(MySurfaceView mv)
|
||||
{
|
||||
//加载顶点着色器的脚本内容
|
||||
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex_moon.sh", mv.getResources());
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
//加载片元着色器的脚本内容
|
||||
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag_moon.sh", mv.getResources());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
ShaderUtil.checkGlError("==ss==");
|
||||
mProgram = createProgram(mVertexShader, mFragmentShader);
|
||||
//获取程序中顶点位置属性引用id
|
||||
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
|
||||
//获取程序中顶点经纬度属性引用id
|
||||
maTexCoorHandle=GLES20.glGetAttribLocation(mProgram, "aTexCoor");
|
||||
//获取程序中顶点法向量属性引用id
|
||||
maNormalHandle= GLES20.glGetAttribLocation(mProgram, "aNormal");
|
||||
//获取程序中总变换矩阵引用id
|
||||
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
|
||||
//获取程序中摄像机位置引用id
|
||||
maCameraHandle=GLES20.glGetUniformLocation(mProgram, "uCamera");
|
||||
//获取程序中光源位置引用id
|
||||
maSunLightLocationHandle=GLES20.glGetUniformLocation(mProgram, "uLightLocationSun");
|
||||
//获取位置、旋转变换矩阵引用id
|
||||
muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
|
||||
}
|
||||
|
||||
public void drawSelf(int texId)
|
||||
{
|
||||
//制定使用某套shader程序
|
||||
GLES20.glUseProgram(mProgram);
|
||||
//将最终变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
|
||||
//将位置、旋转变换矩阵传入shader程序
|
||||
GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
|
||||
//将摄像机位置传入shader程序
|
||||
GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
|
||||
//将光源位置传入shader程序
|
||||
GLES20.glUniform3fv(maSunLightLocationHandle, 1, MatrixState.lightPositionFBSun);
|
||||
|
||||
//为画笔指定顶点位置数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maPositionHandle,
|
||||
3,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//为画笔指定顶点经纬度数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maTexCoorHandle,
|
||||
2,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
2*4,
|
||||
mTexCoorBuffer
|
||||
);
|
||||
//为画笔指定顶点法向量数据
|
||||
GLES20.glVertexAttribPointer
|
||||
(
|
||||
maNormalHandle,
|
||||
4,
|
||||
GLES20.GL_FLOAT,
|
||||
false,
|
||||
3*4,
|
||||
mVertexBuffer
|
||||
);
|
||||
//允许顶点位置数据数组
|
||||
GLES20.glEnableVertexAttribArray(maPositionHandle);
|
||||
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
|
||||
GLES20.glEnableVertexAttribArray(maNormalHandle);
|
||||
//绑定纹理
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
|
||||
//绘制三角形
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
|
||||
}
|
||||
|
||||
//自动切分纹理产生纹理数组的方法
|
||||
public float[] generateTexCoor(int bw,int bh)
|
||||
{
|
||||
float[] result=new float[bw*bh*6*2];
|
||||
float sizew=1.0f/bw;//列数
|
||||
float sizeh=1.0f/bh;//行数
|
||||
int c=0;
|
||||
for(int i=0;i<bh;i++)
|
||||
{
|
||||
for(int j=0;j<bw;j++)
|
||||
{
|
||||
//每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
|
||||
float s=j*sizew;
|
||||
float t=i*sizeh;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t;
|
||||
|
||||
result[c++]=s;
|
||||
result[c++]=t+sizeh;
|
||||
|
||||
result[c++]=s+sizew;
|
||||
result[c++]=t+sizeh;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
233
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/MySurfaceView.java
Normal file
233
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/MySurfaceView.java
Normal file
@@ -0,0 +1,233 @@
|
||||
package com.bn.Sample10_2;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.opengl.GLUtils;
|
||||
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 android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import static com.bn.Sample10_2.Constant.*;
|
||||
|
||||
class MySurfaceView extends GLSurfaceView
|
||||
{
|
||||
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
|
||||
private SceneRenderer mRenderer;//场景渲染器
|
||||
|
||||
private float mPreviousX;//上次的触控位置X坐标
|
||||
private float mPreviousY;//上次的触控位置Y坐标
|
||||
|
||||
int textureIdEarth;//系统分配的地球纹理id
|
||||
int textureIdEarthNight;//系统分配的地球夜晚纹理id
|
||||
int textureIdMoon;//系统分配的月球纹理id
|
||||
int textureIdCloud;//系统分配的云层纹理id
|
||||
|
||||
float yAngle=0;//太阳灯光绕y轴旋转的角度
|
||||
float xAngle=0;//摄像机绕X轴旋转的角度
|
||||
|
||||
float eAngle=0;//地球自转角度
|
||||
float cAngle=0;//天球自转的角度
|
||||
|
||||
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();
|
||||
float y = e.getY();
|
||||
switch (e.getAction()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
//触控横向位移太阳绕y轴旋转
|
||||
float dx = x - mPreviousX;//计算触控笔X位移
|
||||
yAngle += dx * TOUCH_SCALE_FACTOR;//设置太阳绕y轴旋转的角度
|
||||
float sunx=(float)(Math.cos(Math.toRadians(yAngle))*100);
|
||||
float sunz=-(float)(Math.sin(Math.toRadians(yAngle))*100);
|
||||
MatrixState.setLightLocationSun(sunx,5,sunz);
|
||||
|
||||
//触控纵向位移摄像机绕x轴旋转 -90~+90
|
||||
float dy = y - mPreviousY;//计算触控笔Y位移
|
||||
xAngle += dy * TOUCH_SCALE_FACTOR;//设置太阳绕y轴旋转的角度
|
||||
if(xAngle>90)
|
||||
{
|
||||
xAngle=90;
|
||||
}
|
||||
else if(xAngle<-90)
|
||||
{
|
||||
xAngle=-90;
|
||||
}
|
||||
float cy=(float) (7.2*Math.sin(Math.toRadians(xAngle)));
|
||||
float cz=(float) (7.2*Math.cos(Math.toRadians(xAngle)));
|
||||
float upy=(float) Math.cos(Math.toRadians(xAngle));
|
||||
float upz=-(float) Math.sin(Math.toRadians(xAngle));
|
||||
MatrixState.setCamera(0, cy, cz, 0, 0, 0, 0, upy, upz);
|
||||
}
|
||||
mPreviousX = x;//记录触控笔位置
|
||||
mPreviousY = y;
|
||||
return true;
|
||||
}
|
||||
|
||||
private class SceneRenderer implements GLSurfaceView.Renderer
|
||||
{
|
||||
Earth earth;//地球
|
||||
Moon moon;//月球
|
||||
Celestial cSmall;//小星星天球
|
||||
Celestial cBig;//大星星天球
|
||||
Cloud cloud;//月球
|
||||
|
||||
public void onDrawFrame(GL10 gl)
|
||||
{
|
||||
//清除深度缓冲与颜色缓冲
|
||||
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
//保护现场
|
||||
MatrixState.pushMatrix();
|
||||
//地球自转
|
||||
MatrixState.rotate(eAngle, 0, 1, 0);
|
||||
//绘制纹理圆球
|
||||
earth.drawSelf(textureIdEarth,textureIdEarthNight);
|
||||
|
||||
//开启混合
|
||||
GLES20.glEnable(GLES20.GL_BLEND);
|
||||
//设置混合因子
|
||||
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
|
||||
cloud.drawSelf(textureIdCloud);//绘制云层
|
||||
//关闭混合
|
||||
GLES20.glDisable(GLES20.GL_BLEND);
|
||||
|
||||
|
||||
//推坐标系到月球位置
|
||||
MatrixState.transtate(2f, 0, 0);
|
||||
//月球自转
|
||||
MatrixState.rotate(eAngle, 0, 1, 0);
|
||||
//绘制月球
|
||||
moon.drawSelf(textureIdMoon);
|
||||
//恢复现场
|
||||
MatrixState.popMatrix();
|
||||
|
||||
//保护现场
|
||||
MatrixState.pushMatrix();
|
||||
MatrixState.rotate(cAngle, 0, 1, 0);
|
||||
cSmall.drawSelf();
|
||||
cBig.drawSelf();
|
||||
//恢复现场
|
||||
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, ratio, -1, 1, 4f, 100);
|
||||
//调用此方法产生摄像机9参数位置矩阵
|
||||
MatrixState.setCamera(0,0,7.2f,0f,0f,0f,0f,1.0f,0.0f);
|
||||
//打开背面剪裁
|
||||
GLES20.glEnable(GLES20.GL_CULL_FACE);
|
||||
//初始化纹理
|
||||
textureIdEarth=initTexture(R.drawable.earth);
|
||||
textureIdEarthNight=initTexture(R.drawable.earthn);
|
||||
textureIdMoon=initTexture(R.drawable.moon);
|
||||
textureIdCloud=initTexture(R.drawable.cloud);
|
||||
//设置太阳灯光的初始位置
|
||||
MatrixState.setLightLocationSun(100,5,0);
|
||||
|
||||
//启动一个线程定时旋转地球、月球
|
||||
new Thread()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
while(threadFlag)
|
||||
{
|
||||
//地球自转角度
|
||||
eAngle=(eAngle+2)%360;
|
||||
//天球自转角度
|
||||
cAngle=(cAngle+0.2f)%360;
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
//设置屏幕背景色RGBA
|
||||
GLES20.glClearColor(0.0f,0.0f,0.0f, 1.0f);
|
||||
//创建地球对象
|
||||
earth=new Earth(MySurfaceView.this,2.0f);
|
||||
cloud=new Cloud(MySurfaceView.this,2.02f);
|
||||
//创建月球对象
|
||||
moon=new Moon(MySurfaceView.this,1.0f);
|
||||
//创建小星星天球对象
|
||||
cSmall=new Celestial(1,0,1000,MySurfaceView.this);
|
||||
//创建大星星天球对象
|
||||
cBig=new Celestial(2,0,500,MySurfaceView.this);
|
||||
//打开深度检测
|
||||
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
||||
//初始化变换矩阵
|
||||
MatrixState.setInitStack();
|
||||
}
|
||||
}
|
||||
|
||||
public int initTexture(int drawableId)//textureId
|
||||
{
|
||||
//生成纹理ID
|
||||
int[] textures = new int[1];
|
||||
GLES20.glGenTextures
|
||||
(
|
||||
1, //产生的纹理id的数量
|
||||
textures, //纹理id的数组
|
||||
0 //偏移量
|
||||
);
|
||||
int textureId=textures[0];
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
|
||||
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
|
||||
|
||||
//通过输入流加载图片===============begin===================
|
||||
InputStream is = this.getResources().openRawResource(drawableId);
|
||||
Bitmap bitmapTmp;
|
||||
try
|
||||
{
|
||||
bitmapTmp = BitmapFactory.decodeStream(is);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//通过输入流加载图片===============end=====================
|
||||
|
||||
//实际加载纹理
|
||||
GLUtils.texImage2D
|
||||
(
|
||||
GLES20.GL_TEXTURE_2D, //纹理类型,在OpenGL ES中必须为GL10.GL_TEXTURE_2D
|
||||
0, //纹理的层次,0表示基本图像层,可以理解为直接贴图
|
||||
bitmapTmp, //纹理图像
|
||||
0 //纹理边框尺寸
|
||||
);
|
||||
bitmapTmp.recycle(); //纹理加载成功后释放图片
|
||||
|
||||
return textureId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.bn.Sample10_2;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample10_2_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();
|
||||
Constant.threadFlag=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mGLSurfaceView.onPause();
|
||||
Constant.threadFlag=false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
126
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/ShaderUtil.java
Normal file
126
第10章 混合与雾/Sample10_2/src/com/bn/Sample10_2/ShaderUtil.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package com.bn.Sample10_2;
|
||||
|
||||
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