[work 1] wavy wireframe

[work 1] wavy wireframe

Movie

Source code

about

  • 三角形のワイヤーフレームで600×300の面を生成
  • 各点のz座標をパーリンノイズを使用して変化させる
  • z座標の値をHSBのHにマッピングさせる
  • ofRotateで座標系を回転させる

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"

//========================================
int main( ){

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
	ofSetupOpenGL(1280,720, OF_WINDOW);			// <-------- setup the GL context

	// 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 "Landscape.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:
    ofEasyCam cam;
    Landscape *land;
    float degX;
    float degY;
    float degZ;
};
#include "ofApp.h"

ofApp::ofApp(){
    land = new Landscape();
    degX = 0;
    degY = 0;
    degZ = 0;
}

ofApp::~ofApp(){
    delete land;
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofBackground(0, 0, 0);
    ofSetBackgroundAuto(true);  // clear frame:true
    ofSetFrameRate(fps);
    
    land->setup();
}

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

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    ofRotateXDeg(degX);
    ofRotateZDeg(degZ);
    degX += 0.5;
    degZ += 0.5;
    land->draw();
    cam.end();
}
#ifndef Landscape_hpp
#define Landscape_hpp

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


class Landscape {
public:
    Landscape();
    ~Landscape();
    void setup();
    void update();
    void draw();
    
private:
    ofMesh mesh;
    int cell_size;
    int w, h;
    int land_xoff, land_yoff;
    int rows, cols;
    float xoff, yoff, zoff;
    std::vector<std::vector<float>> z;
    int min_z, max_z;
};
#include "Landscape.hpp"


Landscape::Landscape()
{
    cell_size = 20;
    w = 600;
    h = 400;
    min_z = -200;
    max_z = 200;
}

Landscape::~Landscape()
{
    
}

void Landscape::setup()
{
    cols = w / cell_size;
    rows = h / cell_size;
    
    land_xoff = -(w / 2);
    land_yoff = -(h / 2);
    
    z = std::vector<std::vector<float>>(rows, std::vector<float>(cols, 0));
    
    zoff = 0;
}

void Landscape::update()
{
    yoff = 0;
    for (int i = 0; i < rows; i ++) {
        xoff = 0;
        for (int j = 0; j < cols; j++) {
            z[i][j] = ofMap(ofSignedNoise(xoff, yoff, zoff), -1, 1, min_z, max_z);
            xoff += 0.02;
        }
        yoff += 0.02;
    }
    zoff += 0.01;
}

void Landscape::draw()
{
    ofTranslate(land_xoff, land_yoff, 0);
    ofColor c = ofColor(0);
    
    for (int x = 0; x < (cols - 1); x++) {
        for (int y = 0; y < (rows - 1); y++) {
            int x0 = x * cell_size;
            int y0 = y * cell_size;
            
            mesh.addVertex(ofVec3f(x0, y0, z[y][x]));
            mesh.addVertex(ofVec3f(x0, y0 + cell_size, z[y+1][x]));
            mesh.addVertex(ofVec3f(x0 + cell_size, y0 + cell_size, z[y+1][x+1]));
            mesh.addVertex(ofVec3f(x0 + cell_size, y0, z[y][x+1]));
            
            
            c.setHsb(ofMap(z[y][x], min_z, max_z, 0, 255), 255, 255);
            mesh.addColor(c);
            c.setHsb(ofMap(z[y+1][x], min_z, max_z, 0, 255), 255, 255);
            mesh.addColor(c);
            c.setHsb(ofMap(z[y+1][x+1], min_z, max_z, 0, 255), 255, 255);
            mesh.addColor(c);
            c.setHsb(ofMap(z[y][x+1], min_z, max_z, 0, 255), 255, 255);
            mesh.addColor(c);
            
            mesh.setMode(OF_PRIMITIVE_TRIANGLES);
            mesh.addIndex(0);
            mesh.addIndex(1);
            mesh.addIndex(2);
            mesh.addIndex(0);
            mesh.addIndex(2);
            mesh.addIndex(3);
            
            mesh.drawWireframe();
            mesh.clear();
        }
    }
}

Link to the API reference page

ソースコードで使用したAPIの中からいくつか選んでリストアップしました。

categoryAPI
openframeworksofMesh addVertex
openframeworks
ofMesh setMode
openframeworksofMesh addColor
openframeworksofMesh addIndex
openframeworksofMesh drawWireframe