#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openframeworks");
ofBackground(239);
ofSetColor(39);
ofSetLineWidth(1.5);
ofEnableDepthTest();
ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER);
this->frame.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES);
ofImage image = ofImage("gecko.png");
int span = 8;
ofFbo fbo;
fbo.allocate(ofGetWidth(), ofGetHeight());
fbo.begin();
ofClear(0);
image.draw(ofGetWidth() * 0.5, ofGetHeight() * 0.5);
fbo.end();
ofPixels pixels;
fbo.readToPixels(pixels);
for (int x = 0; x < fbo.getWidth(); x += span) {
for (int y = 0; y < fbo.getHeight(); y += span) {
if (pixels.getColor(x, y) != ofColor(0, 0)) {
for (int z = span * -3; z <= span * 3; z += span) {
this->location_list.push_back(glm::vec3(x - ofGetWidth() * 0.5, ofGetHeight() * 0.5 - y, z));
}
}
}
}
}
//--------------------------------------------------------------
void ofApp::update() {
this->face.clear();
this->frame.clear();
for (auto& location : this->location_list) {
auto noise_value = ofNoise(glm::vec3(
location.x * 0.005,
location.y * 0.0055 - ofGetFrameNum() * 0.02,
location.z * 0.005));
if (noise_value > 0.45) {
this->setBoxToMesh(this->face, this->frame, location, 8, ofColor(39), ofColor(239));
}
else {
this->setBoxToMesh(this->face, this->frame, location, 8, ofColor(239), ofColor(39));
}
}
}
//--------------------------------------------------------------
void ofApp::draw() {
this->cam.begin();
ofRotateY(ofGetFrameNum());
this->face.draw();
this->frame.drawWireframe();
this->cam.end();
}
//--------------------------------------------------------------
void ofApp::setBoxToMesh(ofMesh& face_target, ofMesh& frame_target, glm::vec3 location, float size, ofColor face_color, ofColor frame_color) {
this->setBoxToMesh(face_target, frame_target, location, size, size, size, face_color, frame_color);
}
//--------------------------------------------------------------
void ofApp::setBoxToMesh(ofMesh& face_target, ofMesh& frame_target, glm::vec3 location, float height, float width, float depth, ofColor face_color, ofColor frame_color) {
int face_index = face_target.getNumVertices();
int frame_index = frame_target.getNumVertices();
face_target.addVertex(location + glm::vec3(width * -0.5 * 0.99, height * 0.5 * 0.99, depth * -0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * 0.5 * 0.99, height * 0.5 * 0.99, depth * -0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * 0.5 * 0.99, height * 0.5 * 0.99, depth * 0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * -0.5 * 0.99, height * 0.5 * 0.99, depth * 0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * -0.5 * 0.99, height * -0.5 * 0.99, depth * -0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * 0.5 * 0.99, height * -0.5 * 0.99, depth * -0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * 0.5 * 0.99, height * -0.5 * 0.99, depth * 0.5 * 0.99));
face_target.addVertex(location + glm::vec3(width * -0.5 * 0.99, height * -0.5 * 0.99, depth * 0.5 * 0.99));
face_target.addIndex(face_index + 0); face_target.addIndex(face_index + 1); face_target.addIndex(face_index + 2);
face_target.addIndex(face_index + 0); face_target.addIndex(face_index + 2); face_target.addIndex(face_index + 3);
face_target.addIndex(face_index + 4); face_target.addIndex(face_index + 5); face_target.addIndex(face_index + 6);
face_target.addIndex(face_index + 4); face_target.addIndex(face_index + 6); face_target.addIndex(face_index + 7);
face_target.addIndex(face_index + 0); face_target.addIndex(face_index + 4); face_target.addIndex(face_index + 1);
face_target.addIndex(face_index + 4); face_target.addIndex(face_index + 5); face_target.addIndex(face_index + 1);
face_target.addIndex(face_index + 1); face_target.addIndex(face_index + 5); face_target.addIndex(face_index + 6);
face_target.addIndex(face_index + 6); face_target.addIndex(face_index + 2); face_target.addIndex(face_index + 1);
face_target.addIndex(face_index + 2); face_target.addIndex(face_index + 6); face_target.addIndex(face_index + 7);
face_target.addIndex(face_index + 7); face_target.addIndex(face_index + 3); face_target.addIndex(face_index + 2);
face_target.addIndex(face_index + 3); face_target.addIndex(face_index + 7); face_target.addIndex(face_index + 4);
face_target.addIndex(face_index + 4); face_target.addIndex(face_index + 0); face_target.addIndex(face_index + 3);
frame_target.addVertex(location + glm::vec3(width * -0.5, height * 0.5, depth * -0.5));
frame_target.addVertex(location + glm::vec3(width * 0.5, height * 0.5, depth * -0.5));
frame_target.addVertex(location + glm::vec3(width * 0.5, height * 0.5, depth * 0.5));
frame_target.addVertex(location + glm::vec3(width * -0.5, height * 0.5, depth * 0.5));
frame_target.addVertex(location + glm::vec3(width * -0.5, height * -0.5, depth * -0.5));
frame_target.addVertex(location + glm::vec3(width * 0.5, height * -0.5, depth * -0.5));
frame_target.addVertex(location + glm::vec3(width * 0.5, height * -0.5, depth * 0.5));
frame_target.addVertex(location + glm::vec3(width * -0.5, height * -0.5, depth * 0.5));
frame_target.addIndex(frame_index + 0); frame_target.addIndex(frame_index + 1);
frame_target.addIndex(frame_index + 1); frame_target.addIndex(frame_index + 2);
frame_target.addIndex(frame_index + 2); frame_target.addIndex(frame_index + 3);
frame_target.addIndex(frame_index + 3); frame_target.addIndex(frame_index + 0);
frame_target.addIndex(frame_index + 4); frame_target.addIndex(frame_index + 5);
frame_target.addIndex(frame_index + 5); frame_target.addIndex(frame_index + 6);
frame_target.addIndex(frame_index + 6); frame_target.addIndex(frame_index + 7);
frame_target.addIndex(frame_index + 7); frame_target.addIndex(frame_index + 4);
frame_target.addIndex(frame_index + 0); frame_target.addIndex(frame_index + 4);
frame_target.addIndex(frame_index + 1); frame_target.addIndex(frame_index + 5);
frame_target.addIndex(frame_index + 2); frame_target.addIndex(frame_index + 6);
frame_target.addIndex(frame_index + 3); frame_target.addIndex(frame_index + 7);
for (int i = 0; i < 8; i++) {
this->face.addColor(face_color);
this->frame.addColor(frame_color);
}
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}