How do I load bmp into a GLubyte array?
All,
I am trying to load a BMP file into a GLubyte array (without using aux).
Incredible how what I thought was a trivial task was to suck hours of my time.
It seems that something was not found on Google!
This is what I hacked together, but it doesn't quite work:
// load texture
GLubyte *customTexture;
string fileName("C:\\Development\\Visual Studio 2008\\Projects\\OpenGL_Test\\Crate.bmp");
// Use LoadImage() to get the image loaded into a DIBSection
HBITMAP hBitmap = (HBITMAP)LoadImage( NULL, (LPCTSTR)const_cast<char*>(fileName.c_str()), IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
customTexture = new GLubyte[3*256*256]; // size should be the size of the .bmp file
GetBitmapBits(hBitmap, 3*256*256, (LPVOID) customTexture);
GetBitmapDimensionEx(hBitmap,&szBitmap);
What happens is that the call to LoadImage seems to return Undefined Value (NULL? I can't figure out if it actually loads the BMP or not - a little confused).
I'm currently converting bmps to raw, but it's all easy.
Does anyone have a better and cleaner snippet?
source to share
LoadImage()
can only load bitmaps that are embedded in your executable using the resource compiler - it cannot load external bitmaps from the file system. Fortunately, bitmap files are really easy to read. See Wikipedia for a description of the file format.
Just open the file like any other file ( important ): open it in binary mode, that is, with the option "rb"
, using fopen
or ios::binary
using C ++ ifstream
), read in bitmap dimensions and read in raw pixel data.
source to share
This is a common task, which is why glaux provides you with functions for it, among other things.
Reading a bitmap is a trivial matter, especially if only one depth / bpp is required for accounting.
Also see this question .
source to share