OpenGL 画像ファイルからテクスチャを読み込む

最終更新日31 Jul 2017 01:20

Reference -> Add->System::Drawingも必要

using namespace System::Drawing;
void loadImage() {
    Bitmap^ bitmap = gcnew Bitmap("image.png");
    int width = bitmap->Width;
    int height = bitmap->Height;
    System::Drawing::Rectangle rect = System::Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
    Imaging::BitmapData ^  bitmapData = bitmap->LockBits(rect, Imaging::ImageLockMode::ReadWrite, bitmap->PixelFormat);
    //Format32bppArgbの想定で進みます
    int size = bitmap->Width*bitmap->Height*sizeof(float);
    bitmap->Size;
    unsigned char* dataTexture = new unsigned char[size];
    memcpy(dataTexture, bitmapData->Scan0.ToPointer(), size);
 
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureName);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, bitmap->Width, bitmap->Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, dataTexture);
 
    bitmap->UnlockBits(bitmapData);
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    delete dataTexture;
 
}

参照[1]より

cpluscli opengl-image


ファイル

サポートサイト Wikidot.com cpluscliopengl-image