Skip to content

Instantly share code, notes, and snippets.

@komuro-hiraku
Created September 13, 2012 01:20
Show Gist options
  • Save komuro-hiraku/3711197 to your computer and use it in GitHub Desktop.
Save komuro-hiraku/3711197 to your computer and use it in GitHub Desktop.
Particle
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import jp.classmethod.komuro.openglsurfaceview.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.util.Log;
public class ParticleRenderer implements Renderer {
// パーティクルを100個生成する
public static final int PARTICLE_COUNT = 100;
// コンテキスト
private Context mContext;
private int mWidth;
private int mHeight;
private Random rand = new Random(System.currentTimeMillis());
// パーティクル
private Particle[] mParticles = new Particle[PARTICLE_COUNT];
private int mParticleTexture;
public ParticleRenderer(Context context) {
this.mContext = context;
for (int i = 0; i < PARTICLE_COUNT; i++) {
mParticles[i] = new Particle();
mParticles[i].mX = rand.nextFloat() - 0.5f;
mParticles[i].mY = rand.nextFloat() - 0.5f;
mParticles[i].mSize = rand.nextFloat() * 0.5f;
}
}
public void renderMain(GL10 gl) {
// パーティクルをすべて描画する
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
Particle[] particles = mParticles;
for (int i = 0; i < PARTICLE_COUNT; i++) {
particles[i].draw(gl, mParticleTexture);
}
gl.glDisable(GL10.GL_BLEND);
}
public void onDrawFrame(GL10 gl) {
gl.glViewport(0, 0, mWidth, mHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, 0.5f, -0.5f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 背景を黒にする
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderMain(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
this.mWidth = width;
this.mHeight = height;
// テクスチャをロードする
this.mParticleTexture = loadTexture(gl, mContext.getResources(), R.drawable.ic_launcher);
}
public int loadTexture(GL10 gl, Resources resource, int drawable) {
gl.glEnable(GL10.GL_TEXTURE_2D);
int[] buffers = new int[1];
gl.glGenTextures(1, buffers, 0);
int texture = buffers[0];
Bitmap bitmap = BitmapFactory.decodeResource(resource, drawable);
// テクスチャ情報の設定
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Linearアルゴリズムを選択
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Bitmapを破棄
bitmap.recycle();
return texture;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO 自動生成されたメソッド・スタブ
}
public class Particle {
public float mX;
public float mY;
public float mSize;
public Particle() {
this.mX = 0.0f;
this.mY = 0.0f;
this.mSize = 1.0f;
}
public void draw(GL10 gl, int texture) {
float[] uv = {
0.0f, 0.0f, //左上
0.0f, 1.0f, //左下
1.0f, 0.0f, //右上
1.0f, 1.0f, //右下
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(uv.length * 4); // floatサイズ
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer fb = byteBuffer.asFloatBuffer();
fb.put(uv);
fb.position(0);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, fb);
gl.glColor4f(1.0f, 0f, 0.5f, 0.8f);
drawQuad(gl, mX, mY, mSize, mSize);
}
/**
* 描画メソッド
* @param gl
* @param x
* @param y
* @param width
* @param height
*/
public void drawQuad(GL10 gl, float x, float y, float width, float height) {
Log.d("HOGE", "x : " + x + ", y : " + y + ", width : " + width + ", height : " + height);
// float left = ( (float)x / (float)mWidth) * 2.0f - 1.0f;
// float top = ( (float)y / (float)mHeight) * 2.0f - 1.0f;
//
// float right = left + ((float)width / mWidth) * 2.0f;
// float bottom = top + ((float)height / mHeight) * 2.0f;
float left = x;
float top = y;
float right = left + width;
float bottom = top + height;
top = -top;
bottom = - bottom;
float[] positions = {
left, top, 0.0f,
left, bottom, 0.0f,
right, top, 0.0f,
right, bottom, 0.0f,
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(positions.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer fb = byteBuffer.asFloatBuffer();
fb.put(positions);
fb.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, fb);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment