# Different behaviors of default vs. P2D renderer with low-alpha curves

**URL:** <https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982>\
**Category:** Coding Questions\
**Created:** [May 7, 2019, 1:46am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982 "2019-05-07T01:46:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![blakej](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@blakej](https://discourse.processing.org/u/blakej)\
**Post date:** [May 7, 2019, 1:46am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/1 "2019-05-07T01:46:12Z")

</div>

Hi all,

I’m trying to understand a difference in behavior between the default 2D renderer and the P2D renderer. In short, the default renderer seems to generate a “softer” texture when overlapping many curves with low alpha value.

The following example code shows the difference I’m seeing:

```auto
boolean defrender = false;

void settings() {
  if (defrender) {
    size(600, 200);
  } else {
    size(600, 200, P2D);
  }
}

void setup() {
  if (defrender) {
    stroke(255, 32);
  } else {
    stroke(255, 16);
  }

  randomSeed(1);
  background(0);
  noFill();

  for (float i = 1; i < 50; i++) {
    final float dt = random(TWO_PI);
    beginShape();
    for (float j = 0; j < 600; j++) {
      curveVertex(j, 100 + 100 * sin(dt + TWO_PI * i * j / 600));
    }
    endShape();
  }
}

void draw() {
  save((defrender ? "default" : "p2d") + ".png");
  exit();
}

```

If I run it with defrender = false, I get the upper half of the following image, whereas with defrender = true, I get the lower half:

![combined](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0efe8ef3edaef8cdd426a8913d25175fb7f44896.jpeg)

A couple things about this stand out to me:

- I have to cut the alpha value in half to get a similar-looking result when using the P2D renderer. (Running it with alpha = 32 and the P2D renderer gives a result that is clearly much brighter.)
- The P2D renderer (top) seems to cause individual line overlaps to stand out much more than the default renderer (bottom).

For the thing I’m developing, I prefer the look of the default renderer in this situation, though the P2D renderer has functionality that I’m relying on elsewhere. Does anyone have ideas about how I can get the P2D renderer to behave more like the default one in this case? Also, any ideas why P2D is roughly twice as bright for a given alpha value?

Thanks in advance for any thoughts. I’m using Processing 3.5.3 with MacOS 10.13.6.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 10, 2019, 3:55pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/2 "2019-05-10T15:55:00Z")

</div>

I don’t know specifically re:alpha, but I can tell you:

The default renderer and P2D have many, many differences in capabilities and behaviors, as they are entirely different architectures.

The default renderer is JAVA2D. The P2D renderer is OpenGL, specifically JOGL 2.3. You can find some more pointers to specifics and a bit of history on P2D (/P3D) here:

> **[processing/processing](https://github.com/processing/processing/wiki/Advanced-OpenGL)**
>
> Source code for the Processing Core and Development Environment (PDE) - processing/processing

You can also find the different renderers in the processing source – although some of the differences you are interested in may be located in JOGL or Java2D themselves, which are external dependencies of Processing (although I am not an expert).

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 10, 2019, 4:36pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/3 "2019-05-10T16:36:44Z")

</div>

JAVA2D uses JAVA AWT ( **A** bstract **W** indow **T** oolkit) and is basically a simple 2D rendering engine. There was a Java 3D library (worked with AWT) which sat on top of OpenGL and DirectX but this has been superseded by JavaFX.

OpenGL supports both 2D and 3D rendering but in truth OpenGL is about rendering triangles and is not very good with lines. With regard to alpha I seem to remember that creating semi-transparent surfaces was a multi step process but my memory might be playing tricks on me and time has moved on since I did that.

---

<div class="post-metadata">

**Author:** ![blakej](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@blakej](https://discourse.processing.org/u/blakej)\
**Post date:** [May 10, 2019, 6:33pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/4 "2019-05-10T18:33:37Z")

</div>

Thanks (both of you) for taking a look at this. I figured the answer would be something like that, and realistically it’s probably not worth my time to invest the effort to deeply understand the mechanics of the backends.

So then, thoughts on a workaround. The project I’m building has one common file (with settings() / setup() / draw() etc) that is used by dozen of small subprojects. Each of the subprojects draws something generally circular, but each one is different. I’d like to be able to specify, for a given subproject, whether it should use the P2D renderer. A straightforward way of doing that would be to add a boolean to each subproject, but I’m probably going to wind up using JAVA2D for all but a handful of them, and I’d rather not have to add the variable in the overwhelmingly common case. Is there a way to declare something like an optional variable, which if it exists would override a default behavior? (I’m thinking of something like the concept of weak symbols in C, if that’s familiar.)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 10, 2019, 7:13pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/5 "2019-05-10T19:13:41Z")

</div>

