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 & HelpPrograms › image/texture feedback - out of memory
Page Index Toggle Pages: 1
image/texture feedback - out of memory (Read 1096 times)
image/texture feedback - out of memory
May 4th, 2010, 12:11pm
 
Hi,

I have recently been trying to learn more about images and texturing, and I came up with the sketch below that textures cube faces with an image from the previous frame to create an 'infinite cube' feedback effect.

It seems to work to a point, but runnning in P3D I get an out of memory error after a few seconds.  In OPENGL it runs fine without any memory errors. I'm not sure what is triggering the out of memory as the PImage is already instantiated and is not being re-created, just re-populated on each draw cycle (as far as I understand things!)

Also running in P3D the texture mapping gets distored as the cube rotates, which doesn't happed in OPENGL.  Is this just due to the way the different renderers operate?

Code:

import processing.opengl.*;
float r = 8;
float fr = 20;
PImage tex;
float bg = 255;

void setup()
{
 frameRate(fr);
 size(512,512,P3D);
 background(bg);
 tex = get();
}

void draw()
{
 background(bg);
 directionalLight(0, 0, 0, -1, 0, 0);
 lights();  
 camera(-25, -25, 1.5, 0, 0, 0, 0, 1, 0);
 
 translate(512/2,512/2);
 rotateY((r)/10);
 rotateZ((r)/3);
 cube(200);
 translate(-512/2,-512/2);
 
 r += PI / fr;
 tex = get();
}

void cube(float sz)
{
 cubeFace(sz);
 rotateX(PI/2);
 cubeFace(sz);
 rotateX(PI/2);
 cubeFace(sz);
 rotateX(PI/2);
 cubeFace(sz);
 rotateY(PI/2);
 cubeFace(sz);
 rotateY(PI);
 cubeFace(sz);
}

void cubeFace(float sz)
{
 noStroke();
 beginShape(QUADS);
 texture(tex);
 vertex(0 - sz / 2, 0 - sz / 2, 0 - sz / 2, 0, 0);
 vertex(0 + sz / 2, 0 - sz / 2, 0 - sz / 2, tex.height, 0);
 vertex(0 + sz / 2, 0 + sz / 2, 0 - sz / 2, tex.height, tex.width);
 vertex(0 - sz / 2, 0 + sz / 2, 0 - sz / 2, 0, tex.width);
 endShape(CLOSE);
}


Re: image/texture feedback - out of memory
Reply #1 - May 4th, 2010, 12:21pm
 
Here is another sketch that uses a similar technique but in a different way.  Once it has 'Spun up', small mouse movements can create some pretty cool effects.

Originally this was also giving me the same out of memory error running in P3D, but it seems to be OK now.

I'm not sure what the difference is with this one that prevents the error, but I'm glad it seems ot be working for me now! :)

Code:

import processing.opengl.*;
float rot = 0, rotc = 5;
float fr= 20;
PImage tex;
float rd = 0, gn = 127, bl = 255;
float rc = 3, gc = 50, bc = 10;

void setup()
{
 frameRate(fr);
 size(512,512,P3D);
 background(0);
 tex = get();
}

void draw()
{
 background(0);

 //Rotate and shift in z dimension
 translate(width/2, height/2, -10);
 rotateZ(rot);
 translate(-width/2, -height/2, 0);
 
 //set colour
 rd += rc;
 gn += gc;
 bl += bc;
 if (rd >= 255 || rd <= 0) rc = -rc;
 if (gn >= 255 || gn <= 0) gc = -gc;
 if (bl >= 255 || bl <= 0) bc = -bc;
 
 //Draw textured plane
 strokeWeight(1);
 stroke(color(rd, gn, bl, 150));
 beginShape(QUADS);
 texture(tex);
 vertex(0, 0, 0, 0);
 vertex(width, 0, tex.width, 0);
 vertex(width,height, tex.width, tex.height);
 vertex(0,height, 0, tex.height);
 endShape(CLOSE);
 
 //Draw brush
 fill(color(rd, gn, bl));
 ellipse(mouseX, mouseY, 20, 20);
 
 //get the next texture from the screen image
 tex = get();
 
 //increment rotation
 rot += (PI/(100 * fr));  
}
Re: image/texture feedback - out of memory
Reply #2 - May 15th, 2010, 5:13am
 
I have just come across a very similar sketch on openprocessing that runs fine and it has spurred me into looking at this one again.

http://www.openprocessing.org/visuals/?visualID=6009

The only difference I could see  that I thought might make a difference is that in my approach I have the PImage as an instance variable, but Martin creates it inside the draw method.  I figured this may effect garbage collection

I tried changing my code to use this approach, but I am still having the same out of memory problem.

Can anyone see what the difference is that is causing my code to fail after a few second, but this one run fine

My current code:
Code:

float r = 0; //rotation angle
float bg = 255; //Background color

void setup()
{
 size(512,512,P3D);
 background(bg);
 noStroke();
}

void draw()
{
 //get current image then clear the screen
 PImage tex = get();
 background(bg);
 
 //lights, camera...
 directionalLight(0, 0, 0, -1, 0, 0);
 lights();  
 camera(-25, -25, 1.5, 0, 0, 0, 0, 1, 0);
 
 //translate origin to centre and rotate
 translate(width/2,height/2);
 rotateY((r)/10);
 rotateZ((r)/3);
 
 //draw cube
 cube(200, tex);

 //Increase rotation value for next cycle
 r += PI / frameRate;
}

void cube(float sz, PImage t)
{
 cubeFace(sz,t);
 rotateX(PI/2);
 cubeFace(sz,t);
 rotateX(PI/2);
 cubeFace(sz,t);
 rotateX(PI/2);
 cubeFace(sz,t);
 rotateY(PI/2);
 cubeFace(sz,t);
 rotateY(PI);
 cubeFace(sz,t);
}

void cubeFace(float sz, PImage t)
{
 //draw textured plane
 beginShape();
 texture(t);
 vertex(0 - sz / 2, 0 - sz / 2, 0 - sz / 2, 0, 0);
 vertex(0 + sz / 2, 0 - sz / 2, 0 - sz / 2, t.height, 0);
 vertex(0 + sz / 2, 0 + sz / 2, 0 - sz / 2, t.height, t.width);
 vertex(0 - sz / 2, 0 + sz / 2, 0 - sz / 2, 0, t.width);
 endShape();
}
Re: image/texture feedback - out of memory
Reply #3 - May 16th, 2010, 3:30am
 
I am perplex... I looked at the sketches with a profiler (JVisualVM shipped with Sun's JDK) and I saw very different behaviors.

The first applet quickly eats all the available memory, even after at pushed it to 800MB. Memory profiling shown 38 int[] arrays (the pixel arrays) having survived more than 4 garbage collections...

The second applet, looking similar, regularly garbage collect the created arrays, so remains around 20MB of memory usage.

I will try and investigate to see where the difference relies.

[EDIT] No time for an in-deep analysis of P3D source code, but the first sketch is using the texture 6 times, in a "complex" shape, so maybe an underlying structure is holding these textures. A kind of memory leak, although I don't know at which level.

[EDIT] Tried with PImage tex = new PImage[6]; and making 6 get() each time: using lot of memory, but trying to avoid multiple links on each texture.
Memory usage is less steady, ie. I see some of it garbage collected, but I still end with an OOME.
Page Index Toggle Pages: 1