[ Video ]
[ About ]
真ん中の球体はリボンとの距離を比較して色を変える。
The sphere in the middle changes color based on its distance from the ribbon.
1 2 3 4 5 6 7 8 9 10 11 |
if (glm::distance(avg, this->red_location) > glm::distance(avg, this->blue_location)) { this->sphere_face.addColor(ofColor(0, 203, 255)); this->sphere_face.addColor(ofColor(0, 203, 255)); this->sphere_face.addColor(ofColor(0, 203, 255)); } else{ this->sphere_face.addColor(ofColor(255, 0, 153)); this->sphere_face.addColor(ofColor(255, 0, 153)); this->sphere_face.addColor(ofColor(255, 0, 153)); } |
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
#pragma once #include "ofMain.h" class ofApp : public ofBaseApp { <pre class="lang:default decode:true " title="ofApp.cpp" >#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofNoFill(); ofSetColor(39); ofEnableDepthTest(); ofSetCircleResolution(60); ofSetLineWidth(2); auto ico_sphere = ofIcoSpherePrimitive(80, 5); this->triangle_list.insert(this->triangle_list.end(), ico_sphere.getMesh().getUniqueFaces().begin(), ico_sphere.getMesh().getUniqueFaces().end()); this->line.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES); this->sphere_line.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES); } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(39); this->face.clear(); this->line.clear(); this->sphere_face.clear(); this->sphere_line.clear(); float x_span = 1; auto noise_seed = glm::vec2(ofRandom(1000), ofRandom(1000)); for (int x_param = -1; x_param <= 1; x_param += 2) { int start_index = this->face.getNumVertices(); for (float x = 100; x < 250; x += x_span) { float next_x = x + x_span; int index = this->face.getNumVertices(); float angle_x = ofMap(ofNoise(noise_seed.x, x * 0.0035 + ofGetFrameNum() * 0.0085), 0, 1, PI * -2, PI * 2); auto rotation_x = glm::rotate(glm::mat4(), angle_x, glm::vec3(1, 0, 0)); float angle_y = ofMap(ofNoise(noise_seed.y, x * 0.0035 + ofGetFrameNum() * 0.0085), 0, 1, PI * -2, PI * 2); auto rotation_y = glm::rotate(glm::mat4(), angle_y, glm::vec3(0, 1, 0)); float next_angle_x = ofMap(ofNoise(noise_seed.x, next_x * 0.0035 + ofGetFrameNum() * 0.0085), 0, 1, PI * -2, PI * 2); auto next_rotation_x = glm::rotate(glm::mat4(), next_angle_x, glm::vec3(1, 0, 0)); float next_angle_y = ofMap(ofNoise(noise_seed.y, next_x * 0.0035 + ofGetFrameNum() * 0.0085), 0, 1, PI * -2, PI * 2); auto next_rotation_y = glm::rotate(glm::mat4(), next_angle_y, glm::vec3(0, 1, 0)); vector<glm::vec3> vertices; vertices.push_back(glm::vec4(x * x_param, 20, 0, 0) * rotation_y * rotation_x); vertices.push_back(glm::vec4(next_x * x_param, 20, 0, 0)* next_rotation_y* next_rotation_x); vertices.push_back(glm::vec4(next_x * x_param, -20, 0, 0) * next_rotation_y * next_rotation_x); vertices.push_back(glm::vec4(x * x_param, -20, 0, 0) * rotation_y * rotation_x); this->face.addVertices(vertices); this->line.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->line.addIndex(index + 0); this->line.addIndex(index + 1); this->line.addIndex(index + 2); this->line.addIndex(index + 3); if (x_param == -1) { this->face.addColor(ofColor(255, 0, 153)); this->face.addColor(ofColor(255, 0, 153)); this->face.addColor(ofColor(255, 0, 153)); this->face.addColor(ofColor(255, 0, 153)); }else{ this->face.addColor(ofColor(0, 203, 255)); this->face.addColor(ofColor(0, 203, 255)); this->face.addColor(ofColor(0, 203, 255)); this->face.addColor(ofColor(0, 203, 255)); } if (x == 100 && x_param == -1) { this->red_location = (vertices[0] + vertices[3]) / 2; } if (x == 100 && x_param == 1) { this->blue_location = (vertices[0] + vertices[3]) / 2; } } this->line.addIndex(start_index + 0); this->line.addIndex(start_index + 3); this->line.addIndex(this->line.getNumVertices() - 1); this->line.addIndex(this->line.getNumVertices() - 2); } for (int i = 0; i < this->triangle_list.size(); i++) { vector<glm::vec3> vertices; glm::vec3 avg = (this->triangle_list[i].getVertex(0) + this->triangle_list[i].getVertex(1) + this->triangle_list[i].getVertex(2)) / 3; vertices.push_back(this->triangle_list[i].getVertex(0)); vertices.push_back(this->triangle_list[i].getVertex(1)); vertices.push_back(this->triangle_list[i].getVertex(2)); this->sphere_face.addVertices(vertices); this->sphere_line.addVertices(vertices); if (glm::distance(avg, this->red_location) > glm::distance(avg, this->blue_location)) { this->sphere_face.addColor(ofColor(0, 203, 255)); this->sphere_face.addColor(ofColor(0, 203, 255)); this->sphere_face.addColor(ofColor(0, 203, 255)); } else{ this->sphere_face.addColor(ofColor(255, 0, 153)); this->sphere_face.addColor(ofColor(255, 0, 153)); this->sphere_face.addColor(ofColor(255, 0, 153)); } this->sphere_line.addColor(ofColor(239)); this->sphere_line.addColor(ofColor(239)); this->sphere_line.addColor(ofColor(239)); this->sphere_face.addTriangle(this->sphere_face.getNumVertices() - 1, this->sphere_face.getNumVertices() - 2, this->sphere_face.getNumVertices() - 3); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 1); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 2); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 2); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 3); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 1); this->sphere_line.addIndex(this->sphere_line.getNumVertices() - 3); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofDrawCircle(glm::vec3(), 81); ofRotateX(ofGetFrameNum() * 0.37); ofRotateY(ofGetFrameNum() * 0.72); this->face.draw(); this->line.drawWireframe(); this->sphere_face.draw(); //this->sphere_line.drawWireframe(); this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
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) {};
ofEasyCam cam;
ofMesh face, line, sphere_face, sphere_line;
vector<ofMeshFace> triangle_list;
glm::vec3 blue_location;
glm::vec3 red_location;
};