openGL es2.0パーティクルシステム方式の球爆発

2630 ワード

一、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=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);
}