robustify pathnames some more

This commit is contained in:
David Rose 2003-02-20 17:10:28 +00:00
parent 1e36ac21c4
commit 0945d3c127
11 changed files with 213 additions and 111 deletions

View File

@ -52,15 +52,6 @@ bool egg_flatten_siblings = config_egg2pg.GetBool("egg-flatten-siblings", false)
bool egg_show_collision_solids = config_egg2pg.GetBool("egg-show-collision-solids", false);
// When this is true, keep texture pathnames exactly the same as they
// appeared in the egg file, in particular leaving them as relative
// paths, rather than letting them reflect the full path at which they
// were found. This is particularly useful when generating bam files.
// However, if the same texture is named by two different relative
// paths, these will still be collapsed into one texture (using one of
// the relative paths, chosen arbitrarily).
bool egg_keep_texture_pathnames = config_egg2pg.GetBool("egg-keep-texture-pathnames", false);
// When this is true, a <NurbsCurve> entry appearing in an egg file
// will load a ClassicNurbsCurve object instead of the default, a
// NurbsCurve object. This only makes a difference when the NURBS++

View File

@ -52,7 +52,6 @@ extern EXPCL_PANDAEGG bool egg_ignore_decals;
extern EXPCL_PANDAEGG bool egg_flatten;
extern EXPCL_PANDAEGG bool egg_flatten_siblings;
extern EXPCL_PANDAEGG bool egg_show_collision_solids;
extern EXPCL_PANDAEGG bool egg_keep_texture_pathnames;
extern EXPCL_PANDAEGG bool egg_load_classic_nurbs_curves;
extern EXPCL_PANDAEGG bool egg_accept_errors;
extern EXPCL_PANDAEGG EggRenderMode::AlphaMode egg_alpha_mode;

View File

@ -487,15 +487,6 @@ load_texture(TextureDef &def, const EggTexture *egg_tex) {
return false;
}
if (egg_keep_texture_pathnames) {
tex->set_filename(egg_tex->get_filename());
if (egg_tex->has_alpha_file()) {
tex->set_alpha_filename(egg_tex->get_alpha_file());
} else {
tex->clear_alpha_filename();
}
}
apply_texture_attributes(tex, egg_tex);
CPT(RenderAttrib) apply = get_texture_apply_attributes(egg_tex);

View File

