We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The following code can draw a 3D matrix of 256x256x256 points. However, it gets very slow even if the matrix is set as just 1x256x256 points.
May I ask how to efficiently draw millions of points in Processing, especially using the peasyCam and controlP5 libraries?
Thanks!
//////////////////////////////////////////////////////////
import processing.opengl.*;
import toxi.processing.*;
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.math.*;
ToxiclibsSupport gfx;
import peasy.*;
PeasyCam cam;
Boolean camOrtho = false;
import controlP5.*;
ControlP5 cp5;
ArrayList<Pt> pts = new ArrayList<Pt>();
//////////////////////////////////////////////////////////
void setup(){
size(1000, 1000, OPENGL);
smooth(8);
frameRate(60);
gfx=new ToxiclibsSupport(this);
setupCam();
for (int r=0; r<256; r++) {
for (int g=0; g<256; g++) {
for (int b=0; b<1; b++) {
color _c = color(r,g,b);
Vec3D _l = new Vec3D(r/5, g/5, b);
pts.add(new Pt(_l, _c));
}
}
}
println("num of points: ", pts.size());
}
//////////////////////////////////////////////////////////
void draw(){
background(120);
// draw stuff here ...
//drawPt();
for (int i=pts.size()-1; i>=0; i--) {
Pt p = pts.get(i);
p.display();
}
// draw gizmo and bounding box
drawGizmo();
drawBbox();
}
class Pt {
Vec3D loc;
color c;
Pt (Vec3D _l, color _c) {
loc = _l;
c = _c;
}
void display () {
strokeWeight(1);
stroke(loc.x*5, loc.y*5, loc.z*5);
//point(loc.x, loc.y, loc.z);
gfx.point(loc);
}
}
//////////////////////////////////////////////////////////
void setupCam () {
//use the following to avoid pt been shown in fixed size (P2.0b8)
hint(ENABLE_STROKE_PERSPECTIVE);
//----- perspective -----
float fov = PI/3; // field of view
float nearClip = 1;
float farClip = 100000;
float aspect = float(width)/float(height);
perspective(fov, aspect, nearClip, farClip);
// looking at [0,0,0] from [0,0,150]
cam = new PeasyCam(this, 0, 0, 0, 150);
cam.setWheelScale(0.1); // set mouse wheel sensitivity
}
//////////////////////////////////////////////////////////
void drawGizmo () {
// draw coordinates gizmo at (0,0,0)
strokeWeight(0.5);
stroke(255,0,0);
line(0,0,0, 10,0,0);
stroke(0,255,0);
line(0,0,0, 0,-10,0);
stroke(0,0,255);
line(0,0,0, 0,0,10);
}
//////////////////////////////////////////////////////////
void drawBbox () {
// draw bounding box of 100x100x100
// centered at (0,0,0)
stroke(0);
noFill();
box(100);
}
Answers
maybe you can enter all (when they are constant) in a GROUP PShape in setup() and work with this. See tutorial on PShape please
Thanks, Chrisir!
But, the PShape method doesn't seem to improve the performance much ...:-S
Hm.... I thought it would.... sry....
It's very inefficient to have a separate PShape for each point. Instead you can place all the points in single PShape using POINTS, where each vertex is a point.
edit...
IF you ever got your code working, could you please post a copy of it so that others of us with similar issues could learn from your success?
Hello !
I did an example with 128 x 128 x 128, it represents more than 2 millions triangles and I think it's enough.
256 x 256 x 256 is too big and create an error ;
200 x 200 x200 works but too slow...
128 x128 x128 works at 60FPS :)
The main code is located inside the vertex shader, not in java.
Here is the code inside the pde :
The code of the vertex shader :
and the code of the fragment shader
If you want that all your point are front of the camera (not rotated), you can modify a bit the vertexShader like that :
thanks, tlecoz! May I ask how to run your code? I don't know about those non-processing part...
Hello ! 1) Copy/paste the pde part in a sketch.
2) save the sketch somewhere on your disk
3) in Processing, click on the "sketch" button (in the top-menu), then choose "show sketch folder", the place where you saved the pde will appear on the screen.
4) create a folder called "data" at the root of your pde (at the same level) and go into it
5) create a file called "vert.glsl" and copy-paste the vertex shader part.
6) create a file called "frag.glsl" and copy-paste the fragment shader part.
7) be sure that your pde and your glsl files are saved
8) run the sketch
You should read the shader tutorial to understand exactly what happened in the pde part, and the basic of fragment/vertex shader.
Here some comments concerning the vertex shader part only (the part you will not find in a tutorial)
Good luck !
Dear tlecoz, thank you very much! It is very efficient using your approach. I need to learn more about shader coding for this type of heave display and animation task.