www.pudn.com > 3DmaxSLoader.rar > Texture.cpp


// Texture.cpp: implementation of the Texture class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "3DSLoader.h" 
#include "Texture.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
extern GLuint texturem;//纹理对象 
extern GLuint	texture[4]; 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
Texture::Texture() 
{ 
 
} 
 
Texture::~Texture() 
{ 
 
} 
 
AUX_RGBImageRec *Texture::LoadBMP(const char *Filename)	//载入一个Bitmap图像 
{ 
	FILE *File=NULL;								// File Handle 
 
	if (!Filename)									//确保该文件被指定 
	{ 
		return NULL;								//否则返回NULL 
	} 
 
	File=fopen(Filename,"r");					 
 
	if (File)										//文件不存在? 
	{ 
		fclose(File);								//关闭 
		return auxDIBImageLoad(Filename);			//加载Bitmap文件返回一个指针 
	} 
 
	return NULL;									//加载失败则返回NULL 
} 
 
 
GLuint Texture::LoadGLTexture( const char *filename )// 载入Bitmaps并将其转换成纹理 
{ 
	bool Status=true; 
	AUX_RGBImageRec *pImage;						//为纹理创建存储空间 
 
	pImage = LoadBMP( filename );					//加载指定名称的Bitmaps文件 
 
	if ( pImage != NULL && pImage->data != NULL )  //纹理图像存在 
	{ 
		glGenTextures(1, &texturem);//生成一个文理对象名称的列表,返回名称列表的数组 
 
		//典型的由Bitmaps文件数据生成纹理的过程 
		glBindTexture(GL_TEXTURE_2D, texturem);//把当前纹理状态绑定到纹理对象 
		glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->sizeX, pImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->data); 
		//定义一个二维的纹理图像 
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
		//设置纹理贴图参数,GL_TEXTURE_MIN_FILTER:指定文理图像缩小方法或过滤器;GL_LINEAR:在Mip基层上执行线性过滤 
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
        //GL_TEXTURE_MAG_FILTER:指定文理图像扩大方法或过滤器 
		free(pImage->data);							//释放纹理图像数据 
		free(pImage);								// 
	} 
 
  return Status; 
} 
 
 
int Texture::LoadGLTextures() 
{ 
	int Status=FALSE; 
    AUX_RGBImageRec *TextureImage[1];	//为纹理创建存储空间 
    memset(TextureImage,0,sizeof(void *)*1);//置0 
 
  if (TextureImage[0]=LoadBMP("texture/asphalt.bmp"))//加载Bitmaps文件 
  { 
		Status=TRUE; 
	  glGenTextures(1, &texture[0]);//生成一个新文理对象名称,地址texture[0] 
            
      glBindTexture(GL_TEXTURE_2D, texture[0]); 
	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
	  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);            
  } 
   
  if (TextureImage[0]=LoadBMP("texture/sky.bmp")) 
  { 
	  Status=TRUE;							 
	  glGenTextures(1, &texture[1]); 
 
 	  glBindTexture(GL_TEXTURE_2D, texture[1]); 
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
  	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
   	  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);            
  } 
 
  if (TextureImage[0]=LoadBMP("texture/Lightmap_256x256.bmp")) 
  { 
	  Status=TRUE;		 
	  glGenTextures(1, &texture[2]); 
		 
 	  glBindTexture(GL_TEXTURE_2D, texture[2]); 
   	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
   	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
   	  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);            
  } 
 
 
  if (TextureImage[0]=LoadBMP("texture/Water.bmp")) 
  { 
		Status=TRUE; 
		glGenTextures(1, &texture[3]); 
 
		glBindTexture(GL_TEXTURE_2D, texture[4]); 
 		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
 		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
 		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);            
  } 
 
  if (TextureImage[0])						// If Texture Exists 
	{ 
		if (TextureImage[0]->data)			// If Texture Image Exists 
		{ 
			free(TextureImage[0]->data);	// Free The Texture Image Memory 
		} 
		free(TextureImage[0]);				// Free The Image Structure 
	} 
 
  return Status;							// Return The Status 
}