如何加载bmp到GLubyte数组? [英] How to load bmp into GLubyte array?

查看:661
本文介绍了如何加载bmp到GLubyte数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我正在尝试将一个bmp文件加载到GLubyte数组中(不使用aux)。

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.

在Google上似乎找不到任何东西!

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);

会发生什么是LoadImage的调用似乎返回未定义的值确定它是否真的加载bmp或有点困惑)。

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).

现在我把bmps转换为raw,这很容易。

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

任何人都有更好更干净的代码段?

Anyone has any better and cleaner snippet?

推荐答案

LoadImage ()只能使用资源编译器加载嵌入到可执行文件中的位图 - 它无法从文件系统加载外部位图。幸运的是,位图文件是真的很容易阅读自己。有关文件格式的说明,请参见维基百科

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.

只要以任何其他文件打开文件(重要:以二进制模式打开该文件,即使用rb选项使用 fopen ios :: binary 标志使用C ++ ifstream ),读取位图维度,并读入原始像素数据。

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.

这篇关于如何加载bmp到GLubyte数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