Skip to content

Instantly share code, notes, and snippets.

@zzuegg
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzuegg/d81d8d2a9e6db994de2e to your computer and use it in GitHub Desktop.
Save zzuegg/d81d8d2a9e6db994de2e to your computer and use it in GitHub Desktop.
public class JoglAsyncLoader implements Runnable, GLEventListener {
LinkedBlockingQueue<LoadingTask> loadingTasks;
GLContext asyncContext;
GLOffscreenAutoDrawable asyncOffscreenDrawable;
Thread loadingThread;
public JoglAsyncLoader() {
loadingThread = new Thread(this);
GLContext renderContext = GLContext.getCurrent();
asyncOffscreenDrawable = GLDrawableFactory.getFactory(renderContext.getGLDrawable().getGLProfile()).createOffscreenAutoDrawable(null, renderContext.getGLDrawable().getChosenGLCapabilities(), null, 2, 2);
asyncOffscreenDrawable.setSharedAutoDrawable((GLAutoDrawable) renderContext.getGLDrawable());
asyncOffscreenDrawable.addGLEventListener(this);
//Pretty critical lines, if jme calls makeCurrent somewhere the context has to be released first
renderContext.release();
asyncOffscreenDrawable.display();
renderContext.makeCurrent(); //Also very critical since you must make the context again current on jme's render thread
}
@Override
public void init(GLAutoDrawable drawable) {
asyncContext = drawable.getContext();
loadingThread.start();
}
@Override
public void run() {
asyncContext.makeCurrent(); //Binds the async context to the loading thread. Anywhere after this line. GLContext.getCurrent().getGL() gives you a valid gl object to mess around with ;)
LoadingTask currentTask;
while (true) {
try {
currentTask = loadingTasks.poll(1, TimeUnit.DAYS);
currentTask.executeGL();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
protected interface LoadingTask {
void executeGL();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment