Android 中opengl es灯光效果实例
生活随笔
收集整理的這篇文章主要介紹了
Android 中opengl es灯光效果实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、還是要準備一張圖片,放在res/drawable中
二、燈光效果代碼:
/*** 設(shè)置燈光*///設(shè)置環(huán)境光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, AmbientBuffer);//設(shè)置漫射光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseBuffer);//設(shè)置燈光位置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;//頂點數(shù)組private int[] textCood;//紋理數(shù)組float step = 0.3f;float xrot,yrot; //旋轉(zhuǎn)float xSpeed,ySpeed; //移動速度private int[] textures = new int[1];private IntBuffer vertexBuffer; //頂點緩沖private IntBuffer textCoodBuffer; //紋理緩沖/*** 設(shè)置燈光* @param context*///環(huán)境光private float[] lightAmbient;private FloatBuffer AmbientBuffer;//漫射光private float[] lightDiffuse;private FloatBuffer diffuseBuffer;//光源位置private float[] lightPosition;private FloatBuffer positionBuffer;/*** 初始化緩沖數(shù)據(jù)*/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);//環(huán)境光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);}/*** 初始化頂點、紋理、燈光數(shù)據(jù)*/private void initData(){//頂點數(shù)組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 };//紋理數(shù)組,貼圖時注意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);//移動和旋轉(zhuǎn)設(shè)置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);//設(shè)置頂點和紋理,經(jīng)常忘記設(shè)置,唉!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();//視圖大小設(shè)置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);/*** 設(shè)置紋理*///啟用紋理gl.glEnable(GL10.GL_TEXTURE_2D);//創(chuàng)建紋理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);/*** 設(shè)置燈光*///設(shè)置環(huán)境光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, AmbientBuffer);//設(shè)置漫射光gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseBuffer);//設(shè)置燈光位置gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionBuffer);//啟用1號燈光gl.glEnable(GL10.GL_LIGHT1); }/*** 操作鍵盤上的上、下、左、右、確認鍵進行正方體的翻轉(zhuǎn)和燈光操作* @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、運行效果
按鍵盤中的確認鍵后效果圖:
好像搞的太黑了,其實是有圖的,認真看下把,呵呵。。
就這么多了
總結(jié)
以上是生活随笔為你收集整理的Android 中opengl es灯光效果实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js change事件 获取新值和旧值_
- 下一篇: 使用李天平代码生成器中分页存储过程的问题