Jump to content

Qt and threading


photo

Recommended Posts

I'm currently working from the QtApp example in which the main thread is controlled by Qt, and the Unigine engine lives in the same thread. It seems that it's not possible to make engine calls outside of the main thread (which makes sense), but it's generally good practice to offload expensive tasks to a non-GUI thread.

 

For example, I'm attempting to load a large mesh (>1 million triangles) at runtime. This currently hangs the program for a number of seconds. Ideally, I would create a new thread and load the mesh there, making it such that the GUI doesn't freeze. However, this doesn't work since meshes must be created/loaded in the engine thread (which is also the Qt/GUI thread).

 

What options do I have for achieving this functionality? It seems like the best method would be to initialize the engine in a separate (non-GUI) thread, but I'm not sure how the engine and GUI threads can be synchronized.

 

Thanks,

Kevin

 

Link to comment

Thanks! This looks promising. Is there a way to generalize this to .mesh files / more specific node types? E.g., can I perform the following calls asynchronously:

Unigine::ObjectMeshStaticPtr mesh = Unigine::ObjectMeshStatic::create(0);
mesh->loadMesh("file.mesh");

I haven't had any luck finding code examples using asynch calls.

Link to comment

You have to wrap your mesh as ObjectMeshStatic node in an external XML node file (can be done in the editor by exporting imported mesh node. In general all kinds of Uni gone node types, material libraries can be placed in such node file for async loading)

 

When you want to load this mesh node file kick off the background load via loadAsyncNode() and store returned load operation identifier in some list. On each follow-up frame check end of load via getAsyncNode(id). If return value is <> 0 than you can cast the return value to ObjectMeshStatic and use it as appropriate (This should work if ObjectMeshStati node is the only node in the file, otherwise you have to traverse returned node hierarchy for finding mesh node).

 

Might be necessary to add the node to the editor for automatic memory garbage collection on destruction, otherwise you'll have to handle object deletion on your own (though I am not 100% sure here)

Link to comment
×
×
  • Create New...