如何从jna引用指针中检索java类

时间:2016-08-31 08:18:41

标签: java pointers reference jna

在我们使用jna创建openvr sample之后,我将jogl移植到binding

几乎在最后(在渲染控制器和跟踪基站之前),我试图从C ++中的引用指针中检索java中的模型类。

C ++代码here

while ( 1 )
{
    error = vr::VRRenderModels()->LoadRenderModel_Async( pchRenderModelName, &pModel );
    if ( error != vr::VRRenderModelError_Loading )
        break;

    ThreadSleep( 1 );
}

LoadRenderModel_Async here

/** Loads and returns a render model for use in the application. pchRenderModelName should be a render model name
* from the Prop_RenderModelName_String property or an absolute path name to a render model on disk. 
*
* The resulting render model is valid until VR_Shutdown() is called or until FreeRenderModel() is called. When the 
* application is finished with the render model it should call FreeRenderModel() to free the memory associated
* with the model.
*
* The method returns VRRenderModelError_Loading while the render model is still being loaded.
* The method returns VRRenderModelError_None once loaded successfully, otherwise will return an error. */
virtual EVRRenderModelError LoadRenderModel_Async( const char *pchRenderModelName, RenderModel_t **ppRenderModel ) = 0;

jna将其翻译为(here):

public interface LoadRenderModel_Async_callback extends Callback {

    int apply(Pointer pchRenderModelName, PointerByReference ppRenderModel);
};

在java上我试图以这种方式加载模型(here):

int error;
PointerByReference modelPtrRef = new PointerByReference();

while (true) {

    Pointer stringPointer = new Memory(modelName.length());

    error = renderModels.LoadRenderModel_Async.apply(stringPointer, modelPtrRef);

    if (error != VR.EVRRenderModelError.VRRenderModelError_Loading) {
        break;
    }

    try {
        Thread.sleep(1);
    } catch (InterruptedException ex) {
        Logger.getLogger(ModelsRender.class.getName()).log(Level.SEVERE, null, ex);
    }
}

RenderModel_t m = new RenderModel_t(modelPtrRef.getValue());
m.read();

m为空,即vertexData为空且vertexCount为0,在c ++上我改为使用有效值。

如何从c ++中的引用指针中检索java类?

2 个答案:

答案 0 :(得分:1)

这里有点简单:

if (errorBuffer.get(0) != VR.EVRRenderModelError.VRRenderModelError_None) {
    return null;
}

int error;
PointerByReference pref = new PointerByReference();

while (true) {

    error = renderModels.LoadRenderModel_Async.apply(modelName, pref);

    if (error != VR.EVRRenderModelError.VRRenderModelError_Loading) {
        break;
    }

    try {
        Thread.sleep(1);
    } catch (InterruptedException ex) {
        Logger.getLogger(ModelsRender.class.getName()).log(Level.SEVERE, null, ex);
    }
}
RenderModel_t model = new RenderModel_t(pref.getValue());


public class RenderModel_t {
    public RenderModel_t(Pointer p) {
        super(p);
        read();
    }
}

答案 1 :(得分:0)

解决了,我需要创建一个指针modelPtr,然后指向一个指针modelPtrPtr并将其传递给LoadRenderModel_Async

最后,我通过将指针的值传递给指针和read()来实例化一个模型类,以检索从本机到堆内存的值:

    RenderModel_t.ByReference modelPtr = new RenderModel_t.ByReference();

    if (errorBuffer.get(0) != VR.EVRRenderModelError.VRRenderModelError_None) {
        return null;
    }

    int error;
    PointerByReference modelPtrPtr = new PointerByReference(modelPtr.getPointer());

    while (true) {

        Pointer stringPointer = new Memory(modelName.length() + 1);
        stringPointer.setString(0, modelName);

        error = renderModels.LoadRenderModel_Async.apply(stringPointer, modelPtrPtr);

        if (error != VR.EVRRenderModelError.VRRenderModelError_Loading) {
            break;
        }

        try {
            Thread.sleep(1);
        } catch (InterruptedException ex) {
            Logger.getLogger(ModelsRender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    RenderModel_t model = new RenderModel_t(modelPtrPtr.getValue());
    model.read();