Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
logiciels:openframeworks:atelier:accueil [2015/03/11 17:19] resonance [Cours 4] |
logiciels:openframeworks:atelier:accueil [2016/02/01 00:11] (Version actuelle) resonance |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Atelier OpenFrameworks ====== | ====== Atelier OpenFrameworks ====== | ||
- | Pour commencer, il faut trois fichiers | + | Atelier donné au Fablab LFO pour les [[http:// |
+ | |||
+ | <WRAP round download 55%> | ||
+ | **Tous les fichiers | ||
+ | </ | ||
===== Fichiers ===== | ===== Fichiers ===== | ||
+ | |||
+ | Pour commencer, il faut trois fichiers : main.cpp, ofApp.h et ofApp.cpp. Ces fichiers sont générés par le // | ||
++++ main.cpp | | ++++ main.cpp | | ||
Ligne 300: | Ligne 306: | ||
===== Cours 4 : objets ===== | ===== Cours 4 : objets ===== | ||
+ | {{: | ||
- | ++++ Code | | + | ++++ ofApp.h |
<code cpp> | <code cpp> | ||
+ | #pragma once | ||
+ | |||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | class ofApp : public ofBaseApp{ | ||
+ | public: | ||
+ | void setup(); | ||
+ | void update(); | ||
+ | void draw(); | ||
+ | void keyPressed(int key); | ||
+ | void mousePressed(int x, int y, int button); | ||
+ | vector< | ||
+ | }; | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | |||
+ | ++++ ofApp.cpp | | ||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | void ofApp:: | ||
+ | for( int i = 0; i < 10; i=i+1){ | ||
+ | balls.push_back(ofBall()); | ||
+ | } | ||
+ | ofBackground(255); | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | for( int i = 0; i < balls.size(); | ||
+ | balls[i].update(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | for( int i = 0; i < balls.size(); | ||
+ | balls[i].draw(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | if (key == ' | ||
+ | glReadBuffer(GL_FRONT); | ||
+ | ofSaveScreen(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | balls.push_back(ofBall()); | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | |||
+ | ++++ ofBall.h | | ||
+ | <code cpp> | ||
+ | // ofBall.h | ||
+ | // Created by rux on 3/31/14. | ||
+ | |||
+ | #ifndef _OF_BALL | ||
+ | #define _OF_BALL | ||
+ | #include " | ||
+ | |||
+ | class ofBall { | ||
+ | |||
+ | // place public functions or variables declarations here | ||
+ | public: | ||
+ | ofBall(); // constructor | ||
+ | // methods, equivalent to specific functions of your class objects | ||
+ | void update(); // update method, used to refresh your objects properties | ||
+ | void draw(); // draw method, this where you'll do the object' | ||
+ | |||
+ | // variables | ||
+ | float x; // position | ||
+ | float y; | ||
+ | float speedY; // speed and direction | ||
+ | float speedX; | ||
+ | int dim; // size | ||
+ | ofColor color; | ||
+ | |||
+ | private: // place private functions or variables declarations here | ||
+ | |||
+ | }; // don't forget the semicolon!! | ||
+ | #endif | ||
+ | |||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ++++ ofBall.cpp | | ||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | ofBall:: | ||
+ | x = ofRandom(0, ofGetWidth()); | ||
+ | y = ofRandom(0, ofGetHeight()); | ||
+ | speedX = ofRandom(-10, | ||
+ | speedY = ofRandom(-10, | ||
+ | dim = ofRandom(5, 30);; | ||
+ | color.set(ofRandom(255), | ||
+ | } | ||
+ | |||
+ | void ofBall:: | ||
+ | if(x < 0 ){ | ||
+ | x = 0; | ||
+ | speedX *= -1; | ||
+ | } else if(x > ofGetWidth()){ | ||
+ | x = ofGetWidth(); | ||
+ | speedX *= -1; | ||
+ | } | ||
+ | |||
+ | if(y < 0 ){ | ||
+ | y = 0; | ||
+ | speedY *= -1; | ||
+ | } else if(y > ofGetHeight()){ | ||
+ | y = ofGetHeight(); | ||
+ | speedY *= -1; | ||
+ | } | ||
+ | |||
+ | x+=speedX; | ||
+ | y+=speedY; | ||
+ | } | ||
+ | |||
+ | void ofBall:: | ||
+ | ofSetColor(color); | ||
+ | ofCircle(x, y, dim); | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ===== Cours 5 : fichier ===== | ||
+ | * Fichiers police et textes : {{: | ||
+ | {{: | ||
+ | |||
+ | ++++ ofApp.h | | ||
+ | <code cpp> | ||
+ | #pragma once | ||
+ | #include " | ||
+ | |||
+ | class ofApp : public ofBaseApp{ | ||
+ | |||
+ | public: | ||
+ | void setup(); | ||
+ | void update(); | ||
+ | void draw(); | ||
+ | | ||
+ | ofTrueTypeFont font; | ||
+ | ofTTFCharacter ttfChar; | ||
+ | float angle; | ||
+ | vector< | ||
+ | vector< | ||
+ | ofEasyCam camera; | ||
+ | }; | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ++++ ofApp.cpp | | ||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | void ofApp:: | ||
+ | ofBackground(255, | ||
+ | glEnable(GL_DEPTH_TEST); | ||
+ | // | ||
+ | // | ||
+ | glAlphaFunc(GL_GREATER, | ||
+ | glEnable(GL_ALPHA_TEST) ; | ||
+ | |||
+ | font.loadFont(" | ||
+ | ttfChar = font.getCharacterAsPoints(' | ||
+ | ofSetFullscreen(false); | ||
+ | angle = 0.0; | ||
+ | |||
+ | // Fichier de mots | ||
+ | ifstream infile; | ||
+ | infile.open(" | ||
+ | if (!infile) cout << " | ||
+ | string w; | ||
+ | while (infile >> w) { | ||
+ | words.push_back(w); | ||
+ | positions.push_back(ofPoint(ofRandom(0, | ||
+ | ofRandom(0, | ||
+ | ofRandom(0, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | // Brownian motion | ||
+ | for (int i = 0; i < words.size(); | ||
+ | positions[i].x += ofRandom(-1, | ||
+ | positions[i].y += ofRandom(-1, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | bool wordIsTooHip(string w) { | ||
+ | if (w == " | ||
+ | else return false; | ||
+ | } | ||
+ | void ofApp:: | ||
+ | ofFill(); | ||
+ | camera.begin(); | ||
+ | ofPushMatrix(); | ||
+ | for (int i = 0; i< words.size(); | ||
+ | ofPushMatrix(); | ||
+ | ofTranslate(positions[i]); | ||
+ | if ( wordIsTooHip(words[i]) ) ofSetColor(255, | ||
+ | else ofSetColor(0, | ||
+ | font.drawString(words[i], | ||
+ | ofPopMatrix(); | ||
+ | } | ||
+ | ofPopMatrix(); | ||
+ | camera.end(); | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ===== Cours 6 : Mesh ===== | ||
+ | * Fichier image : {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ++++ ofApp.h | | ||
+ | <code cpp> | ||
+ | // from http:// | ||
+ | |||
+ | #pragma once | ||
+ | #include " | ||
+ | |||
+ | class ofApp : public ofBaseApp{ | ||
+ | public: | ||
+ | void setup(); | ||
+ | void update(); | ||
+ | void draw(); | ||
+ | |||
+ | ofMesh mesh; | ||
+ | ofImage image; | ||
+ | ofEasyCam easyCam; | ||
+ | vector< | ||
+ | }; | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++ ofApp.cpp | | ||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | void ofApp:: | ||
+ | ofSetFrameRate(60); | ||
+ | image.loadImage(" | ||
+ | image.resize(200, | ||
+ | mesh.setMode(OF_PRIMITIVE_LINES); | ||
+ | |||
+ | float intensityThreshold = 150.0; | ||
+ | int w = image.getWidth(); | ||
+ | int h = image.getHeight(); | ||
+ | |||
+ | for (int x=0; x<w; ++x) { | ||
+ | for (int y=0; y<h; ++y) { | ||
+ | ofColor c = image.getColor(x, | ||
+ | float intensity = c.getLightness(); | ||
+ | if (intensity >= intensityThreshold) { | ||
+ | float saturation = c.getSaturation(); | ||
+ | float z = ofMap(saturation, | ||
+ | // We shrunk our image by a factor of 4, so we need to multiply our pixel | ||
+ | // locations by 4 in order to have our mesh cover the openFrameworks window | ||
+ | ofVec3f pos(4*x, 4*y, z); | ||
+ | //ofVec3f pos(x, y, 0.0); | ||
+ | mesh.addVertex(pos); | ||
+ | mesh.addColor(c); | ||
+ | // It will create a ofVec3f with 3 random values | ||
+ | // These will allow us to move the x, y and z coordinates of each vertex independently | ||
+ | offsets.push_back(ofVec3f(ofRandom(0, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | //cout << mesh.getNumVertices() << endl; | ||
+ | |||
+ | // Let's add some lines! | ||
+ | float connectionDistance = 30; | ||
+ | int numVerts = mesh.getNumVertices(); | ||
+ | for (int a=0; a< | ||
+ | for (int b=a+1; b< | ||
+ | ofVec3f verta = mesh.getVertex(a); | ||
+ | ofVec3f vertb = mesh.getVertex(b); | ||
+ | float distance = verta.distance(vertb); | ||
+ | if (distance <= connectionDistance) { | ||
+ | mesh.addIndex(a); | ||
+ | mesh.addIndex(b); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | int numVerts = mesh.getNumVertices(); | ||
+ | |||
+ | for (int i=0; i< | ||
+ | ofVec3f vert = mesh.getVertex(i); | ||
+ | float time = ofGetElapsedTimef(); | ||
+ | float timeScale = 5.0; | ||
+ | float displacementScale = 0.75; | ||
+ | ofVec3f timeOffsets = offsets[i]; | ||
+ | |||
+ | // A typical design pattern for using Perlin noise uses a couple parameters: | ||
+ | // ofSignedNoise(time*timeScale+timeOffset)*displacementScale | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // Combine all of those parameters together, and you've got some nice control over your noise | ||
+ | vert.x += (ofSignedNoise(time*timeScale+timeOffsets.x)) * displacementScale; | ||
+ | vert.y += (ofSignedNoise(time*timeScale+timeOffsets.y)) * displacementScale; | ||
+ | vert.z += (ofSignedNoise(time*timeScale+timeOffsets.z)) * displacementScale; | ||
+ | mesh.setVertex(i, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | // | ||
+ | ofColor centerColor = ofColor(85, 78, 68); | ||
+ | ofColor edgeColor(0, | ||
+ | ofBackgroundGradient(centerColor, | ||
+ | |||
+ | easyCam.begin(); | ||
+ | ofPushMatrix(); | ||
+ | ofTranslate(-ofGetWidth()/ | ||
+ | mesh.draw(); | ||
+ | ofPopMatrix(); | ||
+ | easyCam.end(); | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ===== Cours 7 : vidéo ASCII ===== | ||
+ | * Fichier police à mettre dans " | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ++++ ofApp.h | | ||
+ | <code cpp> | ||
+ | #pragma once | ||
+ | #include " | ||
+ | |||
+ | class ofApp : public ofBaseApp{ | ||
+ | |||
+ | public: | ||
+ | void setup(); | ||
+ | void update(); | ||
+ | void draw(); | ||
+ | void keyPressed(int key); | ||
+ | |||
+ | ofVideoGrabber vidGrabber; | ||
+ | int camWidth; | ||
+ | int camHeight; | ||
+ | string asciiCharacters; | ||
+ | ofTrueTypeFont | ||
+ | }; | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++ ofApp.cpp | | ||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | void ofApp:: | ||
+ | ofBackground(0, | ||
+ | ofSetFrameRate(60); | ||
+ | ofEnableAlphaBlending(); | ||
+ | camWidth = 640; // try to grab at this size. | ||
+ | camHeight = 480; | ||
+ | vidGrabber.setVerbose(true); | ||
+ | vidGrabber.initGrabber(camWidth, | ||
+ | font.loadFont(" | ||
+ | |||
+ | //this set of characters comes from processing: | ||
+ | // | ||
+ | //changed order slightly to work better for mapping | ||
+ | asciiCharacters = string(" | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | vidGrabber.update(); | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | // change background video alpha value based on the mouse position | ||
+ | // float videoAlphaValue = ofMap(mouseX, | ||
+ | // ofSetColor(255, | ||
+ | // vidGrabber.draw(0, | ||
+ | |||
+ | ofPixelsRef pixelsRef = vidGrabber.getPixelsRef(); | ||
+ | ofSetHexColor(0xffffff); | ||
+ | |||
+ | for (int i = 0; i < camWidth; i+= 7){ | ||
+ | for (int j = 0; j < camHeight; j+= 9){ | ||
+ | // get the pixel and its lightness (lightness is the average of its RGB values) | ||
+ | float lightness = pixelsRef.getColor(i, | ||
+ | // calculate the index of the character from our asciiCharacters array | ||
+ | int character = powf( ofMap(lightness, | ||
+ | // draw the character at the correct location | ||
+ | font.drawString(ofToString(asciiCharacters[character]), | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ofApp:: | ||
+ | // in fullscreen mode, on a pc at least, the | ||
+ | // first time video settings the come up | ||
+ | // they come up *under* the fullscreen window | ||
+ | // use alt-tab to navigate to the settings | ||
+ | // window. we are working on a fix for this... | ||
+ | if (key == ' | ||
+ | vidGrabber.videoSettings(); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
++++ | ++++ |