コード例 #1
0
ファイル: texture.cpp プロジェクト: JeanRintoul/Brainmobile
//--------------------------------------------------------------------------
/// \brief	This function loads the specified image file and returns the 
///			openGL texture object for it. Only tga, bmp and pcx images are
///			supported.
/// \param	filename	-	the image file to use for the texture
/// \return	compressed	-	if true DXT compression will be used.
/// 
unsigned int LoadTexture(const char* filename,bool compressed, bool makeMips, int *outWidth, int *outHeight)
{
#ifndef _WIN32  // #if MACINTOSH
	// This is a HACK for the Mac version which has some directory trouble.
	// This hardcoded path should be fixed soon to something more flexible.
	char buffer[1000];
	const char *currentDirectory = getcwd( buffer, 1000);
	// 
	// printf("Texture Directorily: %s\n",currentDirectory);
	// 
	// char *strstr = strFind(currentDirectory,"build");
	// const char *substring = strstr(currentDirectory, "build");
	// printf("substring: %s\n", substring);
	// chdir(currentDirectory);
	// chdir("/Users/puck 1/Desktop/trunk/BrainMobile/runtime");
	// chdir("/Users/SPL/Desktop/DWDT/dwd/BrainMobile/runtime");
	chdir("/Applications/DWD/dwd/BrainMobile/runtime");	
	
	
#endif

	// check to see if file is already loaded.
	std::map<std::string,TexRef>::iterator it = g_Textures.find(filename);
	if ( it!=g_Textures.end()) {
		it->second.ref++;
		return it->second.idx;
	}

	// if not, try to load the file.
	unsigned int w=0,h=0,bpp=0;
	unsigned char* pixels=0;

	int len = static_cast<int>(strlen(filename));

	// load a bmp
	if( (filename[len-3] == 'b' || filename[len-3] == 'B') &&
		(filename[len-2] == 'm' || filename[len-2] == 'M') &&
		(filename[len-1] == 'p' || filename[len-1] == 'P') ) {
		if(!LoadBmpImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else
	// load a pcx
	if( (filename[len-3] == 'p' || filename[len-3] == 'P') &&
		(filename[len-2] == 'c' || filename[len-2] == 'C') &&
		(filename[len-1] == 'x' || filename[len-1] == 'X') ) {
		if(!LoadPcxImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else
	// load a tga 
	if( (filename[len-3] == 't' || filename[len-3] == 'T') &&
		(filename[len-2] == 'g' || filename[len-2] == 'G') &&
		(filename[len-1] == 'a' || filename[len-1] == 'A') ) {
		if(!LoadTgaImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else {
		std::cerr << "Unsupported image format\n";
		return 0;
	}

	// generat the correct texture type for the image
	unsigned int tex_object;
	switch(bpp) {
	case 1:
		tex_object = MakeGlTexture(GL_ALPHA,pixels,w,h,compressed, makeMips);
		break;
	case 2:
		break;
	case 3:
		tex_object = MakeGlTexture(GL_RGB,pixels,w,h,compressed, makeMips);
		break;
	case 4:
		tex_object = MakeGlTexture(GL_RGBA,pixels,w,h,compressed, makeMips);
		break;
	default:
		break;
	}

	int data_size=0;
	if(compressed) {
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE,&data_size);
	}
	else {
		data_size = bpp*w*h;
	}


	// delete the pixel data because we no longer need it
	free(pixels);
	
	// insert the texture into a map to keep track of it
	g_Textures.insert( std::make_pair( std::string(filename), TexRef(tex_object,data_size) ) );

	if (outWidth != NULL)
		*outWidth = w;
	if (outHeight != NULL)
		*outHeight = h;

	// return GL texture object
	return tex_object;

}
コード例 #2
0
ファイル: texture.cpp プロジェクト: guruk2/INFINITE_LOOP
//--------------------------------------------------------------------------
/// \brief	This function loads the specified image file and returns the 
///			openGL texture object for it. Only tga, bmp and pcx images are
///			supported.
/// \param	filename	-	the image file to use for the texture
/// \return	compressed	-	if true DXT compression will be used.
///
unsigned int LoadTexture(const char* filename,bool compressed) {

	// check to see if file is already loaded.
	std::map<std::string,TexRef>::iterator it = g_Textures.find(filename);
	if ( it!=g_Textures.end()) {
		it->second.ref++;
		return it->second.idx;
	}

	// if not, try to load the file.
	unsigned int w=0,h=0,bpp=0;
	unsigned char* pixels=0;

	int len = static_cast<int>(strlen(filename));

	// load a bmp
	if( (filename[len-3] == 'b' || filename[len-3] == 'B') &&
		(filename[len-2] == 'm' || filename[len-2] == 'M') &&
		(filename[len-1] == 'p' || filename[len-1] == 'P') ) {
		if(!LoadBmpImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else
	// load a pcx
	if( (filename[len-3] == 'p' || filename[len-3] == 'P') &&
		(filename[len-2] == 'c' || filename[len-2] == 'C') &&
		(filename[len-1] == 'x' || filename[len-1] == 'X') ) {
		if(!LoadPcxImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else
	// load a tga 
	if( (filename[len-3] == 't' || filename[len-3] == 'T') &&
		(filename[len-2] == 'g' || filename[len-2] == 'G') &&
		(filename[len-1] == 'a' || filename[len-1] == 'A') ) {
		if(!LoadTgaImage(filename,&pixels,&w,&h,&bpp))
			return 0;
	}
	else {
		std::cerr << "Unsupported image format\n";
		return 0;
	}

	// generat the correct texture type for the image
	unsigned int tex_object;
	switch(bpp) {
	case 1:
		tex_object = MakeGlTexture(GL_ALPHA,pixels,w,h,compressed);
		break;
	case 2:
		break;
	case 3:
		tex_object = MakeGlTexture(GL_RGB,pixels,w,h,compressed);
		break;
	case 4:
		tex_object = MakeGlTexture(GL_RGBA,pixels,w,h,compressed);
		break;
	default:
		break;
	}

	int data_size=0;
	if(compressed) {
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE,&data_size);
	}
	else {
		data_size = bpp*w*h;
	}


	// delete the pixel data because we no longer need it
	free(pixels);
	
	// insert the texture into a map to keep track of it
	g_Textures.insert( std::make_pair( std::string(filename), TexRef(tex_object,data_size) ) );

	// return GL texture object
	return tex_object;

}