hellkite 日記と雑記とメモ。

Shiki Kazamaの駄文と音楽と、時々技術な感じ

GLSLでsamplerを使うときはTextureのImage Formatに注意


スポンサーリンク


はい、タイトルそのまんまです。。

ドキュメントをよく読まずにハマる典型ですね。今回は、2日はまりました。。

So for samplers, floating-point samplers begin with "sampler". Signed integer samplers begin with "isampler", and unsigned integer samplers begin with "usampler". If you attempt to read from a sampler where the texture's Image Format doesn't match the sampler's basic format (usampler2D with a GL_R8I, or sampler1D with GL_R8UI, for example), all reads will produce undefined values.

Sampler (GLSL) - OpenGL Wiki

ちゃんと書いてあるっていうね。


RGB 3ch 8bitのテクスチャをusamplerで取得する場合、

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, pixel_width, pixel_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);

ではなく、

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8UI, pixel_width , pixel_height, 0, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 0);

が、正しい。