费劲千辛万苦(真的是很辛苦~)终于解决Qt下使用OpenGL时发生的贴图问题了。使用了QImage压根不行,也不明白为什么Qt自带的例子里能载入进去。或许和位图的大小,还有深度等有关吧。
具体原因我也不清楚,但总之现在能给各种模型都整上纹理贴图了,那就很不错啦,哈。使用的是一个BMPTextrue类。具体内容如下。
CBMPLoader.h的内容:
具体原因我也不清楚,但总之现在能给各种模型都整上纹理贴图了,那就很不错啦,哈。使用的是一个BMPTextrue类。具体内容如下。
CBMPLoader.h的内容:
01 | #ifndef __CBMPLOADER_H__ |
02 | #define __CBMPLOADER_H__ |
03 | |
04 | #include "stdafx.h" |
05 | |
06 | #define BITMAP_ID 0x4D42 /**< 位图文件的标志 */ |
07 | |
08 | /** 位图载入类 */ |
09 | class CBMPLoader |
10 | { |
11 | public : |
12 | CBMPLoader(); |
13 | ~CBMPLoader(); |
14 | |
15 | bool LoadBitmap( const char * filename); /**< 装载一个bmp文件 */ |
16 | void FreeImage(); /**< 释放图像数据 */ |
17 | |
18 | unsigned int ID; /**< 生成纹理的ID号 */ |
19 | int imageWidth; /**< 图像宽度 */ |
20 | int imageHeight; /**< 图像高度 */ |
21 | unsigned char *image; /**< 指向图像数据的指针 */ |
22 | }; |
23 | |
24 | #endif //__CBMPLOADER_H__ |
CBMPLoader.cpp的内容:
01 | #include"CBMPLoader.h" /**< 包含头文件 */ |
02 | |
03 | /** 构造函数 */ |
04 | CBMPLoader::CBMPLoader() |
05 | { |
06 | /** 初始化成员值为0 */ |
07 | image = 0; |
08 | imageWidth = 0; |
09 | imageHeight = 0; |
10 | } |
11 | |
12 | /** 析构函数 */ |
13 | CBMPLoader::~CBMPLoader() |
14 | { |
15 | FreeImage(); /**< 释放图像数据占据的内存 */ |
16 | } |
17 | |
18 | /** 装载一个位图文件 */ |
19 | bool CBMPLoader::LoadBitmap( const char *file) |
20 | { |
21 | FILE *pFile = 0; /**< 文件指针 */ |
22 | |
23 | /** 创建位图文件信息和位图文件头结构 */ |
24 | BITMAPINFOHEADER bitmapInfoHeader; |
25 | BITMAPFILEHEADER header; |
26 | |
27 | unsigned char textureColors = 0; /**< 用于将图像颜色从BGR变换到RGB */ |
28 | |
29 | /** 打开文件,并检查错误 */ |
30 | pFile = fopen (file, "rb" ); |
31 | if (pFile == 0) |
32 | return false ; |
33 | |
34 | /** 读入位图文件头信息 */ |
35 | fread (&header, sizeof (BITMAPFILEHEADER), 1, pFile); |
36 | |
37 | /** 检查该文件是否为位图文件 */ |
38 | if (header.bfType != BITMAP_ID) |
39 | { |
40 | fclose (pFile); /**< 若不是位图文件,则关闭文件并返回 */ |
41 | return false ; |
42 | } |
43 | |
44 | /** 读入位图文件信息 */ |
45 | fread (&bitmapInfoHeader, sizeof (BITMAPINFOHEADER), 1, pFile); |
46 | |
47 | /** 保存图像的宽度和高度 */ |
48 | imageWidth = bitmapInfoHeader.biWidth; |
49 | imageHeight = bitmapInfoHeader.biHeight; |
50 | |
51 | /** 确保读取数据的大小 */ |
52 | if (bitmapInfoHeader.biSizeImage == 0) |
53 | bitmapInfoHeader.biSizeImage = bitmapInfoHeader.biWidth * |
54 | bitmapInfoHeader.biHeight * 3; |
55 | |
56 | /** 将指针移到数据开始位置 */ |
57 | fseek (pFile, header.bfOffBits, SEEK_SET); |
58 | |
59 | /** 分配内存 */ |
60 | image = new unsigned char [bitmapInfoHeader.biSizeImage]; |
61 | |
62 | /** 检查内存分配是否成功 */ |
63 | if (!image) /**< 若分配内存失败则返回 */ |
64 | { |
65 | delete [] image; |
66 | fclose (pFile); |
67 | return false ; |
68 | } |
69 | |
70 | /** 读取图像数据 */ |
71 | fread (image, 1, bitmapInfoHeader.biSizeImage, pFile); |
72 | |
73 | /** 将图像颜色数据格式进行交换,由BGR转换为RGB */ |
74 | for ( int index = 0; index < ( int )bitmapInfoHeader.biSizeImage; index+=3) |
75 | { |
76 | textureColors = image[index]; |
77 | image[index] = image[index + 2]; |
78 | image[index + 2] = textureColors; |
79 | } |
80 | |
81 | fclose (pFile); /**< 关闭文件 */ |
82 | return true ; /**< 成功返回 */ |
83 | } |
84 | |
85 | /** 释放内存 */ |
86 | void CBMPLoader::FreeImage() |
87 | { |
88 | /** 释放分配的内存 */ |
89 | if (image) |
90 | { |
91 | delete [] image; |
92 | image = 0; |
93 | } |
94 | } |
使用方法:
1 | //载入位图 |
2 | m_BMPTexture.LoadBitmap(filename) |
3 | //位图的宽 |
4 | m_BMPTexture.imageWidth |
5 | //位图的高 |
6 | m_BMPTexture.imageHeight |
7 | //位图数据 |
8 | m_BMPTexture.image |
这次就难去截图了,将就点吧,有问题评论就是了~
本文固定链接: http://liusir.name/opengl-texture-problems-qt-version.html | 民警小刘