Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Last active September 26, 2015 09:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riebschlager/92d135235d0432a617b1 to your computer and use it in GitHub Desktop.
Save riebschlager/92d135235d0432a617b1 to your computer and use it in GitHub Desktop.
collage
#include "ofApp.h"
void ofApp::setup(){
source.loadImage("source.jpg");
source.resize(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.allocate(CANVAS_WIDTH, CANVAS_HEIGHT, GL_RGBA);
canvas.begin();
ofBackground(0, 0, 0);
canvas.end();
for (int i = 0; i < NUMBER_OF_SLICES; i++) {
ofImage img;
img.loadImage("slices/slice-" + ofToString(i) + ".png");
slices.push_back(img);
}
for (int x = 0; x < canvas.getWidth(); x++){
for (int y = 0; y < canvas.getHeight(); y++) {
data[x][y] = ofNoise(x * NOISE_SCALE, y * NOISE_SCALE);
}
}
}
void ofApp::render() {
randomPoint.x = ofRandom(canvas.getWidth());
randomPoint.y = ofRandom(canvas.getHeight());
float noise = data[(int) floor(randomPoint.x)][(int) floor(randomPoint.y)];
ofColor color = source.getColor((int) floor(randomPoint.x), (int) floor(randomPoint.y));
ofImage slice = slices[floor(ofMap(noise, 0, 1, 0, NUMBER_OF_SLICES))];
//ofImage slice = slices[floor(ofRandom(NUMBER_OF_SLICES))];
float scale = ofMap(noise, 0, 1, SCALE_MIN, SCALE_MAX);
canvas.begin();
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
ofSetColor(color, ofRandom(ALPHA_MIN, ALPHA_MAX));
ofPushMatrix();
ofTranslate(randomPoint.x - slice.width / 2, randomPoint.y - slice.height / 2);
ofScale(scale, scale);
ofRotate(ofMap(noise, 0, 1, ROTATION_MIN, ROTATION_MAX));
slice.draw(0, 0);
ofPopMatrix();
canvas.end();
}
void ofApp::update(){
for(int i = 0; i < 1000; i++) {
render();
}
}
void ofApp::draw(){
ofBackground(255, 255, 255);
ofSetColor(255, 255, 255);
canvas.draw(0, 0, ofGetWidth(), ofGetHeight());
}
void ofApp::saveFbo(){
ofFbo img;
ofPixels pixels;
img.allocate(CANVAS_WIDTH, CANVAS_HEIGHT, GL_RGBA);
img.begin();
ofBackground(0, 0, 0);
canvas.draw(0, 0);
img.end();
img.readToPixels(pixels);
ofSaveImage(pixels, "output/image" + ofToString(ofGetUnixTime()) + ".tif");
}
void ofApp::keyPressed(int key){
if(key == 's') {
saveFbo();
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
#include <vector>
#define CANVAS_WIDTH 1920 * 2
#define CANVAS_HEIGHT 1080 * 2
#define NUMBER_OF_SLICES 5
#define SCALE_MIN 0.1
#define SCALE_MAX 2
#define ROTATION_MIN -180
#define ROTATION_MAX 180
#define ALPHA_MIN 0
#define ALPHA_MAX 255
#define NOISE_SCALE 0.0001523423
class ofApp : 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);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void saveFbo();
void render();
ofImage source;
ofFbo canvas;
ofPoint randomPoint;
vector<ofImage> slices;
int numberOfSlices;
float data[CANVAS_WIDTH][CANVAS_HEIGHT];
};
@dorald
Copy link

dorald commented Sep 26, 2015

Hi @riebschlager
I try to run this code on openFrameworks 0.9 but doesn't draw nothing, just black screen. I have 10 .png slices 256 × 256 and one source image .jpg 640 × 640.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment