More Related Content
Similar to openFrameworks基礎 たくさんの図形を動かす 静的配列と動的配列 - 芸大グラフィックスプログラミング演習B
Similar to openFrameworks基礎 たくさんの図形を動かす 静的配列と動的配列 - 芸大グラフィックスプログラミング演習B (20)
More from Atsushi Tadokoro
More from Atsushi Tadokoro (20)
openFrameworks基礎 たくさんの図形を動かす 静的配列と動的配列 - 芸大グラフィックスプログラミング演習B
- 7. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofBackgroundHex(0x000000); //背景色を黒に
pos.x = ofGetWidth()/2;
/初期位置を画面の中心に:x
/
pos.y = ofGetHeight()/2; //初期位置を画面の中心に:y
}
void testApp::update(){
pos.x += 4.0; //位置を更新:x
pos.y += 3.0; //位置を更新:y
//画面の端まできたら、反対方向から出現
if (pos.x > ofGetWidth()) {pos.x = 0;}
if (pos.x < 0) {pos.x = ofGetWidth();}
if (pos.y > ofGetHeight()) {pos.y = 0;}
if (pos.y < 0) {pos.y = ofGetHeight();}
}
void testApp::draw(){
ofSetHexColor(0x3399ff); //描画する色を指定
ofCircle(pos.x, pos.y, 20); //更新された値を利用して円を描く
}
- 11. ‣
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
ofVec2f pos; //位置を記録するための変数(正確にはオブジェクト)
ofVec2f vel; //パーティクルの速度(Velocity)
};
- 12. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofBackgroundHex(0x000000); //背景色を黒に
//初期位置を画面の中心に
pos.x = ofGetWidth()/2;
pos.y = ofGetHeight()/2;
//速度をランダムに決定
vel.x = ofRandom(-10, 10);
vel.y = ofRandom(-10, 10);
}
void testApp::update(){
//位置を更新する
pos += vel;
//画面の端に来たら反対側へ
if (pos.x > ofGetWidth()) pos.x = 0; //右
if (pos.x < 0) pos.x = ofGetWidth(); //左
if (pos.y > ofGetHeight()) pos.y = 0; //下
if (pos.y < 0) pos.y = ofGetHeight(); //上
}
- 16. ‣
‣
‣
‣
( )
pos[0]
pos[1]
pos[2] NUM
- 17. ‣
(pos) (vel)
pos[0] vel[0]
pos[1] vel[1]
pos[2] vel[2]
- 20. ‣
‣
for (int i = 0; i < 100; i++) { //100回くりかえし
//速度をランダムに決定
vel[i].x = ofRandom(-10, 10);
vel[i].y = ofRandom(-10, 10);
}
‣
- 22. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofBackgroundHex(0x000000); //背景色を黒に
for (int i = 0; i < NUM; i++) { //NUM回くりかえし
//初期位置を画面の中心に
pos[i].x = ofGetWidth()/2;
pos[i].y = ofGetHeight()/2;
//速度をランダムに決定
vel[i].x = ofRandom(-10, 10);
vel[i].y = ofRandom(-10, 10);
}
}
void testApp::update(){
for (int i = 0; i < NUM; i++) { //NUM回くりかえし
//位置を更新する
pos[i] += vel[i];
//画面の端に来たら反対側へ
if (pos[i].x > ofGetWidth()) pos[i].x = 0; //右
if (pos[i].x < 0) pos[i].x = ofGetWidth(); //左
if (pos[i].y > ofGetHeight()) pos[i].y = 0; //下
if (pos[i].y < 0) pos[i].y = ofGetHeight(); //上
}
}
- 28. ‣
‣
‣
particles.push_back(p);
‣
particles.pop_back();
‣
particles.clear();
- 30. ‣
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void mouseDragged(int x, int y, int button);
void keyReleased(int key);
vector<ofVec2f> pos; //位置の可変配列
vector<ofVec2f> vel; //パーティクルの速度の可変配列
};
- 31. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofEnableBlendMode(OF_BLENDMODE_ADD); //色を加算合成に
ofBackgroundHex(0x000000); //背景色を黒に
}
void testApp::update(){
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
//位置を更新する
pos[i] += vel[i];
//画面の端に来たら反対側へ
if (pos[i].x > ofGetWidth()) pos[i].x = 0; //右
if (pos[i].x < 0) pos[i].x = ofGetWidth(); //左
if (pos[i].y > ofGetHeight()) pos[i].y = 0; //下
if (pos[i].y < 0) pos[i].y = ofGetHeight(); //上
}
}
- 32. ‣
void testApp::draw(){
ofSetHexColor(0x3399ff); //描画する色を指定
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
ofCircle(pos[i].x, pos[i].y, 4); //更新された値を利用して円を描く
}
//現在の数とフレームレートを表示
string log;
log = "particle num = " + ofToString(pos.size(), 0) + "n";
log += "framerate = " + ofToString(ofGetFrameRate(), 4);
ofSetHexColor(0xffffff);
ofDrawBitmapString(log, 20, 20);
}
void testApp::mouseDragged(int x, int y, int button) {
ofVec2f p; //位置の変数を一時的に生成
p.set(x, y); //位置をマウスをクリックした場所に設定
pos.push_back(p); //可変配列の末尾に位置を追加
ofVec2f v; //速度の変数を一時的に生成
v.set(ofRandom(-1, 1), ofRandom(-1, 1)); //速度を設定
vel.push_back(v); //可変配列の末尾に速度を追加
}
- 33. ‣
void testApp::keyReleased(int key) {
if (key == 'c') { //「c」キーを押したら
pos.clear(); //位置の配列を初期化
vel.clear(); //速度の配列を初期化
}
if (key == 'f') { //「f」キーを押したら
ofToggleFullscreen(); //フルスクリーンon/off
}
}
- 37. ‣
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void mouseDragged(int x, int y, int button);
void keyReleased(int key);
vector<ofVec2f> pos; //位置の可変配列
vector<ofVec2f> vel; //パーティクルの速度の可変配列
vector<ofColor> col; //色の可変配列
};
- 38. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofEnableBlendMode(OF_BLENDMODE_ADD); //色を加算合成に
ofBackgroundHex(0x000000); //背景色を黒に
}
void testApp::update(){
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
//位置を更新する
pos[i] += vel[i];
//画面の端に来たら反対側へ
if (pos[i].x > ofGetWidth()) pos[i].x = 0; //右
if (pos[i].x < 0) pos[i].x = ofGetWidth(); //左
if (pos[i].y > ofGetHeight()) pos[i].y = 0; //下
if (pos[i].y < 0) pos[i].y = ofGetHeight(); //上
}
}
- 39. ‣
void testApp::draw(){
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
ofSetColor(col[i].r, col[i].g, col[i].b);//ランダムな色で描画
ofCircle(pos[i].x, pos[i].y, 4); //更新された値を利用して円を描く
}
//現在の数とフレームレートを表示
string log;
log = "particle num = " + ofToString(pos.size(), 0) + "n";
log += "framerate = " + ofToString(ofGetFrameRate(), 4);
ofSetHexColor(0xffffff);
ofDrawBitmapString(log, 20, 20);
}
- 40. ‣
void testApp::mouseDragged(int x, int y, int button) {
ofVec2f p; //位置の変数を一時的に生成
p.set(x, y); //位置をマウスをクリックした場所に設定
pos.push_back(p); //可変配列の末尾に位置を追加
ofVec2f v; //速度の変数を一時的に生成
v.set(ofRandom(-1, 1), ofRandom(-1, 1)); //速度を設定
vel.push_back(v); //可変配列の末尾に速度を追加
ofColor c; //色を一時的に生成
c.r = ofRandom(255); //red成分をランダムに
c.g = ofRandom(255); //green成分をランダムに
c.b = ofRandom(255); //blue成分をランダムに
col.push_back(c);
}
void testApp::keyReleased(int key) {
if (key == 'c') { //「c」キーを押したら
pos.clear(); //位置の配列を初期化
vel.clear(); //速度の配列を初期化
}
if (key == 'f') { //「f」キーを押したら
ofToggleFullscreen(); //フルスクリーンon/off
}
}
- 45. ‣
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void mouseDragged(int x, int y, int button);
void keyReleased(int key);
vector<ofVec3f> pos; //位置の可変配列(3D)
vector<ofVec3f> vel; //パーティクルの速度の可変配列(3D)
};
- 46. ‣
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
//更新を秒間60コマに(fps)
ofEnableBlendMode(OF_BLENDMODE_ADD); //色を加算合成に
ofBackgroundHex(0x000000); //背景色を黒に
}
void testApp::update(){
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
//位置を更新する
pos[i] += vel[i];
}
}
void testApp::draw(){
glEnable(GL_DEPTH_TEST); //奥行の判定をする
ofSetHexColor(0x3399ff);
for (int i = 0; i < pos.size(); i++) { //NUM回くりかえし
ofPushMatrix();
ofTranslate(pos[i]); //位置を設定
ofCircle(0, 0, 4); //更新された値を利用して円を描く
ofPopMatrix();
}
glDisable(GL_DEPTH_TEST);
- 47. ‣
//現在の数とフレームレートを表示
string log;
log = "particle num = " + ofToString(pos.size(), 0) + "n";
log += "framerate = " + ofToString(ofGetFrameRate(), 4);
ofSetHexColor(0xffffff);
ofDrawBitmapString(log, 20, 20);
}
void testApp::mouseDragged(int x, int y, int button) {
ofVec3f p; //位置の変数を一時的に生成
p.set(x, y); //位置をマウスをクリックした場所に設定
pos.push_back(p); //可変配列の末尾に位置を追加
ofVec3f v; //速度の変数を一時的に生成
v.set(ofRandom(-1, 1), ofRandom(-1, 1), ofRandom(-1, 1));//速度を設定
vel.push_back(v); //可変配列の末尾に速度を追加
}
- 48. ‣
void testApp::keyReleased(int key) {
if (key == 'c') { //「c」キーを押したら
pos.clear(); //位置の配列を初期化
vel.clear(); //速度の配列を初期化
}
if (key == 'f') { //「f」キーを押したら
ofToggleFullscreen(); //フルスクリーンon/off
}
}