最終更新:
mikk_ni3_92 2010年02月21日(日) 18:34:17履歴
現在地:メニュー >> Cg >> Cg編07
Index :Cg編06<< Cg編07 >> Cg編08
基本的にはCg編04と変わらない。
【例】:画像の読み込み
今回は、物体の中心に原点があるオブジェクト座標系で考える。
そこでテクセル値は、頂点座標のベクトルをそのまま使ってサンプリングする。
【頂点シェーダ】
【フラグメントシェーダ】
Index :Cg編06<< Cg編07 >> Cg編08
基本的にはCg編04と変わらない。
- 画像を読み込んでキューブマップオブジェクトとして格納
- テクセル値を取り出す時には、「texCUBE関数」を使う。
【例】:画像の読み込み
//テクスチャ読み込み void LoadTexture() { GLenum cubefaces[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z }; //6つを1つにまとめて扱う事ができる glGenTextures(1,&texId); glBindTexture(GL_TEXTURE_CUBE_MAP,texId); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); cv::Ptr<IplImage> imgA; for(int loop = 0;loop < 6;++loop) { imgA = cvLoadImage(filename[loop]); if(imgA.empty()) { std::cerr << filename[loop] << " : Can't Load Image\n"; exit(EXIT_FAILURE); } glTexImage2D(cubefaces[loop],0,GL_RGB,imgA->width,imgA->height,0,GL_BGR,GL_UNSIGNED_BYTE,imgA->imageData); imgA.release(); } glBindTexture(GL_TEXTURE_CUBE_MAP,0); } … … //Cgの有効化 cgGLBindProgram(CgVertexProgram); cgGLEnableProfile(CgVertexProfile); cgGLBindProgram(CgFragmentProgram); cgGLEnableProfile(CgFragmentProfile); cgGLEnableTextureParameter(CgFs_CubeDecal); Draw(); cgGLDisableTextureParameter(CgFs_CubeDecal);//無効化 cgGLDisableProfile(CgVertexProfile); cgGLDisableProfile(CgFragmentProfile);
今回は、物体の中心に原点があるオブジェクト座標系で考える。
そこでテクセル値は、頂点座標のベクトルをそのまま使ってサンプリングする。
【頂点シェーダ】
//入力頂点 struct VertexIn { float4 position : POSITION; float3 color : COLOR; }; //出力頂点 struct VertexOut { float4 position : POSITION; float3 color : COLOR; float3 texcoord : TEX0; }; //-------- 頂点シェーダメイン関数 ---------// VertexOut CgVertexMain(in VertexIn input, uniform float4x4 modelViewProj, … …) { VertexOut output;//出力用 … … output.texcoord = input.position.xyz;//頂点のベクトルをテクスチャ座標にする return output; }
【フラグメントシェーダ】
//フラグメントの入力 struct FragmentIn { float4 position : POSITION; //ラスタライズ用(使用しない) float3 color : COLOR; float3 texcoord : TEX0;//(x,y,z) }; //フラグメントの出力 struct FragmentOut { float3 color : COLOR; }; //--------- フラグメントシェーダメイン関数 ------------// FragmentOut CgFragmentMain(in FragmentIn input,uniform samplerCUBE cubeDecal) { FragmentOut output;//フラグメント出力 output.color = texCUBE(cubeDecal,input.texcoord); return output; }