运动模糊

来源:互联网 发布:银行卡数据隔空采集器 编辑:程序博客网 时间:2024/04/29 12:54

运动模糊效果是指物体移动时,会拖带一个尾巴。通过颜色混合,可以实现这种效果。实现原理就是每隔一小段位移以某种alpha值重复绘制物体, 通过与前面画的物体进行颜色混合来实现模糊的效果。

混合因子选择如下:

S因子:GL_SRC_ALPHA

D因子:GL_ONE

 

实例代码如下:

 

public class MyRenderer implements Renderer {private Square square1 = null;private static final float M_PI = 3.14159265358979323846f;private static final float blur_detail = 24;private float x = -2.0f;public MyRenderer(Context ctx) {square1 = new Square(ctx, false);}@Overridepublic void onDrawFrame(GL10 gl) {gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// Replace the current matrix with the identity matrixgl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();if (x > 2.0f)x = -2.0f;x += 0.05f;gl.glPushMatrix();float ball_alpha = 1 / blur_detail;gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);gl.glTranslatef(x, 0, 0);for (int i = 0; i < blur_detail; ++i) {gl.glTranslatef(0.5f / blur_detail, 0, -0.5f / blur_detail);/* comment the following line for quick but boring linear blur */ball_alpha = (float) (Math.sin((M_PI / blur_detail) * i) / blur_detail);gl.glColor4f(1, 1, 1, ball_alpha);square1.draw(gl);}gl.glPopMatrix();}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {float ratio = (float) width / height;gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();GLU.gluPerspective(gl, 45, ratio, 0.01f, 100);GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 1, 0);gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();}@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// TODO Auto-generated method stubgl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// Enable Smooth Shading, default not really needed.gl.glShadeModel(GL10.GL_SMOOTH);gl.glEnable(GL10.GL_BLEND);gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);}}


 

原创粉丝点击