习惯了自己编写一些常用的代码,并打包成可随时可用的类。CTexture就是一个读取纹理的类,可以方便的应用到OpenGL中。个人感觉不错。
头文件:
/**/
/*************************************************************************
* 读取纹理的类 CTexture
* 作者: (QQ=63659875 Email=guojianbest@163.com)
*
* 最后修改2007-4-23,加入RGB(A)读入
*
* 可以读取BMP,TGA,RGB,JPG(未做)文件.
* 使用前必须要包含相关的头文件和库
* 部分源码来源于网络,如果错误请联系
*
* 简洁的使用方法:
*1. 定义变量 CTexture m_texture;
*2. 在初始化中载入纹理 m_texture.LoadBMP("test.bmp");
* 如果是带有通道的则这样 m_texture.LoadAlphaBMP("test.bmp");
*3. 绑定纹理,开始绘制
* glEnable(GL_TEXTURE_2D);
* glBindTexture(GL_TEXTURE_2D,m_texture.GetID());
* m_texture.EnableAlpha(true); //如果是带有通道
* glBegin(...);
* glTexCoord2f(...); //纹理坐标
* glEnd();
* m_texture.EnableAlpha(false); //如果是带有通道
* glDisable(GL_TEXTURE_2D);
***************************************************************************/
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#ifndef _TEXTURE_H
#define
_TEXTURE_H
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/*/
//载入OpenGL相关的头文件
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
//载入OpenGL相关的库
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "glaux.lib" )
/*/
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
BITMAP_ID 0x4D42
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
TGA信息的结构体
struct
TGAInfoHeader
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
unsigned char m_ucHeader[6];
unsigned int m_uiBytesPerPixel;
unsigned int m_uiImageSize;
unsigned int m_uiTemp;
unsigned int m_uiType;
unsigned int m_uiHeight;
unsigned int m_uiWidth;
unsigned int m_uiBPP;
}
;
extern
unsigned
char
ucUTGAcompare[
12
];
extern
unsigned
char
ucCTGAcompare[
12
];
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
RGB
typedef
struct
_ImageRec
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
unsigned short imagic;
unsigned short type;
unsigned short dim;
unsigned short xsize, ysize, zsize;
unsigned int min, max;
unsigned int wasteBytes;
char name[80];
unsigned long colorMap;
FILE *file;
unsigned char *tmp, *tmpR, *tmpG, *tmpB;
unsigned long rleEnd;
unsigned int *rowStart;
int *rowSize;
}
ImageRec;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
//
//
类定义开始
class
CTexture
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
unsigned int m_ID;
unsigned int m_width;
unsigned int m_height;
unsigned char* m_ucpData;
unsigned int m_uiBPP;
unsigned int m_uiWidth;
unsigned int m_uiHeight;
bool m_bIsLoaded;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public:
CTexture();
~CTexture();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//载入普通的bmp
bool LoadBMP(char *fName);
//载入bmp带有模拟的alpha通道
//必须是24位的BMP文件,黑色作为透明的颜色
bool LoadAlphaBMP(char *fName);
//载入bmp-detail贴图
bool LoadDetailBMP(char *fName);
bool LoadRGB(char *fName); //载入rgb
bool LoadRGBA(char *fName); //rgb带有alpha通道,即rgba文件
//载入tga(压缩或者未压缩的; 24位,32位都可以)
bool LoadTGA(char *fName);
//带有alpha通道的TGA,必须是32位的
bool LoadAlphaTGA(char *fName);
//tga-detail贴图
bool LoadDetailTGA(char * fName);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//获得纹理的ID
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
inline unsigned int GetID(void)...{
return m_ID;
};
//得到宽度
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
inline unsigned int GetWidth(void)...{
return m_width;
};
//得到高度
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
inline unsigned int GetHeight(void)...{
return m_height;
};
//是否载入完成
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
inline bool isLoaded(void)...{
return m_bIsLoaded;
};
//开启Alpha
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
inline void EnableAlpha(bool b)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(b)...{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);
glEnable(GL_BLEND);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else...{
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
bool Load( char* a_szFilename, float a_fMinFilter, float a_fMaxFilter, bool a_bMipmap= false );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected:
bool LoadBMPData(char* a_szFilename, float a_fMinFilter, float a_fMaxFilter, bool a_bMipmap= false);
AUX_RGBImageRec *LoadBMPFile(CHAR *Filename);
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);//未使用
unsigned char *LoadBitmapFileWithAlpha(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);//未使用
bool LoadCompressedTGA( void );
bool LoadUncompressedTGA( void );
bool LoadData(char * szFilename);
bool Create( unsigned int uiWidth, unsigned int uiHeight, unsigned int uiBPP );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//RGB
void bwtorgba(unsigned char *b,unsigned char *l,int n);
void latorgba(unsigned char *b, unsigned char *a,unsigned char *l,int n);
void rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n);
void rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n);
void ConvertShort(unsigned short *array, long length);
void ConvertLong(unsigned *array, long length);
ImageRec * ImageOpen(const char *fileName);
void ImageClose(ImageRec *image);
void ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z);
unsigned char* read_texture(char *name, int *width, int *height, int *components);
}
;
#endif
cpp文件:
太长,无法贴。