-
-
Save zzuegg/d81d8d2a9e6db994de2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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