日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android opengl es 雾化效果实例

發布時間:2025/3/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 雾化效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。