android opengl es 雾化效果实例
生活随笔
收集整理的這篇文章主要介紹了
android opengl es 雾化效果实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
霧相關:
三種霧的計算方法,怎么設置霧的顏色和霧的范圍
設置霧的背景顏色: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 ;@Overridepublic 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) ;} }3、渲染類 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();//初始化緩沖}@Overridepublic 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.0fgl.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;}@Overridepublic 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();}@Overridepublic 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);}}
4、運行效果:
總結
以上是生活随笔為你收集整理的android opengl es 雾化效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据挖掘网址
- 下一篇: fastreport dll_报表如何连