Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Optimizing draw function

$
0
0

The question I have is that in the following code I am only changing 1 box every second or so, but i am having to redraw all 8000 blocks each draw cycle. Is there a way to have draw only redraw the changed objects? Is there any way to write these boxes to a single shape or similar and once I update the single box every few seconds, then I update the stores shape, or do I have to redraw everything every 1/60 second..... Any suggestions on improving any areas of this code are appreciated.

import peasy.*;
import shapes3d.*;
import processing.opengl.*;

PeasyCam cam;
int   CPS = 20;                         // number of cells per side - no greater than 100
int   winSize = 1024;                   //size of display window in both directions

Box[][][] box = new Box[CPS][CPS][CPS];
Shape3D picked = null;
boolean clicked = false;
float boundries = winSize *.8;
float gridSize = boundries / CPS;
float cellSize = gridSize * .4;
float offset=(CPS-1)*gridSize/2.0;
double rotateAngle = .002;
color[] faceColor = { 0x44FF0000, 0x4400FF00, 0x440000FF, 0x44FFFF00, 0x4400FFFF, 0x44FF00FF  };

//_________________________________________________________________
void setup() {
  size(1024, 1024, P3D);
  cursor(CROSS);
  cam = new PeasyCam(this, winSize*1.25);

  for (int i = 0; i < CPS; i++) {    for (int j = 0; j < CPS; j++) {      for (int k = 0; k < CPS; k++) {
        box[i][j][k] = new Box(this, cellSize, cellSize, cellSize);
        box[i][j][k].moveTo(i*gridSize-offset,j*gridSize-offset,k*gridSize-offset);
        for (int c = 0; c<faceColor.length; c++) {box[i][j][k].fill(faceColor[c],int(pow(2,c)));}  //every side of the cells needs a different color
        box[i][j][k].drawMode(S3D.SOLID);
    }}}                   }

//_________________________________________________________________
void draw() {

  background(127);
  pushMatrix();
      for (int i = 0; i < CPS; i++) {       for (int j = 0; j < CPS; j++) {        for (int k = 0; k < CPS; k++) {
           box[i][j][k].draw();   }}}
  popMatrix();
  cam.rotateY(rotateAngle);   cam.rotateX(rotateAngle);   cam.rotateZ(rotateAngle);
  }

//_________________________________________________________________
void mouseClicked() {if (mouseButton == LEFT) {rotateAngle=0;} else if (mouseButton == RIGHT) {rotateAngle=.002;}}

Viewing all articles
Browse latest Browse all 2896

Trending Articles