Sketch running slow

edited June 2015 in Programming Questions

Hello,

I am trying to figure out why my code is running so slow, I think it might be due to the number of calculations required with each loop through the code, but I can't figure out a way of optimizing it. If anyone could help out that would be great!

//slider1 = size
//slider2 = U Divisions
//slider3 = V Divisions
//slider4 = Edge Chamfer
//slider5 = Corner Chamfer

import controlP5.*;
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.hemesh.*;
import wblut.geom.*;

float sldr1;
float sldr2;
float sldr3;
float sldr4;

HE_Mesh mesh;
WB_Render render;

//Modifiers -- Modifiers -- Modifiers -- Modifiers --//
HEM_ChamferEdges modifier;
//Modifiers -- Modifiers -- Modifiers -- Modifiers --//

ControlP5 cp5;

void setup() {
  size(400, 400, OPENGL);
  noStroke();
  smooth(8);

  createMesh();

  modifier=new HEM_ChamferEdges();
  modifier.setDistance(50);  

  mesh.modify(modifier);

  cp5 = new ControlP5(this);

  cp5.addSlider("slider1")
    .setPosition(10, 5)
      .setSize(100, 10)
        .setRange(1, 200)
          .setValue(128)
            ;

  cp5.addSlider("slider2")
    .setPosition(10, 15)
      .setSize(100, 10)
        .setRange(1, 100)
          .setValue(20)
            ;

  cp5.addSlider("slider3")
    .setPosition(10, 25)
      .setSize(100, 10)
        .setRange(1, 100)
          .setValue(20)
            ;

  cp5.addSlider("slider4")
    .setPosition(10, 35)
      .setSize(100, 10)
        .setRange(1, 10)
          .setValue(1)
            ;
}

void draw() {
  background(255);
  pushMatrix();
  translate(200, 200, -300);
  rotateY(mouseX*1.0f/width*TWO_PI);
  rotateX(mouseY*1.0f/height*TWO_PI);
  createMesh();
  modifier.setDistance(sldr4); 
  mesh.modify(modifier);
  stroke(0);
  render.drawEdges(mesh);
  noStroke();
  render.drawFaces(mesh);
  popMatrix();
}

void slider1(float theValue1) {
  sldr1 = theValue1;
}

void slider2(float theValue2) {
  sldr2 = theValue2;
}

void slider3(float theValue3) {
  sldr3 = theValue3;
}

void slider4(float theValue4) {
  sldr4 = theValue4;
}


void createMesh() {
  HEC_Sphere creator=new HEC_Sphere();
  creator.setRadius(sldr1); 
  creator.setUFacets(int(sldr2));
  creator.setVFacets(int(sldr3));
  mesh=new HE_Mesh(creator); 
  HET_Diagnosis.validate(mesh);
  render=new WB_Render(this);
}

Answers

  • I haven't used hemesh, but I suspect you shouldn't be creating new instances of it's classes each frame in createMesh(). I imagine the intention is you create your dependencies in setup, having declared globally, and then call appropriate methods in draw to display the result

  • I haven't used hemesh, but I suspect you shouldn't be creating new instances of it's classes each frame in createMesh(). I imagine the intention is you create your dependencies in setup, having declared globally, and then call appropriate methods in draw to display the result

    Hi, yes I think you are right - ideally I would create the mesh in setup() but I am using sliders to change variables in the mesh so I think I need to figure a way of looping through the modifiers but only create the mesh once.

  • You could only create the new mesh once you receive new values in the sliders.

  • I've managed to get it sorted by using the HE_Mesh dynamic mesh. Thanks

  • For future reference if you use sliders then use the slider change function to do this type of stuff: then slowdown only occurs during user interaction; which is more likely to be expected...

Sign In or Register to comment.