Already called beginDraw() error in OPENGL mode using PGraphics

Hi,

I keep getting the following error 'Already called beginDraw()' when using Processing 2.03 with OPENGL and PGraphics objects. It seems to be much the same as the issue raised here: http://forum.processing.org/one/topic/pgraphic-begindraw-keeps-throwing-null-pointer-exceptions.html I'm not too experience when it comes to investigating the openGL errors (or at all for that matter).

Any help would be much appreciated, thanks.

Also I think this is probably a question for 'Core Library Questions' but I can't seem to post in the other processing forum and this one doesn't seem to have that category.

Thanks

My code is below;

import com.onformative.leap.LeapMotionP5;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Hand;
import codeanticode.syphon.*;

PGraphics canvas;
SyphonServer server;

LeapMotionP5 leap;

int sides = 6;
int rows = 2;
int lastSides = 0;
int radius = 400;
float theta, thetaInc;
PImage absImage;
int Col= 30;
float offsetX, offsetY;
int mode = 2;
float t1x = 0.1, t1y = 0.4;
float t2x = 0.4, t2y = 0.1;
float t3x = 0.4, t3y = 0.4;
int flip = 1;

PVector hands[] = new PVector [2];

void setup(){
 background(0);
 size(960,480,OPENGL);

  canvas = createGraphics(960, 480, OPENGL);
  server = new SyphonServer(this, "Processing Syphon");

 canvas.beginDraw();
 canvas.textureMode(NORMAL);
 canvas.noFill();
 canvas.noStroke();
 //canvas.strokeWeight(3);
 canvas.endDraw();
 thetaInc = TWO_PI/sides;

 leap = new LeapMotionP5(this);

  absImage = loadImage("/Volumes/data/library/image/absolutTexture/absolut.jpg");
  if (absImage == null) throw new RuntimeException("pattern.jpg not found !!");

  println("setup complete");
}

void draw(){

  if(!leap.getFingerList().isEmpty()){
    sides=6;
  }

  for (Finger finger : leap.getFingerList()) {
    sides+=2;

  }
  lastSides = sides;

  for (Hand hand: leap.getHandList()) {
    int i = 0;
    hands[0] = leap.getPosition(hand);
    i++;
  }


  theta=0;
  thetaInc = TWO_PI/sides;


  offsetX = map(hands[0].x, 0,width,0,0.5) * flip;
  offsetY = map(hands[0].y, 0,width,0,0.5);
  if (mode == 1) {

    canvas.beginDraw();
    canvas.background(255);
    canvas.pushMatrix();
    canvas.translate(width/2,height/2);
    canvas.rotate(round(hands[0].z*0.01)*0.1);
    canvas.translate(-width/2,-height/2);



    for(int i = 0; i<sides; i+=2){

      canvas.pushMatrix();
      canvas.translate(width/2, height/2);
      canvas.rotate(thetaInc);
      canvas.beginShape(TRIANGLE);
      canvas.texture(absImage);  
      canvas.vertex(0,                           0,                             t1x+offsetX,   t1y+offsetY);
      canvas.vertex(cos(theta+thetaInc)*radius,  sin(theta+thetaInc)*radius,    t2x+offsetX,   t2y+offsetY);
      canvas.vertex(cos(theta)*radius,           sin(theta)*radius,             t3x+offsetX,   t3y+offsetY);
      canvas.endShape();
      canvas.popMatrix();

      theta+=thetaInc;


      canvas.pushMatrix();
      canvas.translate(width/2, height/2);
      canvas.rotate(thetaInc);
      canvas.beginShape(TRIANGLE);
      canvas.texture(absImage);  
      canvas.vertex(0,                            0,                            t1x+offsetX,    t1y+offsetY);
      canvas.vertex(cos(theta+thetaInc)*radius,   sin(theta+thetaInc)*radius,   t3x+offsetX,    t3y+offsetY);
      canvas.vertex(cos(theta)*radius,            sin(theta)*radius,            t2x+offsetX,    t2y+offsetY);
      canvas.endShape();
      canvas.popMatrix();

      theta+=thetaInc;




      Col +=30;
    }

    canvas.popMatrix();
    canvas.endDraw();
  }

  if (mode == 2) {
    sides=3;
    canvas.beginDraw();
    canvas.background(255);
    //canvas.pushMatrix();

    for(int i = 0; i<sides; i+=1){
      for(int j =0; j<rows; j++) {

        canvas.pushMatrix();
        canvas.translate(i*(width/sides), j*(height/rows));

        canvas.beginShape(TRIANGLE);
        canvas.texture(absImage);  
        canvas.vertex(0,                    0,                     t1x+offsetX,   t1y+offsetY);
        canvas.vertex(width/sides,          0,                     t2x+offsetX,   t2y+offsetY);
        canvas.vertex(width/sides/2,        height/rows/2,         t3x+offsetX,   t3y+offsetY);
        canvas.endShape();

        canvas.beginShape(TRIANGLE);
        canvas.texture(absImage);  
        canvas.vertex(0,                    height/rows,            t2x+offsetX,    t2y+offsetY);
        canvas.vertex(width/sides/2,        height/rows/2,          t3x+offsetX,    t3y+offsetY);
        canvas.vertex(width/sides,          height/rows,            t1x+offsetX,    t1y+offsetY);
        canvas.endShape();

        canvas.beginShape(TRIANGLE);
        canvas.texture(absImage);  
        canvas.vertex(0,                    0,                     t1x+offsetX,   t1y+offsetY);
        canvas.vertex(width/sides/2,        height/rows/2,         t3x+offsetX,   t3y+offsetY);
        canvas.vertex(0,                    height/rows,           t2x+offsetX,   t2y+offsetY);
        canvas.endShape();

        canvas.beginShape(TRIANGLE);
        canvas.texture(absImage);  
        canvas.vertex(width/sides,          0,                      t2x+offsetX,    t2y+offsetY);
        canvas.vertex(width/sides/2,        height/rows/2,          t3x+offsetX,    t3y+offsetY);
        canvas.vertex(width/sides,          height/rows,            t1x+offsetX,    t1y+offsetY);
        canvas.endShape();

        canvas.popMatrix();

      }
    }

    //canvas.popMatrix();
    canvas.endDraw();

  }

  image(canvas,0,0);
 // server.sendImage(canvas);


}

