2008/05/12

Multiple Texture

テクスチャーを2種類使ったサンプルです。どうやら次のような手順で2種類のテクスチャーを使うようです。
Sample procedure for using two texture.

#設定:一度やればいいようです。
#Setting parameters - you shoud do this only once
glEnable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

#テクスチャーの空き番号を必要個数取得します。
# get multiple available texture ID
texture=glGenTextures(2)

#ひとつ目のテクスチャーに設定
# set the first texture as current texture
glBindTexture(GL_TEXTURE_2D, texture[0])

# 画像ファイルから数列にテクスチャデータを読み込みます
# read data from graphic file and store data in image.tostring()
image=Image.open("ksmt.bmp")
if len(image.getbands())!=4:
image=image.convert("RGBA")
size=image.size

# image.tostring()という数列のデータを使ってテクスチャーを作ります
# generate texture from image.tostring()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size[0], size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, image.tostring())

# 意味不明ですが、glTexImage2Dの後に次の2行がないとうまくテクスチャ ができません。
# I don't know why I should do this every time after glTexImage2D, but I must do this anyway.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

# 二つ目のテクスチャに設定します。
# set the second texture as current texture
glBindTexture(GL_TEXTURE_2D, texture[1])

# ファイル(この場合TIF)から数列に画像データを読み込みます
# read data from the second file to memory
image=Image.open("f.tif")
if len(image.getbands())!=4:
image=image.convert("RGBA")
size=image.size

# 数列からテクスチャを作ります
# generate the second texture from memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size[0], size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, image.tostring())

# 再びこの2行が必要です。どうやらテクスチャごとに必要のようです。
# I must do this again. I don't know the reason. This looks like a paramater setting.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

#Display Funcの中でテクスチャを指定します。
#inside Display function
glDisable(GL_TEXTURE_2D) #テクスチャ不要の場合 no texture
#図形描画 #テクスチャなし図形 instanciate object with no texture
glEnable(GL_TEXTURE_2D) #テクスチャ貼り付け enable texture mapping
glBindTexture(GL_TEXTURE_2D, texture[0]) #ひとつ目のテクスチャを使う use the first texture
# 図形描画 #ひとつ目のテクスチャ instance object with the first texture
glBindTexture(GL_TEXTURE_2D, texture[0]) #ふたつ目のテクスチャを使う secont texture
#図形描画 #ふたつ目のテクスチャ instance object with the second texture

0 件のコメント: