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.
IndexProgramming Questions & HelpSyntax Questions › PGraphics(P3D) not able to render bitmaps
Page Index Toggle Pages: 1
PGraphics(P3D) not able to render bitmaps? (Read 1144 times)
PGraphics(P3D) not able to render bitmaps?
Jun 12th, 2007, 10:05am
 
i am trying to create an off-screen buffer to do some bitmap-manipulation in, here's a simple version of what i'm trying to do:

Code:

PImage pattern;
PGraphics canvas;

void setup(){
size(200, 200);
pattern = loadImage("a_pattern.jpg");
canvas = createGraphics(160, 160, P3D);
canvas.noStroke();
canvas.tint(255);
}

void draw(){
background(200, 80, 0);
image(pattern, 5, 5);

updateCanvas();
image(canvas, 20, 20);
}

void updateCanvas(){
canvas.beginDraw();
canvas.background(33, 125, 180);
canvas.image(pattern, mouseX-20, mouseY-20);
canvas.endDraw();
}


try the applet here: http://www.beyondthree.com/_dev/pgraphics_bitmap

it seems there is something broken with the way PImages are rendered into the PGraphics object - the PImage makes the PGraphics object transparent!!! :/

any ideas? hacks? threads i should look at?

thanks,
+ mikkel
Re: PGraphics(P3D) not able to render bitmaps?
Reply #1 - Jun 13th, 2007, 12:10am
 
See http://dev.processing.org/bugs/show_bug.cgi?id=468 .

Long story short, this is a bug, should be fixed in one of the next releases, and can be sort of hacked around by using P3D in your size() statement but JAVA2D in the createGraphics() statement.  I'm not entirely sure if everything will work right using that hack, but things are at least slightly less broken in my experience.
Re: PGraphics(P3D) not able to render bitmaps?
Reply #2 - Jun 13th, 2007, 12:15am
 
Also see http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1171574044 , where fry explains how to hack around these issues in a better way.
Re: PGraphics(P3D) not able to render bitmaps?
Reply #3 - Jun 13th, 2007, 12:58am
 
ahh! thanks! - should have read the prev. posts more carefully!

so, i've got this working now:

Code:

PImage pattern;
PGraphics canvas;

void setup(){
size(200, 200);
pattern = loadImage("a_pattern.jpg");
canvas = createGraphics(160, 160, JAVA2D);
canvas.noStroke();
canvas.tint(255);
}

void draw(){
background(200, 80, 0);
image(pattern, 5, 5);

updateCanvas();
image(canvas, 20, 20);
}

void updateCanvas(){
canvas.beginDraw();
//canvas.loadPixels(); // need this if P3D is set in size()
canvas.background(33, 125, 180);
canvas.image(pattern, mouseX-20, mouseY-20);
canvas.modified = true;
canvas.endDraw();
}


...but - unfortunately this doesn't quite solve my problem. I failed to mention that size() needs to be OPENGL since I am using some GL_ stuff in my app.

modifying the code above to use OPENGL causes this error:
java.lang.ClassCastException: processing.opengl.PGraphicsOpenGL$ImageCache

code looks like this:
Code:

import processing.opengl.*;

PImage pattern;
PGraphics canvas;

void setup(){
size(200, 200, OPENGL);
pattern = loadImage("a_pattern.jpg");
canvas = createGraphics(160, 160, JAVA2D);
canvas.noStroke();
canvas.tint(255);
}

void draw(){
background(200, 80, 0);
image(pattern, 5, 5);

updateCanvas();
image(canvas, 20, 20);
}

void updateCanvas(){
canvas.beginDraw();
//canvas.loadPixels(); // need this if P3D is set in size()
canvas.background(33, 125, 180);
canvas.image(pattern, mouseX-20, mouseY-20);
canvas.modified = true;
canvas.endDraw();
}



...seems like OPENGL and JAVA2D doesn't like each other much! Sad

