openFrameworks で Shader を使う方法をみてゆきます。


GLSL

シェーダとは、3DCGにおいて陰影処理を行うプログラムをいいます。

そして、シェーダのためのプログラミング言語をシェーディング言語といいます。

openFrameworks では、GLSL(OpenGL Shading Language)をサポートしています。

GLSL はバージョンがいくつかあります(参考)が、ここでは GLSL バージョン 1.5 を使いたいと思います。 今回使用するバージョンは下記です。

OpenGL version 3.2
GLSL version 1.5
version directive #version150

OpenGL version 3.2 を使うための準備

まずは、OpenGL version 3.2 を使うために、main.cpp の変更が必要です。

#include "ofMain.h"
#include "ofApp.h"

int main() {
    // ofSetupOpenGL(1024, 768, OF_WINDOW);

    ofGLWindowSettings settings;
    settings.setGLVersion(3, 2);
    ofCreateWindow(settings);

    ofRunApp(new ofApp());
}

平面プリミティブの作成

平面プリミティブ ofPlanePrimitive に GLSL を適用させてみます。

...略...

class ofApp : public ofBaseApp {
    public:
    ...略...
    
    ofPlanePrimitive plane;
};
void ofApp::setup() {
    float planeScale = 0.75;
    int planeWidth = ofGetWidth() * planeScale;
    int planeHeight = ofGetHeight() * planeScale;
    int planeGridSize = 20;
    int planeColums = planeWidth / planeGridSize;
    int planeRows = planeHeight / planeGridSize;
    plane.set(planeWidth, planeHeight, planeColums, planeRows, OF_PRIMITIVE_TRIANGLES);
}

void ofApp::draw() {
    ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    plane.drawWireframe();
    // plane.draw();
}

shader01.png