PGraphics and PApplets

Hello. Im trying to do a preview output of main buffer Graphic in a extra pApplet.

Here my code:

layerWindow p;
LayerManager layer;


void settings(){
size(1280, 720, P3D);
}

void setup(){
  p = new layerWindow();
  layer = new LayerManager();
}

void draw(){

}

class LayerManager {

  ArrayList<PGraphics>layers;


  LayerManager() {

    layers = new ArrayList<PGraphics>();

    layers.add(createGraphics(width, height, P3D));
    
  }


  void update(PApplet p) {

    layers.get(0).beginDraw();
    //buffer.clear();
    //buffer.shader(vs.get(0).shader);
    //layers.get(0).hint(DISABLE_DEPTH_TEST);
    layers.get(0).hint(DISABLE_OPTIMIZED_STROKE);
    layers.get(0).hint(DISABLE_DEPTH_MASK);
    layers.get(0).sphere(100);
    layers.get(0).endDraw();
 
      tint(255, 50);
      image(layers.get(0), 0, 0);

    
  }

}

public class layerWindow extends PApplet {  

  layerWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(1280, 720, P2D);
  }

  void setup() {
  }

  void draw() {
    layer.update(this);
  }
}

but it return 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 functions as long as they are called from draw(),
but not in event handling functions such as keyPressed()
or mousePressed().

How can i draw a PGraphic image in a extra Applet? Thanks!

I think you are not allowed for multiple PApplets to access the P2D/P3D renderers at the same time. In other words, only one applet is allowed to access OpenGL per sketch. Do you need the OpenGL renderer for your task?

Kf

I do others things with a P2D in the extra applet and it works…
i need a copy of that PGraphics buffer and feed into the main canvas and in an extra Applet.

  • Only 1 Thread can write to a PGraphics at the same time! :zipper_mouth_face:
  • Although many can read from 1 at the same time. :grinning:
  • However PGraphics multithreading writing doesn’t seem it’s the exact problem there. :dizzy_face:
  • The only instance of LayerManager is created by the main PApplet and its reference stored in its global field layer.
  • Inside its constructor, a P3D createGraphics() is invoked, w/ its OpenGL context bound to the main PApplet.
  • However, you attempt to change its attributes under another PApplet instance! :cold_sweat:
  • You’re gonna need to ask the main PApplet, which owns that PGraphics, to do that instead! :upside_down_face:

thanks! any idea how can i do it? or some starting point to research?

  • Make your other PApplet have its own PGraphics.
  • Then your main PApplet can display it via image().

Using multiple PApplets inside the same JVM with OpenGL is a recipe for disaster! Unfortunately, static state in the renderer causes instability and/or crashes, somewhat dependent on your drivers and what exactly you’re doing.

The best way to do this, although unfortunately not platform-neutral, would be to use the Syphon (macOS) and/or Spout (Windows) libraries to share graphics across processes. This is done via texture sharing on the GPU, so will perform better than anything you do to share image data across PApplets inside Processing anyway. Annoyingly there’s no Linux equivalent though.