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 › optimising rendering
Page Index Toggle Pages: 1
optimising rendering (Read 416 times)
optimising rendering
Mar 5th, 2008, 6:30pm
 
Hi there,

I'm working on a bit of code to draw lots of lines of text in a sketch, and to overlap and animate them, with alpha blending. Loaded from atext file, each line of text is a separate object with its own attribs.

Trouble is, when a relatively modest number of objects is painted (e.g., <100), the animation bogs down and becomes a bit jittery.

Anyy suggestions to optimize the code to run more smoothly would be warmly welcomed!

-B

// Code to scroll lines of poetry down the screen
// by Brock Craft, March 2008.

String [] rawtext = new String[10629]; // an array to hold each line of text
int x=0;
Fragment[] f;

void setup(){

 size(600,400,P3D);
 frameRate(30);
 fill(255);
 PFont font = loadFont("Apple-Chancery-48.vlw");
 textFont(font,22);
 textAlign(CENTER);
 String lines[] = loadStrings("a_poem.txt");
 f=new Fragment[100];// or use 10629 if we want to use the whole text file
    for (int i=0; i < 100; i++) {
   rawtext[i] = lines[i];  // rawtext is not necessary, but leftover from other kludging
   f[i]=new Fragment(rawtext[i],width/2,i*40,random(5));
 }

}

void draw(){
 background(0);
 for (int i=0;i<50;i++){
   f[i].display();
   f[i].sink();
 }  
if (mousePressed){
      for (int i=0;i<100;i++){
   f[i].reset();
   f[i].reseed();
 }
}

}

class Fragment
{
 String t; // text
 float xpos; // xposition
 float ypos; // yposition
 float weight; // "weight" or heaviness of text

   Fragment(String it, float ixp, float iyp, float iw) {
   xpos = ixp;
   ypos = iyp;
   t = it;
   weight = iw;
 }

 void sink (){
   if (ypos<height+50){
     ypos=ypos+weight;
   }
 }

 void reseed(){
   weight=random(5);
 }
 
 void reset(){
    ypos=random(10);
 }

 void display() {
   float a=weight*50;
   fill(255,255,255,a);
   text(t,xpos, ypos);
   translate(0,0,10);
 }
}
Re: optimising rendering
Reply #1 - Mar 5th, 2008, 7:00pm
 
I executed your code with 100,1000 and 10000 lines of text and perfomance seems fine. At 800x600 runs smoothly, sometimes the framerate drops a bit, but it's not so bad.

At 1280x900 things got more choppy using P3D, but where quite smooth using OpenGL.

Maybe it's your video card the one that's slow. When I mean choppy I mean I notice the drop in the fps if I'm looking at it closely. If it where to be in some kind of installation, part of something bigger, I wouldn't worry about it.
Re: optimising rendering
Reply #2 - Mar 7th, 2008, 10:12pm
 
Hey thanks for your reply!

I tried it on a Newer Mac Workstation and it was a bit choppy at larger resolutions - say 1200,1600. It will be in an installation, so I want the animation to run really smoothly. I may try it on a PC that I have with a beefy graphics card. I've seen partcle systems that have thousands of objects rendering smoothly. Perhaps the fact that I'm using text is slowing things down.

This does raise the question of whether OpenGL or P3D is faster - or whether it makes any difference. I would expect OpenGL is much faster, but I am really not familiar enough with the two technologies to know the answer.

If you think of any other ways to optimize the code or have other thoughts, do post them.

Cheers!
Page Index Toggle Pages: 1