We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsSoftware,  Documentation,  Website Suggestions › choosing the PGraphics we'd like to draw on
Page Index Toggle Pages: 1
choosing the PGraphics we'd like to draw on (Read 5708 times)
choosing the PGraphics we'd like to draw on
Jun 30th, 2008, 1:24am
 
PhiLho  wrote on Jun 29th, 2008, 2:56pm:
It would be nice to have an instruction telling Processing that any further graphic command will go in a given buffer, but I don't know if such command exists yet.


So we could do something like this :
Code:
PGraphics buffer = createGraphics(width, height, P3D);
buffer.beginDraw();

nextDrawingInstructionsWillDrawOn(buffer);
fill(200);
rect(10, 10, width-20, height-20);

nextDrawingInstructionsWillDrawOnMainScreen();
fill(250, 150, 0);
rect(10, 10, width-20, height-20);

buffer.endDraw();


instead of specifying the buffer everytime there's a drawing instruction :

Code:
buffer.background(255);
buffer.fill(200);
buffer.stroke(250, 150, 0);
buffer.rect(10, 10, width-20, height-20);
buffer...


which gets really annoying when there's more than 20 instructions...

What do you think? Any pros/cons that comes to your mind?

Re: choosing the PGraphics we'd like to draw on
Reply #1 - Jun 30th, 2008, 4:49pm
 
your "buffer." syntax is the "right" way to do it.

but, since the papplet calls just redirect to the pgraphics (and potentially the recorder)...

this is probably considered a "hack" approach, so not officially sanctioned, but you can often get away with swapping in/out the "g" reference like so:

Quote:


PGraphics originalG;
PGraphics bufferG;

void setup() {
 size(400,400,JAVA2D);
 originalG = g;
 bufferG = createGraphics(200,200,JAVA2D);
}

void useBufferG() {
 g.endDraw();
 g = bufferG;
 g.beginDraw();
}

void useOriginalG() {
 g.endDraw();
 g = originalG;
 g.beginDraw();
}

void draw() {
 // main screen red background, green line
 useOriginalG();
 background(255,0,0);
 stroke(0,255,0);
 strokeWeight(3);
 line(10,10,390,390);
 // offscreen blue background, yellow line
 useBufferG();
 background(0,0,255);
 stroke(255,255,0);
 strokeWeight(3);
 line(10,10,190,190);
 // blit the offscreen to center of main screen
 useOriginalG();
 image(bufferG,100,100);
}


(you might also be able to just use the recorder mechanism -- if you don't mind that drawing also occurs on the main graphics)
Re: choosing the PGraphics we'd like to draw on
Reply #2 - Jun 30th, 2008, 5:57pm
 
wow, that trick is very helpful. thank you very much for this explanation.

ps: you should add it to the hacks section ;-)
Re: choosing the PGraphics we'd like to draw on
Reply #3 - Jun 30th, 2008, 6:06pm
 
Clever!  I like it.
Re: choosing the PGraphics we'd like to draw on
Reply #4 - Jun 30th, 2008, 7:55pm
 
I guess i have to admit that i dont get what is going on there...Maybe im a bit confused by 2 different images and showing the one above the other.
How would it look like if I want to choose between saving an image to a file or show it onscreen?

--------edit:

Seems that the unlekkers Tilesaver is more what ive been looking for, but i still encounter some problems. Ive somebody used it before and knows whats wrong i appreciate help

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=LibraryProblems;action=display;num=1214850610;start=0
Re: choosing the PGraphics we'd like to draw on
Reply #5 - Dec 26th, 2009, 3:10pm
 
// blit the offscreen to center of main screen  
useOriginalG();
image(bufferG,100,100);
...
In P3D mode it works very well, but when I use "bufferG = createGraphics(1180,700,OPENGL);" It gives NullPointerException for the string "image(bufferG,1180,800);". Do you how to use it with OPENGL? Where can I read about that?
Re: choosing the PGraphics we'd like to draw on
Reply #6 - Dec 26th, 2009, 3:54pm
 
Here is what the reference says :

Quote:
It's not possible to use createGraphics() with OPENGL, because it doesn't allow offscreen use.
Re: choosing the PGraphics we'd like to draw on
Reply #7 - Dec 26th, 2009, 4:03pm
 
 Do I need to use OPENGL offscreen buffer in this case?
Re: choosing the PGraphics we'd like to draw on
Reply #8 - Dec 27th, 2009, 3:34am
 
Your question makes no sense, the quoted reference seems quite clear: "OPENGL [...] doesn't allow offscreen use"
OpenGL is managed by the GPU, the graphics card, acting on the video buffer, ie. directly on-screen. It cannot act off-screen. Either you draw something more or less flat in another mode and use that as an OpenGL texture, or use P3D if you need 3D off-screen, as it creates 3D with CPU, so anywhere you want.
Re: choosing the PGraphics we'd like to draw on
Reply #9 - Dec 27th, 2009, 4:22am
 
for off-screen rendering with OpenGL have a look at the glgraphics library
Re: choosing the PGraphics we'd like to draw on
Reply #10 - Dec 27th, 2009, 8:52am
 
  Thank you all for the explanation - I understood where to start from.
Re: choosing the PGraphics we'd like to draw on
Reply #11 - Dec 28th, 2009, 4:43am
 
For reference, after frankBerg's message, I searched opengl offscreen rendering with Google and found some hints...
Some advices are to make a hidden window: so OpenGL do the usual rendering in video buffer but it doesn't show on screen.
I also saw a reference to pBuffers (pixel buffers), faking a window memory, if I understood correctly.
So, there are tricks for off-screen OpenGL rendering. They just don't come out of the box in Processing! Smiley
Page Index Toggle Pages: 1