More Related Content More from Atsushi Tadokoro (20) Sbaw0910139. $ cd ~/Desktop/
$ svn checkout http://vanderlin.googlecode.com/svn/
trunk/ofxBox2d ofxBox2d
16. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxVectorMath.h" //ofxVectorMath
#include "ofxBox2d.h" //ofxBox2d
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofxBox2d box2d; //Box2D
vector <ofxBox2dCircle> circles; //
};
17. #include "testApp.h"
void testApp::setup(){
//
ofSetVerticalSync(true);
ofBackground(0, 0, 0);
//Box2D
box2d.init(); //Box2D
box2d.setGravity(0,5); // 5
box2d.createBounds(0, 0, ofGetWidth(), ofGetHeight()); //
box2d.setFPS(30); //30fps
}
void testApp::update(){
box2d.update(); //Box2D
}
18. void testApp::draw(){
//circles
for(int i=0; i<circles.size(); i++) {
circles[i].draw();
}
//Box2D
box2d.draw();
}
void testApp::keyPressed(int key){
}
void testApp::keyReleased(int key){
}
void testApp::mouseMoved(int x, int y){
}
void testApp::mouseDragged(int x, int y, int button){
}
19. void testApp::mousePressed(int x, int y, int button){
//
float r = ofRandom(10, 40); //
ofxBox2dCircle circle; //ofxBox2dCircle( )
circle.setPhysics(1.0, 0.8, 0.5); // ( )
circle.setup(box2d.getWorld(), mouseX, mouseY, r); //
circles.push_back(circle); // circles
}
void testApp::mouseReleased(int x, int y, int button){
}
void testApp::windowResized(int w, int h){
}
23. #include "CustomCircle.h"
void CustomCircle::draw()
{
float radius = getRadius(); //
glPushMatrix(); //
glTranslatef(getPosition().x, getPosition().y, 0); //
ofFill(); //
ofSetColor(31,127,255,100); // 1
ofCircle(0, 0, radius);
// 1
ofSetColor(31,127,255,200); // 2
ofCircle(0, 0, radius*0.7); // 2
glPopMatrix(); //
}
24. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxVectorMath.h" //ofxVectorMath
#include "ofxBox2d.h" //ofxBox2d
#include "CustomCircle.h" //CustomCircle
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofxBox2d box2d; //Box2D
vector <CustomCircle> circles; //CustomCircle
};
25. #include "testApp.h"
void testApp::setup(){
//
ofSetVerticalSync(true);
ofEnableAlphaBlending();
ofSetCircleResolution(64);
ofBackground(0, 0, 0);
//Box2D
box2d.init(); //Box2D
box2d.setGravity(0,5); // 5
box2d.createBounds(0, 0, ofGetWidth(), ofGetHeight()); //
box2d.setFPS(30); //30fps
}
void testApp::update(){
box2d.update(); //Box2D
}
26. void testApp::draw(){
//circles
for(int i=0; i<circles.size(); i++) {
circles[i].draw();
}
//Box2D
box2d.draw();
}
void testApp::keyPressed(int key){
// c
if (key == 'c') {
float r = ofRandom(5, 20); //
CustomCircle c; //CustomCircle
c.setPhysics(1.0, 0.8, 0.5); //
c.setup(box2d.getWorld(), mouseX, mouseY, r); //
circles.push_back(c); // circles
}
}
27. void testApp::keyReleased(int key){
}
void testApp::mouseMoved(int x, int y){
}
void testApp::mouseDragged(int x, int y, int button){
}
void testApp::mousePressed(int x, int y, int button){
}
void testApp::mouseReleased(int x, int y, int button){
}
void testApp::windowResized(int w, int h){
}
30. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxVectorMath.h" //ofxVectorMath
#include "ofxBox2d.h" //ofxBox2d
#include "CustomCircle.h" //CustomCircle
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofxBox2d box2d; //Box2D
vector <CustomCircle> circles; //CustomCircle
vector <ofxBox2dRect> rects; // ofxBox2dRect
};
31. #include "testApp.h"
void testApp::setup(){
//
ofSetVerticalSync(true);
ofEnableAlphaBlending();
ofSetCircleResolution(64);
ofBackground(0, 0, 0);
//Box2D
box2d.init(); //Box2D
box2d.setGravity(0,5); // 5
box2d.createFloor();
box2d.checkBounds(false);
box2d.setFPS(30); //30fps
//
for (int i=0; i<100; i++) { //100
ofxBox2dRect r; //ofxBox2dRect( ) r
float w = 2; // 2
float h = 2; // 2
float x = ofRandom(50, ofGetWidth()-50); // x
32. r.setPhysics(1.0, 1.2, 0.5); //
r.setup(box2d.getWorld(), x, y, w, h, true); //
rects.push_back(r); // rects
}
}
void testApp::update(){
box2d.update(); //Box2D
}
void testApp::draw(){
//circles
for(int i=0; i<circles.size(); i++) {
circles[i].draw();
}
//rects
for(int i=0; i<rects.size(); i++) {
rects[i].draw();
}
//Box2D
box2d.draw();
33. void testApp::keyPressed(int key){
// c
if (key == 'c') {
float r = ofRandom(5, 20); //
CustomCircle c; //CustomCircle
c.setPhysics(1.0, 0.8, 0.5); //
c.setup(box2d.getWorld(), mouseX, mouseY, r); //
circles.push_back(c); // circles
}
// r
if (key == 'r') {
for(int i=0; i<circles.size(); i++) {
circles[i].destroyShape();
}
circles.clear();
}
}
void testApp::keyReleased(int key){
}
void testApp::mouseMoved(int x, int y){
}
34. void testApp::mouseDragged(int x, int y, int button){
}
void testApp::mousePressed(int x, int y, int button){
}
void testApp::mouseReleased(int x, int y, int button){
}
void testApp::windowResized(int w, int h){
}
39. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
};
#endif
40. #include "testApp.h"
void testApp::setup(){
ofBackground(0,0,0);
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
}
void testApp::update(){
bool bNewFrame = false;
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
}
}
void testApp::draw(){
colorImg.draw(20,20);
}
43. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
};
44. #include "testApp.h"
void testApp::setup(){
ofBackground(0, 0, 0);
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320,240);
}
void testApp::update(){
bool bNewFrame = false;
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage = colorImg;
}
}
void testApp::draw(){
ofSetColor(0xffffff);
colorImg.draw(20,20);
grayImage.draw(360,20);
}
47. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
bool bLearnBakground;
int threshold;
};
48. #include "testApp.h"
void testApp::setup(){
ofBackground(0, 0, 0);
ofEnableAlphaBlending();
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);
bLearnBakground = true;
threshold = 100;
}
void testApp::update(){
bool bNewFrame = false;
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
49. grayBg = grayImage;
bLearnBakground = false;
}
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
}
}
void testApp::draw(){
ofNoFill();
ofSetColor(0xffffff);
colorImg.draw(20,20);
grayImage.draw(360,20);
grayBg.draw(20,280);
grayDiff.draw(360,280);
ofSetColor(0xffffff);
char reportStr[1024];
sprintf(reportStr, "threshold %i (press: +/-)", threshold);
ofDrawBitmapString(reportStr, 20, 600);
}
50. void testApp::keyPressed (int key){
switch (key){
case ' ':
bLearnBakground = true;
break;
case '+':
threshold ++;
if (threshold > 255) threshold = 255;
break;
case '-':
threshold --;
if (threshold < 0) threshold = 0;
break;
}
}
...( )...
53. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
bool bLearnBakground;
int threshold;
};
54. #include "testApp.h"
void testApp::setup(){
ofBackground(0, 0, 0);
ofEnableAlphaBlending();
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);
bLearnBakground = true;
threshold = 100;
}
void testApp::update(){
bool bNewFrame = false;
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage = colorImg;
if (bLearnBakground == true){
55. bLearnBakground = false;
}
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);
}
}
void testApp::draw(){
ofNoFill();
ofSetColor(0xffffff);
colorImg.draw(20,20);
grayImage.draw(360,20);
grayBg.draw(20,280);
grayDiff.draw(360,280);
for (int i = 0; i < contourFinder.nBlobs; i++){
contourFinder.blobs[i].draw(360,540);
}
ofSetColor(0xffffff);
char reportStr[1024];
sprintf(reportStr, "threshold %i (press: +/-)nnum blobs found %i", threshold,
contourFinder.nBlobs);
ofDrawBitmapString(reportStr, 20, 600);
}
56. void testApp::keyPressed (int key){
switch (key){
case ' ':
bLearnBakground = true;
break;
case '+':
threshold ++;
if (threshold > 255) threshold = 255;
break;
case '-':
threshold --;
if (threshold < 0) threshold = 0;
break;
}
}
...( )...
59. #ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
bool bLearnBakground;
int threshold;
};
60. #include "testApp.h"
void testApp::setup(){
ofBackground(0, 0, 0);
ofEnableAlphaBlending();
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);
bLearnBakground = true;
threshold = 100;
}
void testApp::update(){
bool bNewFrame = false;
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage = colorImg;
if (bLearnBakground == true){
grayBg = grayImage;
bLearnBakground = false;
61. grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);
}
}
void testApp::draw(){
ofFill();
ofSetColor(0, 31, 255, 200);
for( int i=0; i<(int)contourFinder.blobs.size(); i++ ) {
ofBeginShape();
for( int j=0; j<contourFinder.blobs[i].nPts; j++ ) {
ofVertex( contourFinder.blobs[i].pts[j].x * 4, contourFinder.blobs[i].pts[j].y * 4 );
}
ofEndShape();
}
ofSetColor(0xffffff);
char reportStr[1024];
sprintf(reportStr, "threshold %i (press: +/-)nnum blobs found %i", threshold,
contourFinder.nBlobs);
ofDrawBitmapString(reportStr, 20, 10);
}
62. void testApp::keyPressed (int key){
switch (key){
case ' ':
bLearnBakground = true;
break;
case '+':
threshold ++;
if (threshold > 255) threshold = 255;
break;
case '-':
threshold --;
if (threshold < 0) threshold = 0;
break;
}
}
...( )...