Qt下OpenGL贴图问题

   费劲千辛万苦(真的是很辛苦~)终于解决Qt下使用OpenGL时发生的贴图问题了。使用了QImage压根不行,也不明白为什么Qt自带的例子里能载入进去。或许和位图的大小,还有深度等有关吧。
  具体原因我也不清楚,但总之现在能给各种模型都整上纹理贴图了,那就很不错啦,哈。使用的是一个BMPTextrue类。具体内容如下。
  CBMPLoader.h的内容:
 
01#ifndef __CBMPLOADER_H__
02#define __CBMPLOADER_H__
03  
04#include "stdafx.h"
05  
06#define BITMAP_ID 0x4D42    /**< 位图文件的标志 */
07  
08/** 位图载入类 */
09class CBMPLoader
10{
11   public:
12      CBMPLoader();
13      ~CBMPLoader();
14  
15      boolLoadBitmap(constchar* filename);/**< 装载一个bmp文件 */
16      voidFreeImage();               /**< 释放图像数据 */
17  
18      unsignedintID;                /**< 生成纹理的ID号 */
19      intimageWidth;                 /**< 图像宽度 */
20      intimageHeight;                /**< 图像高度 */
21      unsignedchar*image;           /**< 指向图像数据的指针 */
22};
23  
24#endif //__CBMPLOADER_H__

CBMPLoader.cpp的内容:

01#include"CBMPLoader.h"              /**< 包含头文件 */
02  
03/** 构造函数 */
04CBMPLoader::CBMPLoader()
05{
06   /** 初始化成员值为0 */
07    image = 0;
08    imageWidth = 0;
09    imageHeight = 0;
10}
11  
12/** 析构函数 */
13CBMPLoader::~CBMPLoader()
14{
15   FreeImage();/**< 释放图像数据占据的内存 */
16}
17  
18/** 装载一个位图文件 */
19bool CBMPLoader::LoadBitmap(const char *file)
20{
21    FILE*pFile = 0;/**< 文件指针 */
22  
23    /** 创建位图文件信息和位图文件头结构 */
24    BITMAPINFOHEADER bitmapInfoHeader;
25    BITMAPFILEHEADER header;
26  
27    unsignedchartextureColors = 0;/**< 用于将图像颜色从BGR变换到RGB */
28  
29   /** 打开文件,并检查错误 */
30        pFile =fopen(file,"rb");
31                if(pFile == 0)
32                    returnfalse;
33  
34    /** 读入位图文件头信息 */
35    fread(&header,sizeof(BITMAPFILEHEADER), 1, pFile);
36  
37    /** 检查该文件是否为位图文件 */
38    if(header.bfType != BITMAP_ID)
39       {
40           fclose(pFile);            /**< 若不是位图文件,则关闭文件并返回 */
41           returnfalse;
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 =newunsignedchar[bitmapInfoHeader.biSizeImage];
61  
62    /** 检查内存分配是否成功 */
63    if(!image)                       /**< 若分配内存失败则返回 */
64       {
65           delete[] image;
66           fclose(pFile);
67           returnfalse;
68       }
69  
70    /** 读取图像数据 */
71    fread(image, 1, bitmapInfoHeader.biSizeImage, pFile);
72  
73    /** 将图像颜色数据格式进行交换,由BGR转换为RGB */
74    for(intindex = 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    returntrue;        /**< 成功返回 */
83}
84  
85/** 释放内存 */
86void CBMPLoader::FreeImage()
87{
88   /** 释放分配的内存 */
89   if(image)
90      {
91         delete[] image;
92         image = 0;
93      }
94}

使用方法:

 
1//载入位图
2m_BMPTexture.LoadBitmap(filename)
3//位图的宽
4m_BMPTexture.imageWidth
5//位图的高
6m_BMPTexture.imageHeight
7//位图数据
8m_BMPTexture.image

这次就难去截图了,将就点吧,有问题评论就是了~

本文固定链接: http://liusir.name/opengl-texture-problems-qt-version.html | 民警小刘

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值