[work 93] Electric Bulletin Board

[work 93] Electric Bulletin Board

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;
    
    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();
    
    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:
    void setChar(char c);
    
    ofTrueTypeFont font;
    std::string message;
    int msgIndex;
    std::vector<std::vector<std::shared_ptr<Sphere>>> spheres;
};
#endif /* Controller_hpp */
#include "Controller.hpp"


Controller::Controller()
{
    
}


Controller::~Controller()
{
    
}


void Controller::setup()
{
    ofTexture tex;
    ofPixels pix;
    ofFbo fbo;
    
    font.load("font/Helvetica.ttc", 10);
    
    message = "HELLOWORLD";
    msgIndex = 0;
    
    int maxW = 0;
    int maxH = 0;
    for (int i = 0; i < message.size(); i++) {
        std::string s(1, message.at(i));
        tex = font.getStringTexture(s);
        
        fbo.allocate(tex.getWidth(), tex.getHeight());
        fbo.begin();
        ofClear(0);
        tex.draw(0, 0);
        fbo.end();
        
        fbo.readToPixels(pix);
        
        maxW = std::max((int)pix.getWidth(), maxW);
        maxH = std::max((int)pix.getHeight(), maxH);
    }
    
    float size = 130;
    maxW += 2;
    for (int i = 0; i < maxW; i++) {
        std::vector<std::shared_ptr<Sphere>> s;
        for (int j = 0; j < maxH; j++) {
            s.push_back(make_shared<Sphere>(glm::vec3(i * size - ((size * (maxW - 1)) / 2), -j * size + ((size * (maxH - 1)) / 2), 0)));
            s.back()->setup();
            glm::vec3 axis = glm::vec3(0, -1, 0);
            s.back()->setRotAxis(axis);
        }
        spheres.push_back(s);
    }
}


void Controller::update()
{
    bool isrotate = false;
    for (std::vector<std::shared_ptr<Sphere>> sCol : spheres) {
        for (std::shared_ptr<Sphere> sRow : sCol) {
            sRow->update();
            if (sRow->isRotate()) {
                isrotate = true;
            }
        }
    }
    
    if (isrotate == false) {
        if (msgIndex < message.size()) {
            setChar(message.at(msgIndex));
            msgIndex++;
            
            for (std::vector<std::shared_ptr<Sphere>> sCol : spheres) {
                for (std::shared_ptr<Sphere> sRow : sCol) {
                    sRow->start();
                }
            }
        }
    }
}


void Controller::display()
{
    for (std::vector<std::shared_ptr<Sphere>> sCol : spheres) {
        for (std::shared_ptr<Sphere> sRow : sCol) {
            sRow->display();
        }
    }
}


void Controller::setChar(char c)
{
    // clear color
    for (std::vector<std::shared_ptr<Sphere>> sCol : spheres) {
        for (std::shared_ptr<Sphere> sRow : sCol) {
            sRow->setBackColor(ofColor(64));
        }
    }
    
    // set color
    ofTexture tex;
    ofPixels pix;
    ofFbo fbo;
    string str(1, c);
    
    tex = font.getStringTexture(str);
    
    fbo.allocate(tex.getWidth(), tex.getHeight());
    fbo.begin();
    ofClear(0);
    tex.draw(0, 0);
    fbo.end();
    
    fbo.readToPixels(pix);
    
    
    int offsetX = 1;
    int offsetY = 1;
    for (int i = 0; i < pix.getWidth(); i++) {
        for (int j = 0; j < pix.getHeight(); j++) {
            if (pix.getColor(i, j) != ofColor(0, 0, 0, 0)) {
                spheres.at(i + offsetX).at(j + offsetY)->setBackColor(ofColor(255, 255, 0));
            }
        }
    }
}
#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();
    bool isRotate();
    
private:
    void paint();
    
    std::vector<ofMeshFace> faces;
    bool startRot;
    glm::vec3 location;
    glm::vec3 rotAxis;
    int 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(40, 4);
    faces = sphere.getMesh().getUniqueFaces();
    
    ofIcoSpherePrimitive sphereFrame = ofIcoSpherePrimitive(60, 1);
    meshFrame = sphereFrame.getMesh();
    
    setFrontColor(ofColor(64));
    setBackColor(ofColor(64));
    
    setRotAxis(glm::vec3(0, 1, 0));
    rotAngle = 0;
    
    startRot = false;
}


void Sphere::update()
{
    if (startRot) {
        rotAngle = (rotAngle + 2) % 360;
        if (rotAngle == 0) {
            startRot = false;
        }
    }
}


void Sphere::display()
{
    ofPushMatrix();
    ofTranslate(location);
    ofRotateDeg(rotAngle, rotAxis.x, rotAxis.y, rotAxis.z);
    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()
{
    paint();
    startRot = true;
}


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


bool Sphere::isRotate()
{
    return startRot;
}


void Sphere::paint()
{
    mesh.clear();
    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
openframeworksofTrueTypeFont
openframeworksofTexture
openframeworksofPixels
openframeworksofFbo
openframeworksofVboMesh
openframeworksofMesh

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode