I don't understand why the "double buffering" version of the code shown below doesn't seem to improve the rendering significantly as compared with the "non double buffering" version according to frame rate ...
Appreciate your advices on:
1. Am I using "double buffering" correctly?
2. how to get the points shown on top of the lines?
Thanks!
animation:
non double buffering" version:
"double buffering" version:
Appreciate your advices on:
1. Am I using "double buffering" correctly?
2. how to get the points shown on top of the lines?
Thanks!
animation:
non double buffering" version:
- /*---------------------------------
- coded by Ji ZHANG of E.P.i.S.O.D.E
- Jun 2013
- reference: http://www.drifkin.net/
- ---------------------------------*/
- import java.util.Iterator;
- import java.util.Calendar;
- import processing.opengl.*;
- //------ controlP5
- import controlP5.*;
- ControlP5 controlP5;
- boolean GUI = false;
- boolean guiEvent = false;
- Slider[] sliders;
- ArrayList<Pt> ptList = new ArrayList<Pt>();
- int ptNum = 100;
- int screenW = 800;
- int screenH = 800;
- int distThreshold = 70;
- int ptSize = 10;
- //#################################################
- void setup(){
- size(screenW, screenH, OPENGL);
- smooth(8);
- frameRate(60);
- background(0);
- //----- initiate pts -----
- for (int i=0; i<ptNum; i++) {
- PVector l_ = new PVector(random(screenW), random(screenH), 0);
- //PVector v_ = new PVector(random(-2,2), random(-2,2), 0);
- PVector v_ = new PVector(random(-1,1), random(-1,1), 0);
- PVector a_ = new PVector(0,0,0);
- float s_ = ptSize;
- color c_ = color(random(255), random(255), random(255));
- Pt p = new Pt(l_, v_, a_, s_, c_);
- ptList.add(p);
- }
- //----- GUI -----
- setupGUI();
- //guiEvent = false;
- }
- //#################################################
- void draw(){
- background(30);
- //fill(40,1);
- //rect(0,0,width,height);
- for (int i=0; i<1; i++) {
- PVector l_ = new PVector(random(screenW), random(screenH), 0);
- //PVector v_ = new PVector(random(-2,2), random(-2,2), 0);
- PVector v_ = new PVector(random(-1,1), random(-1,1), 0);
- PVector a_ = new PVector(0,0,0);
- float s_ = ptSize;
- color c_1 = color(random(255), random(255), random(255));
- //color c_1 = color(255);
- color c_2 = color(0);
- Pt p1 = new Pt(l_, v_, a_, s_, c_1);
- Pt p2 = new Pt(l_, v_, a_, s_, c_2);
- ptList.add(p1);
- //ptList.add(p2);
- }
- Iterator<Pt> it = ptList.iterator();
- while (it.hasNext()) {
- Pt p = it.next();
- p.run();
- if (p.isDead()) {
- it.remove();
- }
- }
- fill(255);
- text("E.P.i.S.O.D.E", 10,height-10);
- text("Num of pt : "+ptList.size(), 10, 20);
- text("frame rate : "+nfc(frameRate,1), 10, 30);
- //----- draw GUI -----
- //drawGUI();
- }
- class Pt {
- //----- variables
- PVector location;
- PVector velocity;
- PVector acceleration;
- float ptMass;
- float ptSize;
- color ptColor;
- float ptAlpha;
- float age;
- float lifespan;
- //----- constructor
- Pt(PVector l_, PVector v_, PVector a_, float s_, color c_){
- location = l_.get();
- velocity = v_.get();
- acceleration = a_.get();
- ptSize = s_;
- ptColor = c_;
- age = 0;
- ptAlpha = 0;
- }
- //----- run
- void run(){
- update();
- drawLine(ptList);
- displayPt();
- }
- //----- udpate
- void update(){
- velocity.add(acceleration);
- location.add(velocity);
- acceleration.mult(0);
- age += 1;
- if(ptAlpha < 255){
- ptAlpha = age*5;
- }
- }
- //----- display pt -----
- void displayPt(){
- stroke(ptColor, ptAlpha);
- strokeWeight(ptSize);
- point(location.x, location.y, location.z);
- }
- //----- isDead
- boolean isDead(){
- if (
- (location.x>width) ||
- (location.x<0) ||
- (location.y>height) ||
- (location.y<0)
- )
- {
- return true;
- } else {
- return false;
- }
- }
- //----- connection lines -----
- void drawLine(ArrayList<Pt> ptList_){
- for (Pt p : ptList_) {
- PVector pNeighborLocation = new PVector(p.location.x, p.location.y, p.location.z);
- float dist = location.dist(pNeighborLocation);
- if (dist < distThreshold){
- float lineAlpha = map(dist, 0,distThreshold, 255,0);
- stroke(ptColor, lineAlpha);
- strokeWeight(1);
- line(location.x, location.y, location.z,
- pNeighborLocation.x, pNeighborLocation.y, pNeighborLocation.z);
- }
- }
- }
- }
- //////////////////////////////////////////////////
- void setupGUI(){
- controlP5 = new ControlP5(this);
- controlP5.setAutoDraw(false);
- //----- define menu group -----
- ControlGroup ctrl = controlP5.addGroup("menu",15,50)//name,x,y,w,h
- .activateEvent(true)
- //.setColorLabel(color(255))
- .setColorBackground(color(255,100))
- .setBackgroundHeight(height-100)
- .setBackgroundColor(color(255,100))
- .setWidth(210)
- .setHeight(15)
- .close()
- ;
- int left = 5;
- int top = 15;
- int len = 130;
- int posY = 0;
- sliders = new Slider[30];
- int si = 0;
- //name, min, max, default, x, y, w, h
- sliders[si++] = controlP5.addSlider("dist_threshold",0,200,100,left,top+posY,len,15)
- .setLabel("dist threshold")
- .setGroup(ctrl)
- ;
- posY += 30;
- }
- //////////////////////////////////////////////////
- void drawGUI(){
- hint(DISABLE_DEPTH_TEST);
- controlP5.show();
- controlP5.draw();
- hint(ENABLE_DEPTH_TEST);
- }
- //////////////////////////////////////////////////
- //----- event to change dist threshold -----
- public void dist_threshold (float theValue){
- distThreshold = int(theValue);
- }
"double buffering" version:
- /*---------------------------------
- coded by Ji ZHANG of E.P.i.S.O.D.E
- Jun 2013
- reference: http://www.drifkin.net/
- ---------------------------------*/
- import java.util.Iterator;
- import java.util.Calendar;
- import processing.opengl.*;
- //------ controlP5
- import controlP5.*;
- ControlP5 controlP5;
- boolean GUI = false;
- boolean guiEvent = false;
- Slider[] sliders;
- ArrayList<Pt> ptList = new ArrayList<Pt>();
- int ptNum = 100;
- int screenW = 800;
- int screenH = 800;
- int distThreshold = 70;
- int ptSize = 10;
- PGraphics pg;
- //#################################################
- void setup(){
- size(screenW, screenH, OPENGL);
- smooth(8);
- frameRate(60);
- background(0);
- //----- initiate pts -----
- for (int i=0; i<ptNum; i++) {
- PVector l_ = new PVector(random(screenW), random(screenH));
- //PVector v_ = new PVector(random(-2,2), random(-2,2));
- PVector v_ = new PVector(random(-1,1), random(-1,1));
- PVector a_ = new PVector(0,0);
- float s_ = ptSize;
- color c_ = color(random(255), random(255), random(255));
- Pt p = new Pt(l_, v_, a_, s_, c_);
- ptList.add(p);
- }
- //----- GUI -----
- //setupGUI();
- //guiEvent = false;
- //!!! it will be very slow without the "OPENGL" option !!!
- pg = createGraphics(screenW, screenH, OPENGL);
- }
- //#################################################
- void draw(){
- pg.beginDraw();
- pg.background(20);
- //pg.fill(0, 10);
- //pg.rect(0,0,width,height);
- for (int i=0; i<1; i++) {
- PVector l_ = new PVector(random(screenW), random(screenH));
- //PVector v_ = new PVector(random(-2,2), random(-2,2), 0);
- PVector v_ = new PVector(random(-1,1), random(-1,1));
- PVector a_ = new PVector(0,0);
- float s_ = ptSize;
- color c_1 = color(random(255), random(255), random(255));
- //color c_1 = color(255);
- color c_2 = color(0);
- Pt p1 = new Pt(l_, v_, a_, s_, c_1);
- Pt p2 = new Pt(l_, v_, a_, s_, c_2);
- ptList.add(p1);
- //ptList.add(p2);
- }
- Iterator<Pt> it = ptList.iterator();
- while (it.hasNext()) {
- Pt p = it.next();
- p.run();
- if (p.isDead()) {
- it.remove();
- }
- }
- pg.fill(255);
- pg.text("E.P.i.S.O.D.E", 10,height-10);
- pg.text("Num of pt : "+ptList.size(), 10, 20);
- pg.text("frame rate : "+nfc(frameRate,1), 10, 30);
- //----- draw GUI -----
- //drawGUI();
- pg.endDraw();
- image(pg,0,0);
- }
- class Pt {
- //----- variables
- PVector location;
- PVector velocity;
- PVector acceleration;
- float ptMass;
- float ptSize;
- color ptColor;
- float ptAlpha;
- float age;
- float lifespan;
- //----- constructor
- Pt(PVector l_, PVector v_, PVector a_, float s_, color c_){
- location = l_.get();
- velocity = v_.get();
- acceleration = a_.get();
- ptSize = s_;
- ptColor = c_;
- age = 0;
- ptAlpha = 0;
- }
- //----- run
- void run(){
- update();
- display();
- }
- //----- udpate
- void update(){
- velocity.add(acceleration);
- location.add(velocity);
- acceleration.mult(0);
- age += 1;
- if(ptAlpha < 255){
- ptAlpha = age*3;
- }
- }
- //----- display
- void display(){
- drawLine(ptList);
- pg.stroke(ptColor, ptAlpha);
- pg.strokeWeight(ptSize);
- pg.point(location.x, location.y);
- }
- //----- isDead
- boolean isDead(){
- if (
- (location.x>width) ||
- (location.x<0) ||
- (location.y>height) ||
- (location.y<0)
- )
- {
- return true;
- } else {
- return false;
- }
- }
- //----- connection lines -----
- void drawLine(ArrayList<Pt> ptList_){
- for (Pt p : ptList_) {
- PVector pNeighborLocation = new PVector(p.location.x, p.location.y);
- float dist = location.dist(pNeighborLocation);
- if (dist < distThreshold){
- float lineAlpha = map(dist, 0,distThreshold, 255,0);
- pg.stroke(ptColor, lineAlpha);
- pg.strokeWeight(1);
- pg.line(location.x, location.y,
- pNeighborLocation.x, pNeighborLocation.y);
- }
- }
- }
- }
- //////////////////////////////////////////////////
- void setupGUI(){
- controlP5 = new ControlP5(this);
- controlP5.setAutoDraw(false);
- //----- define menu group -----
- ControlGroup ctrl = controlP5.addGroup("menu",15,50)//name,x,y,w,h
- .activateEvent(true)
- //.setColorLabel(color(255))
- .setColorBackground(color(255,100))
- .setBackgroundHeight(height-100)
- .setBackgroundColor(color(255,100))
- .setWidth(210)
- .setHeight(15)
- .close()
- ;
- int left = 5;
- int top = 15;
- int len = 130;
- int posY = 0;
- sliders = new Slider[30];
- int si = 0;
- //name, min, max, default, x, y, w, h
- sliders[si++] = controlP5.addSlider("dist_threshold",0,200,150,left,top+posY,len,15)
- .setLabel("dist threshold")
- .setGroup(ctrl)
- ;
- posY += 30;
- }
- //////////////////////////////////////////////////
- void drawGUI(){
- hint(DISABLE_DEPTH_TEST);
- controlP5.show();
- controlP5.draw();
- hint(ENABLE_DEPTH_TEST);
- }
- //////////////////////////////////////////////////
- //----- event to change dist threshold -----
- public void dist_threshold (float theValue){
- distThreshold = int(theValue);
- }
1