@ -36,7 +36,7 @@ static void desaturate(const unsigned char inColor[3], unsigned char outColor[3]
outColor [ 2 ] = ( unsigned char ) ( ( double ) inColor [ 2 ] + f * ( L - inColor [ 2 ] ) ) ;
}
static bool generate_clut ( const char * paletteFile , tim : : PARAM * outTim )
static bool load_palette ( const char * paletteFile , Color outPalette [ PALETTE_SIZE ] )
{
unsigned char palette [ PALETTE_SIZE * 3 ] ;
@ -48,6 +48,19 @@ static bool generate_clut(const char* paletteFile, tim::PARAM* outTim)
fread ( palette , sizeof ( unsigned char ) * 3 , PALETTE_SIZE , fp ) ;
fclose ( fp ) ;
for ( int c = 0 ; c < PALETTE_SIZE ; + + c )
{
outPalette [ c ] . rgb . r = palette [ 3 * c + 0 ] ;
outPalette [ c ] . rgb . g = palette [ 3 * c + 1 ] ;
outPalette [ c ] . rgb . b = palette [ 3 * c + 2 ] ;
outPalette [ c ] . rgb . a = 0 ;
}
return true ;
}
static bool generate_clut ( const Color palette [ PALETTE_SIZE ] , tim : : PARAM * outTim )
{
tim : : PIX_RGB5 * clut = ( tim : : PIX_RGB5 * ) malloc ( PALETTE_SIZE * sizeof ( tim : : PIX_RGB5 ) * 2 ) ;
if ( clut = = NULL )
return false ;
@ -57,7 +70,7 @@ static bool generate_clut(const char* paletteFile, tim::PARAM* outTim)
for ( int c = 0 ; c < PALETTE_SIZE - 1 ; + + c ) // Final palette entry is for alpha masking
{
unsigned char color [ 3 ] ;
desaturate ( & palette [ 3 * c ] , color ) ;
desaturate ( & palette [ c ] . rgb . r , color ) ;
clut [ c ] . r = color [ 0 ] > > 3 ;
clut [ c ] . g = color [ 1 ] > > 3 ;
@ -82,7 +95,7 @@ static bool generate_clut(const char* paletteFile, tim::PARAM* outTim)
return true ;
}
bool process_textures ( const world_t * world , std : : vector < ps1bsp_texture_t > & outTextures ) // TODO: return TextureDescriptor structs, including average texture color
bool process_textures ( const world_t * world , TextureList & outTextures ) // TODO: return TextureDescriptor structs, including average texture color
{
using spaces_type = rectpack2D : : empty_spaces < false > ;
using rect_type = rectpack2D : : output_rect_t < spaces_type > ;
@ -146,13 +159,16 @@ bool process_textures(const world_t* world, std::vector<ps1bsp_texture_t>& outTe
printf ( " %d textures. Packed texture atlas size: %d x %d \n " , world - > mipheader . numtex , result_size . w , result_size . h ) ;
Color palette [ PALETTE_SIZE ] ;
load_palette ( " palette.lmp " , palette ) ;
tim : : PARAM outTim = { 0 } ;
outTim . format = 1 ; // 8-bit per pixel, all Quake textures use this
outTim . imgXoffs = 512 ;
outTim . imgYoffs = 256 ;
outTim . clutXoffs = 512 ;
outTim . clutYoffs = 0 ;
generate_clut ( " palette.lmp " , & outTim ) ;
generate_clut ( palette , & outTim ) ;
outTim . imgWidth = result_size . w ;
outTim . imgHeight = result_size . h ;
@ -168,7 +184,7 @@ bool process_textures(const world_t* world, std::vector<ps1bsp_texture_t>& outTe
miptex_t * miptex = & world - > miptexes [ texNum ] ;
if ( miptex - > name [ 0 ] = = ' \0 ' ) // Weird edge case on N64START.bsp, corrupt data perhaps?
{
outTextures . push_back ( ps1bsp_texture_t { 0 } ) ; // We have to add something, otherwise the texture IDs get messed up
outTextures . push_back ( TextureDescriptor { 0 } ) ; // We have to add something, otherwise the texture IDs get messed up
continue ;
}
@ -189,19 +205,19 @@ bool process_textures(const world_t* world, std::vector<ps1bsp_texture_t>& outTe
memcpy_s ( ( unsigned char * ) outTim . imgData + ( ( rectangle . y + y ) * result_size . w + rectangle . x ) , rectangle . w * sizeof ( unsigned char ) , texBytes + ( y * rectangle . w ) , rectangle . w * sizeof ( unsigned char ) ) ;
}
ps1bsp_texture_t ps1 tex = { 0 } ;
ps1 tex. w = ( u_char ) rectangle . w ;
ps1 tex. h = ( u_char ) rectangle . h ;
TextureDescriptor tex = { 0 } ;
tex . w = ( u_char ) rectangle . w ;
tex . h = ( u_char ) rectangle . h ;
u_short x = ( rectangle . x / 2 ) + outTim . imgXoffs ; // Divide by 2 to get the coordinate in 16-bit pixel units
u_short y = rectangle . y + outTim . imgYoffs ;
ps1tex . tpage = getTPage ( outTim . format , 0 , x , y ) ;
ps1 tex. uoffs = ( u_char ) ( ( x % 64 ) < < ( 2 - outTim . format ) ) ;
ps1 tex. voffs = ( u_char ) ( y & 0xFF ) ;
tex . ps1tex . tpage = getTPage ( outTim . format , 0 , x , y ) ;
tex . uoffs = ( u_char ) ( ( x % 64 ) < < ( 2 - outTim . format ) ) ;
tex . voffs = ( u_char ) ( y & 0xFF ) ;
// TODO: animated textures
outTextures . push_back ( ps1 tex) ;
outTextures . push_back ( tex ) ;
}
}
}