Live2DのTexture Bind

Live2D Cubism SDKを使ったプログラミングのTexture Bindに関するTipsです.

Cubism SDKのサンプルSimpleLive2Dのsimpleプロジェクトのソースコードを参照すると,Texture Bindのコードは以下のようになっています.

  • Texture Bindのサンプルコード
loadGLTexture
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int loadGLTexture(const char* path)
{
unsigned int id;
...
glEnable(GL_TEXTURE_2D);
glGenTextures(1 , &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
return id;
}

この部分をQtのフレームワークを使って置き換える場合,設定を上手く合わせてやる必要があります.

  • QtでのTexture Bind
loadGLTextureQt
1
2
3
4
5
6
7
8
9
10
GLuint loadGLTextureQt(QGLContext* glContext, const QString& path)
{
QImage textureImage(path);
GLuint textureID = glContext->bindTexture(textureImage, GL_TEXTURE_2D, GL_RGBA, QGLContext::MipmapBindOption);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
return textureID;
}

自分の環境では,QGLContextのbindTexture関数で,QGLContext::MipmapBindOptionを指定してやることにより,サンプルプログラムと同じ表示を行うことができました.Qtを使うことで,画像の読み込み⇒Texture Bindの処理を簡略化して書けるので,コーディングがかなり楽です.