初次提交
7
第10章 混合与雾/Sample10_2/.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
第10章 混合与雾/Sample10_2/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample10_2</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
第10章 混合与雾/Sample10_2/.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
第10章 混合与雾/Sample10_2/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.Sample10_2">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name=".Sample10_2_Activity"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
<uses-sdk android:targetSdkVersion="8"></uses-sdk>
|
||||
</manifest>
|
||||
18
第10章 混合与雾/Sample10_2/assets/frag_cloud.sh
Normal file
@@ -0,0 +1,18 @@
|
||||
//云层着色器
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
varying vec4 ambient;
|
||||
varying vec4 diffuse;
|
||||
varying vec4 specular;
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元从纹理中采样出颜色值
|
||||
vec4 finalColor = texture2D(sTexture, vTextureCoord);
|
||||
//根据颜色值计算透明度
|
||||
finalColor.a=(finalColor.r+finalColor.g+finalColor.b)/3.0;
|
||||
//计算光照因素
|
||||
finalColor=finalColor*ambient+finalColor*specular+finalColor*diffuse;
|
||||
//给此片元颜色值
|
||||
gl_FragColor = finalColor;
|
||||
}
|
||||
33
第10章 混合与雾/Sample10_2/assets/frag_earth.sh
Normal file
@@ -0,0 +1,33 @@
|
||||
//地球着色器
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
varying vec4 vAmbient;
|
||||
varying vec4 vDiffuse;
|
||||
varying vec4 vSpecular;
|
||||
uniform sampler2D sTextureDay;//纹理内容数据
|
||||
uniform sampler2D sTextureNight;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元从纹理中采样出颜色值
|
||||
vec4 finalColorDay;
|
||||
vec4 finalColorNight;
|
||||
|
||||
finalColorDay= texture2D(sTextureDay, vTextureCoord);
|
||||
finalColorDay = finalColorDay*vAmbient+finalColorDay*vSpecular+finalColorDay*vDiffuse;
|
||||
finalColorNight = texture2D(sTextureNight, vTextureCoord);
|
||||
finalColorNight = finalColorNight*vec4(0.5,0.5,0.5,1.0);
|
||||
|
||||
if(vDiffuse.x>0.21)
|
||||
{
|
||||
gl_FragColor=finalColorDay;
|
||||
}
|
||||
else if(vDiffuse.x<0.05)
|
||||
{
|
||||
gl_FragColor=finalColorNight;
|
||||
}
|
||||
else
|
||||
{
|
||||
float t=(vDiffuse.x-0.05)/0.16;
|
||||
gl_FragColor=t*finalColorDay+(1.0-t)*finalColorNight;
|
||||
}
|
||||
}
|
||||
14
第10章 混合与雾/Sample10_2/assets/frag_moon.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
//月球着色器
|
||||
precision mediump float;
|
||||
varying vec2 vTextureCoord;//接收从顶点着色器过来的参数
|
||||
varying vec4 vAmbient;
|
||||
varying vec4 vDiffuse;
|
||||
varying vec4 vSpecular;
|
||||
uniform sampler2D sTexture;//纹理内容数据
|
||||
void main()
|
||||
{
|
||||
//给此片元从纹理中采样出颜色值
|
||||
vec4 finalColor = texture2D(sTexture, vTextureCoord);
|
||||
//给此片元颜色值
|
||||
gl_FragColor = finalColor*vAmbient+finalColor*vSpecular+finalColor*vDiffuse;
|
||||
}
|
||||
7
第10章 混合与雾/Sample10_2/assets/frag_xk.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
//星空着色器
|
||||
precision mediump float;
|
||||
void main()
|
||||
{
|
||||
//给此片元颜色值
|
||||
gl_FragColor = vec4(1.0,1.0,1.0,1.0);
|
||||
}
|
||||
57
第10章 混合与雾/Sample10_2/assets/vertex_cloud.sh
Normal file
@@ -0,0 +1,57 @@
|
||||
//云层着色器
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
uniform vec3 uCamera; //摄像机位置
|
||||
uniform vec3 uLightLocationSun; //太阳光源位置
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
attribute vec3 aNormal; //法向量
|
||||
varying vec2 vTextureCoord; //用于传递给片元着色器的变量
|
||||
varying vec4 ambient;
|
||||
varying vec4 diffuse;
|
||||
varying vec4 specular;
|
||||
|
||||
//定位光光照计算的方法
|
||||
void pointLight( //定位光光照计算的方法
|
||||
in vec3 normal, //法向量
|
||||
inout vec4 ambient, //环境光最终强度
|
||||
inout vec4 diffuse, //散射光最终强度
|
||||
inout vec4 specular, //镜面光最终强度
|
||||
in vec3 lightLocation, //光源位置
|
||||
in vec4 lightAmbient, //环境光强度
|
||||
in vec4 lightDiffuse, //散射光强度
|
||||
in vec4 lightSpecular //镜面光强度
|
||||
){
|
||||
ambient=lightAmbient; //直接得出环境光的最终强度
|
||||
vec3 normalTarget=aPosition+normal; //计算变换后的法向量
|
||||
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition,1)).xyz;
|
||||
newNormal=normalize(newNormal); //对法向量规格化
|
||||
//计算从表面点到摄像机的向量
|
||||
vec3 eye= normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
//计算从表面点到光源位置的向量vp
|
||||
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
vp=normalize(vp);//格式化vp
|
||||
vec3 halfVector=normalize(vp+eye); //求视线与光线的半向量
|
||||
float shininess=50.0; //粗糙度,越小越光滑
|
||||
float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量与vp的点积与0的最大值
|
||||
diffuse=lightDiffuse*nDotViewPosition; //计算散射光的最终强度
|
||||
float nDotViewHalfVector=dot(newNormal,halfVector); //法线与半向量的点积
|
||||
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess)); //镜面反射光强度因子
|
||||
specular=lightSpecular*powerFactor; //计算镜面光的最终强度
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
|
||||
vec4 ambientTemp, diffuseTemp, specularTemp; //存放环境光、散射光、镜面反射光的临时变量
|
||||
|
||||
pointLight(normalize(aNormal),ambientTemp,diffuseTemp,specularTemp,uLightLocationSun,vec4(0.05,0.05,0.05,1.0),vec4(1.0,1.0,1.0,1.0),vec4(0.3,0.3,0.3,1.0));
|
||||
|
||||
ambient=ambientTemp;
|
||||
diffuse=diffuseTemp;
|
||||
specular=specularTemp;
|
||||
|
||||
//将顶点的纹理坐标传给片元着色器
|
||||
vTextureCoord=aTexCoor;
|
||||
}
|
||||
57
第10章 混合与雾/Sample10_2/assets/vertex_earth.sh
Normal file
@@ -0,0 +1,57 @@
|
||||
//地球着色器
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
uniform vec3 uCamera; //摄像机位置
|
||||
uniform vec3 uLightLocationSun; //太阳光源位置
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
attribute vec3 aNormal; //法向量
|
||||
varying vec2 vTextureCoord; //用于传递给片元着色器的变量
|
||||
varying vec4 vAmbient;
|
||||
varying vec4 vDiffuse;
|
||||
varying vec4 vSpecular;
|
||||
|
||||
//定位光光照计算的方法
|
||||
void pointLight( //定位光光照计算的方法
|
||||
in vec3 normal, //法向量
|
||||
inout vec4 ambient, //环境光最终强度
|
||||
inout vec4 diffuse, //散射光最终强度
|
||||
inout vec4 specular, //镜面光最终强度
|
||||
in vec3 lightLocation, //光源位置
|
||||
in vec4 lightAmbient, //环境光强度
|
||||
in vec4 lightDiffuse, //散射光强度
|
||||
in vec4 lightSpecular //镜面光强度
|
||||
){
|
||||
ambient=lightAmbient; //直接得出环境光的最终强度
|
||||
vec3 normalTarget=aPosition+normal; //计算变换后的法向量
|
||||
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition,1)).xyz;
|
||||
newNormal=normalize(newNormal); //对法向量规格化
|
||||
//计算从表面点到摄像机的向量
|
||||
vec3 eye= normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
//计算从表面点到光源位置的向量vp
|
||||
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
vp=normalize(vp);//格式化vp
|
||||
vec3 halfVector=normalize(vp+eye); //求视线与光线的半向量
|
||||
float shininess=50.0; //粗糙度,越小越光滑
|
||||
float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量与vp的点积与0的最大值
|
||||
diffuse=lightDiffuse*nDotViewPosition; //计算散射光的最终强度
|
||||
float nDotViewHalfVector=dot(newNormal,halfVector); //法线与半向量的点积
|
||||
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess)); //镜面反射光强度因子
|
||||
specular=lightSpecular*powerFactor; //计算镜面光的最终强度
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
|
||||
vec4 ambientTemp, diffuseTemp, specularTemp; //存放环境光、散射光、镜面反射光的临时变量
|
||||
|
||||
pointLight(normalize(aNormal),ambientTemp,diffuseTemp,specularTemp,uLightLocationSun,vec4(0.05,0.05,0.05,1.0),vec4(1.0,1.0,1.0,1.0),vec4(0.3,0.3,0.3,1.0));
|
||||
|
||||
vAmbient=ambientTemp;
|
||||
vDiffuse=diffuseTemp;
|
||||
vSpecular=specularTemp;
|
||||
|
||||
//将顶点的纹理坐标传给片元着色器
|
||||
vTextureCoord=aTexCoor;
|
||||
}
|
||||
57
第10章 混合与雾/Sample10_2/assets/vertex_moon.sh
Normal file
@@ -0,0 +1,57 @@
|
||||
//月球着色器
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform mat4 uMMatrix; //变换矩阵
|
||||
uniform vec3 uCamera; //摄像机位置
|
||||
uniform vec3 uLightLocationSun; //太阳光源位置
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec2 aTexCoor; //顶点纹理坐标
|
||||
attribute vec3 aNormal; //法向量
|
||||
varying vec2 vTextureCoord; //用于传递给片元着色器的变量
|
||||
varying vec4 vAmbient;
|
||||
varying vec4 vDiffuse;
|
||||
varying vec4 vSpecular;
|
||||
|
||||
//定位光光照计算的方法
|
||||
void pointLight( //定位光光照计算的方法
|
||||
in vec3 normal, //法向量
|
||||
inout vec4 ambient, //环境光最终强度
|
||||
inout vec4 diffuse, //散射光最终强度
|
||||
inout vec4 specular, //镜面光最终强度
|
||||
in vec3 lightLocation, //光源位置
|
||||
in vec4 lightAmbient, //环境光强度
|
||||
in vec4 lightDiffuse, //散射光强度
|
||||
in vec4 lightSpecular //镜面光强度
|
||||
){
|
||||
ambient=lightAmbient; //直接得出环境光的最终强度
|
||||
vec3 normalTarget=aPosition+normal; //计算变换后的法向量
|
||||
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition,1)).xyz;
|
||||
newNormal=normalize(newNormal); //对法向量规格化
|
||||
//计算从表面点到摄像机的向量
|
||||
vec3 eye= normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
//计算从表面点到光源位置的向量vp
|
||||
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
|
||||
vp=normalize(vp);//格式化vp
|
||||
vec3 halfVector=normalize(vp+eye); //求视线与光线的半向量
|
||||
float shininess=50.0; //粗糙度,越小越光滑
|
||||
float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量与vp的点积与0的最大值
|
||||
diffuse=lightDiffuse*nDotViewPosition; //计算散射光的最终强度
|
||||
float nDotViewHalfVector=dot(newNormal,halfVector); //法线与半向量的点积
|
||||
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess)); //镜面反射光强度因子
|
||||
specular=lightSpecular*powerFactor; //计算镜面光的最终强度
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
|
||||
vec4 ambientTemp, diffuseTemp, specularTemp; //存放环境光、散射光、镜面反射光的临时变量
|
||||
|
||||
pointLight(normalize(aNormal),ambientTemp,diffuseTemp,specularTemp,uLightLocationSun,vec4(0.05,0.05,0.025,1.0),vec4(1.0,1.0,0.5,1.0),vec4(0.3,0.3,0.15,1.0));
|
||||
|
||||
vAmbient=ambientTemp;
|
||||
vDiffuse=diffuseTemp;
|
||||
vSpecular=specularTemp;
|
||||
|
||||
//将顶点的纹理坐标传给片元着色器
|
||||
vTextureCoord=aTexCoor;
|
||||
}
|
||||
12
第10章 混合与雾/Sample10_2/assets/vertex_xk.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
//星空着色器
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
uniform float uPointSize;//点尺寸
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
|
||||
void main()
|
||||
{
|
||||
//根据总变换矩阵计算此次绘制此顶点位置
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1);
|
||||
//
|
||||
gl_PointSize=uPointSize;
|
||||
}
|
||||
BIN
第10章 混合与雾/Sample10_2/bin/Sample10_2.apk
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/classes.dex
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/Celestial.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/Cloud.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/Constant.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/Earth.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/MatrixState.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/Moon.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/MySurfaceView.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/R$attr.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/R$drawable.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/R$string.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/R.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/com/bn/Sample10_2/ShaderUtil.class
Normal file
BIN
第10章 混合与雾/Sample10_2/bin/resources.ap_
Normal file
13
第10章 混合与雾/Sample10_2/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
|
||||
23
第10章 混合与雾/Sample10_2/gen/com/bn/Sample10_2/R.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/* 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.Sample10_2;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int cloud=0x7f020000;
|
||||
public static final int earth=0x7f020001;
|
||||
public static final int earthn=0x7f020002;
|
||||
public static final int icon=0x7f020003;
|
||||
public static final int moon=0x7f020004;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f030000;
|
||||
}
|
||||
}
|
||||
BIN
第10章 混合与雾/Sample10_2/res/drawable-hdpi/cloud.jpg
Normal file
|
After Width: | Height: | Size: 55 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-hdpi/earth.png
Normal file
|
After Width: | Height: | Size: 123 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-hdpi/earthn.png
Normal file
|
After Width: | Height: | Size: 132 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-hdpi/icon.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-hdpi/moon.png
Normal file
|
After Width: | Height: | Size: 148 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-ldpi/icon.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第10章 混合与雾/Sample10_2/res/drawable-mdpi/icon.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
4
第10章 混合与雾/Sample10_2/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample10_2</string>
|
||||
</resources>
|
||||
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
}
|
||||