チュートリアル(Tutorial)




ApplicationFW

SAMPLE http://ooo.iiyudana.net/bin/gl/simplewindow.exe( シンプルウィンドウ サンプル ) DESC OpenGL Application の フレームワークとして次のような構成を用意しておくと便利。 手間なら GLUT などが利用できる。
// 1 回だけ virtual void App::onInit(); // 毎 frame ( 描画毎 ) virtual void App::onDraw(); // マウスイベント virtual void App::onMouseEvent();
アプリケーション毎に具体的な処理をオーバーライドする。
class TestApp : public App {} int main() { TestApp app; // メインループ app.run(); return 0; }
__LINK__(仮想関数でフレームワークをつくる)


下準備

POINT OpenGL は 現在のステート( 色、テクスチャ、変換行列 )を利用してプリミティブを描画する。 ステートを把握しておくことが大切。 変更のない設定は最初にまとめてする。
void init() { // DepthTest ON glEnable( GL_DEPTH_TEST ); // Texture なし glDisable( GL_TEXTURE_2D ); // 画面の背景色を設定する。 glClearColor( 0.5f, 0.5f, 0.5f, 1 ); }



描画毎の設定

描画するタイミングでは、画面をクリア、カメラを設定、図形を描画するという流れになる。
void draw() { // 画面をクリアする。 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // 現在の カメラ座標系( カメラから見た位置 )の設定をする glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); // カメラの画角を設定する glMatrixMode( GL_PROJECTION ); glFrustum(); // この設定で三角形を描画 glBegin( GL_TRIANGLES ); glVertex3f( 0,0,0 ); glVertex3f( 1,0,0 ); glVertex3f( 1,1,0 ); glEnd(); }



三角形の描画

三角形を描画するには, glBegin() - glEnd() 間で頂点情報を OpenGL へ転送する。 頂点属性は以下のものがあるが、座標( Position )の指定は必須。
Position // 位置 Normal // 法線 Texcoord // テクスチャ座標 Color // 頂点カラー Fog Edge
指定した頂点列を組み立てる方法を glBegin() のパラメータとして渡す。
GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POLYGON GL_POINTS GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_QUADS GL_QUAD_STRIP



glVertex

POINT OpenGL では座標は、行列変換のために同次座標 として扱われる。
// glVertex4f( 1, 0, 0, 1 ) と同じ。 glVertex3f( 1, 0, 0 ); glVertex3f( 1, 1, 0 ); glVertex3f( 0, 1, 0 );
他の頂点属性は glVertex をした時に Current の State から Copy される。
glBegin(); // Primary Color を赤にする ( w は 1 になる ) glColor3f( 1, 0, 0 ); // Normal , TexCoord も同じように Current が設定される glNormal3f( 0, 0, 1 ); glTexCoord2f( 0, 0 ); // 以降, Primary Color は赤として 各頂点属性に指定される glVertex3f( 1, 0, 0 ); glVertex3f( 1, 1, 0 ); glVertex3f( 0, 1, 0 ); glEnd();



glNormal

SYNTAX glNormal( ); DESC glColor : 同様に glNormal() は定数設定処理が可能
void drawCube3() { float s = 100; static float v[] = { -s, -s, s, s, -s, s, s, s, s, -s, s, s, -s, -s, -s, s, -s, -s, s, s, -s, -s, s, -s, }; static float n[] = { 0, 0, 1, 0, 0, -1, 1, 0, 0, -1, 0, 0, 0, -1, 0, 0, 1, 0, }; // 6 QUADS static unsigned short idx[] = { 0, 1, 2, 3, 4, 5, 6, 7, 1, 5, 6, 2, 4, 0, 3, 7, 4, 5, 1, 0, 3, 2, 6, 7, }; glBegin( GL_QUADS ); for( int i=0; i<6; i++ ){ // glColor と同じように, 現在の State の影響をうける glNormal3fv( n + (3*i) ); for( int j=0; j<4; j++ ){ int p = idx[ 4*i + j ]; glVertex3fv( v + 3*p ); } } glEnd(); glDisable( GL_LIGHTING ); }



glColor

SYNTAX glColor DESC 頂点カラーを設定する。 glVertex() を call すると その時点の Color が設定される。
// 1個のColorで描画 glColor4f( 1,1,1,1 ); glVertex3f(); glVertex3f(); glVertex3f(); // 各頂点で変更 glColor4f( 1,1,1,1 ); glVertex3f(); glColor4f( 1,0,0,1 ); glVertex3f(); glColor4f( 0,0,1,1 ); glVertex3f();