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.
Page Index Toggle Pages: 1
DisplayList manager (Read 813 times)
DisplayList manager
Sep 1st, 2008, 11:54am
 
I'm trying to knock out some low-hanging fruit libs out of the way. Sorry.. no page for this yet, but I've included a commented example file.

This is a REALLY small library, consisting of only one interface and one method. However, if you use it right you can obtain around 4x speed boost* for your rendering.

DisplayListManager allows you to register any object that implements DisplayList. This in turn gives you an integer that you can hand back to DisplayListManager like a ticket, saying "I want this painted", and the manager will happily do this for you at much faster speeds than you can on your own.

The downside is that you won't be able to modify this drawing much, other than using transforms.

This is great for large screen-filled patterns (SVG), typography (see Vertext). This library is basically a re-packaging of Vertext except without the text. You can now give whatever you want to draw the same speed boost.

Download: http://www.ghost-hack.com/p5/dlmanager/dlmanager.zip

Code:

/*
* @ Michael Chang Sept 1, 2008
*
*  Display List manager example.
*  This example shows you how to use DisplayListManager along with the DisplayList interface.
*  Display Lists are like "recordable" drawings that can speed up your renders to insane speeds.
*
*  On my Macbook Pro I get around a 4x increase in speed.
*
*/


import postprocessing.dlmanager.*;
import processing.opengl.*;

//  decides for this example whether or not we're drawing with display lists
boolean drawUsingDisplayList = true;

//  some arbitrarily "high" number of things to draw
int nCircles = 1000;
int nLines = 20000;

//  this class now implements DisplayList interface and MUST include "preDrawRoutine" and "setDrawRoutine"
class Drawer implements DisplayList{
 //  this function gets called before you record anything to the displaylist manager
 //  for example, if you want to setup any parameters before you do your drawing
 public void preDrawRoutine(){
 }  

 //  this function defines what gets "recorded" into the display list
 //  note that you generally cannot modify what you've recorded after you've done so
 //  thus, adding variables that change the drawing generally do not work
 public void setDrawRoutine(){
   drawStuff();
 }
}

Drawer drawer;
int dlIndex;

//  Some font stuff to show how fast we're actually drawing
PFont font;

void setup(){
 size(300,300, OPENGL);  
 
 //  register PostProcessing library with PApplet
 PostProcessing.register(this);

 font = loadFont("GillSans-Light-12.vlw");

 drawer = new Drawer();  
 //  once we have an instance of our object that's going to draw, register it to the DisplayListManager
 //  "register" returns an int which you should save and use later (see below with DisplayListManager.display...)
 dlIndex = DisplayListManager.register(drawer);
}

void draw(){
 background(255);

 if(drawUsingDisplayList){
   //  you can still apply matrix transformations (push/pop/translate/rotate/scale) to this
   //  note that we're drawing using the DisplayListManager static method "display" and the displaylist number
   DisplayListManager.display(dlIndex);
 }
 else{
   //  draw what we would have normally drawn
   drawStuff();
 }


 //  draw our mode and framerate
 fill(0);
 textFont(font,12);
 if(drawUsingDisplayList)
   text("using display lists at " + nf(frameRate,3,1) + " fps", 12, height-10);
 else
   text("drawing normally at " + nf(frameRate,3,1) + " fps", 12, height - 10);
}

//  this draws a bunch of shapes for testing, you can replace this operation with anything
void drawStuff(){
 noStroke();
 fill(0,5);

 // draw the exact same shapes all the time
 randomSeed(250);    
 for(int i=0; i<nCircles; i++){
   float radius = random(15,65);
   ellipse(random(width), random(height), radius, radius);
 }  
 
 stroke(0,5);
 for(int i=0; i<nLines; i++){
   line(random(width), random(height), random(width), random(height));
 }
}

//  toggle our displaylist usage
void keyPressed(){
 drawUsingDisplayList = !drawUsingDisplayList;
}




* results may vary Smiley
Page Index Toggle Pages: 1