openGL es2.0 粒子系统方式之球体爆炸

一、Java代码:

package com.gzdxid.particles;

import java.nio.FloatBuffer;

import com.gzdxid.utils.BufferUtil;
import com.gzdxid.utils.MatrixState;

import android.opengl.GLES20;

public class GrainShooter {

	int mProgram;
	int muMVPMatrixHandle;
	int uPointSizeHandle;
	int uColorHandle;
	int uTimeHandle;
	int maVelocityHandle;

	private FloatBuffer mVelocityBuffer;

	private int vCount;
	private float pointSize;
	private float timeLive = 0;
	private long timeStamp = 0;

	public GrainShooter(float pointSize, int pointCount, int mProgram) {
		// TODO Auto-generated constructor stub
		this.vCount = pointCount;
		this.pointSize = pointSize;
		initVertex();
		initShader(mProgram);
	}

	private void initVertex() {
		float []velocity=new float[vCount*3];
		for(int i=0;i<vCount;i++){
			double hAngle=2*Math.random()*Math.PI;
			double vAngle=0.35*Math.random()*Math.PI+0.15*Math.PI;
			final double vTotal=1+1*Math.random();
			double vx=vTotal*Math.cos(vAngle)*Math.sin(hAngle);
			double vy=vTotal*Math.sin(vAngle);
			double vz=vTotal*Math.cos(vAngle)*Math.cos(hAngle);
			velocity[i*3]=(float) vx;
			velocity[i*3+1]=(float) vy;
			velocity[i*3+2]=(float) vz;
		}
		mVelocityBuffer=BufferUtil.getFloatBuffer(velocity);
	}

	private void initShader(int mProgram) {
		this.mProgram=mProgram;
		muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
		uPointSizeHandle = GLES20.glGetUniformLocation(mProgram, "uPointSize");
		uColorHandle = GLES20.glGetUniformLocation(mProgram, "uColor");
		uTimeHandle = GLES20.glGetUniformLocation(mProgram, "uTime");
		maVelocityHandle = GLES20.glGetAttribLocation(mProgram, "aVelocity");
	}

	public void drawSelf() {
		long currTimeStamp=System.nanoTime()/1000000;
		if(currTimeStamp-timeStamp>=5){
			timeLive+=0.02f;
			timeStamp=currTimeStamp;
		}
		GLES20.glUseProgram(mProgram);
		GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
		GLES20.glUniform1f(uPointSizeHandle, pointSize);
		GLES20.glUniform1f(uTimeHandle, timeLive);
		GLES20.glUniform3fv(uColorHandle, 1, new float[]{1,0,0}, 0);
		GLES20.glVertexAttribPointer(maVelocityHandle, 3, GLES20.GL_FLOAT, false, 3*4, mVelocityBuffer);
		GLES20.glEnableVertexAttribArray(maVelocityHandle);
		GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vCount);
	}
}
二、定点着色器:

uniform mat4 uMVPMatrix;
uniform float uPointSize;
uniform float uTime;
attribute vec3 aVelocity;
void main()
{
   float currTime=mod(uTime,5);
   float px=aVelocity.x*currTime;
   float py=aVelocity.y*currTime-0.5*1.0*currTime*currTime;
   float pz=aVelocity.z*currTime;
   gl_Position=uMVPMatrix*vec4(px,py,pz,1);
   gl_PointSize=uPointSize;
}

三、片源着色器:

precision mediump float;
uniform vec3 uColor;
void main()
{
  gl_FragColor=vec4(uColor,1.0);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值