Loading...
Logo
Processing Forum
Hey all, just wondering what your opinion is on this. In my app (which i've posted here) I have some particles that leave trails behind them as they move. The trails are basically points with lines between them, where each point has a variable that slowly decays until the point is deemed (old) and removed from the arraylist. I draw the lines between the points with line(), and I also tried doing it with BeginShape/EndShape and GL_BEGIN, etc, but invariably drawing lines seems to be ridiculously slow. When there are a lot of objects on screen it really lags, if I turn off the trails I can have many times more of the particles on screen, not to mention the particles are circles which should make them pretty expensive as well. I find it hard to believe Processing can do 3d graphics and even shaders and convolution effects, but can't draw lines without falling over.

Am I approaching this wrong or is drawing lines just really expensive? I'm at the point of just taking out the trails completely.

Copy code
  1. //each object has three trails behind it
  2. for (int i =0; i<tailPoints1.size()-1;i++)
  3.     {
  4.       //points for trail 1
  5.       TailPoint tailCurrent1 = (TailPoint) tailPoints1.get(i);
  6.       TailPoint tailNext1 = (TailPoint) tailPoints1.get(i+1);
  7.          
  8.       //points for trail 2
  9.       TailPoint tailCurrent2 = (TailPoint) tailPoints2.get(i);
  10.       TailPoint tailNext2 = (TailPoint) tailPoints2.get(i+1);
  11.       //points for trail 3
  12.       TailPoint tailCurrent3 = (TailPoint) tailPoints3.get(i);
  13.       TailPoint tailNext3 = (TailPoint) tailPoints3.get(i+1);
  14.      
  15. //draw lines between the points
  16. //strength is the point's intensity that I use to make the trails fade out towards their end
  17. //it decreases at a certain rate that I can control, and when it reaches zero the point is eliminated
  18.         stroke(por, pog, pob, tailCurrent1.strength);
  19.         if(stripeCounter%2==0)
  20.         stroke(por,pog+50,pob,tailCurrent1.strength/3);
  21.         line(tailCurrent1.x, tailCurrent1.y, tailNext1.x, tailNext1.y);
  22.        
  23.         stroke(por, pog, pob, tailCurrent2.strength);
  24.         if(stripeCounter%2==0)
  25.         stroke(por,pog+50,pob,tailCurrent2.strength/3);
  26.         line(tailCurrent2.x, tailCurrent2.y, tailNext2.x, tailNext2.y);
  27.        
  28.         stroke(por, pog, pob, tailCurrent3.strength);
  29.         if(stripeCounter%2==0)
  30.         stroke(por,pog+50,pob,tailCurrent3.strength/3);
  31.         line(tailCurrent3.x, tailCurrent3.y, tailNext3.x, tailNext3.y);
  32.      
  33.     }

Any thoughts?

Replies(4)

how many lines? you don't say.

are you using smooth()? it's a bit of a killer.

have you tried GlGraphics library?
Drawing lines is expensive.. video cards just don't do them well as it's not often hardware accelerated from what I understand.  It's got nothing to do with Processing - it's an OpenGL / Graphics card thing.  Now you can certainly improve performance of lines by stuffing them into a VBO or something, but still, it will not be equal to points or quads.  I could run 500,000 textured point sprites and my box would be fine, but 10,000 lines brings it to a crawl.  If you figure out any magic, let me know.  I did some research and there are some posts out there on volumetric lines that looked promising.
Like jeff_g said, try using a Vertex Buffer Object.

http://wiki.processing.org/w/Vertex_Buffer_Object_%28VBO%29

I had a similar problem using openframeworks. The idea is that you queue up everything to be drawn at once, rather than drawing each line seperately.
Good idea, I know what a VBO is but didn't think to use it in 2D. I also hope it works on Android, I'm going to try it later. Thanks guys.