and here is the error log;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at kaleidascopeexperiment02_leapSyphon.draw(kaleidascopeexperiment02_leapSyphon.java:95)
    at processing.core.PApplet.handleDraw(PApplet.java:2305)
    at processing.opengl.PGL$PGLListener.display(PGL.java:2601)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:588)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:572)
    at javax.media.opengl.awt.GLCanvas$7.run(GLCanvas.java:1054)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1034)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:909)
    at javax.media.opengl.awt.GLCanvas$8.run(GLCanvas.java:1065)
    at javax.media.opengl.Threading.invoke(Threading.java:193)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:483)
    at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:537)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    at java.awt.Component.dispatchEventImpl(Component.java:4852)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Already called beginDraw()
java.lang.RuntimeException: java.lang.NullPointerException
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:100)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:205)
    at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
    at javax.media.opengl.Threading.invoke(Threading.java:191)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:483)
    at processing.opengl.PGL.requestDraw(PGL.java:1149)
    at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1604)
    at processing.core.PApplet.run(PApplet.java:2176)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.NullPointerException
    at kaleidascopeexperiment02_leapSyphon.draw(kaleidascopeexperiment02_leapSyphon.java:95)
    at processing.core.PApplet.handleDraw(PApplet.java:2305)
    at processing.opengl.PGL$PGLListener.display(PGL.java:2601)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:588)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:572)
    at javax.media.opengl.awt.GLCanvas$7.run(GLCanvas.java:1054)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1034)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:909)
    at javax.media.opengl.awt.GLCanvas$8.run(GLCanvas.java:1065)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Answers

  • Hi! Could it work with P3D instead of OPENGL? Or is that the same?

  • Hi, No it performs the same with P2D and P3D renders (or combinations of them i.e. OPENGL main renderer and P2D for the PGraphics element). The strange thing is that it it runs okay with a simplified version of the sketch, such as just drawing a rectangle to the canvas, however if you add in the rest of the sketch, and get the above error, then remove it, the simplified version no longer works. Which with makes me think it is not a code error as such rather that the graphics card is getting tripped up or bogged down by something. In the thread linked to above in my first post, the (similar) problem was solved by removing the drawing of the second applet to another thread as it was not always complete by the time the main thread went looking for it. I'm not sure if there would be something similar that could be done here. Again, with little knowledge the error messages seem to suggest to me an issue with threading or sequencing of creating the elements. Could it be that the GPU is looking for abn element before the CPU can declare it, or visa versa. Thanks for the help, I hope some of what i said makes sense.

  • Just to add, the crash is intermittent with the full code also (7/10 times), and I need to run through the process of sending it an easy sketch to 'get it working again'. Thanks

Sign In or Register to comment.