any ideas/suggestions/links?

+ m
Re: PGraphics(P3D) not able to render bitmaps?
Reply #4 - Jun 13th, 2007, 4:21am
 
mikkel! it's good to have you back.

as it stands now, java2d and opengl renderers don't like to share images with one another. so if you load an image and draw it with opengl, then you can't draw it to a java2d pgraphics.

for now, the hack is to use loadImage() twice.
Re: PGraphics(P3D) not able to render bitmaps?
Reply #5 - Jun 13th, 2007, 9:28am
 
Ben! - thanks, it's good to be back, will make an effort to hang around the boards a bit more now (somehow 2 years went by between this and my last thread!) yikes!

i'm not sure i understand what you mean by using loadImage() twice.

I want to have a PGraphics in memory where I blend some images at runtime - then I want to read values (using get()) from the PGraphics object and use it to control some objects in my visible openGL environment.

Since there's no PGraphics.loadImage() function, how would I load images into JAVA2D to use there, and then, is there any way at all for the main program to read values from here and render them with openGL?

Sorry if I'm a bit slow (and keep rephrasing the question). Let me know if I'm chasing a red herring and I'll figure out another way to get my system working.

Cheers,
mikkel
Re: PGraphics(P3D) not able to render bitmaps?
Reply #6 - Jun 14th, 2007, 3:16pm
 
when you load this once:

pattern = loadImage("a_pattern.jpg");

the "pattern" image variable can only be used in either opengl or java2d, but not both (they have different image handling mechanisms). so if you want to be able to draw "pattern" with an opengl and a java2d drawing surface, then you have to use:

pattern1 = loadImage("a_pattern.jpg");
pattern2 = loadImage("a_pattern.jpg");

and only use pattern1 with opengl, and pattern2 with java2d. that make more sense?
Re: PGraphics(P3D) not able to render bitmaps?
Reply #7 - Jun 15th, 2007, 9:57am
 
yup - makes sense - but, it still doesn't solve my problem. Sad

i can draw into a PGraphics(JAVA2D) object just fine, but I can't use this PGraphics object in openGL (as a texture for example).

my problem was less about using the same graphic in two different renderers - and more about getting bitmaps into PGraphics and thereafter PGraphics into openGL.

+ m
Re: PGraphics(P3D) not able to render bitmaps?
Reply #8 - Jun 15th, 2007, 2:04pm
 
oh, i thought that was the stuff covered in the other posts.. the other issue you're having is that you (for now) have to call loadPixels() on a java2d surface before drawing it with P3D or OPENGL.
Re: PGraphics(P3D) not able to render bitmaps?
Reply #9 - Jun 16th, 2007, 11:46am
 
sweet!... thanks ben! you just saved me the better part of a day (was about to have to re-write a whole bunch of stuff on my current project).

in the interest of documenting the final working version, here's the code (v. 0124):

Code:

import processing.opengl.*;
PImage pattern;
PGraphics canvas;

void setup(){
size(200, 200, OPENGL);
canvas = createGraphics(160, 160, JAVA2D);
canvas.smooth();

pattern = loadImage("a_pattern.jpg");
}

void draw(){
background(200, 80, 0);
updateCanvas();
image(canvas, 20, 20);
}

void updateCanvas(){
canvas.beginDraw();
canvas.background(33, 125, 180);

// bitmaps work!
canvas.image(pattern, mouseX-20, mouseY-20);

// drawing works!
canvas.stroke(255);
canvas.strokeWeight(3.0);
canvas.noFill();
canvas.ellipse(mouseX-20, mouseY-20, 50, 50);

// use these for now to force it to work! (v. 0124)
canvas.modified = true;
canvas.loadPixels();

canvas.endDraw();
}


and here it is on my server:
http://www.beyondthree.com/_dev/pgraphics_bitmap_works/

+ m
Page Index Toggle Pages: 1