Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


logiciels:openframeworks:atelier:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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 16:59]
resonance [Cours 3 : tableau, matrice]
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 main.cppofApp.h et ofApp.cpp. Ces fichiers sont générés par le //ProjectGenerator//.+Atelier donné au Fablab LFO pour les [[http://reso-nance.org/lfo/article8/week-end-fablab|week-ends fablabs]]les 14 et 15 mars 2015. 
 + 
 +<WRAP round download 55%> 
 +**Tous les fichiers (25 Mo) : {{:logiciels:openframeworks:atelier:ofatelier.zip|}}** 
 +</WRAP>
  
 ===== Fichiers ===== ===== Fichiers =====
 +
 +Pour commencer, il faut trois fichiers : main.cpp, ofApp.h et ofApp.cpp. Ces fichiers sont générés par le //ProjectGenerator//.
  
 ++++ main.cpp | ++++ main.cpp |
Ligne 299: Ligne 305:
  
  
-===== Cours 4 =====+===== Cours 4 : objets ===== 
 +{{:logiciels:openframeworks:atelier:cours04.png?200|}}
  
-++++ Code |+++++ ofApp.h |
 <code cpp> <code cpp>
 +#pragma once
 +
 +#include "ofMain.h"
 +#include "ofBall.h"
 +
 +class ofApp : public ofBaseApp{
 + public:
 + void setup();
 + void update();
 + void draw();
 + void keyPressed(int key);
 + void mousePressed(int x, int y, int button);
 + vector<ofBall> balls;
 +};
 +</code>
 +++++
 +
 +
 +
 +++++ ofApp.cpp |
 +<code cpp>
 +#include "ofApp.h"
 +
 +void ofApp::setup(){
 +    for( int i = 0; i < 10; i=i+1){
 +        balls.push_back(ofBall());
 +    }
 +    ofBackground(255);
 +}
 +
 +void ofApp::update(){
 +    for( int i = 0; i < balls.size(); i=i+1){
 +        balls[i].update();
 +    }
 +}
 +
 +void ofApp::draw(){
 +     for( int i = 0; i < balls.size(); i=i+1){
 +        balls[i].draw();
 +    }
 +}
 +
 +void ofApp::keyPressed(int key){
 +    if (key == 's') {
 +        glReadBuffer(GL_FRONT);  // HACK for windows
 +        ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png");
 +    }
 +}
 +
 +void ofApp::mousePressed(int x, int y, int button){
 +    balls.push_back(ofBall());
 +}
 +</code>
 +++++
 +
 +
 +
 +++++ ofBall.h |
 +<code cpp>
 +//  ofBall.h
 +//  Created by rux on 3/31/14.
 +
 +#ifndef _OF_BALL
 +#define _OF_BALL
 +#include "ofMain.h"
 +
 +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's drawing
 +
 +        // 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
 +
 +</code>
 +++++
 +
 +
 +++++ ofBall.cpp |
 +<code cpp>
 +#include "ofBall.h"
 +
 +ofBall::ofBall(){
 +    x = ofRandom(0, ofGetWidth());      // give some random positioning
 +    y = ofRandom(0, ofGetHeight());
 +    speedX = ofRandom(-10, 10);           // and random speed and direction
 +    speedY = ofRandom(-10, 10);
 +    dim = ofRandom(5, 30);;
 +    color.set(ofRandom(255), ofRandom(255), ofRandom(255));
 +}
 +
 +void ofBall::update(){
 +    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::draw(){
 +    ofSetColor(color);
 +    ofCircle(x, y, dim);
 +}
 +</code>
 +++++
 +
 +===== Cours 5 : fichier =====
 +  * Fichiers police et textes : {{:logiciels:openframeworks:atelier:data.zip|}}
 +{{:logiciels:openframeworks:atelier:cours05.png?200|}}
 +
 +++++ ofApp.h |
 +<code cpp>
 +#pragma once
 +#include "ofMain.h"
 +
 +class ofApp : public ofBaseApp{
 +
 +    public:
 +    void setup();
 +    void update();
 +    void draw();
 +    
 +    ofTrueTypeFont font;
 +    ofTTFCharacter ttfChar;
 +    float angle;
 +    vector<string> words;
 +    vector<ofPoint> positions;
 +    ofEasyCam camera;
 +};
 +</code>
 +++++
 +
 +
 +++++ ofApp.cpp |
 +<code cpp>
 +#include "ofApp.h"
 +
 +void ofApp::setup(){
 +    ofBackground(255,255,255);
 +    glEnable(GL_DEPTH_TEST);
 +    //glEnable(GL_BLEND);
 +    //glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 +    glAlphaFunc(GL_GREATER, 0.5) ;
 +    glEnable(GL_ALPHA_TEST) ;
 +
 +    font.loadFont("Batang.ttf", 24, true, true, true);
 +    ttfChar = font.getCharacterAsPoints('C');
 +    ofSetFullscreen(false);
 +    angle = 0.0;
 +
 +    // Fichier de mots
 +    ifstream infile;
 +    infile.open("./data/miserables.txt");
 +    if (!infile) cout << "can't open file" << endl;
 +    string w;
 +    while (infile >> w) {
 +        words.push_back(w);
 +        positions.push_back(ofPoint(ofRandom(0,700)-350,
 +                                    ofRandom(0,700)-350,
 +                                    ofRandom(0,700)-350));
 +    }
 +}
 +
 +void ofApp::update(){
 +    // Brownian motion
 +    for (int i = 0; i < words.size(); i++) {
 +        positions[i].x += ofRandom(-1,1);
 +        positions[i].y += ofRandom(-1,1);
 +    }
 +}
 +
 +bool wordIsTooHip(string w) {
 +    if (w == "a") return true; // caractère en rouge
 +    else    return false;
 +}
 +void ofApp::draw(){
 +    ofFill();
 +    camera.begin();
 +    ofPushMatrix();
 +        for (int i = 0; i<  words.size(); i=i+1) {
 +            ofPushMatrix();
 +            ofTranslate(positions[i]);
 +            if ( wordIsTooHip(words[i]) ) ofSetColor(255,0,0);
 +            else ofSetColor(0,0,0);
 +            font.drawString(words[i], 0.0, 0.0);
 +            ofPopMatrix();
 +        }
 +    ofPopMatrix();
 +    camera.end();
 +}
 +</code>
 +++++
 +
 +
 +===== Cours 6 : Mesh =====
 +  * Fichier image : {{:logiciels:openframeworks:atelier:stars.png?linkonly|}}
 +
 +{{:logiciels:openframeworks:atelier:05b.png?200|}}
 +
 +++++ ofApp.h |
 +<code cpp>
 +// from http://www.openframeworks.cc/tutorials/graphics/generativemesh.html
 +
 +#pragma once
 +#include "ofMain.h"
 +
 +class ofApp : public ofBaseApp{
 + public:
 + void setup();
 + void update();
 + void draw();
 +
 + ofMesh mesh;
 + ofImage image;
 + ofEasyCam easyCam;
 + vector<ofVec3f> offsets;
 +};
 +</code>
 +++++
 +
 +++++ ofApp.cpp |
 +<code cpp>
 +#include "ofApp.h"
 +
 +void ofApp::setup(){
 +    ofSetFrameRate(60);
 +    image.loadImage("stars.png");
 +    image.resize(200, 200); // car trop de sommets ~ 64000
 +    mesh.setMode(OF_PRIMITIVE_LINES); //OF_PRIMITIVE_POINTS ou 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, y);
 +            float intensity = c.getLightness();
 +            if (intensity >= intensityThreshold) {
 +                float saturation = c.getSaturation();
 +                float z = ofMap(saturation, 0, 255, -100, 100);
 +                // 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,100), ofRandom(0,100), ofRandom(0,100)));
 +            }
 +        }
 +    }
 +
 +    //cout << mesh.getNumVertices() << endl;
 +
 +    // Let's add some lines!
 +    float connectionDistance = 30;
 +    int numVerts = mesh.getNumVertices();
 +    for (int a=0; a<numVerts; ++a) {
 +        for (int b=a+1; b<numVerts; ++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::update(){
 +    int numVerts = mesh.getNumVertices();
 +
 +    for (int i=0; i<numVerts; ++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
 +        //     ofSignedNoise(time) gives us noise values that change smoothly over time
 +        //     ofSignedNoise(time*timeScale) allows us to control the smoothness of our noise (smaller timeScale, smoother values)
 +        //     ofSignedNoise(time+timeOffset) allows us to use the same Perlin noise function to control multiple things and have them look as if they are moving independently
 +        //     ofSignedNoise(time)*displacementScale allows us to change the bounds of the noise from [-1, 1] to whatever we want
 +        // 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, vert);
 +    }
 +}
 +
 +void ofApp::draw(){
 +    //image.draw(0,0);
 +    ofColor centerColor = ofColor(85, 78, 68);
 +    ofColor edgeColor(0, 0, 0);
 +    ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);
 +
 +    easyCam.begin();
 +        ofPushMatrix();
 +            ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
 +            mesh.draw();
 +        ofPopMatrix();
 +    easyCam.end();
 +}
 +</code>
 +++++
 +
 +===== Cours 7 : vidéo ASCII =====
 +  * Fichier police à mettre dans "data" : {{:logiciels:openframeworks:atelier:courriernewbold.zip|}}
 +
 +{{:logiciels:openframeworks:atelier:06a.png?200|}}
 +
 +++++ ofApp.h |
 +<code cpp>
 +#pragma once
 +#include "ofMain.h"
 +
 +class ofApp : public ofBaseApp{
 +
 + public:
 + void setup();
 + void update();
 + void draw();
 + void keyPressed(int key);
 +
 + ofVideoGrabber vidGrabber;
 + int camWidth;
 + int camHeight;
 +        string asciiCharacters;
 +        ofTrueTypeFont  font;
 +};
 +</code>
 +++++
 +
 +++++ ofApp.cpp |
 +<code cpp>
 +#include "ofApp.h"
 +
 +void ofApp::setup(){
 +    ofBackground(0,0,0);
 +    ofSetFrameRate(60);
 +    ofEnableAlphaBlending();
 + camWidth = 640; // try to grab at this size.
 + camHeight = 480;
 + vidGrabber.setVerbose(true);
 + vidGrabber.initGrabber(camWidth,camHeight);
 +    font.loadFont("Courier New Bold.ttf", 9);
 +
 +    //this set of characters comes from processing:
 +    //http://processing.org/learning/library/asciivideo.html
 + //changed order slightly to work better for mapping
 +    asciiCharacters = string("  ..,,,'''``--_:;^^**""=+<>iv%&xclrs)/){}I?!][1taeo7zjLunT#@JCwfy325Fp6mqSghVd4EgXPGZbYkOA8U$KHDBWNMR0Q");
 +}
 +
 +void ofApp::update(){
 + vidGrabber.update();
 +}
 +
 +void ofApp::draw(){
 +    // change background video alpha value based on the mouse position
 +    // float videoAlphaValue = ofMap(mouseX, 0,ofGetWidth(),0,255);
 +    // ofSetColor(255,255,255,videoAlphaValue);
 +    // vidGrabber.draw(0,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,j).getLightness();
 +            // calculate the index of the character from our asciiCharacters array
 +            int character = powf( ofMap(lightness, 0, 255, 0, 1), 2.5) * asciiCharacters.size();
 +            // draw the character at the correct location
 +            font.drawString(ofToString(asciiCharacters[character]), i, j);
 +        }
 +    }
 +}
 +
 +void ofApp::keyPressed (int key){
 + // 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 == 's' || key == 'S'){
 + vidGrabber.videoSettings();
 + }
 +}
 </code> </code>
 ++++ ++++
/home/resonancg/www/wiki/data/attic/logiciels/openframeworks/atelier/accueil.1426089563.txt.gz · Dernière modification: 2015/03/11 16:59 de resonance