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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 中opengl es灯光效果实例

發布時間:2025/3/15 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 中opengl es灯光效果实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、還是要準備一張圖片,放在res/drawable中

二、燈光效果代碼:

/*** 設置燈光*///設置環境光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, AmbientBuffer);//設置漫射光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseBuffer);//設置燈光位置gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionBuffer);//啟用1號燈光gl.glEnable(GL10.GL_LIGHT1);
三、實例代碼如下:

1、activity類代碼

import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.KeyEvent;public class LightOpenglActivity extends Activity {LightRender lightRender ;GLSurfaceView glView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initBitmap.init(this.getResources());lightRender = new LightRender();glView = new GLSurfaceView(this);glView.setRenderer(lightRender);setContentView(glView);}// 處理事件@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {lightRender.onKeyDown(keyCode, event);return super.onKeyDown(keyCode, event);}@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {lightRender.onKeyUp(keyCode, event);return super.onKeyUp(keyCode, event);} }
2、渲染類代碼:

import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;import android.R.bool; import android.content.Context; import android.graphics.Bitmap; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLUtils; import android.view.KeyEvent;public class LightRender implements Renderer {/*** 渲染類* author:pis*/private Context context;private int one = 0x10000;private Bitmap bitmap;public boolean mFlag ;public boolean bLight = true;//是否開啟燈光private int[] vertices;//頂點數組private int[] textCood;//紋理數組float step = 0.3f;float xrot,yrot; //旋轉float xSpeed,ySpeed; //移動速度private int[] textures = new int[1];private IntBuffer vertexBuffer; //頂點緩沖private IntBuffer textCoodBuffer; //紋理緩沖/*** 設置燈光* @param context*///環境光private float[] lightAmbient;private FloatBuffer AmbientBuffer;//漫射光private float[] lightDiffuse;private FloatBuffer diffuseBuffer;//光源位置private float[] lightPosition;private FloatBuffer positionBuffer;/*** 初始化緩沖數據*/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(textCood.length * 4 * 6);tbb.order(ByteOrder.nativeOrder());textCoodBuffer = tbb.asIntBuffer();for (int i = 0; i < 6; i++) {textCoodBuffer.put(textCood);}textCoodBuffer.position(0);//環境光ByteBuffer ambientbb = ByteBuffer.allocateDirect(lightAmbient.length * 4 * 6);ambientbb.order(ByteOrder.nativeOrder());AmbientBuffer = ambientbb.asFloatBuffer();AmbientBuffer.put(lightAmbient);AmbientBuffer.position(0);//漫射光ByteBuffer diffusebb = ByteBuffer.allocateDirect(lightDiffuse.length * 4 * 6);diffusebb.order(ByteOrder.nativeOrder());diffuseBuffer = diffusebb.asFloatBuffer();diffuseBuffer.put(lightDiffuse);diffuseBuffer.position(0);//燈光位置ByteBuffer positionbb = ByteBuffer.allocateDirect(lightPosition.length * 4 * 6);positionbb.order(ByteOrder.nativeOrder());positionBuffer = positionbb.asFloatBuffer();positionBuffer.put(lightPosition);positionBuffer.position(0);}/*** 初始化頂點、紋理、燈光數據*/private void initData(){//頂點數組vertices = new int[] { -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 };//紋理數組,貼圖時注意android中坐標與OpengGL 中定義的不同,android,y軸是向下的textCood = new int[] { 0, 0, one, 0, 0, one, one, one };//燈光lightAmbient = new float[]{0.5f,0.5f,0.5f,1.0f};lightDiffuse = new float[]{1.0f,1.0f,1.0f,1.0f};lightPosition = new float[]{0.0f,0.0f,2.0f,1.0f};}public LightRender() {initData();initBuffer();}@Overridepublic void onDrawFrame(GL10 gl) {//清除顏色和深度緩存gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glLoadIdentity();//啟用燈光gl.glEnable(GL10.GL_LIGHTING);//啟用頂點和紋理緩存gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//移動和旋轉設置gl.glTranslatef(0.0f, 0.0f, -6.0f);gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);//設置頂點和紋理,經常忘記設置,唉!gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);gl.glTexCoordPointer(2,GL10.GL_FIXED,0,textCoodBuffer);//繪制六個面,貼圖for (int i = 0; i < 6; i++) {gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);}//取消緩存,需我們自己手動gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);gl.glLoadIdentity();if (mFlag) {xrot += 0.5f;yrot += 0.5f;}if (!bLight) {gl.glDisable(GL10.GL_LIGHT1);} else {gl.glEnable(GL10.GL_LIGHT1);}}@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, 10);//觀察模型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.glClearColor(0, 0, 0, 0);//啟用陰影平滑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, textures, 0);//綁定紋理gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);//生成紋理GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, initBitmap.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);/*** 設置燈光*///設置環境光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, AmbientBuffer);//設置漫射光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseBuffer);//設置燈光位置gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionBuffer);//啟用1號燈光gl.glEnable(GL10.GL_LIGHT1); }/*** 操作鍵盤上的上、下、左、右、確認鍵進行正方體的翻轉和燈光操作* @param keyCode* @param event* @return*/public boolean onKeyDown(int keyCode, KeyEvent event){switch ( keyCode ){case KeyEvent.KEYCODE_DPAD_UP:mFlag = true;xSpeed =-step;break;case KeyEvent.KEYCODE_DPAD_DOWN:mFlag = true;xSpeed = step;break;case KeyEvent.KEYCODE_DPAD_LEFT:mFlag = true;ySpeed =-step;break;case KeyEvent.KEYCODE_DPAD_RIGHT:mFlag = true;ySpeed =step;break;case KeyEvent.KEYCODE_DPAD_CENTER:bLight = !bLight;break;}return false;}public boolean onKeyUp(int keyCode, KeyEvent event){mFlag = false;return false;} }
3、初始化bitmap類代碼

import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory;public class initBitmap {public static Bitmap bitmap;public static void init(Resources res){bitmap = BitmapFactory.decodeResource(res, R.drawable.balloons) ;} }
4、運行效果


按鍵盤中的確認鍵后效果圖:

好像搞的太黑了,其實是有圖的,認真看下把,呵呵。。

就這么多了


總結

以上是生活随笔為你收集整理的Android 中opengl es灯光效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。

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