From c299cba83467fd0915e3d5bfeb01e6bdd0486bc4 Mon Sep 17 00:00:00 2001 From: enn0x Date: Sun, 24 Jan 2010 21:56:02 +0000 Subject: [PATCH] Fixed loading of binary meshes from virtual file system. --- panda/src/physx/physxFileStream.cxx | 51 +++++++++++++++++------------ panda/src/physx/physxFileStream.h | 12 +++++-- panda/src/physx/physxKitchen.cxx | 4 +-- panda/src/physx/physxMeshPool.cxx | 35 ++++++++------------ panda/src/physx/physxMeshPool.h | 2 +- 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/panda/src/physx/physxFileStream.cxx b/panda/src/physx/physxFileStream.cxx index 3720416fe4..6209e6d285 100644 --- a/panda/src/physx/physxFileStream.cxx +++ b/panda/src/physx/physxFileStream.cxx @@ -16,14 +16,22 @@ #include "stdio.h" +#include "virtualFileSystem.h" + //////////////////////////////////////////////////////////////////// // Function: PhysxFileStream::Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// -PhysxFileStream::PhysxFileStream(const char *filename, bool load) : fp(NULL) +PhysxFileStream::PhysxFileStream(const Filename &fn, bool load) : _fp(NULL), _vf(NULL), _in(NULL) { - fp = fopen(filename, load ? "rb" : "wb"); + if (load) { + _vf = VirtualFileSystem::get_global_ptr()->get_file(fn); + _in = _vf->open_read_file(true); + } + else { + _fp = fopen(fn.c_str(), "wb"); + } } //////////////////////////////////////////////////////////////////// @@ -33,7 +41,8 @@ PhysxFileStream::PhysxFileStream(const char *filename, bool load) : fp(NULL) //////////////////////////////////////////////////////////////////// PhysxFileStream::~PhysxFileStream() { - if (fp) fclose(fp); + if (_fp) fclose(_fp); + if (_vf) _vf->close_read_file(_in); } //////////////////////////////////////////////////////////////////// @@ -44,8 +53,8 @@ PhysxFileStream::~PhysxFileStream() NxU8 PhysxFileStream::readByte() const { NxU8 b; - size_t r = fread(&b, sizeof(NxU8), 1, fp); - NX_ASSERT(r); + _in->read((char *)&b, sizeof(NxU8)); + NX_ASSERT(!(_in->bad())); return b; } @@ -57,8 +66,8 @@ NxU8 PhysxFileStream::readByte() const NxU16 PhysxFileStream::readWord() const { NxU16 w; - size_t r = fread(&w, sizeof(NxU16), 1, fp); - NX_ASSERT(r); + _in->read((char *)&w, sizeof(NxU16)); + NX_ASSERT(!(_in->bad())); return w; } @@ -70,8 +79,8 @@ NxU16 PhysxFileStream::readWord() const NxU32 PhysxFileStream::readDword() const { NxU32 d; - size_t r = fread(&d, sizeof(NxU32), 1, fp); - NX_ASSERT(r); + _in->read((char *)&d, sizeof(NxU32)); + NX_ASSERT(!(_in->bad())); return d; } @@ -83,8 +92,8 @@ NxU32 PhysxFileStream::readDword() const float PhysxFileStream::readFloat() const { NxReal f; - size_t r = fread(&f, sizeof(NxReal), 1, fp); - NX_ASSERT(r); + _in->read((char *)&f, sizeof(NxReal)); + NX_ASSERT(!(_in->bad())); return f; } @@ -96,8 +105,8 @@ float PhysxFileStream::readFloat() const double PhysxFileStream::readDouble() const { NxF64 f; - size_t r = fread(&f, sizeof(NxF64), 1, fp); - NX_ASSERT(r); + _in->read((char *)&f, sizeof(NxF64)); + NX_ASSERT(!(_in->bad())); return f; } @@ -108,8 +117,8 @@ double PhysxFileStream::readDouble() const //////////////////////////////////////////////////////////////////// void PhysxFileStream::readBuffer(void *buffer, NxU32 size) const { - size_t w = fread(buffer, size, 1, fp); - NX_ASSERT(w); + _in->read((char *)buffer, size); + NX_ASSERT(!(_in->bad())); } //////////////////////////////////////////////////////////////////// @@ -119,7 +128,7 @@ void PhysxFileStream::readBuffer(void *buffer, NxU32 size) const //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeByte(NxU8 b) { - size_t w = fwrite(&b, sizeof(NxU8), 1, fp); + size_t w = fwrite(&b, sizeof(NxU8), 1, _fp); NX_ASSERT(w); return *this; } @@ -131,7 +140,7 @@ NxStream &PhysxFileStream::storeByte(NxU8 b) //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeWord(NxU16 w) { - size_t ww = fwrite(&w, sizeof(NxU16), 1, fp); + size_t ww = fwrite(&w, sizeof(NxU16), 1, _fp); NX_ASSERT(ww); return *this; } @@ -143,7 +152,7 @@ NxStream &PhysxFileStream::storeWord(NxU16 w) //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeDword(NxU32 d) { - size_t w = fwrite(&d, sizeof(NxU32), 1, fp); + size_t w = fwrite(&d, sizeof(NxU32), 1, _fp); NX_ASSERT(w); return *this; } @@ -155,7 +164,7 @@ NxStream &PhysxFileStream::storeDword(NxU32 d) //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeFloat(NxReal f) { - size_t w = fwrite(&f, sizeof(NxReal), 1, fp); + size_t w = fwrite(&f, sizeof(NxReal), 1, _fp); NX_ASSERT(w); return *this; } @@ -167,7 +176,7 @@ NxStream &PhysxFileStream::storeFloat(NxReal f) //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeDouble(NxF64 f) { - size_t w = fwrite(&f, sizeof(NxF64), 1, fp); + size_t w = fwrite(&f, sizeof(NxF64), 1, _fp); NX_ASSERT(w); return *this; } @@ -179,7 +188,7 @@ NxStream &PhysxFileStream::storeDouble(NxF64 f) //////////////////////////////////////////////////////////////////// NxStream &PhysxFileStream::storeBuffer(const void *buffer, NxU32 size) { - size_t w = fwrite(buffer, size, 1, fp); + size_t w = fwrite(buffer, size, 1, _fp); NX_ASSERT(w); return *this; } diff --git a/panda/src/physx/physxFileStream.h b/panda/src/physx/physxFileStream.h index cf0762f8ff..ea27272819 100644 --- a/panda/src/physx/physxFileStream.h +++ b/panda/src/physx/physxFileStream.h @@ -16,6 +16,8 @@ #define PHYSXFILESTREAM_H #include "pandabase.h" +#include "virtualFile.h" +#include "filename.h" #include "physx_includes.h" @@ -26,7 +28,7 @@ class EXPCL_PANDAPHYSX PhysxFileStream : public NxStream { public: - PhysxFileStream(const char *filename, bool load); + PhysxFileStream(const Filename &fn, bool load); virtual ~PhysxFileStream(); virtual NxU8 readByte() const; @@ -44,7 +46,13 @@ public: virtual NxStream &storeBuffer(const void *buffer, NxU32 size); private: - FILE* fp; + + // write + FILE* _fp; + + // read + PT(VirtualFile) _vf; + istream *_in; }; #endif // PHYSXFILESTREAM_H diff --git a/panda/src/physx/physxKitchen.cxx b/panda/src/physx/physxKitchen.cxx index 1148b30206..df8afc8752 100644 --- a/panda/src/physx/physxKitchen.cxx +++ b/panda/src/physx/physxKitchen.cxx @@ -63,7 +63,7 @@ cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc, const Filename &filename) nassertr_always(filename.touch(), false); nassertr_always(meshDesc.is_valid(), false); - PhysxFileStream stream = PhysxFileStream(filename.c_str(), false); + PhysxFileStream stream = PhysxFileStream(filename, false); return _cooking->NxCookConvexMesh(meshDesc.get_desc(), stream); } @@ -79,7 +79,7 @@ cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc, const Filename &filena nassertr_always(filename.touch(), false); nassertr_always(meshDesc.is_valid(), false); - PhysxFileStream stream = PhysxFileStream(filename.c_str(), false); + PhysxFileStream stream = PhysxFileStream(filename, false); return _cooking->NxCookTriangleMesh(meshDesc.get_desc(), stream); } diff --git a/panda/src/physx/physxMeshPool.cxx b/panda/src/physx/physxMeshPool.cxx index ada4132e59..a278ca8686 100644 --- a/panda/src/physx/physxMeshPool.cxx +++ b/panda/src/physx/physxMeshPool.cxx @@ -16,6 +16,7 @@ #include "physxConvexMesh.h" #include "physxTriangleMesh.h" #include "physxFileStream.h" +#include "virtualFileSystem.h" PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes; PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes; @@ -23,26 +24,20 @@ PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes; //PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes; //////////////////////////////////////////////////////////////////// -// Function: PhysxMeshPool::prepare_filename +// Function: PhysxMeshPool::check_file // Access: Private -// Description: Checks if the filename is valid, then resolves the -// filename on the Panda3D model search patch, and -// finally checks if the filename exists. +// Description: //////////////////////////////////////////////////////////////////// bool PhysxMeshPool:: -prepare_filename(Filename &fn) { +check_filename(const Filename &fn) { - if (fn.empty()) { - // Invalid filename. - physx_cat.error() << "Invalid filename\n"; + if (!(VirtualFileSystem::get_global_ptr()->exists(fn))) { + physx_cat.error() << "File does not exists: " << fn << endl; return false; } - fn.resolve_filename(get_model_path()); - - if (!fn.exists()) { - // Non-existent filename. - physx_cat.error() << "Invalid filename\n"; + if (!(VirtualFileSystem::get_global_ptr()->is_regular_file(fn))) { + physx_cat.error() << "Not a regular file: " << fn << endl; return false; } @@ -55,10 +50,9 @@ prepare_filename(Filename &fn) { // Description: //////////////////////////////////////////////////////////////////// PhysxConvexMesh *PhysxMeshPool:: -load_convex_mesh(const Filename &filename) { +load_convex_mesh(const Filename &fn) { - Filename fn(filename); - if (!prepare_filename(fn)) return NULL; + if (!check_filename(fn)) return NULL; PhysxConvexMesh *mesh; @@ -66,7 +60,7 @@ load_convex_mesh(const Filename &filename) { if (it == _convex_meshes.end()) { // Not found; load mesh. NxConvexMesh *meshPtr; - PhysxFileStream stream = PhysxFileStream(fn.to_os_specific().c_str(), true); + PhysxFileStream stream = PhysxFileStream(fn, true); mesh = new PhysxConvexMesh(); nassertr_always(mesh, NULL); @@ -95,10 +89,9 @@ load_convex_mesh(const Filename &filename) { // Description: //////////////////////////////////////////////////////////////////// PhysxTriangleMesh *PhysxMeshPool:: -load_triangle_mesh(const Filename &filename) { +load_triangle_mesh(const Filename &fn) { - Filename fn(filename); - if (!prepare_filename(fn)) return NULL; + if (!check_filename(fn)) return NULL; PhysxTriangleMesh *mesh; @@ -106,7 +99,7 @@ load_triangle_mesh(const Filename &filename) { if (it == _triangle_meshes.end()) { // Not found; load mesh. NxTriangleMesh *meshPtr; - PhysxFileStream stream = PhysxFileStream(fn.to_os_specific().c_str(), true); + PhysxFileStream stream = PhysxFileStream(fn, true); mesh = new PhysxTriangleMesh(); nassertr_always(mesh, NULL); diff --git a/panda/src/physx/physxMeshPool.h b/panda/src/physx/physxMeshPool.h index 65c5d482d9..ae27c7fc0e 100644 --- a/panda/src/physx/physxMeshPool.h +++ b/panda/src/physx/physxMeshPool.h @@ -50,7 +50,7 @@ PUBLISHED: static void list_contents(ostream &out); private: - static bool prepare_filename(Filename &fn); + static bool check_filename(const Filename &fn); typedef pmap ConvexMeshes; typedef pmap TriangleMeshes;