Browse Source

Fixed loading of binary meshes from virtual file system.

enn0x 16 years ago
parent
commit
c299cba834

+ 30 - 21
panda/src/physx/physxFileStream.cxx

@@ -16,14 +16,22 @@
 
 
 #include "stdio.h"
 #include "stdio.h"
 
 
+#include "virtualFileSystem.h"
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PhysxFileStream::Constructor
 //     Function: PhysxFileStream::Constructor
 //       Access: Public
 //       Access: Public
 //  Description:
 //  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()
 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 PhysxFileStream::readByte() const
 {
 {
   NxU8 b;
   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;
   return b;
 }
 }
 
 
@@ -57,8 +66,8 @@ NxU8 PhysxFileStream::readByte() const
 NxU16 PhysxFileStream::readWord() const
 NxU16 PhysxFileStream::readWord() const
 {
 {
   NxU16 w;
   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;
   return w;
 }
 }
 
 
@@ -70,8 +79,8 @@ NxU16 PhysxFileStream::readWord() const
 NxU32 PhysxFileStream::readDword() const
 NxU32 PhysxFileStream::readDword() const
 {
 {
   NxU32 d;
   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;
   return d;
 }
 }
 
 
@@ -83,8 +92,8 @@ NxU32 PhysxFileStream::readDword() const
 float PhysxFileStream::readFloat() const
 float PhysxFileStream::readFloat() const
 {
 {
   NxReal f;
   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;
   return f;
 }
 }
 
 
@@ -96,8 +105,8 @@ float PhysxFileStream::readFloat() const
 double PhysxFileStream::readDouble() const
 double PhysxFileStream::readDouble() const
 {
 {
   NxF64 f;
   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;
   return f;
 }
 }
 
 
@@ -108,8 +117,8 @@ double PhysxFileStream::readDouble() const
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void PhysxFileStream::readBuffer(void *buffer, NxU32 size) 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)
 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);
   NX_ASSERT(w);
   return *this;
   return *this;
 }
 }
@@ -131,7 +140,7 @@ NxStream &PhysxFileStream::storeByte(NxU8 b)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 NxStream &PhysxFileStream::storeWord(NxU16 w)
 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);
   NX_ASSERT(ww);
   return *this;
   return *this;
 }
 }
@@ -143,7 +152,7 @@ NxStream &PhysxFileStream::storeWord(NxU16 w)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 NxStream &PhysxFileStream::storeDword(NxU32 d)
 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);
   NX_ASSERT(w);
   return *this;
   return *this;
 }
 }
@@ -155,7 +164,7 @@ NxStream &PhysxFileStream::storeDword(NxU32 d)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 NxStream &PhysxFileStream::storeFloat(NxReal f)
 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);
   NX_ASSERT(w);
   return *this;
   return *this;
 }
 }
@@ -167,7 +176,7 @@ NxStream &PhysxFileStream::storeFloat(NxReal f)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 NxStream &PhysxFileStream::storeDouble(NxF64 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);
   NX_ASSERT(w);
   return *this;
   return *this;
 }
 }
@@ -179,7 +188,7 @@ NxStream &PhysxFileStream::storeDouble(NxF64 f)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 NxStream &PhysxFileStream::storeBuffer(const void *buffer, NxU32 size)
 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);
   NX_ASSERT(w);
   return *this;
   return *this;
 }
 }

+ 10 - 2
panda/src/physx/physxFileStream.h

@@ -16,6 +16,8 @@
 #define PHYSXFILESTREAM_H
 #define PHYSXFILESTREAM_H
 
 
 #include "pandabase.h"
 #include "pandabase.h"
+#include "virtualFile.h"
+#include "filename.h"
 
 
 #include "physx_includes.h"
 #include "physx_includes.h"
 
 
@@ -26,7 +28,7 @@
 class EXPCL_PANDAPHYSX PhysxFileStream : public NxStream {
 class EXPCL_PANDAPHYSX PhysxFileStream : public NxStream {
 
 
 public:
 public:
-  PhysxFileStream(const char *filename, bool load);
+  PhysxFileStream(const Filename &fn, bool load);
   virtual ~PhysxFileStream();
   virtual ~PhysxFileStream();
 
 
   virtual NxU8 readByte() const;
   virtual NxU8 readByte() const;
@@ -44,7 +46,13 @@ public:
   virtual NxStream &storeBuffer(const void *buffer, NxU32 size);
   virtual NxStream &storeBuffer(const void *buffer, NxU32 size);
 
 
 private:
 private:
-  FILE* fp;
+
+  // write
+  FILE* _fp;
+
+  // read
+  PT(VirtualFile) _vf;
+  istream *_in;
 };
 };
 
 
 #endif // PHYSXFILESTREAM_H
 #endif // PHYSXFILESTREAM_H

+ 2 - 2
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(filename.touch(), false);
   nassertr_always(meshDesc.is_valid(), 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);
   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(filename.touch(), false);
   nassertr_always(meshDesc.is_valid(), 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);
   return _cooking->NxCookTriangleMesh(meshDesc.get_desc(), stream);
 }
 }
 
 

+ 14 - 21
panda/src/physx/physxMeshPool.cxx

@@ -16,6 +16,7 @@
 #include "physxConvexMesh.h"
 #include "physxConvexMesh.h"
 #include "physxTriangleMesh.h"
 #include "physxTriangleMesh.h"
 #include "physxFileStream.h"
 #include "physxFileStream.h"
+#include "virtualFileSystem.h"
 
 
 PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes;
 PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes;
 PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes;
 PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes;
@@ -23,26 +24,20 @@ PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes;
 //PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes;
 //PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: PhysxMeshPool::prepare_filename
+//     Function: PhysxMeshPool::check_file
 //       Access: Private
 //       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::
 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;
     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;
     return false;
   }
   }
 
 
@@ -55,10 +50,9 @@ prepare_filename(Filename &fn) {
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 PhysxConvexMesh *PhysxMeshPool::
 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;
   PhysxConvexMesh *mesh;
 
 
@@ -66,7 +60,7 @@ load_convex_mesh(const Filename &filename) {
   if (it == _convex_meshes.end()) {
   if (it == _convex_meshes.end()) {
     // Not found; load mesh.
     // Not found; load mesh.
     NxConvexMesh *meshPtr;
     NxConvexMesh *meshPtr;
-    PhysxFileStream stream = PhysxFileStream(fn.to_os_specific().c_str(), true);
+    PhysxFileStream stream = PhysxFileStream(fn, true);
 
 
     mesh = new PhysxConvexMesh();
     mesh = new PhysxConvexMesh();
     nassertr_always(mesh, NULL);
     nassertr_always(mesh, NULL);
@@ -95,10 +89,9 @@ load_convex_mesh(const Filename &filename) {
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 PhysxTriangleMesh *PhysxMeshPool::
 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;
   PhysxTriangleMesh *mesh;
 
 
@@ -106,7 +99,7 @@ load_triangle_mesh(const Filename &filename) {
   if (it == _triangle_meshes.end()) {
   if (it == _triangle_meshes.end()) {
     // Not found; load mesh.
     // Not found; load mesh.
     NxTriangleMesh *meshPtr;
     NxTriangleMesh *meshPtr;
-    PhysxFileStream stream = PhysxFileStream(fn.to_os_specific().c_str(), true);
+    PhysxFileStream stream = PhysxFileStream(fn, true);
 
 
     mesh = new PhysxTriangleMesh();
     mesh = new PhysxTriangleMesh();
     nassertr_always(mesh, NULL);
     nassertr_always(mesh, NULL);

+ 1 - 1
panda/src/physx/physxMeshPool.h

@@ -50,7 +50,7 @@ PUBLISHED:
   static void list_contents(ostream &out);
   static void list_contents(ostream &out);
 
 
 private:
 private:
-  static bool prepare_filename(Filename &fn);
+  static bool check_filename(const Filename &fn);
 
 
   typedef pmap<Filename, PT(PhysxConvexMesh)> ConvexMeshes;
   typedef pmap<Filename, PT(PhysxConvexMesh)> ConvexMeshes;
   typedef pmap<Filename, PT(PhysxTriangleMesh)> TriangleMeshes;
   typedef pmap<Filename, PT(PhysxTriangleMesh)> TriangleMeshes;