[work 82] Roller coaster
![[work 82] Roller coaster](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/08/outFrameImg0100-1.png?fit=1280%2C720&ssl=1)
Movie
Source code
about
- 三角形を並べてコースターのレールを作る
- レールに沿ってカメラを移動させる
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"
//================================
int main( ){
// 4K:4096x2160
// 2K:2048x1080
// FullHD:1920x1080
// HD:1440x1080
// HD720p:1280x720
// DVD:720x480
// setup the GL context
ofSetupOpenGL(1280, 720, OF_WINDOW);
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
ofApp();
~ofApp();
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 mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
private:
ofCamera cam;
ofMesh mesh;
std::vector<ofVec3f> baseT;
std::vector<ofVec3f> direction;
std::vector<std::vector<ofVec3f>> triangles;
std::vector<ofVec3f> camPos;
std::vector<float> camAngle;
float xoff1, xoff2, xoff3;
int startFrame;
int camPosIndex;
int dirIndex;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(255);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
camPos.push_back(ofVec3f(0, 0, 0));
cam.setGlobalPosition(camPos.back());
cam.lookAt(glm::vec3(0, 0, 0));
ofVec3f d = ofVec3f(0, 1, 0);
float ext = 200;
baseT.push_back(d * ext);
baseT.push_back(d.rotate(120, ofVec3f(0, 0, 1)) * ext);
baseT.push_back(d.rotate(120, ofVec3f(0, 0, 1)) * ext);
direction.push_back(ofVec3f(0, 0, 0));
xoff1 = ofRandom(10000);
xoff2 = ofRandom(10000);
xoff3 = ofRandom(10000);
startFrame = 30;
camPosIndex = 0;
dirIndex = 0;
}
//--------------------------------------------------------------
void ofApp::update(){
float xAngle = 0;
float yAngle = 0;
float zAngle = 0;
xAngle = ofMap(ofSignedNoise(xoff1), -1, 1, -90, 90);
yAngle = ofMap(ofSignedNoise(xoff2), -1, 1, -90, 90);
zAngle = ofMap(ofSignedNoise(xoff3), -1, 1, -90, 90);
xoff1 += 0.01;
xoff2 += 0.01;
xoff3 += 0.01;
float dist = 50;
ofVec3f d = ofVec3f(0, 0, -1) * dist;
d.rotate(xAngle, ofVec3f(1, 0, 0));
d.rotate(yAngle, ofVec3f(0, 1, 0));
d.rotate(zAngle, ofVec3f(0, 0, 1));
ofVec3f dir = direction.back() + d;
direction.push_back(dir);
int step = 2;
for (int i = 0; i < step; i++) {
ofVec3f pos = camPos.back() + (d * (1 / (float)step));
camPos.push_back(pos);
camAngle.push_back(zAngle);
}
std::vector<ofVec3f> p = baseT;
for (int i = 0; i < p.size(); i++) {
p.at(i).rotate(xAngle, ofVec3f(1, 0, 0));
p.at(i).rotate(yAngle, ofVec3f(0, 1, 0));
p.at(i).rotate(zAngle, ofVec3f(0, 0, 1));
p.at(i) += direction.at(dirIndex);
}
triangles.push_back(p);
dirIndex++;
if (startFrame < ofGetFrameNum()) {
ofVec3f pos = camPos.at(camPosIndex);
ofVec3f at = camPos.at(camPosIndex + 20);
float angle = camAngle.at(camPosIndex);
cam.setGlobalPosition(pos);
cam.lookAt(at);
cam.rollDeg(angle);
camPosIndex++;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
cam.begin();
mesh.clear();
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
for (std::vector<ofVec3f> t : triangles) {
mesh.addVertex(t.at(0));
mesh.addVertex(t.at(1));
mesh.addVertex(t.at(2));
mesh.addColor(ofColor(0));
mesh.addColor(ofColor(0));
mesh.addColor(ofColor(0));
mesh.drawWireframe();
}
cam.end();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 's') {
ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
img.save("screenshot.png");
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofCamera |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode