#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(255);
ofSetLineWidth(2);
ofEnableDepthTest();
this->frame.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES);
int span = 80;
for (int x = -240; x <= 240; x += span) {
for (int y = -240; y <= 240; y += span) {
for (int z = -240; z <= 240; z += span) {
this->location_list.push_back(glm::vec3(x, y, z));
}
}
}
}
//--------------------------------------------------------------
void ofApp::update() {
ofSeedRandom(39);
this->face.clear();
this->frame.clear();
float size = 65;
for (int i = 0; i < this->location_list.size(); i++) {
this->setBoxToMesh(this->face, this->frame, this->location_list[i], size);
}
}
//--------------------------------------------------------------
void ofApp::draw() {
this->cam.begin();
ofRotateY(ofGetFrameNum() * 0.333333);
this->face.draw();
this->frame.drawWireframe();
ofSetColor(0);
for (auto& vertex : this->face.getVertices()) {
ofDrawSphere(vertex, 3);
}
this->cam.end();
}
//--------------------------------------------------------------
void ofApp::setBoxToMesh(ofMesh& face_target, ofMesh& frame_target, glm::vec3 location, float size) {
this->setBoxToMesh(face_target, frame_target, location, size, size, size);
}
//--------------------------------------------------------------
void ofApp::setBoxToMesh(ofMesh& face_target, ofMesh& frame_target, glm::vec3 location, float height, float width, float depth) {
int param = (int)(ofGetFrameNum() * 3 + (location.y + 220) * 7) % 400;
if (param > 300) { param = 0; }
else if (param > 200) { param = ofMap(param, 200, 300, 100, 0); }
else if (param > 100) { param = 100; }
int face_index = face_target.getNumVertices();
int frame_index = frame_target.getNumVertices();
vector<glm::vec3> vertices;
vertices.push_back(glm::vec3(width * -0.5, height * 0.5, depth * -0.5));
vertices.push_back(glm::vec3(width * 0.5, height * 0.5, depth * -0.5));
vertices.push_back(glm::vec3(width * 0.5, height * 0.5, depth * 0.5));
vertices.push_back(glm::vec3(width * -0.5, height * 0.5, depth * 0.5));
vertices.push_back(glm::vec3(width * -0.5, height * -0.5, depth * -0.5));
vertices.push_back(glm::vec3(width * 0.5, height * -0.5, depth * -0.5));
vertices.push_back(glm::vec3(width * 0.5, height * -0.5, depth * 0.5));
vertices.push_back(glm::vec3(width * -0.5, height * -0.5, depth * 0.5));
for (auto& vertex : vertices) {
auto noise_vertex = glm::vec3(
ofMap(ofNoise(ofRandom(1000), ofGetFrameNum() * 0.015), 0, 1, -width * 0.5, width * 0.5),
ofMap(ofNoise(ofRandom(1000), ofGetFrameNum() * 0.015), 0, 1, -height * 0.5, height * 0.5),
ofMap(ofNoise(ofRandom(1000), ofGetFrameNum() * 0.015), 0, 1, -depth * 0.5, depth * 0.5));
auto distance = vertex - noise_vertex;
auto d = (distance / 100) * param;
face_target.addVertex(location + vertex * 0.99 - d);
frame_target.addVertex(location + vertex - d);
}
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.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++) {
face_target.addColor(ofColor(255));
frame_target.addColor(ofColor(0));
}
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}