Hello. I’m trying to load a 32 bit image with the Windows Imaging Component and use it as an opengl texture. The problem is that my code generates a runtime error from nvoglv32.dll(presumably the nvidia opengl driver).
Here is the code to load the image with WIC:
VOID GetImageFromFile(LPCWSTR file, IWICBitmap** bitmap)
{
IWICImagingFactory* factory = NULL;
IWICBitmapDecoder* decoder = NULL;
IWICBitmapFrameDecode* frame = NULL;
IWICFormatConverter* converter = NULL;
CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&factory));
factory->CreateDecoderFromFilename(file, NULL, GENERIC_READ | GENERIC_WRITE, WICDecodeMetadataCacheOnDemand, &decoder);
decoder->GetFrame(0, &frame);
factory->CreateFormatConverter(&converter);
converter->Initialize(frame, GUID_WICPixelFormat32bppBGRA, WICBitmapDitherTypeNone, NULL, 0.0, WICBitmapPaletteTypeCustom);
factory->CreateBitmapFromSource(frame, WICBitmapNoCache, bitmap);
SafeRelease(factory);
SafeRelease(decoder);
SafeRelease(frame);
SafeRelease(converter);
}
And here is the code that calls the above function and creates the texture:
BOOL Create(LPCWSTR file)
{
if (!glIsEnabled(GL_TEXTURE_2D))
glEnable(GL_TEXTURE_2D);
IWICBitmap* bitmap = NULL;
IWICBitmapLock* lock = NULL;
GetImageFromFile(file, &bitmap);
if (!bitmap)
return FALSE;
WICRect r = {0};
bitmap->GetSize((UINT*)&r.Width, (UINT*)&r.Height);
BYTE* data = NULL;
UINT len = 0;
bitmap->Lock(&r, WICBitmapLockRead, &lock);
lock->GetDataPointer(&len, &data);
UINT glid = 0;
glGenTextures(1, &glid);
glBindTexture(GL_TEXTURE_2D, glid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, r.Width, r.Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
lock->Release();
bitmap->Release();
return (GL_NO_ERROR == glGetError());
}
If I change GL_BGRA_EXT in the call to glTexImage2D() to GL_BGR_EXT everything works fine. Any ideas why this is happening? If I’m stuck using GL_BGR_EXT will the texture still be created with 4 pixel components(RGBA) as specified? I’m really getting burnt out on experimenting here and all I want is to transfer the alpha channels from the file on disk to the opengl texture.
PS. I know the code is ugly with very little error checking, but after all the experimenting I’ve done I’m certain that the problem lies in the call to glTexImage2D().