Panda3D
|
00001 // Filename: txaFileFilter.cxx 00002 // Created by: drose (27Jul06) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "txaFileFilter.h" 00016 #include "palettizer.h" 00017 #include "txaFile.h" 00018 #include "textureImage.h" 00019 #include "sourceTextureImage.h" 00020 #include "texturePool.h" 00021 #include "dconfig.h" 00022 #include "configVariableFilename.h" 00023 #include "virtualFileSystem.h" 00024 #include "config_util.h" 00025 00026 NotifyCategoryDeclNoExport(txafile); 00027 NotifyCategoryDef(txafile, ""); 00028 00029 // A few lines to register this filter type with the TexturePool when 00030 // the shared library is loaded. 00031 Configure(config_txaFileFilter); 00032 ConfigureFn(config_txaFileFilter) { 00033 TxaFileFilter::init_type(); 00034 TexturePool *pool = TexturePool::get_global_ptr(); 00035 pool->register_filter(new TxaFileFilter); 00036 } 00037 00038 TypeHandle TxaFileFilter::_type_handle; 00039 TxaFile *TxaFileFilter::_txa_file; 00040 bool TxaFileFilter::_got_txa_file; 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: TxaFileFilter::post_load 00044 // Access: Public, Virtual 00045 // Description: This method is called after each texture has been 00046 // loaded from disk, via the TexturePool, for the first 00047 // time. By the time this method is called, the Texture 00048 // has already been fully read from disk. This method 00049 // should return the Texture pointer that the 00050 // TexturePool should actually return (usually it is the 00051 // same as the pointer supplied). 00052 //////////////////////////////////////////////////////////////////// 00053 PT(Texture) TxaFileFilter:: 00054 post_load(Texture *tex) { 00055 if (!_got_txa_file) { 00056 read_txa_file(); 00057 } 00058 00059 TextureImage tex_image; 00060 string name = tex->get_filename().get_basename_wo_extension(); 00061 tex_image.set_name(name); 00062 00063 SourceTextureImage *source = tex_image.get_source 00064 (tex->get_fullpath(), tex->get_alpha_fullpath(), 0); 00065 PNMImage pnm_image; 00066 tex->store(pnm_image); 00067 source->set_header(pnm_image); 00068 tex_image.set_source_image(pnm_image); 00069 00070 tex_image.pre_txa_file(); 00071 00072 bool matched = _txa_file->match_texture(&tex_image); 00073 if (txafile_cat.is_debug()) { 00074 if (!matched) { 00075 txafile_cat.debug() 00076 << "Not matched: " << name << "\n"; 00077 } else { 00078 txafile_cat.debug() 00079 << "Matched: " << name << "\n"; 00080 } 00081 } 00082 00083 tex_image.post_txa_file(); 00084 00085 PNMImage dest(tex_image.get_x_size(), 00086 tex_image.get_y_size(), 00087 tex_image.get_num_channels(), 00088 pnm_image.get_maxval()); 00089 dest.quick_filter_from(pnm_image); 00090 00091 tex->load(dest); 00092 00093 // Create an EggTexture to pass back the requested alpha mode to 00094 // the egg loader, if the texture is now being loaded from an egg 00095 // file. 00096 PT_EggTexture egg_tex = new EggTexture(tex->get_name(), tex->get_fullpath()); 00097 const TextureProperties &props = tex_image.get_properties(); 00098 00099 egg_tex->set_alpha_mode(tex_image.get_alpha_mode()); 00100 egg_tex->set_format(props._format); 00101 egg_tex->set_minfilter(props._minfilter); 00102 egg_tex->set_minfilter(props._magfilter); 00103 egg_tex->set_anisotropic_degree(props._anisotropic_degree); 00104 00105 tex->set_aux_data("egg", egg_tex); 00106 00107 return tex; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: TxaFileFilter::read_txa_file 00112 // Access: Private, Static 00113 // Description: Reads the textures.txa file named by the variable 00114 // txa-file. Called only once, at startup. 00115 //////////////////////////////////////////////////////////////////// 00116 void TxaFileFilter:: 00117 read_txa_file() { 00118 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00119 00120 // We need to create a global Palettizer object to hold some of the 00121 // global properties that may be specified in a txa file. 00122 if (pal == (Palettizer *)NULL) { 00123 pal = new Palettizer; 00124 } 00125 00126 _txa_file = new TxaFile; 00127 _got_txa_file = true; 00128 00129 ConfigVariableFilename txa_file 00130 ("txa-file", Filename("textures.txa"), 00131 PRC_DESC("Specify the name of the txa file to load when the txafile texture filter" 00132 "is in effect.")); 00133 00134 Filename filename = txa_file; 00135 vfs->resolve_filename(filename, get_model_path()); 00136 00137 if (!vfs->exists(filename)) { 00138 txafile_cat.warning() 00139 << "Filename " << filename << " not found.\n"; 00140 } else { 00141 filename.set_text(); 00142 istream *ifile = vfs->open_read_file(filename, true); 00143 if (ifile == (istream *)NULL) { 00144 txafile_cat.warning() 00145 << "Filename " << filename << " cannot be read.\n"; 00146 } else { 00147 if (!_txa_file->read(*ifile, filename)) { 00148 txafile_cat.warning() 00149 << "Syntax errors in " << filename << "\n"; 00150 } else { 00151 txafile_cat.info() 00152 << "Read " << filename << "\n"; 00153 } 00154 vfs->close_read_file(ifile); 00155 } 00156 } 00157 }