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;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        initBitmap.init(this.getResources());
        
        lightRender = new LightRender();
        glView = new GLSurfaceView(this);
        glView.setRenderer(lightRender);
        
        setContentView(glView);
    }
	// 处理事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	lightRender.onKeyDown(keyCode, event);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public 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();
		
	}

	
	@Override
	public 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);
		}
	}

	@Override
	public 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();
	}

	@Override
	public 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、运行效果


按键盘中的确认键后效果图:

好像搞的太黑了,其实是有图的,认真看下把,呵呵。。

就这么多了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android OpenGL ES 3 是 Android 平台上的三维图形渲染接口,它是基于 OpenGL ES 3.0 标准实现的。OpenGL ES 是一种面向嵌入式系统的精简版 OpenGL,它专门用于在移动设备等资源受限的环境进行实时图形渲染。 Android OpenGL ES 3 带来了很多新的功能和改进,为开发者提供了更强大的图形渲染能力。其一些重要的特性包括可编程着色器、多重采样抗锯齿、高精度着色、纹理压缩等。 通过可编程着色器,开发者可以通过编写自定义的顶点和片元着色器来实现更复杂的图形效果。这允许开发者更灵活地处理顶点和像素数据,从而实现更高质量的渲染。 多重采样抗锯齿是一种抗锯齿技术,通过在渲染过程对像素进行多次采样,最后再对采样结果进行平均,从而减少图像边缘的锯齿状边缘。这可以提供更平滑和真实感的图像。 高精度着色是一项改进,它使得在渲染过程可以使用更高精度的数据表示,从而减少了计算误差,提供更准确的渲染效果。 纹理压缩是一项优化技术,它允许图像纹理在被加载到 GPU 之前进行压缩,从而减少了纹理数据的存储空间和传输带宽。这可以提高应用程序的性能和效率。 综上所述,Android OpenGL ES 3 是一个强大的图形渲染接口,它增加了很多新功能和改进,可以帮助开发者实现更高质量、更真实感的图形效果,提高应用程序的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值