How to load bmp into GLubyte array?

大兔子大兔子 提交于 2019-12-11 17:25:44

问题


All,

I am trying to load up a bmp file into a GLubyte array (without using aux).

It is unbelievable how what I thought would have been a trivial task is sucking up hours of my time.

Can't seem to find anything on Google!

This is what I hacked together but it's not quite working:

// 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 the call to LoadImage seems to be returning Undefined Value (NULL? I am not able to figure out if it's actually loading the bmp or not - a bit confused).

At the moment I am converting bmps to raw then it's all easy.

Anyone has any better and cleaner snippet?


回答1:


LoadImage() can only load bitmaps that are embedded into your executable file with the resource compiler - it can't load external bitmaps from the filesystem. Fortunately, bitmap files are really simple to read yourself. See Wikipedia for a description of the file format.

Just open up the file like you would with any other file (important: open it in binary mode, i.e. with the "rb" option using fopen or the ios::binary flag using the C++ ifstream), read in the bitmap dimensions, and read in the raw pixel data.




回答2:


It is a common task, that's why glaux, among others, gives you functions for it.

Reading a bitmap is a trivial matter, especially if there is only one depth/bpp to account for.

Also see this question.



来源:https://stackoverflow.com/questions/328019/how-to-load-bmp-into-glubyte-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!