如何从 jna 引用指针中检索 java class
How to retrieve java class from jna reference pointer
我正在用 jna 移植 openvr sample to jogl, after we created the binding。
几乎在最后(在渲染控制器和跟踪基站之前),我在尝试从 C++ 中的引用指针检索 java 中的模型 class 时遇到了困难。
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
是 null 而 vertexCount
是 0,在 c++ 上我有有效的值..
如何从 C++ 中的引用指针中检索 java class?
已解决,我需要创建一个指针 modelPtr
,然后创建一个指向指针 modelPtrPtr
的指针并将其传递给 LoadRenderModel_Async
。
最后,我实例化了一个模型 class,方法是将指针的值传递给一个指针,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();
这里有一些更简单的东西:
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();
}
}
我正在用 jna 移植 openvr sample to jogl, after we created the binding。
几乎在最后(在渲染控制器和跟踪基站之前),我在尝试从 C++ 中的引用指针检索 java 中的模型 class 时遇到了困难。
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
是 null 而 vertexCount
是 0,在 c++ 上我有有效的值..
如何从 C++ 中的引用指针中检索 java class?
已解决,我需要创建一个指针 modelPtr
,然后创建一个指向指针 modelPtrPtr
的指针并将其传递给 LoadRenderModel_Async
。
最后,我实例化了一个模型 class,方法是将指针的值传递给一个指针,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();
这里有一些更简单的东西:
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();
}
}