Cgをインストールする時に一緒に保存されるサンプルプログラムで,Cgの勉強.
まずは,Cgを使う時のルールみたいなのを,最初のサンプルファイル(01_vertex_program)を参考にまとめてみる.
このプログラムはバーテックスシェーダのみしか使用してないけど,そっちの方がシンプルで読みやすいと思う.
Cgを使う時は,
Cgを使う準備
Cgを有効にする
という作業が必要.
Cgを使う準備
Cgコンテキスト作成
まず最初に,Cgコンテキストを作成する.
myCgContext = cgCreateContext();
プロファイル作成
次に,プロファイルを取得して,シェーダの中身が書かれているファイル(.cg)を読み込む.
myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(myCgVertexProfile);
checkForCgError("selecting vertex profile");
myCgVertexProgram =
cgCreateProgramFromFile(
myCgContext, / Cg runtime context /
CG_SOURCE, / Program in human-readable form /
myVertexProgramFileName, / Name of file containing program /
myCgVertexProfile, / Profile: OpenGL ARB vertex program /
myVertexProgramName, / Entry function name /
NULL); / No extra compiler options /
checkForCgError("creating vertex program from file");
cgGLLoadProgram(myCgVertexProgram);
checkForCgError("loading vertex program");
cgGLGetLatestProfile関数の引数をCG_GL_VERTEXとするとバーテックスシェーダが取得出来る(フラグメントシェーダの場合はCG_GL_FRAGMENTとすればよいのかな?).
cgCreateProgramFromFile関数で,指定したファイル(今回はC2E1v_green.cg)からプログラムを作成.
cgGLLoadProgramで,GPUにシェーダを転送する.
Cgを有効にする
上記のようにして行った設定を,有効化する.
cgGLBindProgram(myCgVertexProgram);
checkForCgError("binding vertex program");
cgGLEnableProfile(myCgVertexProfile);
checkForCgError("enabling vertex profile");
cgGLBindProgram関数でシェーダをバインドして,cgGLEnableProfile関数で,プロファイルを有効にする.
バーテックスシェーダで何をやっているかは別にまとめる.