I am Charmie

メモとログ

05_texture_sampling_program

今回は,04で使用しなかったtextureについて.
バーテックスシェーダは前回と同じで,フラグメントシェーダが変わっている.ひとまずC3E3f_texture.cgを載せる.
struct C3E3f_Output {
float4 color : COLOR;
};

C3E3f_Output C3E3f_texture(float2 texCoord : TEXCOORD0,
uniform sampler2D decal : TEX0)
{
C3E3f_Output OUT;
OUT.color = tex2D(decal,texCoord);
return OUT;
}
今回のフラグメントシェーダは二つの引数(テクスチャ座標とsampler2D型のdecal)を入力としている.テクスチャ座標はラスタライザによって内挿されたもの.同様にラスタライザによって内挿されたカラーは使わない.
samplerというものは,Cgがサンプルできる外部オブジェクト(テクスチャとか)を意味する.今回のsampler2Dの場合,2次元のテクスチャを示す事になる.
各Sampler TypeにおけるApplicationsについての表を,みにくいかもしれないけど転載する.

sampler1D: 1D functions
sampler2D: Decals, normal maps, gloss maps, shadow maps, and others
sampler3D: Volumetric data, 3D attenuation functions
samplerCUBE: Environment maps, normalization cube maps
samplerRECT; Video images, photographs, temporary buffers

sampler2Dが一番色々と使われそうな感じがする.
実際にプログラムを実行してみると,テクスチャをマッピングされた三角形が表示される.

GL側では,テクスチャの設定が加わっている.
main関数内(テクスチャのロード)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); / Tightly packed texture data. /
glBindTexture(GL_TEXTURE_2D, 666);
/ Load demon decal texture with mipmaps. /
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8,
128, 128, GL_RGB, GL_UNSIGNED_BYTE, myDemonTextureImage);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
cgGLSetTextureParameter(myCgFragmentParam_decal, 666);
checkForCgError("setting decal 2D texture");

display関数内(テクスチャパラメータを有効にする)
cgGLEnableTextureParameter(myCgFragmentParam_decal);
checkForCgError("enable decal texture");