Skip to content

Instantly share code, notes, and snippets.

@gatana
Created May 1, 2016 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatana/42bf3584dc856bb2e33347be0eb89283 to your computer and use it in GitHub Desktop.
Save gatana/42bf3584dc856bb2e33347be0eb89283 to your computer and use it in GitHub Desktop.
#include "ofApp.h"
//pointer and video variable info
int vidX = 320;
int vidY = 240;
int pointerX = 0;
int pointerY = 0;
int pointerRad = 20;
//array for images
int i = 0;
//used within _i
int _i = 0;
ofVideoPlayer mov1;
//eye move variables
int w, h, X, Y, Z, midX, midY, avX, avX2, avatarY, size;
//--------------------------------------------------------------
void ofApp::setup(){
//particle system
gui.setup();
//eye mvmt
w = ofGetWidth();
h = ofGetHeight();
midX = ofGetWidth()/2;
midY = ofGetHeight()/2;
avX = midX;
avX2 = midX;
avatarY = midY;
vidGrabber.setVerbose(true);
vidGrabber.setup(vidX,vidY); // setup live video grabbing
colorImg.allocate(vidX,vidY); // color image display
grayImage.allocate(vidX,vidY); // grayscale display
grayBg.allocate(vidX,vidY); // contour image
grayDiff.allocate(vidX,vidY); // b/w differencing image
bLearnBakground = true;
threshold = 80;
myFont.load("stars.ttf",45);
miniStars.load("stars.ttf",20);
stars.load("stars.jpg");
eyes.load("eyes.png");
//shooting star:
//allocate our fbos.
//providing the dimensions and the format for the,
rgbaFbo.allocate(400, 400, GL_RGBA); // with alpha, 8 bits red, 8 bits green, 8 bits blue, 8 bits alpha, from 0 to 255 in 256 steps
#ifdef TARGET_OPENGLES
rgbaFboFloat.allocate(400, 400, GL_RGBA ); // with alpha, 32 bits red, 32 bits green, 32 bits blue, 32 bits alpha, from 0 to 1 in 'infinite' steps
ofLogWarning("ofApp") << "GL_RGBA32F_ARB is not available for OPENGLES. Using RGBA.";
#endif
rgbaFbo.begin();
ofClear(255,255,255, 0);
rgbaFbo.end();
//snow
ofSetVerticalSync(true);
// billboard particles
for (int i=0; i<NUM_BILLBOARDS; i++) {
pos[i].x = ofRandomWidth();
pos[i].y = ofRandomHeight();
vel[i].x = ofRandomf();
vel[i].y = ofRandomf();
home[i] = pos[i];
pointSizes[i] = ofRandom(2, 40);
rotations[i] = ofRandom(0, 360);
}
// set the vertex data
vbo.setVertexData(pos, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);
if(ofIsGLProgrammableRenderer()){
shader.load("shaderGL3/Billboard");
}else{
shader.load("shaderGL2/Billboard");
}
ofDisableArbTex();
texture.load("snow.png");
// we are getting the location of the point size attribute
// we then set the pointSizes to the vertex attritbute
// we then bind and then enable
shader.begin();
int pointAttLoc = shader.getAttributeLocation("pointSize");
vbo.setAttributeData(pointAttLoc, pointSizes, 1, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);
// rotate the snow based on the velocity
int angleLoc = shader.getAttributeLocation("angle");
vbo.setAttributeData(angleLoc, rotations, 1, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);
shader.end();
}
//snow end
//--------------------------------------------------------------
void ofApp::update(){
//shootingStar
ofEnableAlphaBlending();
//lets draw some graphics into our two fbos
rgbaFbo.begin();
rgbaFbo.end();
//snow
ofVec2f mouse(ofGetMouseX(), ofGetMouseY());
ofVec2f mouseVec(ofGetPreviousMouseX()-ofGetMouseX(), ofGetPreviousMouseY()-ofGetMouseY());
mouseVec.limit(10.0);
for (int i=0; i<NUM_BILLBOARDS; i++) {
ofSeedRandom(i);
if(mouse.distance(pos[i]) < ofRandom(100, 200)) {
vel[i] -= mouseVec;
}
pos[i] += vel[i];
vel[i] *= 0.84f;
if(pos[i].x < 0) pos[i].x = ofGetWidth();
if(pos[i].x > ofGetWidth()) pos[i].x = 0;
if(pos[i].y < 0) pos[i].y = ofGetHeight();
if(pos[i].y > ofGetHeight()) pos[i].y = 0;
ofVec2f center(ofGetWidth()/2, ofGetHeight()/2);
ofVec2f frc = home[i] - pos[i];
if(frc.length() > 20.0) {
frc.normalize();
frc *= 0.84;
vel[i] += frc;
}
// get the 2d heading
float angle = (float)atan2(-vel[i].y, vel[i].x) + PI;
rotations[i] = (angle * -1.0);
}
//snow end
bool bNewFrame = false;
vidGrabber.update();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){ // if we have a new frame
colorImg.setFromPixels(vidGrabber.getPixels());
grayImage = colorImg;
if (bLearnBakground == true){ // if we hit space, this grabs a "control" image
grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
bLearnBakground = false;
}
// take the abs value of the difference between background and incoming and then threshold:
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
// find max 10 blobs with no holes in them
contourFinder.findContours(grayDiff, 20, (vidX*vidY)/3, 10, false); // false == dont find holes in our blobs
}
}
//--------------------------------------------------------------
void ofApp::drawFboTest(){
fadeAmnt = 40;
if(ofGetKeyPressed('1')){
fadeAmnt = 1;
}else if(ofGetKeyPressed('2')){
fadeAmnt = 5;
}else if(ofGetKeyPressed('3')){
fadeAmnt = 15;
}
//1 - Fade Fbo
//
//this is where we fade the fbo
//by drawing a rectangle the size of the fbo with a small alpha value, we can slowly fade the current contents of the fbo.
ofFill();
ofSetColor(255,255,255, fadeAmnt);
ofDrawRectangle(0,0,400,400);
//
// //2 - Draw graphics
//
ofNoFill();
ofSetColor(255,255,255);
//RECTANGLE
//we move a line across the screen based on the time
//the %400 makes the number stay in the 0-400 range.
int shiftX = (ofGetElapsedTimeMillis() / 8 ) % 400;
ofDrawRectangle(rgbaFbo.getHeight()-30, shiftX, 3, 30);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == OF_KEY_RIGHT){
_i++;
}
if (key == OF_KEY_LEFT){
_i--;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
avX = x;
avX2 = x;
avatarY = y;
size = 90;
}
void eyees() {
// // dialate pupils
// if (size < 70){
// size++;
// } else if (size >= 70) {
// size--;
// }
// collision, keep pupils inside eyes
if (avX < 500 ) {
avX = avX - 350;
avX= 500;
}
if (avX2 < 500) {
avX2 = avX2 - 350;
avX2 = 540;
}
}
void ofApp::draw(){
stars.draw(0,0);
// loop pages, back and forth
if (_i < 0) {
_i = 10;
}
if (_i > 10){
_i = 0;
}
switch(_i) {
case 0:
myFont.drawString("A POEM BY \n \n AE HOUSMAN", 50, 150);
break;
case 1:
miniStars.drawString ("THE STARS I HAVE SEEN THEM FALL ", 40, 90);
//blink
eyees();
eyes.draw(30, 60);
//(x, y, ,radius)
ofDrawCircle(avX - 40 ,230, 10);
ofDrawCircle(avX2 + 40,250, 10);
//ofSetColor(255, 180, 0, alpha);
// ofDrawCircle(445, 230, 10);
// ofDrawCircle(540, 250, 10);
if (ofGetMousePressed() == true){
alpha = 0;
} else {
alpha = 255;
break;
case 2:
miniStars.drawString("BUT WHEN THEY DROP AND DIE", 40, 90);
//shooting star
rgbaFbo.draw(100,0);
break;
case 3:
miniStars.drawString("NO STAR IS LOST AT ALL", 40, 90);
break;
case 4:
miniStars.drawString("FROM ALL THE STAR SOWN SKY", 40, 90);
// gui.draw();
break;
case 5:
miniStars.drawString("THE TOIL OF ALL THAT BE", 40, 90);
break;
case 6:
miniStars.drawString("HELPS NOT THE PRIMAL FAULT", 40, 90);
if (contourFinder.nBlobs > 0) { // if any detected TODO: go w largest
pointerX = ofMap(contourFinder.blobs[0].boundingRect.x, vidX, 0, 0, ofGetWidth(), true);
pointerY = ofMap(contourFinder.blobs[0].boundingRect.y, 0, vidY, 0, ofGetHeight(), true);
//ofDrawCircle(pointerX, pointerY, pointerRad);
ofDrawRectangle(0, 500, pointerY, 1200);
// // grayImage.draw(vidX + 40, 20);
// grayDiff.draw(vidX + 40, vidY*2 + 40);
contourFinder.draw(vidX*2 + 60, vidY*2 + 40);
break;
case 7:
miniStars.drawString("IT RAINS INTO THE SEA", 40, 90);
break;
case 8:
miniStars.drawString("AND STILL THE SEA IS SALT", 40, 90);
shader.begin();
ofEnablePointSprites();
texture.getTexture().bind();
vbo.updateVertexData(pos, NUM_BILLBOARDS);
// rotate the snow based on the velocity
int angleLoc = shader.getAttributeLocation("angle");
vbo.updateAttributeData(angleLoc, rotations, NUM_BILLBOARDS);
vbo.draw(GL_POINTS, 0, NUM_BILLBOARDS);
texture.getTexture().unbind();
ofDisablePointSprites();
shader.end();
}
break;
case 9:
myFont.drawString("INTERPRETATION AND \n FONT BY ANA SOFIA REMIS", 80, 620);
}
break;
// case 10:
// myFont.drawString("INTERPRETATION AND \n FONT BY ANA SOFIA REMIS", 90, 90);
// break;
}
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
ofPoint pt;
pt.set(x,y);
//x1 y1 x2 y2
ofDrawLine(mouseX,mouseY,x,y);
// line.addVertex(pt);
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment