fabmax / physx-jni

Java JNI bindings for Nvidia PhysX
MIT License
88 stars 9 forks source link

Could you please support PxDefaultFileInputData and PxSerialization ? #15

Closed newcoder7777 closed 3 years ago

newcoder7777 commented 3 years ago

c++ code:

char fileName[50]; cout << "please input file path:" << endl; cin >> fileName; PxDefaultFileInputData inputData = physx::PxDefaultFileInputData(fileName); bool isValid = inputData.isValid(); if (isValid) { printf("exist file"); } else { printf("error! not exist file"); exit(0); return; }

registry = PxSerialization::createSerializationRegistry(gPhysics); cooking = PxCreateCooking(PX_PHYSICS_VERSION, gFoundation, PxCookingParams(gPhysics->getTolerancesScale())); PxCollection convexCollection = PxSerialization::createCollectionFromXml(inputData, cooking, registry, NULL); gScene->addCollection(convexCollection); convexCollection->release(); registry->release();

fabmax commented 3 years ago

I will see what I can do. However, I prefer PxDefaultMemoryInputData + java file IO. This way serialization should also work with the javascript version of the library.

fabmax commented 3 years ago

This should work now:

    Vector_PxU8 data = new Vector_PxU8();
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName))) {
        int b = in.read();
        while (b != -1) {
            data.push_back((byte) b);
            b = in.read();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    PxDefaultMemoryInputData memIn = new PxDefaultMemoryInputData(TypeHelpers.voidToU8Ptr(data.data()), data.size());
    PxSerializationRegistry sr = PxSerialization.createSerializationRegistry(PhysXTestEnv.physics);
    PxCollection loadedCollection = PxSerialization.createCollectionFromXml(memIn, PhysXTestEnv.cooking, sr);
    scene.addCollection(loadedCollection);

Loading the data byte-by-byte through the Vector_PxU8 is not the most efficient way, but the easiest.

newcoder7777 commented 3 years ago

Thank you very much!