Is it possible to use Variables with PGraphics

Hi,

I am rewriting a lot of Sketches with PGraphics, so that they can be transmitted via Syphon to external software (such as Isadora, MAX or Resolume). I got stuck when trying to convert one of the examples developed on Gene's Kogan Generative Art Class @ tenlegs. I guess the problem is related to the variables, but cannot figure a way out of that. Any suggestions?


original code

int a, b, d;

void setup() {
  size (640, 480);
  frameRate(60);
  background(255);
  a = 0;
  b = height;
  d = 3;
}

void draw() {
 if (a > width) {
   stroke(255);
   d = -d;
 }
 else if (a < 0) {
   stroke(0);
   d = -d;
 } 

 line(a, 0, a, b);
 a += d;
}

new code

import codeanticode.syphon.*;

PGraphics lineSeg;
SyphonServer server;

int a = 0;
int b = height;
int d = 3;

void setup() {
  size (640, 480, P3D);
  lineSeg = createGraphics(640, 480, P3D);
  server = new SyphonServer(this, "Processing Syphon");
      // frameRate(60);
      // background(255);
}

void draw() { 
 lineSeg.beginDraw();
 lineSeg.background(255);
 if (a > width) {
   lineSeg.stroke(255);
   d = -d;
 }
 else if (a < 0) {
   lineSeg.stroke(0);
   d = -d;
 } 
q
 lineSeg.line(a, 0, a, b);
 a += d;

 image(lineSeg, 0, 0);
}

Answers

  • edited April 2014

    I believe that I have fixed your changes - they were mostly trivial, but that probably comes with experience dealing with the language. I haven't tested the Syphon part (I don't have a Mac, or Syphon for that matter), but it works as expected otherwise.

    The things that stood out:

    • Make sure you call endDraw().
    • Call background() in setup(), not draw() for this type of effect.
    • Initialize the variables in setup() - height, for instance, is 0 until size() runs.
    • P3D doesn't like 2D drawing, so use P2D or Java2D instead.

    Corrected code:

    import codeanticode.syphon.*;
    
    PGraphics lineSeg;
    SyphonServer server;
    
    //Initialize variables in setup()
    int a;
    int b;
    int d;
    
    void setup() {
      size (640, 480, P3D);
      lineSeg = createGraphics(640, 480, P2D); //Switch to P2D
      server = new SyphonServer(this, "Processing Syphon");
      // frameRate(60);
    
      //Initialize variables in setup()
      a = 0;
      b = height;
      d = 3;
    
      lineSeg.background(255); //Move background() call to setup()
    }
    
    void draw() {
      lineSeg.beginDraw();
      //Move background() call to setup()
    
      if (a > width) {
        lineSeg.stroke(255);
        d = -d;
      } else if (a < 0) {
        lineSeg.stroke(0);
        d = -d;
      }
    
      lineSeg.line(a, 0, a, b);
      a += d;
    
      lineSeg.endDraw(); //Make sure to call endDraw()
    
      image(lineSeg, 0, 0);
    }
    

    P.S.: Also, I moved the topic to the sub-forum "Questions about Code" because you include a specific code snippet.

  • edited April 2014

    hi, i pasted you code and tried to run it, but i get:

    java.lang.NullPointerException as follows.

    Caused by: java.lang.NullPointerException
        at processing.opengl.PJOGL.depthMask(PJOGL.java:2379)
        at processing.opengl.PGraphicsOpenGL.backgroundImpl(PGraphicsOpenGL.java:5285)
        at processing.core.PGraphics.backgroundFromCalc(PGraphics.java:6981)
        at processing.core.PGraphics.background(PGraphics.java:6896)
        at lineBackAndForth.setup(lineBackAndForth.java:40)
        at processing.core.PApplet.handleDraw(PApplet.java:2281)
        at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
        at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:652)
        at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:636)
        at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1284)
        at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1106)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:981)
        at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1295)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
        at java.awt.EventQueue.access$200(EventQueue.java:103)
        at java.awt.EventQueue$3.run(EventQueue.java:694)
        at java.awt.EventQueue$3.run(EventQueue.java:692)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    
  • and now I am referring to this http://wiki.processing.org/w/Why_do_I_get_a_NullPointerException?, to see if I can solve the problem.

  • Are you sure you need to use the P3D mode? Looks like the sketch is 2D. You get an OpenGL error, because of this P3D. Try without it.

  • edited May 2014

    PhiLho, actually the core aspect of my question was about the best way to include variables on a PGraphics context. I have converted a lot of Sketches to PGraphics to transmit then via Syphon to Isadora, but got stuck with this particular one. My assumption was that the problem was related with the variables. Anyway, I get the NullPointer Exception both with P2D and P3D. Thanks for commenting!

  • Use JAVA2D! P2D is a disguised OpenGL engine in Processing 2.x.x! [..]

Sign In or Register to comment.