霧效果其實很容易實現,我先總結一下:
(1)首先肯定是將背景色設為我們霧的顏色
(2)開啟霧效果
(3)設定霧的背景色
(4)設定霧方程式
霧方程式有三種表現形式 EXP(最老),EXP2(EXP加強版),LINEAR(霧的最好方程式,當然用這個),如果要用LINEAR這種,則要多霧start,end的設定,表示霧從多遠開始,從多遠停止
。像LINEAR這種稱之為線性霧(不是我發明的,OPENGL超級寶典上這樣說,設定稠密度無效)。
(5)最後,設定霧品質優先還是效率優先。
ok,上源碼
(1)Activity
package sim.feel;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class Fog extends Activity {
/** Called when the activity is first created. */
private GLSurfaceView surfaceView;
private Renderer renderer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 載入圖片
BitGL.init(this.getResources());
surfaceView = new GLSurfaceView(this);
renderer = new MyRenderer(this);
surfaceView.setRenderer(renderer);
setContentView(surfaceView);
}
}
class BitGL {
public static Bitmap bitmap;
public static void init(Resources resources) {
bitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
}
}
(2)Renderer類
package sim.feel;
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.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
public class MyRenderer implements Renderer {
// Context
public Context context;
// 頂點紋理相關
private int one = 0x10000;
// Bitmap
private Bitmap bitmap;
// 紋理相關
private int[] textureids;
// vertexBuffer
private IntBuffer vertexBuffer;
// texBuffer
private IntBuffer texBuffer;
// 旋轉方向
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, 0,
one,0,
0,one,
one, one
};
// 霧模式
private int fogMode[]={
GL10.GL_EXP,
GL10.GL_EXP2,
GL10.GL_LINEAR
};
// 霧顏色為灰白色
private float fogColor[]={
0.5f,0.5f,0.5f,1.0f
};
public MyRenderer(Context context) {
this.context = context;
// 初始化
textureids = new int[1];
// 執行個體化bitmap
bitmap = BitGL.bitmap;
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());
texBuffer = tbb.asIntBuffer();
//為每一個面貼上紋理
for (int i = 0; i < 6; i++) {
texBuffer.put(texCoords);
}
texBuffer.position(0);
}
@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, texBuffer); // Define
//向z軸裡移入6.0f
gl.glTranslatef(0.0f, 0.0f, -5.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.6f;
zrot += 0.3f;
}
@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, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// We'll Clear To The Color Of The Fog ( Modified )
gl.glClearColor(0.5f,0.5f,0.5f,1.0f);
//設定霧模式
gl.glFogx(GL10.GL_FOG_MODE, fogMode[2]);
//霧顏色
gl.glFogfv(GL10.GL_FOG_COLOR, fogColor,0);
//更好的外觀,需求大的霧(反之為gl.glHint(GL10.GL_FOG_HINT, GL10.GL_FASTEST);)
gl.glHint(GL10.GL_FOG_HINT, GL10.GL_NICEST);
//霧密度在模式為LINEAR下無效
//gl.glFogf(GL10.GL_FOG_DENSITY, 0.1f);
//開始位置
gl.glFogf(GL10.GL_FOG_START, 1.0f);
//結束位置
gl.glFogf(GL10.GL_FOG_END, 5.0f);
//啟用霧
gl.glEnable(GL10.GL_FOG);
// 告訴系統對透視進行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 啟用陰影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
// 清除深度緩衝
gl.glClearDepthf(one);
// 啟用深度測試
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所做深度測試的類型
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_TEXTURE_2D);
// 建立紋理
gl.glGenTextures(1, textureids, 0);
// 綁定要使用的紋理
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[0]);
// 產生紋理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// 線性濾波
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
}
}
運行效果(個人感到很cool):