Zaslal: Weny Sky
Jazyk: C++
Vloženo: 2.2.2008, 11:04
Stáhnout jako soubor
//----------------------------------------------------------------------------- // Name: BAMWE::loadTexture(int TID, const char* name) // Desc: nacte texturu //----------------------------------------------------------------------------- int BAMWE::loadTexture(int TID, const char* name) { SDL_Surface *surface; GLenum texture_format; GLint num_of_colors; size_t name_length; SDL_RWops *rwop; std::map<int,bool>::iterator active_texture; if(name == NULL) { log(__FILE__, __LINE__, "NULL pointer"); return -1; } // ziskani noveho TID if(TID == BAMWE_TEXTURE_DEFAULT) { do { TID = free_TID.top(); free_TID.pop(); } while (used_textures.find(TID) != used_textures.end()); } else if((active_texture = used_textures.find(TID)) != used_textures.end() && active_texture->second) { const unsigned int deleted_textures[1] = {textures[TID]}; log(NULL, 0, "Texture \"%s\" with ID:%d was replaced by texture \"%s\"", textures_files[TID].c_str(), TID, name); glDeleteTextures(1, deleted_textures); log_error(); } name_length = strlen(name); if(name_length <= 4) { log(NULL, 0, "Bad texture name length (%s)", name); return -1; } if(name[name_length - 4] != '.') { log(NULL, 0, "Bad file name format (%s)", name); return -1; } // nacitaji se normalni nekomprimovane textury if(strcmp(&name[name_length-3], "dds") != 0) { rwop = SDL_RWFromFile(name, "rb"); if(rwop == NULL) { log(NULL, 0, "Texture (%s): %s", name, SDL_GetError( )); return -1; } if(strcmp(&name[name_length-3], "png") == 0) surface = IMG_LoadPNG_RW(rwop); else if(strcmp(&name[name_length-3], "tga") == 0) surface = IMG_LoadTGA_RW(rwop); else if(strcmp(&name[name_length-3], "jpg") == 0) surface = IMG_LoadJPG_RW(rwop); else if(strcmp(&name[name_length-3], "bmp") == 0) surface = IMG_LoadBMP_RW(rwop); else { log(NULL, 0, "Unrecognized texture format(%s). Try to lower case the file extension.", name); SDL_FreeRW(rwop); return -1; } if(!surface) { log(NULL, 0, "Texture (%s) load error : %s", name, IMG_GetError()); SDL_FreeRW(rwop); return -1; } SDL_FreeRW(rwop); // kontrola, jestli je sirka nasobkem dvou if ( (surface->w & (surface->w - 1)) != 0 ) log(__FILE__, __LINE__, "WARNING: %s's width is not a power of 2", name); // kontrola, jestli je vyska nasobkem dvou if ( (surface->h & (surface->h - 1)) != 0 ) log(__FILE__, __LINE__, "WARNING: %s's height is not a power of 2", name); // urce barevnych kanalu num_of_colors = surface->format->BytesPerPixel; if (num_of_colors == 4) // s alfakanalem { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGBA; else texture_format = GL_BGRA; } else if (num_of_colors == 3) // bez alfakanalu { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGB; else texture_format = GL_BGR; } else { log(NULL, 0, "Texture (%s) bad color depth", name); SDL_FreeSurface(surface); return -1; } // vygenerovani ukazatele pro aktualni texturu glGenTextures( 1, &textures[TID] ); log_error(); // bindnuti textury glBindTexture( GL_TEXTURE_2D, textures[TID] ); log_error(); // nastaveni parametru textury glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); log_error(); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); log_error(); // nahrani dat do textury glTexImage2D( GL_TEXTURE_2D, 0, num_of_colors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels ); log_error(); #ifdef _DEBUG log(0,0,"Create texture ID(%d) from %s : %dx%d-%db - %d", TID, name, surface->w, surface->h, num_of_colors*8, texture_format); #endif textures_width[TID] = surface->w; textures_height[TID] = surface->h; SDL_FreeSurface( surface ); used_textures[TID] = true; textures_files[TID] = name; return TID; } else // nacteni komprimovane textury { DDS_IMAGE_DATA *pDDSImageData = loadDDSTextureFile( name ); if( pDDSImageData != NULL ) { int nHeight = pDDSImageData->height; int nWidth = pDDSImageData->width; int nNumMipMaps = pDDSImageData->numMipMaps; int nBlockSize; if( pDDSImageData->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ) nBlockSize = 8; else nBlockSize = 16; #ifdef _DEBUG log(0,0,"Create texture ID(%d) from %s : %dx%d - %d", TID, name, pDDSImageData->width,pDDSImageData->height, pDDSImageData->format); #endif textures_width[TID] = pDDSImageData->width; textures_height[TID] = pDDSImageData->height; glGenTextures( 1, &textures[TID] ); log_error(); glBindTexture( GL_TEXTURE_2D, textures[TID]); log_error(); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); log_error(); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); log_error(); int nSize; int nOffset = 0; // Nacteni mip-map for( int i = 0; i < nNumMipMaps; ++i ) { if( nWidth == 0 ) nWidth = 1; if( nHeight == 0 ) nHeight = 1; nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize; glCompressedTexImage2DARB( GL_TEXTURE_2D, i, pDDSImageData->format, nWidth, nHeight, 0, nSize, pDDSImageData->pixels + nOffset ); log_error(); nOffset += nSize; // polovicni velikost textury pro dalsi mip-map nWidth = (nWidth / 2); nHeight = (nHeight / 2); } } if( pDDSImageData != NULL ) { if( pDDSImageData->pixels != NULL ) delete [] pDDSImageData->pixels; delete pDDSImageData; } used_textures[TID] = true; textures_files[TID] = name; return TID; } }
© 2006 Michal Tuláček, Syntax Highlight - GeSHi (thx bref)