トップページ > 空即是色 > 2007年の記事 > Particle Test Built with Processing
空即是色 Particle Test Built with Processing
2007年 11月18日
JOGL'sを使用してパーティクルを描画。
JOGL'sについてはこの辺を参考にした。
- all manner of distractions » Blog Archive » Mini-tutorial: Additive Blending
- Processing 1.0 (BETA) - opengl + PImage + glTexImage2D
- Processing 1.0 (BETA) - shaders in processing
applet化する方法が分からないので、一枚ずつJPEGで書き出してffmpegでまとめてみた。
ffmpegの使い方は以下を参考。
以下、ソース。
Particle Test
import javax.media.opengl.*; import processing.opengl.*; import java.nio.*;
//JOGL's PGraphicsOpenGL pgl; GL gl;
//constant numbers for particle float DAMPING_VELOCITY = 0.98; float GRAVITY = 0; int AGE_MAX = 30; int NUM_GENERATION_AT_ONE_LOOP = 30;
//particle list ArrayList particles; ArrayList clearParticles;
//texture PImage texImage; int[] texID;
//screen capture PImage cp; boolean isCap = false; int cpTimer = 0;
void setup(){ int i;
size(480, 360, OPENGL); background(0); frameRate(30); noSmooth();
//initialize JOGL's pgl = (PGraphicsOpenGL) g; gl = pgl.gl;
randomSeed(int(random(1,1000)));
//initialize particle list particles = new ArrayList(); clearParticles = new ArrayList();
//load image for texture texImage = loadImage("p.png");
//generate texture and bind texture pgl.beginGL(); gl.glEnable(GL.GL_TEXTURE_2D); texID = new int[1]; gl.glGenTextures(1,texID,0); gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]); gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, 4, texImage.width, texImage.height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(texImage.pixels) ); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); pgl.endGL();
//initialize image for screen capture cp = createImage(width,height,RGB); }
void draw(){ int i; Particle p; Iterator it;
background(0);
//generate for(i=0; i<NUM_GENERATION_AT_ONE_LOOP; i++){ if(!mousePressed){ p = new Particle(mouseX, mouseY, random(-5,5), random(-5,5)); } else{ p = new Particle(mouseX, mouseY, random(-15,15), random(-15,15)); } particles.add(p); }
//brend func pgl.beginGL(); gl.glDisable(GL.GL_DEPTH_TEST); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); pgl.endGL();
//update and draw clearParticles.clear(); it = particles.iterator(); while(it.hasNext()){ p = (Particle)it.next(); p.update(); p.drawMe(); if(p.age>AGE_MAX){ clearParticles.add(p); } }
//clear if(clearParticles.size() != 0){ it = clearParticles.iterator(); while(it.hasNext()){ p = (Particle)it.next(); particles.remove(p); } }
// screen capture if(isCap){ cp = get(); cp.save("cap/cp_"+cpTimer+".jpg"); cpTimer++; } }
void keyPressed(){ if(key=='s'){ isCap = true; }else if(key=='a'){ isCap = false; } }
///////////////////Color4f/////////////////// class Color4f { float r, g, b, a; public Color4f(){ r = g = b = a = 0.0; } public Color4f(float inR, float inG, float inB, float inA){ r = inR; g = inG; b = inB; a = inA; } }
///////////////////Particle/////////////////// class Particle { float x, y; float vx, vy; float size = 100; int age = 0; Color4f c;
public Particle(float inX, float inY, float inVx, float inVy){ x = inX; y = inY; vx = inVx; vy = inVy; age = 0; c = new Color4f(random(0,1), random(0,1), random(0,1), 1); }
void drawMe(){ float helfSize = size/2; float d = 1.0-(float)age/AGE_MAX; c.a = d;
pgl.beginGL(); gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]); gl.glColor4f(c.r, c.g, c.b, c.a); gl.glBegin(GL.GL_QUADS); gl.glTexCoord2f(0,0); gl.glVertex2f(x-helfSize, y-helfSize); gl.glTexCoord2f(1,0); gl.glVertex2f(x+helfSize, y-helfSize); gl.glTexCoord2f(1,1); gl.glVertex2f(x+helfSize, y+helfSize); gl.glTexCoord2f(0,1); gl.glVertex2f(x-helfSize, y+helfSize); gl.glEnd(); pgl.endGL(); }
void update(){ vy += GRAVITY; x += vx; y += vy; vx *= DAMPING_VELOCITY; vy *= DAMPING_VELOCITY; age++; } }
トラックバック
この記事のトラックバックURL:http://null-null.net/mt/mt-tb.cgi/491