www.pudn.com > WaveSimulation.rar > Texture.cpp


// Texture.cpp: implementation of the CTexture class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Texture.h" 
#include  
#include  
#include 			 
#include  
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CTexture::CTexture() 
{ 
	m_pTextures = NULL; 
	m_nTextures = 0; 
} 
CTexture::~CTexture() 
{ 
	glDeleteTextures(m_nTextures,m_pTextures); 
} 
 
void CTexture::UseTexture(int textureid) 
{ 
	glBindTexture(GL_TEXTURE_2D,textureid); 
} 
 
void CTexture::InitTexture() 
{ 
	m_nTextures = 7; 
	m_pTextures = new unsigned int[m_nTextures]; 
	glGenTextures(m_nTextures,m_pTextures); 
 
	LoadBmpexture("skin\\seabottomSkin.bmp",m_pTextures[0]); 
 
	char filename[64]; 
	for(int i=0;i<5;i++) 
	{ 
		wsprintf(filename,"skin\\skyb%d.bmp",i); 
		LoadBmpexture(filename,m_pTextures[i + 1]); 
 	} 
	//º£Ë® 
	LoadBmpexture("skin\\water.bmp",m_pTextures[6]); 
} 
 
 
 
 
 
void CTexture::LoadBmpexture(char *texturefilename,unsigned int textureid) 
{ 
	glBindTexture(GL_TEXTURE_2D,textureid); 
 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);///GL_NEAREST 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);///GL_LINEAR_MIPMAP_LINEAR GL_NEAREST 
 
	FILE *f; 
 
	f=fopen(texturefilename,"rb"); 
 
	if (f != NULL)  
		fclose(f); 
	else 
		MessageBox( NULL, "Open Skin file failed!", "Error", MB_OK | MB_ICONERROR ); 
 
    AUX_RGBImageRec *texture; 
 
    texture = auxDIBImageLoad( texturefilename ); 
 
	int sizex; 
	int sizey; 
 
	if ( !texture )  
	{ 
		MessageBox( NULL, "Open Skin file failed!", "Error", MB_OK | MB_ICONERROR ); 
		sizex = 256; 
		sizey = 256; 
		unsigned char *bmpdata = new unsigned char[sizex * sizey * 3]; 
		memset(bmpdata,180,sizeof(sizex * sizey * 3)); 
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizex, sizey,  
						  GL_RGB, GL_UNSIGNED_BYTE, bmpdata ); 
		delete []bmpdata; 
		return; 
	} 
 
	sizex = texture->sizeX; 
	sizey = texture->sizeY; 
 
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizex, sizey,  
		              GL_RGB, GL_UNSIGNED_BYTE, texture->data ); 
 
	if(texture->data) 
	{ 
		delete []texture->data; 
		delete texture; 
	} 
}