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灯光效果实例相关推荐
- Android平台OpenGL ES图像处理(improving)
在Android平台一般使用OpenGL ES进行图像处理.在OpenGL ES中编写算法,实现效果,最后将处理的结果传输给 CPU,然后生成最终的照片. 直播中的美颜,对性能有很高的要求,无法使用特 ...
- 适用于ios和android,适用于iOS和Android的OpenGL ES差异
我有一个适用于iOS和 Android的OpenGL ES应用程序.大多数代码是很久以前由另一个人编写的,现在我必须维护它. OpenGL的使用看起来相当简单(游戏是2D,只使用纹理精灵以简单的方式) ...
- android 中的悬浮按钮,Android 中FloatingActionButton(悬浮按钮)实例详解
android 中floatingactionbutton(悬浮按钮)实例详解 一.介绍 这个类是继承自imageview的,所以对于这个控件我们可以使用imageview的所有属性 二.使用准备, ...
- Android 硬件 OpenGL ES 模拟设计概述
简介 Android 平台的 OpenGL ES 模拟由多个组件实现,它们是: 一些宿主机的 "翻译器" 库.它们实现了由 Khronos 定义的 EGL,GLES 1.1 和 G ...
- 【Qt for Android】OpenGL ES 绘制彩色立方体
Qt 内置对OpenGL ES的支持.选用Qt进行OpenGL ES的开发是很方便的,很多辅助类都已经具备.从Qt 5.0開始添加了一个QWindow类,该类既能够使用OpenGL绘制3D图形,也能够 ...
- 【Android】OpenGL ES for Android 教程1 - HelloWorld
本教程及后续教程全部参考或者转载于:OpenGL ES Tutorial for Android 本例相当于openGL的HelloWorld程序. 先贴代码,代码中的注释比较具体了. 主Activi ...
- Android中用OpenGL ES Tracer分析绘制过程
Tracer for OpenGL ES(http://developer.android.com/tools/help/gltracer.html)是Android SDK中新增加的开发工具,可逐帧 ...
- Android Studio OpenGL ES绘制三棱锥/四面体的多纹理贴图 每个面使用一张图片渲染
本文参考了王刚的<疯狂Android讲义(第3版)>P554-P559 要求:利用OpenGL ES绘制一个三棱锥,并对每个面进行纹理贴图,每个面使用不同的图片进行渲染. 环境:Andro ...
- Android下Opengl ES实现单屏幕双眼显示
http://blog.csdn.net/u011371324/article/details/68946779 默认情况下,Opengl ES使用系统提供的帧缓冲区作为绘图表面,一般情况下,如果只在 ...
最新文章
- 8大领域、6大机遇!读懂2020年AI医疗新风向
- NuGet EntityFramework 常用命令
- STM32F4 编程手册学习1_编程模型
- tensorflow 学习资料汇总
- 【codevs1316NOIP2012PJ】文化之旅,无题
- 富士康立讯精密可能仍在苹果汽车代工商候选名单中
- mysql 拼音首字母_Mysql:拼音首字母查询(超高性能)
- 小心中中国移动“短号集群网”的招
- 朗沃20140424
- 深入进货单-期初单据
- python极简讲义 pdf_Python极简讲义:一本书入门数据分析与机器学习
- python用实现FGO自动挂机战斗思路
- 中国汉字一、二级字库的汉字与unicode编码(十六进制)对照表(收藏)
- [Ant]Note of develop java with Ant
- 学生信息管理系统-GUI
- 一个软件完整的开发流程,不会程序也能自己制作手机App
- js 骂人不带脏字 (!(~+[]) + {})[--[~+][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]] 图解...
- nodejs调用wasm系列-保利威视-难度等级二
- MATLAB 8.1 R2013a license.lic 问题
- 推荐一个Mac上的软件卸载工具
热门文章
- 程序员如何接私单做SOHO一族
- VC用ADO访问数据库全攻略 作者 相生昌
- 苦逼的是怎么又有东西没记住,但我们依然每天坚持一遍、一遍又一遍指导记住为止。
- mysql router docker_MySQL Router 完全讲解
- 怎么在百度云上部署php文件,在百度云主机上部署thinkphp5.1的注意事项
- php类和对象-作用,php 类和对象
- ROS服务中自定义数据类型
- 刘小乐教授 - 生物信息学云论坛第三场报告会
- php按照二维数组某个字段排序,PHP 二维数组根据某个字段排序
- vue框架可以配合php做企业站,基于Vue和PHP打造前后端分离的通用管理系统(一)...