[work 92] Rotating Spheres

[work 92] Rotating Spheres

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"
#include "Controller.hpp"


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:
    std::shared_ptr<Controller> control;
    
    ofLight light;
    ofEasyCam cam;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(0);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    ofEnableDepthTest();
    ofEnableSmoothing();
    
    light.enable();
    light.setPointLight();
    light.setPosition(glm::vec3(0, 800, 0));
    light.setAmbientColor(ofFloatColor(0.6, 0.6, 0.6, 1.0));
    light.setDiffuseColor(ofFloatColor(0.4, 0.4, 0.4));
    light.setSpecularColor(ofFloatColor(1.0, 1.0, 1.0));
    
    control = make_shared<Controller>();
    control->setup();
}

//--------------------------------------------------------------
void ofApp::update(){
    control->update();
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    control->display();
    cam.end();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    if (key == 's') {
        ofImage img;
        img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
        img.save("screenshot.png");
    }
}
#ifndef Controller_hpp
#define Controller_hpp

#include <stdio.h>
#include "ofMain.h"
#include "Sphere.hpp"

class Controller {
public:
    Controller();
    ~Controller();
    void setup();
    void update();
    void display();
    
private:
    std::vector<std::shared_ptr<Sphere>> spheres;
};
#endif /* Controller_hpp */
#include "Controller.hpp"


Controller::Controller()
{
    
}


Controller::~Controller()
{
    
}


void Controller::setup()
{
    for (int i = -1; i < 2; i++) {
        for (int j = -1; j < 2; j++) {
            spheres.push_back(make_shared<Sphere>(glm::vec3(i * 250, 0, j * 250)));
            spheres.back()->setup();
            glm::vec3 axis = glm::vec3(0, 0, 0);
            while (axis == glm::vec3(0, 0, 0)) {
                axis = glm::vec3((int)ofRandom(-2, 2), (int)ofRandom(-2, 2), (int)ofRandom(-2, 2));
            }
            spheres.back()->setRotAxis(axis);
            spheres.back()->start();
        }
    }
}


void Controller::update()
{
    for (shared_ptr<Sphere> s : spheres) {
        s->update();
    }
}


void Controller::display()
{
    for (shared_ptr<Sphere> s : spheres) {
        s->display();
    }
}
#ifndef Sphere_hpp
#define Sphere_hpp

#include <stdio.h>
#include "ofMain.h"

class Sphere {
public:
    Sphere(glm::vec3 l);
    ~Sphere();
    void setup();
    void update();
    void display();
    
    void setFrontColor(ofColor c);
    void setBackColor(ofColor c);
    void setRotAxis(glm::vec3 a);
    void start();
    void pause();
    
private:
    void paint();
    
    std::vector<ofMeshFace> faces;
    bool startRot;
    float radius;
    glm::vec3 location;
    glm::vec3 rotAxis;
    float rotAngle;
    ofVboMesh mesh;
    ofVboMesh meshFrame;
    ofColor frontCol, backCol;
};
#endif /* Sphere_hpp */
#include "Sphere.hpp"



Sphere::Sphere(glm::vec3 l)
{
    location = l;
}


Sphere::~Sphere()
{
    
}


void Sphere::setup()
{
    pause();
    
    ofIcoSpherePrimitive sphere = ofIcoSpherePrimitive(60, 3);
    faces = sphere.getMesh().getUniqueFaces();
    
    ofIcoSpherePrimitive sphereFrame = ofIcoSpherePrimitive(120, 2);
    meshFrame = sphereFrame.getMesh();
    
    setFrontColor(ofColor(255));
    setBackColor(ofColor(128));
    paint();
    
    setRotAxis(glm::vec3(0, 1, 0));
    rotAngle = 180 / (ofGetTargetFrameRate() * 3);
}


void Sphere::update()
{
    if (startRot) {
        for (auto &v : mesh.getVertices()) {
            glm::mat4 m = glm::rotate(glm::mat4(), ofDegToRad(rotAngle), rotAxis);
            v = glm::vec4(v, 0) * m;
        }
        
        for (auto &v : meshFrame.getVertices()) {
            glm::mat4 m = glm::rotate(glm::mat4(), ofDegToRad(rotAngle), rotAxis);
            v = glm::vec4(v, 0) * m;
        }
    }
}


void Sphere::display()
{
    ofPushMatrix();
    ofTranslate(location);
    mesh.draw();
    meshFrame.drawWireframe();
    ofPopMatrix();
}



void Sphere::setFrontColor(ofColor c)
{
    frontCol = c;
}


void Sphere::setBackColor(ofColor c)
{
    backCol = c;
}


void Sphere::setRotAxis(glm::vec3 a)
{
    rotAxis = a;
}


void Sphere::start()
{
    startRot = true;
}


void Sphere::pause()
{
    startRot = false;
}


void Sphere::paint()
{
    for (ofMeshFace &f : faces) {
        for (int i = 0; i < 3; i++) {
            mesh.addVertex(f.getVertex(i));
        }
        
        glm::vec3 n = f.getFaceNormal();
        if (n.z < 0) {
            mesh.addColor(frontCol);
            mesh.addColor(frontCol);
            mesh.addColor(frontCol);
        } else {
            mesh.addColor(backCol);
            mesh.addColor(backCol);
            mesh.addColor(backCol);
        }
    }
}

Link to the reference page

ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。

categoryAPI/Lib
openframeworksofVboMesh
openframeworksofMesh
openframeworksglm::rotate
openframeworksofIcoSpherePrimitive
openframeworksofLight

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode