今回はOpenGLを使ってGlow表現を試してみました。
参考にしたのはこのサイト。
スクリーンショットだと分かりにくいですが、星と煙(?)はガクガク揺れてます。
Z方向にバウンドする壁を設けていないので、時間が経つと↑こんな感じになります。
少しいじるとビジュアライザに応用できそう・・・
flight404は正方形を作ってそこにテクスチャとして画像を貼付けているようですが、僕は手抜きをしてProcessingにもとから付いてるimage()メソッドを使いました。
ソースコードは以下。何も考えずに書いていったので綺麗じゃないかもしれませんが・・・
(ここからzipファイルをダウンロードできます。こっちは画像が入ってます。)
/***********************************
*
* Opengl_glow.pde
*
***********************************/
int STARS_NUM = 200;
int CORONA_NUM = 100;
float GRAVITY = 0.1;
float MAX_VELOCITY = 5;
Star[] stars = new Star[STARS_NUM];
Corona[] coronas = new Corona[CORONA_NUM];
void setup(){
size(800,400,OPENGL);
hint( ENABLE_OPENGL_4X_SMOOTH );
background(0);
PImage coronaImg=loadImage("corona.png");
for(int i=0; i<CORONA_NUM; i++){
float w=random(20,200);
float h=w;
Point pos = new Point(random(0+w,width-w), random(0+h,height-h));
PVector vel = new PVector(random(-2,2), random(-2,2));
coronas[i] = new Corona(coronaImg,pos,vel,w,h);
}
PImage starImg = loadImage("star.png");
for(int i=0; i<STARS_NUM; i++){
float w=random(1,30);
if(i%23==0) w=random(70,100);
float h=w;
Point pos = new Point(random(0+w,width-w), random(0+h,height-h));
PVector vel = new PVector(random(-6,6), random(-6,6));
stars[i]=new Star(starImg,pos,vel,w,h);
}
}
void draw(){
newFrame();
glUtil();
for(int i=0;i<CORONA_NUM; i++){
coronas[i].move();
coronas[i].bounce();
coronas[i].display();
}
for(int i=0; i<STARS_NUM; i++){
stars[i].move();
stars[i].bounce();
stars[i].display();
}
}
void newFrame(){
background(0,20);
}
/***********************************
*
* Point.pde
*
***********************************/
class Point{
float x;
float y;
float z;
Point(){ }
Point(float x,float y){
this.x = x;
this.y = y;
}
Point(float x,float y,float z){
this(x,y);
this.z = z;
}
}
/***********************************
*
* Star.pde
*
***********************************/
class Star{
PImage starImg;
Point pos = new Point();
PVector vel = new PVector();
float w;
float h;
Star(){ }
Star(PImage starImg,Point pos,PVector vel,float w,float h){
this.starImg=starImg;
this.pos = pos;
this.vel = vel;
this.w = w;
this.h = h;
}
void display(){
pushMatrix();
translate(0,0,pos.z);
image(starImg,pos.x-w/2,pos.y-h/2,w,h);
popMatrix();
}
void move(){
pos.x+=random(-5,5);
pos.y+=random(-5,5);
pos.z+=random(-10,10);
}
void bounce(){
if(pos.x+w/2>width || pos.x-w/2<0){
vel.x*= -1;
if(pos.x+w/2>width) pos.x=width-w/2;
else pos.x=w/2;
}
if(pos.y+h/2>height || pos.y-h/2<0){
vel.y*= -1;
if(pos.y+h/2>height) pos.y=height-h/2;
else pos.y=h/2;
}
}
}
/***********************************
*
* Corona.pde
*
***********************************/
class Corona extends Star{
PImage coronaImg;
Corona(){ }
Corona(PImage coronaImg, Point pos, PVector vel, float w, float h){
super(coronaImg, pos, vel, w, h);
}
}
/***********************************
*
* Util.pde
*
***********************************/
import processing.opengl.*;
import javax.media.opengl.*;
PGraphicsOpenGL pgl;
GL gl;
protected void glUtil() {
pgl = (PGraphicsOpenGL) g;
//gl = pgl.gl;
//pgl.beginGL();
gl=pgl.beginGL();
gl.glDisable(GL.GL_DEPTH_TEST); // This fixes the overlap issue
gl.glEnable(GL.GL_BLEND);// Turn on the blend mode
gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE); // Define the blend mode
pgl.endGL();
}
public void capture(char str) {
if(keyPressed && key == str) {
saveFrame("data/img-####.png");
}
}
public void vertex(PVector p) {
vertex(p.x, p.y, p.z);
}
/** Utility for PVector class
@param c1 control 1 of PVector class
@param c2 control 2 of PVector class
@param p point of PVector class
*/
public void bezierVertex(PVector c1, PVector c2, PVector p) {
bezierVertex(c1.x, c1.y, c1.z, c2.x, c2.y, c2.z, p.x, p.y, p.z);
}
public void curveVertex(PVector p) {
curveVertex(p.x, p.y, p.z);
}


0 件のコメント:
コメントを投稿