Problem with perspective() and clipping plane
in
Programming Questions
•
1 year ago
Hi there Processingeers!
I'm playing around with moving particles, which are drawn as lines from the previous position to the new one. The problem comes when I zoom in, getting too close to these lines, as the turn from small lines moving inside a 3D space to lines crossing the display from one side to the other, and thus looking like lines in a 2D plane.
After looking into the forum, I thought it would be related to the near z plane clipping distance, so I tried setting a higher distance with perspective(). But it doesnt matter how high I set this distance, it is always looking the same, even setting a near plane distance very close to the far plane distance, all the particles are drawn.
I was using scale() for zooming, so I decided trying camera() instead (just trying here), but Im getting the same problem.
I have created a simple code to show this:
- firefly[] fireflies = new firefly[20];
- int lastX=0;
- int lastY=0;
- float zoom=1.0;
- void setup()
- {
- size(300,300, P3D);
- smooth();
- background(0);
- for(int i =0; i< fireflies.length;i++)
- {
- fireflies[i] = new firefly();
- }
- float cameraZ =((height/2.0) / tan(PI*60.0/360.0));
- perspective(PI/3.0, width/height, cameraZ*9.5, cameraZ*10.0);// ridiculously high near z plane
- }
- void draw(){
- background(0);
- stroke(247,247,82);
- //
- camera(width/2.0, height/2.0, zoom * (height/2.0) / tan(PI*60.0 / 360.0) , width/2.0, height/2.0, 0, 0, 1, 0);
- //translation for rotating around the centre of the sketch
- pushMatrix();
- translate(width/2,height/2,0);
- rotateX(radians(mouseY%360));
- rotateY(radians(mouseX%360));
- translate(-width/2, -height/2,0);
- // scale(zoom);
- for(int i =0; i< fireflies.length;i++)
- {
- fireflies[i].update(); //update position and draw
- }
- popMatrix();
- println(zoom);
- }
- void keyPressed()
- {
- if (key=='a')
- zoom+=0.1;
- else if (key == 'z')
- zoom-=0.1;
- }
- class firefly
- {
- int lastX;
- int lastY;
- int lastZ;
- firefly()
- {
- lastX=(int)random(0, width);
- lastY=(int)random(0, height);
- lastZ=(int)random(0, -100);
- }
- void update()
- {
- int newX=(int)(lastX + random(-5,5));
- int newY=(int)(lastY + random(-5,5));
- int newZ=(int)(lastZ + random(-5,5));
- line(this.lastX,this.lastY, this.lastZ, newX, newY, newZ );
- this.lastX=newX;this.lastY=newY;this.lastZ=newZ;
- }
- }
Just move the mouse and press 'z' to zoom in toward the particles to see what I mean. Pardon my rudimentary zoom method.
Any help would be highly appreciated! Thanks!
1