Drawing many object is really slow

edited October 2013 in Programming Questions

hi, im trying to draw around 2000 static pshapes (RECT), not moving or doing anything facny, just in specifficy xy location. these acutally correspond to midi notes, in any case i have an arraylist(for the sake of being able to add beats on the go later on) of all the objects (class), and creating them seems fast, but drawing all of them takes a few mins, wich is really anyoing, anything can be done?

the code in its simplfied form is

ArrayList<Beat> beats;

void setup(){
      size(xmax, ymax, P3D);
      Gstep = 20;//float(xmax)/200;
      loadMidiFile();
      createKeyboard();
      createnotes();
    }

   void draw(){
      pushMatrix();
      for(int i = 0; i < beats.size(); i++){
      tempBeat = (Beat)beats.get(i);
      tempBeat.display();        
      }
      popMatrix();
    }

   class Beat {

  PShape beat;
  float x, y;
  int beatcolor,beatlenth;

  Beat(int beat_lenth, int beat_color, int beat_x_pos, int beat_channel) {
    beatcolor = beat_color;
    beatlenth = beat_lenth;
   if (beat_channel < 0){beat_channel = 1;}
    y = keys[beat_channel].yPosition- (keys[beat_channel].keyWidth / 2.0); //(beat_channel * 10) ;
    x = beat_x_pos;
    beat = createShape(RECT,x, y, beatlenth, keys[beat_channel].keyWidth);
    beat.setFill(color(beatcolor));
    beat.setStroke(false);
  }

  void display() {
    shape(beat);
    }
  }

Answers

  • where is the closing bracket for this for loop? for(int i = 0; i < beats.size(); i++){

    are the push/popMatrix lines necessary?

  • edited October 2013

    just a typo i copied and pased from the code, you get the idea thou :), and no the push and pop are not necesry just for this, but removing them makes not much of a diffrenace..

  • I guess you should look for createShape(GROUP). ;)

  • so how would that work, create a global group and have the object add to it? then draw it? will i be able to move them idividually after?

  • Sorry I still dunno much about PShape objects. Only thing I remember is that grouped shapes render much faster! O:-)

  • gotoloop is right. grouping the individual shapes will make it way faster. and they can still be moved individually. in your draw-function only the group gets drawn: shape(groupShape) the examples in the processing installation show how it works.

  • Answer ✓

    Just curious, why do you use createShape() to draw a simple rectangle? Isn't a rect() more efficient there?

    A few minutes to draw 2000 shapes seem a bit slow, indeed. Is it a few minutes for each frame?
    On my system, OpenGL takes a long time to start up, but once it is running, it is OK.

  • Answer ✓

    If you just use rect() it should only take a few seconds each frame at most

  • il try your advices and report back, and yeah it take few min to start but then runs smoothly... thanks

  • Due to the way OpenGL works, drawing many small PShape objects separately is very inefficient. As people already mentioned, two alternatives are to put all the shapes together inside a group PShape, or simply use the rect() function, the latter being the best option in this case, I think.

  • edited October 2013

    thanks all, indeed using rect makes it instantly fast.

Sign In or Register to comment.