PGraphics with noSmooth() does not draw anything in the first loop

edited December 2013 in Programming Questions

Hello,

i got a problem with my PGraphics buffer. If i enable noSmooth, nothing is drawn to the buffer in the first loop. The image remains empty. When i redraw the image everything is working fine. See my example code below:

    PGraphics buf;  

public void setup() {  
  size(800, 600, P3D);  
  buf = createGraphics(800, 600, P3D);  
}

    public void draw() {  
    buf.beginDraw();  
    buf.background((float)200.0);  
    buf.lights();  
    buf.noSmooth();  

    buf.rect(width/2, height/2, 50, 50);  

    buf.endDraw();
    buf.save(".../sandbox_buf.png");

    noLoop();
}

  public void mousePressed() {  
     redraw();      
  } 

Any ideas why nothing is drawn to the buffer in the first loop?
Thanks for your help!

Answers

  • buf.save(".../sandbox_buf.png");
    

    Why you got 3 dots before the file's name? :-w

    Try out w/o any of it: buf.save("sandbox_buf.png");
    Or to save to "/data" instead: buf.save(dataPath("sandbox_buf.png"));

  • Answer ✓

    An alternative version: O:-)

    // forum.processing.org/two/discussion/1760/
    // pgraphics-with-nosmooth-does-not-draw-anything-in-the-first-loop
    
    PGraphics buf;  
    
    void setup() {  
      size(800, 600, P3D);
      noLoop();
    
      buf = createGraphics(width, height);
    
      buf.beginDraw();
      buf.noSmooth();
      buf.strokeWeight(2);
      buf.stroke(0);
      buf.endDraw();
    }
    
    void draw() {
      buf.background(0350);
      buf.fill((color) random(#000000));
      buf.rect(random(width), random(height), random(100), random(80));
      buf.endDraw();
    
      buf.save(dataPath("pic-" + frameCount + ".png"));
    
      background(buf);
    }
    
    void mousePressed() {  
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
  • Hello,
    sorry for the mistakable filename. I just removed my absolute filepath and replaced it by '...' for the forum entry.
    Thanks a lot for your answer and the alternative version. It is working from the first loop on. Just for understanding, can you explain why your version is working and mine not?
    And do i just need to call the buf.beginDraw() once and not everytime before i draw something to the buffer?

  • AFAIK, apart from the ... file name, your original code worked for me! #:-S
    And in my whole experience w/ Processing since v1.5.1, just 1 call to beginDraw() would be enough! 8-}

  • I found the difference between your code and mine - it's the P3D (in my original code i draw 3D objects to the buffer).
    When initializing the buffer with P3D the first image remains empty. When initializing it without P3D it is working from the first loop on.
    Apparantly that problem is somehow related to my computer as it is working for you. I am using Ubuntu 13.04. Any ideas where this is coming from?

  • edited December 2013

    I guess I've removed the P3D argument from my 1st run along w/ removing the ... from file name! :-??
    And I'm using Lubuntu 13.04 + GeForce 9800GT + OpenJDK 7! =:)

  • So does it work for you even with P3D or did you remove it when trying my code as well?
    Should i open an issue for that?

  • As mentioned, from the 1st run, I've removed both the ellipsis and the P3D argument from the createGraphics()! (~~)

  • The solution for my problem was calling noSmooth() before beginDraw() like this:

       void setup() {
          size(800, 600, P3D);
          noLoop();
    
          buf = createGraphics(width, height);
          buf.noSmooth();
    
          buf.beginDraw();
          buf.strokeWeight(2);
          buf.stroke(0);
          buf.endDraw();
       }
    
  • edited January 2014

    Nice finding, @amyro ! Although it's an enigmatically weird fix! 8-}

    P.S.: Makes no diff. for me whether noSmooth() is before or after beginDraw(), w/ or w/o P3D!
    Although I'm still using the old Processing v2.0.2! @-)

  • I'm using Processing 2.1 and GoToLoop's code snippet works for me.

Sign In or Register to comment.