アプリ版:「スタンプのみでお礼する」機能のリリースについて

こんにちは、

よく探してたが、
C言語のOpenGLで一つだけじゃなくて複数のテクスチャをロードする方法は?

見つかったコードは 「一つだけのテクスチャをロードする」

ーーーーーーーーーーー
void LoadGLTextures() {
// Load Texture
Image *image1;

// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad("Data/01.bmp", image1)) {
exit(1);
}

// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture

// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};
ーーーーーーーーーーー
お願いします

A 回答 (1件)

全容がわからないのですが、これで画像1枚は読み込める、というのであれば下記のようにすれが複数テクスチャを使えるようになると思います。





void LoadGLTextures(char* filename) {
// Load Texture
Image *image1;

// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad(filename, image1)) {
exit(1);
}

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture

// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);

// たぶんここでimage1のfreeが抜けている
};



まず、上記関数を作っておき、読み込みはこのようにします。



int num_textures = 4;
// ↑ texture配列の大きさとあわせてください

glGenTextures(num_textures, texture);


glBindTexture(GL_TEXTURE_2D, texture[0]);
LoadGLTextures("Data/01.bmp");

glBindTexture(GL_TEXTURE_2D, texture[1]);
LoadGLTextures("Data/02.bmp");

glBindTexture(GL_TEXTURE_2D, texture[2]);
LoadGLTextures("Data/03.bmp");

glBindTexture(GL_TEXTURE_2D, texture[3]);
LoadGLTextures("Data/04.bmp");



そして使用時には、このようにすると、対象のテクスチャを使用できます。

glBindTexture(GL_TEXTURE_2D, texture[2]); // texture[2]にセットした画像を使う

// あとは普通の描画
glBegin(...);

...
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!