# Drawing a shader generated PGraphics onto a non OpenGL PGraphics object

**URL:** https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264
**Category:** Coding Questions
**Created:** [December 15, 2020, 11:50am UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264 "2020-12-15T11:50:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![r.jaoui](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r.jaoui/32/11777_2.png) [@r.jaoui](https://discourse.processing.org/u/r.jaoui)
#### Post date: [December 15, 2020, 11:50am UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/1 "2020-12-15T11:50:08Z")

</div>

Hello everyone !

I am currently developping quite a big project, and for simplicity I have chosen to use Processing as a graphical engine. I am coding with Eclipse Neon.

The issue is that I have developped GLSL Fragment shaders to accelerate image processing, and then apply them to a P2D PGraphics object (which works fine), the general PApplet is also P2D but there are a few in-between PGraphics objects that aren’t P2D and that I can’t make P2D since for some reason this messes up the whole look of the project (and this represent quite a substantial amount of time).

So basically, I run a shader onto a P2D PGraphics object and I then want to draw the resulting image onto a non OpenGL PGraphics object, and for some reason this doesn’t work, even though I assumed that after rendering the shader on the first PGraphics object, I would simply have an image I could render elsewhere…

Thank you for your help !

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 15, 2020, 8:07pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/2 "2020-12-15T20:07:30Z")

</div>

Maybe transferring the contents of _pixels[]_ to another’s via **arrayCopy()** might work:

> **[PImage::pixels\[\] \\ Language (API) \\ Processing 3+](https://Processing.org/reference/PImage_pixels.html)**

  

> **[arrayCopy() \\ Language (API) \\ Processing 3+](https://Processing.org/reference/arrayCopy_.html)**

---

<div class="post-metadata">

### Author: ![r.jaoui](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r.jaoui/32/11777_2.png) [@r.jaoui](https://discourse.processing.org/u/r.jaoui)
#### Post date: [December 16, 2020, 1:45pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/3 "2020-12-16T13:45:25Z")

</div>

@GoToLoop Thanks for your reply !

So I’ve tried adapting my code the following way :

```auto

	public void render(){
		this.image.beginDraw();
		this.image.filter(alphaOverlayShader);
		this.image.endDraw();
	}
	
	public PGraphics getImage() {
		PGraphics out = context.createGraphics(w, h);
		this.image.beginDraw();
		this.image.loadPixels();
		out.beginDraw();
		out.loadPixels();
		PApplet.arrayCopy(this.image.pixels, out.pixels);
		out.updatePixels();
		out.endDraw();
		this.image.updatePixels();
		this.image.endDraw();
		return out;
	}

```

But this still doesn’t work, or at least I am still doing something wrong…

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 16, 2020, 4:21pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/4 "2020-12-16T16:21:39Z")

</div>

> [@r.jaoui](#):
>
> But this still doesn’t work,

Merely stating it doesn’t work w/o describing exactly what happened doesn’t help.

And btW, we just use **updatePixels()** and/or **loadPixels()** for _pixels[]_.

Methods **beginDraw()** & **endDraw()** shouldn’t be used on that context.

And given you’re **createGraphics()** every time rather than using an existing 1 you can instead clone a PGraphics as a PImage calling method **get()**:

> **[get() / Reference](https://processing.org/reference/PImage_get_.html)**
>
> Reads the color of any pixel or grabs a section of an image. If no parameters are specified, the entire image is returned. Use the x and y parameters to get the value of one pixel. Get a…

---

<div class="post-metadata">

### Author: ![r.jaoui](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r.jaoui/32/11777_2.png) [@r.jaoui](https://discourse.processing.org/u/r.jaoui)
#### Post date: [December 16, 2020, 6:07pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/5 "2020-12-16T18:07:56Z")

</div>

> [@GoToLoop](#):
>
> Merely stating it doesn’t work w/o describing exactly what happened doesn’t help.

Yeah you’re right. So basically the sketch runs as if the shader wasn’t applied (this shader doesn’t modify the current **pixels** array, but it actually draws over it, in this case a checkerboard pattern) and the result here is that it’s as if this image is transparent.

> [@GoToLoop](#):
>
> And btW, we just use **updatePixels()** and/or **loadPixels()** for _pixels[]_ .
> 
> Methods **beginDraw()** & **endDraw()** shouldn’t be used on that context.

Oh I didn’t know that, I sometimes run into NullPointerExceptions when **loadPixels()** is called without **beginDraw()** before but I think that this is due to the fact there was actually nothing drawn onto the **PGraphics** beforehand.

> [@GoToLoop](#):
>
> And given you’re **createGraphics()** every time rather than using an existing 1 you can instead clone a PGraphics as a PImage calling method **get()** :

This is also one of the things I tried before. This is why I tried recreating my issue on a simpler sketch :

```auto
PGraphics inBetween;
PGraphics shadedLayer;

PImage image;

void setup(){
  size(1280, 720, P2D);
  
  inBetween = createGraphics(1280, 720);
  
  inBetween.beginDraw();
  inBetween.background(255);
  inBetween.endDraw();
  
  shadedLayer = createGraphics(1280, 720, P2D);
  
  image = loadImage("image.png");
}

void draw(){
  shadeLayer();
  
  method1();
  //method2();
  //method3();
  //method4();
  
  image(inBetween, 0, 0);
}

void method1(){
  inBetween.beginDraw();
  inBetween.image(shadedLayer, 0, 0);
  inBetween.endDraw();
}

void method2(){
  shadedLayer.loadPixels();
  inBetween.loadPixels();
  arrayCopy(shadedLayer.pixels, inBetween.pixels);
  inBetween.updatePixels();
  shadedLayer.updatePixels();
}

void method3(){
  inBetween.beginDraw();
  inBetween.image(shadedLayer.get(), 0, 0);
  inBetween.endDraw();
}

void method4(){
  shadedLayer.loadPixels();
  inBetween.loadPixels();
  for(int x = 0; x < shadedLayer.width; x++){
    for(int y = 0; y < shadedLayer.height; y++){
      inBetween.pixels[x + y * inBetween.width] = shadedLayer.pixels[x + y * shadedLayer.width];
    }
  }
  inBetween.updatePixels();
  shadedLayer.updatePixels();
}

void shadeLayer(){
  shadedLayer.beginDraw();
  shadedLayer.image(image, 0, 0);
  shadedLayer.endDraw();
  shadedLayer.filter(BLUR, 10);
}

```

But in this case all methods seem to work so… perhaps my issue isn’t even where I thought it was…

Thanks for your help @GoToLoop by the way.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 16, 2020, 6:30pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/6 "2020-12-16T18:30:39Z")

</div>

I know nothing about Processing’s shader implementation.  
I guess it’s tightly bound to a PGraphics and can’t be transferred.

---

<div class="post-metadata">

### Author: ![r.jaoui](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r.jaoui/32/11777_2.png) [@r.jaoui](https://discourse.processing.org/u/r.jaoui)
#### Post date: [January 9, 2021, 8:37pm UTC](https://discourse.processing.org/t/drawing-a-shader-generated-pgraphics-onto-a-non-opengl-pgraphics-object/26264/7 "2021-01-09T20:37:46Z")

</div>

**I am writing this answer to close subject** : apparently to apply a shader, something has to already have been drawn onto the PGraphics object, weither it is P2D or not. The simple fix was to apply a background color before applying the shader, weither it is a modifyer fragment shader or a “rendering” shader that draws “on top” of the previous pixels.
