#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(255);
ofEnableDepthTest();
this->frame.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES);
}
//--------------------------------------------------------------
void ofApp::update() {
ofSeedRandom(39);
this->face.clear();
this->frame.clear();
int theta = 90;
for (int radius = 30; radius <= 360; radius += 30) {
auto noise_value = ofNoise(radius * 0.0008 - ofGetFrameNum() * 0.005);
float angle;
glm::highp_mat4 rotation;
if (noise_value < 0.49) {
angle = ofMap(noise_value, 0, 0.48, -PI * 2, PI * 2);
rotation = glm::rotate(glm::mat4(), angle, glm::vec3(0, 1, 0));
}
else if (noise_value > 0.52) {
angle = ofMap(noise_value, 0.52, 1, -PI * 2, PI * 2);
rotation = glm::rotate(glm::mat4(), angle, glm::vec3(1, 0, 0));
}
for (int phi = 0; phi < 360; phi += 3) {
auto index = this->face.getNumVertices();
vector<glm::vec3> vertices;
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
for (auto& vertex : vertices) {
vertex = glm::vec4(vertex, 0) * rotation;
}
auto frame_index = this->frame.getNumVertices();
this->face.addVertices(vertices);
this->frame.addVertices(vertices);
this->face.addIndex(index + 0); this->face.addIndex(index + 1); this->face.addIndex(index + 2);
this->face.addIndex(index + 0); this->face.addIndex(index + 2); this->face.addIndex(index + 3);
this->face.addIndex(index + 4); this->face.addIndex(index + 5); this->face.addIndex(index + 6);
this->face.addIndex(index + 4); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 0); this->face.addIndex(index + 4); this->face.addIndex(index + 5);
this->face.addIndex(index + 0); this->face.addIndex(index + 5); this->face.addIndex(index + 1);
this->face.addIndex(index + 2); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 2); this->face.addIndex(index + 7); this->face.addIndex(index + 3);
this->frame.addIndex(index + 0); this->frame.addIndex(index + 1);
this->frame.addIndex(index + 2); this->frame.addIndex(index + 3);
this->frame.addIndex(index + 4); this->frame.addIndex(index + 5);
this->frame.addIndex(index + 6); this->frame.addIndex(index + 7);
for (int i = 0; i < vertices.size(); i++) {
this->face.addColor(ofColor(0));
this->frame.addColor(ofColor(255));
}
}
}
for (int base_x = -750; base_x <= 750; base_x += 750 * 2) {
for (int radius = 30; radius <= 360; radius += 30) {
auto noise_value = ofNoise(radius * 0.0008 - ofGetFrameNum() * 0.005);
float angle;
glm::highp_mat4 rotation;
if (noise_value < 0.48) {
angle = ofMap(noise_value, 0, 0.48, -PI * 2, PI * 2);
rotation = glm::rotate(glm::mat4(), angle, glm::vec3(0, 1, 0));
}
for (int phi = 0; phi < 360; phi += 3) {
auto index = this->face.getNumVertices();
vector<glm::vec3> vertices;
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
for (auto& vertex : vertices) {
vertex += glm::vec3(base_x, 0, 0);
vertex = glm::vec4(vertex, 0) * rotation;
}
auto frame_index = this->frame.getNumVertices();
this->face.addVertices(vertices);
this->frame.addVertices(vertices);
this->face.addIndex(index + 0); this->face.addIndex(index + 1); this->face.addIndex(index + 2);
this->face.addIndex(index + 0); this->face.addIndex(index + 2); this->face.addIndex(index + 3);
this->face.addIndex(index + 4); this->face.addIndex(index + 5); this->face.addIndex(index + 6);
this->face.addIndex(index + 4); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 0); this->face.addIndex(index + 4); this->face.addIndex(index + 5);
this->face.addIndex(index + 0); this->face.addIndex(index + 5); this->face.addIndex(index + 1);
this->face.addIndex(index + 2); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 2); this->face.addIndex(index + 7); this->face.addIndex(index + 3);
this->frame.addIndex(index + 0); this->frame.addIndex(index + 1);
this->frame.addIndex(index + 2); this->frame.addIndex(index + 3);
this->frame.addIndex(index + 4); this->frame.addIndex(index + 5);
this->frame.addIndex(index + 6); this->frame.addIndex(index + 7);
for (int i = 0; i < vertices.size(); i++) {
this->face.addColor(ofColor(0));
this->frame.addColor(ofColor(255));
}
}
}
for (int base_y = -750; base_y <= 750; base_y += 750 * 2) {
for (int radius = 30; radius <= 360; radius += 30) {
auto noise_value = ofNoise(radius * 0.0008 - ofGetFrameNum() * 0.005);
float angle;
glm::highp_mat4 rotation;
if (noise_value > 0.52) {
angle = ofMap(noise_value, 0.52, 1, -PI * 2, PI * 2);
rotation = glm::rotate(glm::mat4(), angle, glm::vec3(1, 0, 0));
}
for (int phi = 0; phi < 360; phi += 3) {
auto index = this->face.getNumVertices();
vector<glm::vec3> vertices;
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
radius * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta + 1) * DEG_TO_RAD),
radius * cos((theta + 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin((phi + 3) * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
vertices.push_back(glm::vec3(
(radius - 30) * cos(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
(radius - 30) * sin(phi * DEG_TO_RAD) * sin((theta - 1) * DEG_TO_RAD),
radius * cos((theta - 3) * DEG_TO_RAD)
));
for (auto& vertex : vertices) {
vertex += glm::vec3(0, base_y, 0);
vertex = glm::vec4(vertex, 0) * rotation;
}
auto frame_index = this->frame.getNumVertices();
this->face.addVertices(vertices);
this->frame.addVertices(vertices);
this->face.addIndex(index + 0); this->face.addIndex(index + 1); this->face.addIndex(index + 2);
this->face.addIndex(index + 0); this->face.addIndex(index + 2); this->face.addIndex(index + 3);
this->face.addIndex(index + 4); this->face.addIndex(index + 5); this->face.addIndex(index + 6);
this->face.addIndex(index + 4); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 0); this->face.addIndex(index + 4); this->face.addIndex(index + 5);
this->face.addIndex(index + 0); this->face.addIndex(index + 5); this->face.addIndex(index + 1);
this->face.addIndex(index + 2); this->face.addIndex(index + 6); this->face.addIndex(index + 7);
this->face.addIndex(index + 2); this->face.addIndex(index + 7); this->face.addIndex(index + 3);
this->frame.addIndex(index + 0); this->frame.addIndex(index + 1);
this->frame.addIndex(index + 2); this->frame.addIndex(index + 3);
this->frame.addIndex(index + 4); this->frame.addIndex(index + 5);
this->frame.addIndex(index + 6); this->frame.addIndex(index + 7);
for (int i = 0; i < vertices.size(); i++) {
this->face.addColor(ofColor(0));
this->frame.addColor(ofColor(255));
}
}
}
}
}
}
//--------------------------------------------------------------
void ofApp::draw() {
this->cam.begin();
ofRotateY(310);
this->face.draw();
this->frame.drawWireframe();
this->cam.end();
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}