@ -31,10 +31,9 @@ has_filename() const {
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::get_filename
// Access: Published
// Description: Returns the filename that has been set. Use
// this in conjunction with get_name() to get the names
// of the file(s) that were loaded into the buffer. See
// set_filename().
// Description: Returns the filename that has been set. This is the
// name of the file as it was requested. Also see
// get_fullpath().
////////////////////////////////////////////////////////////////////
INLINE const Filename &ImageBuffer::
get_filename() const {
@ -55,16 +54,63 @@ has_alpha_filename() const {
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::get_alpha_filename
// Access: Published
// Description: Returns the alpha_filename that has been set. Use
// this in conjunction with get_name() to get the names
// of the file(s) that were loaded into the buffer. See
// set_alpha_filename().
// Description: Returns the alpha_filename that has been set. If
// this is set, it represents the name of the alpha
// component, which is stored in a separate file. See
// also get_filename(), and get_alpha_fullpath().
////////////////////////////////////////////////////////////////////
INLINE const Filename &ImageBuffer::
get_alpha_filename() const {
return _alpha_filename;
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::has_fullpath
// Access: Published
// Description: Returns true if the fullpath has been set and
// is available. See set_fullpath().
////////////////////////////////////////////////////////////////////
INLINE bool ImageBuffer::
has_fullpath() const {
return !_fullpath.empty();
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::get_fullpath
// Access: Published
// Description: Returns the fullpath that has been set. This is the
// full path to the file as it was found along the
// texture search path.
////////////////////////////////////////////////////////////////////
INLINE const Filename &ImageBuffer::
get_fullpath() const {
return _fullpath;
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::has_alpha_fullpath
// Access: Published
// Description: Returns true if the alpha_fullpath has been set and
// is available. See set_alpha_fullpath().
////////////////////////////////////////////////////////////////////
INLINE bool ImageBuffer::
has_alpha_fullpath() const {
return !_alpha_fullpath.empty();
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::get_alpha_fullpath
// Access: Published
// Description:
// Returns the alpha_fullpath that has been set. This
// is the full path to the alpha part of the image file
// as it was found along the texture search path.
////////////////////////////////////////////////////////////////////
INLINE const Filename &ImageBuffer::
get_alpha_fullpath() const {
return _alpha_fullpath;
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::set_filename
@ -127,3 +173,52 @@ INLINE void ImageBuffer::
clear_alpha_filename() {
_alpha_filename = Filename();
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::set_fullpath
// Access: Public
// Description: Sets the full pathname to the file that contains the
// image's contents, as found along the search path.
// Normally, this is set automatically when the image is
// loaded, for instance via Texture::read().
////////////////////////////////////////////////////////////////////
INLINE void ImageBuffer::
set_fullpath(const Filename &fullpath) {
_fullpath = fullpath;
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::clear_fullpath
// Access: Public
// Description: Removes the alpha fullpath, if it was previously set.
// See set_fullpath().
////////////////////////////////////////////////////////////////////
INLINE void ImageBuffer::
clear_fullpath() {
_fullpath = Filename();
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::set_alpha_fullpath
// Access: Public
// Description: Sets the full pathname to the file that contains the
// image's alpha channel contents, as found along the
// search path. Normally, this is set automatically
// when the image is loaded, for instance via
// Texture::read().
////////////////////////////////////////////////////////////////////
INLINE void ImageBuffer::
set_alpha_fullpath(const Filename &alpha_fullpath) {
_alpha_fullpath = alpha_fullpath;
}
////////////////////////////////////////////////////////////////////
// Function: ImageBuffer::clear_alpha_fullpath
// Access: Public
// Description: Removes the alpha fullpath, if it was previously set.
// See set_alpha_fullpath().
////////////////////////////////////////////////////////////////////
INLINE void ImageBuffer::
clear_alpha_fullpath() {
_alpha_fullpath = Filename();
}

View File

@ -48,18 +48,24 @@ write_datagram(BamWriter *, Datagram &me)
switch (bam_texture_mode) {
case BTM_unchanged:
break;
case BTM_fullpath:
filename = get_fullpath();
alpha_filename = get_alpha_fullpath();
break;
case BTM_relative:
filename.find_on_searchpath(get_texture_path());
filename.find_on_searchpath(get_model_path());
filename = get_fullpath();
alpha_filename = get_alpha_fullpath();
filename.find_on_searchpath(get_texture_path()) ||
filename.find_on_searchpath(get_model_path());
if (gobj_cat.is_debug()) {
gobj_cat.debug()
<< "Texture file " << get_filename() << " found as " << filename << "\n";
}
alpha_filename.find_on_searchpath(get_texture_path());
alpha_filename.find_on_searchpath(get_model_path());
alpha_filename.find_on_searchpath(get_texture_path()) ||
alpha_filename.find_on_searchpath(get_model_path());
if (gobj_cat.is_debug()) {
gobj_cat.debug()
<< "Alpha image " << get_alpha_filename() << " found as " << alpha_filename << "\n";

View File

@ -57,15 +57,27 @@ PUBLISHED:
INLINE bool has_alpha_filename() const;
INLINE const Filename &get_alpha_filename() const;
INLINE bool has_fullpath() const;
INLINE const Filename &get_fullpath() const;
INLINE bool has_alpha_fullpath() const;
INLINE const Filename &get_alpha_fullpath() const;
public:
INLINE void set_filename(const Filename &filename);
INLINE void clear_filename();
INLINE void set_alpha_filename(const Filename &alpha_filename);
INLINE void clear_alpha_filename();
INLINE void set_fullpath(const Filename &fullpath);
INLINE void clear_fullpath();
INLINE void set_alpha_fullpath(const Filename &alpha_fullpath);
INLINE void clear_alpha_fullpath();
private:
Filename _filename;
Filename _alpha_filename;
Filename _fullpath;
Filename _alpha_fullpath;
public:
//Abstract class, so no factory methods for Reading and Writing

View File

@ -166,22 +166,30 @@ Texture::
// Description: Reads the texture from the indicated filename.
////////////////////////////////////////////////////////////////////
bool Texture::
read(const Filename &name) {
PNMImage pnmimage;
read(const Filename &fullpath) {
PNMImage image;
if (!pnmimage.read(name)) {
if (!image.read(fullpath)) {
gobj_cat.error()
<< "Texture::read() - couldn't read: " << name << endl;
<< "Texture::read() - couldn't read: " << fullpath << endl;
return false;
}
// Check to see if we need to scale it.
consider_rescale(pnmimage, name);
if (!has_name()) {
set_name(fullpath.get_basename_wo_extension());
}
if (!has_filename()) {
set_filename(fullpath);
clear_alpha_filename();
}
set_name(name.get_basename_wo_extension());
set_filename(name);
clear_alpha_filename();
return load(pnmimage);
set_fullpath(fullpath);
clear_alpha_fullpath();
// Check to see if we need to scale it.
consider_rescale(image, get_name());
return load(image);
}
////////////////////////////////////////////////////////////////////
@ -191,51 +199,60 @@ read(const Filename &name) {
// to get a 4-component image
////////////////////////////////////////////////////////////////////
bool Texture::
read(const Filename &name, const Filename &gray) {
PNMImage pnmimage;
if (!pnmimage.read(name)) {
read(const Filename &fullpath, const Filename &alpha_fullpath) {
PNMImage image;
if (!image.read(fullpath)) {
gobj_cat.error()
<< "Texture::read() - couldn't read: " << name << endl;
<< "Texture::read() - couldn't read: " << fullpath << endl;
return false;
}
PNMImage grayimage;
if (!grayimage.read(gray)) {
PNMImage alpha_image;
if (!alpha_image.read(alpha_fullpath)) {
gobj_cat.error()
<< "Texture::read() - couldn't read: " << gray << endl;
<< "Texture::read() - couldn't read: " << alpha_fullpath << endl;
return false;
}
consider_rescale(pnmimage, name);
if (!has_name()) {
set_name(fullpath.get_basename_wo_extension());
}
if (!has_filename()) {
set_filename(fullpath);
set_alpha_filename(alpha_fullpath);
}
set_fullpath(fullpath);
set_alpha_fullpath(alpha_fullpath);
consider_rescale(image, get_name());
// The grayscale (alpha channel) image must be the same size as the
// main image.
if (pnmimage.get_x_size() != grayimage.get_x_size() ||
pnmimage.get_y_size() != grayimage.get_y_size()) {
if (image.get_x_size() != alpha_image.get_x_size() ||
image.get_y_size() != alpha_image.get_y_size()) {
gobj_cat.info()
<< "Automatically rescaling " << gray << " from "
<< grayimage.get_x_size() << " by " << grayimage.get_y_size() << " to "
<< pnmimage.get_x_size() << " by " << pnmimage.get_y_size() << "\n";
<< "Automatically rescaling " << alpha_fullpath.get_basename()
<< " from " << alpha_image.get_x_size() << " by "
<< alpha_image.get_y_size() << " to " << image.get_x_size()
<< " by " << image.get_y_size() << "\n";
PNMImage scaled(pnmimage.get_x_size(), pnmimage.get_y_size(),
grayimage.get_num_channels(),
grayimage.get_maxval(), grayimage.get_type());
scaled.quick_filter_from(grayimage);
grayimage = scaled;
PNMImage scaled(image.get_x_size(), image.get_y_size(),
alpha_image.get_num_channels(),
alpha_image.get_maxval(), alpha_image.get_type());
scaled.quick_filter_from(alpha_image);
alpha_image = scaled;
}
// Make the original image a 4-component image
pnmimage.add_alpha();
for (int x = 0; x < pnmimage.get_x_size(); x++) {
for (int y = 0; y < pnmimage.get_y_size(); y++) {
pnmimage.set_alpha(x, y, grayimage.get_gray(x, y));
image.add_alpha();
for (int x = 0; x < image.get_x_size(); x++) {
for (int y = 0; y < image.get_y_size(); y++) {
image.set_alpha(x, y, alpha_image.get_gray(x, y));
}
}
set_name(name.get_basename_wo_extension());
set_filename(name);
set_alpha_filename(gray);
return load(pnmimage);
return load(image);
}
////////////////////////////////////////////////////////////////////
@ -519,19 +536,14 @@ PixelBuffer *Texture::
get_ram_image() {
if (!has_ram_image() && has_filename()) {
// Now we have to reload the texture image.
string name = get_name();
gobj_cat.info()
<< "Reloading texture " << name << "\n";
<< "Reloading texture " << get_name() << "\n";
if (has_alpha_filename()) {
read(get_filename(), get_alpha_filename());
if (has_alpha_fullpath()) {
read(get_fullpath(), get_alpha_fullpath());
} else {
read(get_filename());
read(get_fullpath());
}
// Just in case the read operation changed our name, we should
// change it back.
set_name(name);
}
if (has_ram_image()) {
@ -688,9 +700,6 @@ make_Texture(const FactoryParams &params) {
} else {
me->set_name(name);
if (gobj_cat.is_debug()) {
gobj_cat->debug() << "Created texture " << me->get_name() << endl;
}
me->fillin(scan, manager);
}
return me;

View File

@ -79,9 +79,9 @@ PUBLISHED:
bool bAllocateRAM);
~Texture();
bool read(const Filename &name);
bool read(const Filename &name, const Filename &gray);
bool write(const Filename &name = "") const;
bool read(const Filename &fullpath);
bool read(const Filename &fullpath, const Filename &alpha_fullpath);
bool write(const Filename &fullpath = "") const;
void set_wrapu(WrapMode wrap);
void set_wrapv(WrapMode wrap);

View File

@ -66,8 +66,8 @@ load_texture(const string &filename) {
// file cannot be found, returns NULL.
////////////////////////////////////////////////////////////////////
INLINE Texture *TexturePool::
load_texture(const string &filename, const string &grayfilename) {
return get_ptr()->ns_load_texture(filename, grayfilename);
load_texture(const string &filename, const string &alpha_filename) {
return get_ptr()->ns_load_texture(filename, alpha_filename);
}
////////////////////////////////////////////////////////////////////

View File

@ -74,12 +74,12 @@ ns_load_texture(const Filename &orig_filename) {
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
vfs->resolve_filename(filename, get_texture_path());
vfs->resolve_filename(filename, get_model_path());
vfs->resolve_filename(filename, get_texture_path()) ||
vfs->resolve_filename(filename, get_model_path());
} else {
filename.resolve_filename(get_texture_path());
filename.resolve_filename(get_model_path());
filename.resolve_filename(get_texture_path()) ||
filename.resolve_filename(get_model_path());
}
Textures::const_iterator ti;
@ -94,13 +94,13 @@ ns_load_texture(const Filename &orig_filename) {
PT(Texture) tex = new Texture;
if (!tex->read(filename)) {
// This texture was not found.
gobj_cat.error() << "Unable to read texture " << filename << "\n";
gobj_cat.error()
<< "Unable to read texture " << filename << "\n";
return NULL;
}
if (bam_texture_mode == BTM_unchanged) {
tex->set_filename(orig_filename);
}
// Set the original filename, before we searched along the path.
tex->set_filename(orig_filename);
_textures[filename] = tex;
return tex;
@ -113,9 +113,9 @@ ns_load_texture(const Filename &orig_filename) {
////////////////////////////////////////////////////////////////////
Texture *TexturePool::
ns_load_texture(const Filename &orig_filename,
const Filename &orig_grayfilename) {
const Filename &orig_alpha_filename) {
Filename filename(orig_filename);
Filename grayfilename(orig_grayfilename);
Filename alpha_filename(orig_alpha_filename);
if (!fake_texture_image.empty()) {
return ns_load_texture(fake_texture_image);
@ -123,18 +123,18 @@ ns_load_texture(const Filename &orig_filename,
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
vfs->resolve_filename(filename, get_texture_path());
vfs->resolve_filename(filename, get_model_path());
vfs->resolve_filename(filename, get_texture_path()) ||
vfs->resolve_filename(filename, get_model_path());
vfs->resolve_filename(grayfilename, get_texture_path());
vfs->resolve_filename(grayfilename, get_model_path());
vfs->resolve_filename(alpha_filename, get_texture_path()) ||
vfs->resolve_filename(alpha_filename, get_model_path());
} else {
filename.resolve_filename(get_texture_path());
filename.resolve_filename(get_model_path());
filename.resolve_filename(get_texture_path()) ||
filename.resolve_filename(get_model_path());
grayfilename.resolve_filename(get_texture_path());
grayfilename.resolve_filename(get_model_path());
alpha_filename.resolve_filename(get_texture_path()) ||
alpha_filename.resolve_filename(get_model_path());
}
Textures::const_iterator ti;
@ -145,19 +145,18 @@ ns_load_texture(const Filename &orig_filename,
}
gobj_cat.info()
<< "Loading texture " << filename << " and grayscale texture "
<< grayfilename << endl;
<< "Loading texture " << filename << " and alpha component "
<< alpha_filename << endl;
PT(Texture) tex = new Texture;
if (!tex->read(filename, grayfilename)) {
if (!tex->read(filename, alpha_filename)) {
// This texture was not found.
gobj_cat.error() << "Unable to read texture " << filename << "\n";
return NULL;
}
if (bam_texture_mode == BTM_unchanged) {
tex->set_filename(orig_filename);
tex->set_alpha_filename(orig_grayfilename);
}
// Set the original filenames, before we searched along the path.
tex->set_filename(orig_filename);
tex->set_alpha_filename(orig_alpha_filename);
_textures[filename] = tex;
return tex;

View File

@ -44,7 +44,7 @@ PUBLISHED:
INLINE static bool verify_texture(const string &filename);
INLINE static Texture *load_texture(const string &filename);
INLINE static Texture *load_texture(const string &filename,
const string &grayfilename);
const string &alpha_filename);
INLINE static void add_texture(Texture *texture);
INLINE static void release_texture(Texture *texture);
INLINE static void release_all_textures();
@ -59,7 +59,7 @@ private:
bool ns_has_texture(const Filename &orig_filename);
Texture *ns_load_texture(const Filename &orig_filename);
Texture *ns_load_texture(const Filename &orig_filename,
const Filename &orig_grayfilename);
const Filename &orig_alpha_filename);
void ns_add_texture(Texture *texture);
void ns_release_texture(Texture *texture);
void ns_release_all_textures();