I’m working on a live visualizer program for a band, which is structured like this:
The main Draw() loop creates a thread, wherein whichever animation function is currently running is updated. These animation functions often use one or more PGraphics objects, which are later drawn to the main screen.
Everything was working great for the first version, which was all 2D. Now, I’m working on converting everything over to P3D to get a little fancier.
However, using this exact same setup except with P3D PGraphics objects, the program crashes with the following error:
“You are trying to draw outside OpenGL’s animation thread. Place all drawing commands in the draw() function, or inside your own function as long as they are called from draw(), but not in any event handling functions such as keyPressed() or mousePressed()”
I haven’t changed anything except making the sketch P3D, and making the PGraphics objects P3D. Does anyone out there have any experience with this? If it’s possible, I’d love to keep the current thread() setup for this application rather than restructure the whole thing.
I’ve tended to avoid draw threading in the past, because Processing isn’t really set up for it – but based on what I have heard I am surprised that your first (JAVA2D?) version ever worked at all. Under normal circumstances I thought that you shouldn’t be able to draw from a thread without Bad Things Happening. Here is the general advice for making thread-based operations draw-free – essentially, signal the main animation thread that content is ready for drawing:
You cannot draw to the screen from a function called by thread() . Because it runs independently, the code will not be synchronized to the animation thread, causing strange or at least inconsistent results. Use thread() to load files or do other tasks that take time. When the task is finished, set a variable that indicates the task is complete, and check that from inside your draw() method. thread() / Reference / Processing.org
Thanks for the reply! The quote you’ve added here is basically what I’m doing: the thread() methods draw to a PGraphics object and then flag a boolean. Then, in draw(), if the bool is True, I use image() to draw the PGraphics to the main screen – so nothing is actually drawn to the main window in the treads themselves. This works fine when the PGraphics is initialized with the standard 2D renderer – it’s only when the PGraphics is set to P3D that I get that error.