雾相关:
三种雾的计算方法,怎么设置雾的颜色和雾的范围
设置雾的背景颜色:gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
设置雾气的模式:gl.glFogf(GL10.GL_FOG_MODE, fogMode[2]);
设置雾气的颜色:gl.glFogfv(GL10.GL_FOG_COLOR, fogColor, 0);
设置雾的密度:模式为LINEAR时,不需要设置
设置系统如何计算雾气:gl.glHint(GL10.GL_FOG_HINT, GL10.GL_LINEAR);
雾气的开始位置:gl.glFogf(GL10.GL_FOG_START, 1);
雾气的结束位置:gl.glFogf(GL10.GL_FOG_END, 5);
雾气的初始化:gl.glEnable(GL10.GL_FOG);
实例代码如下:
1、Activity类
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class FogOpenglActivity extends Activity {
GLSurfaceView gView;
FogRenderer fogRenderer ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBitmap.init(this.getResources());
gView = new GLSurfaceView(this);
fogRenderer = new FogRenderer(this);
gView.setRenderer(fogRenderer);
setContentView(gView);
}
}
2、初始化贴图图片bitmap类
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* 将图片转成bitmap
* @author wuchanghua
*
*/
public class initBitmap {
public static Bitmap bitmap;
public static void init(Resources res){
bitmap = BitmapFactory.decodeResource(res, R.drawable.beach) ;
}
}
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
/**
* 雾效果渲染类
* @author wuchanghua
* date:2012-07--03
*/
public class FogRenderer implements Renderer {
public Context context;
private int one = 0x10000;
private Bitmap bitmap;
private int[] textures;
private float xrot,yrot,zrot;
// 正方体顶点
private int[] vertices = { one, one, -one, -one, one, -one, one, one, one,
-one, one, one, one, -one, one, -one, -one, one,
one, -one, -one, -one, -one, -one, one, one, one,
-one, one, one, one, -one, one, -one, -one, one,
one, -one, -one, -one, -one, -one, one, one, -one,
-one, one, -one, -one, one, one, -one, one, -one,
-one, -one, one, -one, -one, -one, one, one, -one,
one, one, one, one, -one, -one, one, -one, one };
//纹理点
private int[] texCoords = {
0, one,
one, one,
0, 0,
one, 0
};
//顶点和纹理缓冲
private IntBuffer vertexBuffer;
private IntBuffer texCoordsBuffer;
/**
* 设置雾的模式:
* 1.GL_EXP: 充满整个屏幕的基本渲染的雾,看起来有点不真实
* 2.GL_EXP2:比GL_EXP更进一步,看起来有深度些
* 3.GL_LINER:最好的渲染模式。物体淡入淡出的效果更自然
*/
private int[] fogMode = {GL10.GL_EXP,GL10.GL_EXP2,GL10.GL_LINEAR};
/**
* 设置雾的颜色:灰色的雾效果
*/
private float[] fogColor = {0.5f,0.5f,0.5f,1.0f};
//初始化缓冲
private void initBuffer(){
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asIntBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4 * 6);
tbb.order(ByteOrder.nativeOrder());
texCoordsBuffer = tbb.asIntBuffer();
//为每一个面贴上纹理
for (int i = 0; i < 6; i++) {
texCoordsBuffer.put(texCoords);
}
texCoordsBuffer.position(0);
}
public FogRenderer(Context context){
this.context = context;
textures = new int[1];
bitmap = initBitmap.bitmap;
initBuffer();//初始化缓冲
}
@Override
public void onDrawFrame(GL10 gl) {
// 清除深度和颜色缓存
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
//开启顶点和纹理缓冲
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//设置点点和纹理
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoordsBuffer);
//向z轴里移入6.0f
gl.glTranslatef(0.0f, 0.0f, -6.0f);
// 设置3个方向的旋转
gl.glRotatef(xrot, one, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, one, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, one);
// 绘制正方体
for (int i = 0; i < 6; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// 设置旋转角度
xrot += 0.5f;
yrot += 0.4f;
zrot += 0.6f;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// 设置场景大小
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// 投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION);
// 重置视图
gl.glLoadIdentity();
// 设置视图的大小
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 15);
// 设置观察模型
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 黑色背景色
gl.glClearColorx(0, 0, 0, 0);
// 启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// 深度测试类型
gl.glDepthFunc(GL10.GL_LEQUAL);
// 设置深度缓存
gl.glClearDepthf(1.0f);
// 启用纹理
gl.glEnable(GL10.GL_TEXTURE_2D);
// 创建纹理
gl.glGenTextures(1, textures, 0);
// 绑定纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
//线性滤波
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);//放大时
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);//缩小时
/**
* 初始化雾的效果
*/
//设置雾的背景颜色
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
//设置雾气的模式
gl.glFogf(GL10.GL_FOG_MODE, fogMode[2]);
//设置雾气的颜色
gl.glFogfv(GL10.GL_FOG_COLOR, fogColor, 0);
//设置系统如何计算雾气
gl.glHint(GL10.GL_FOG_HINT, GL10.GL_LINEAR);
//雾气的开始位置
gl.glFogf(GL10.GL_FOG_START, 1);
//雾气的结束位置
gl.glFogf(GL10.GL_FOG_END, 5);
//雾气的初始化
gl.glEnable(GL10.GL_FOG);
}
}