> [@blakej](#):
>
> Is there a way to declare something like an optional variable, which if it exists would override a default behavior? (I’m thinking of something like the concept of weak symbols in C, if that’s familiar.)

I’m not sure I understand.

The default is:

```auto
size(600, 200);

```

…and, if you want to override that, you can pass:

```auto
size(600, 200, P2D);

```

…so, if you wanted to abstract, that you could make it:

```auto
String RENDERER = JAVA2D; // or P2D
setup(){
  size(600, 200, RENDERER);
}

```

…is this what you are talking about? In what code file(s) are you hoping to do this configuration, and for what outputs run in what contexts – are the .pde files runing in the PDE, or is this an IntelliJ / Eclipse project…?

---

<div class="post-metadata">

**Author:** ![blakej](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@blakej](https://discourse.processing.org/u/blakej)\
**Post date:** [May 10, 2019, 8:28pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/6 "2019-05-10T20:28:44Z")

</div>

Yes, something much like that. setup() lives in main.pde, and main.pde is linked into many other directories so it can be used by many other files (c00.pde through c50.pde, each one as a separate Processing project). I would like to add code to one or two of the c\*.pde files to indicate that main.pde should use P2D for that project, in such a way that I don’t have to modify the other 49 c\*.pde’s (to indicate that they should use the default of JAVA2D) _and_ that main.pde will work with all of them.

This is running either in the PDE or using processing-java.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 10, 2019, 9:21pm UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/7 "2019-05-10T21:21:39Z")

</div>

Hmm. Sounds complicated, given that PDE files are concatenated into a into a single text file before being translated into .java. When you say “linked” you mean linux file system hard links?

You could do it like this:

```auto
templates/
  main.pde
  settingsJAVA2D.pde
  settingsP2D.pde

myJAVA2Dsketch /
  main.pde (hard link)
  myJAVA2Dsketch.pde
  settingsJAVA2D.pde (hard link)

myP2Dsketch /
  main.pde (hard link)
  myP2Dsketch.pde
  settingsP2D.pde (hard link)

```

In template/main.pde, define setup() and shared functions / classes. Your setup should not contain size().

In template/settingsJAVA2D.pde and in template/settingsP2D.pde, define settings(). It should contain size().

When you create a new sketch, hard link the appropriate settings file from the template directory – or override by _not_ hard linking it, and just creating a new one.

> **[settings() / Reference](https://processing.org/reference/settings_.html)**
>
> The settings() function is new with Processing 3.0. It's not needed in most sketches. It's only useful when it's absolutely necessary to define the parameters to size() with a variable. …

> The **settings()** function is new with Processing 3.0. It’s not needed in most sketches. It’s only useful when it’s absolutely necessary to define the parameters to **size()** with a variable.

As an alternate approach, you might consider using two different branches of a git repository to maintain your template, with a different settings in each, all maintained under a single main.pde file. To create a new sketch, pull from the appropriate branch. To update all your sketches with new changes to main.pde, loop over your sketches and git pull.

---

<div class="post-metadata">

**Author:** ![blakej](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@blakej](https://discourse.processing.org/u/blakej)\
**Post date:** [May 11, 2019, 12:57am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/8 "2019-05-11T00:57:07Z")

</div>

Yep, I’m using file system hardlinks. Eventually I’ll switch to some sort of repo-based approach, but this works for now.

Your suggestion makes sense, and sounds about as simple as anything. I’ll probably actually just have something like a “rendererJAVA2D.pde” and “rendererP2D.pde”, which each define a single String that’s set to either JAVA2D or P2D, so I can use that value in multiple places. (I also call createGraphics() in a few places, and if I’m using P2D for the main renderer I want to use it there as well.)

Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 11, 2019, 2:37am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/9 "2019-05-11T02:37:41Z")

</div>

> [@blakej](#):
>
> which each define a single String

Great. If that’s ALL you are doing then can just assign that global variable in your mySketch.pde and use a single generic main.pde which loads the variable in settings() – then only two files and one hardlink required per sketch. I thought you were going to have more customization than that.

~~Notice that JAVA2D / P2D isn’t actually a string – it is a PConstant that references an int.~~

So you can just use `String RENDERER = JAVA2D; // or P2D` as in my example above into your sketch pde, and then put `settings(){ size(600, 200, RENDERER); }` in the shared main.pde.

---

<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:** [May 11, 2019, 3:15am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/10 "2019-05-11T03:15:53Z")

</div>

> [@jeremydouglass](#):
>
> Notice that JAVA2D / P2D isn’t actually a String – it is a PConstants that references an `int`.

Actually, all renderer constants are of datatype String: 🙄  
[Processing.GitHub.io/processing-javadocs/core/processing/core/PConstants.html#JAVA2D](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PConstants.html#JAVA2D)

Some of the sketches I post in the forum got something like `static final String RENDER = FX2D;` for example. 😏

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 11, 2019, 3:24am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/11 "2019-05-11T03:24:42Z")

</div>

> [@GoToLoop](#):
>
> all renderer constants are of datatype String

Oops! Should have actually checked, not gone from memory. Fixing that above.

---

<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:** [May 11, 2019, 3:27am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/12 "2019-05-11T03:27:13Z")

</div>

1st time I had to deal w/ it, many years ago, I was also surprised it was a String instead of an `int`, as most constants in PConstants are. 😮

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 11, 2019, 8:11am UTC](https://discourse.processing.org/t/different-behaviors-of-default-vs-p2d-renderer-with-low-alpha-curves/10982/13 "2019-05-11T08:11:00Z")

</div>

Why not have each c??.pde file have its own offscreen buffer either JAVA2D or P2D which the code draws to. In the main sketch you simply copy the buffer to the display.
