Render clipping of near polygon objects in Processing -
i'm modelling 3d objects in processing , have issue whereby when objects close camera renders clipped (i.e. closest bits of object camera not rendered). happens both in p3d , opengl modes. right-hand image below illustrate:
any idea how stop happening? code below, , testable online @ openprocessing.org. many in advance.
void setup() { size(600, 400, p3d); stroke(0); rectmode(center); fill(200); // initialise camera variables scaley = pi/height; scalex = 2*pi/width; camdir = pi/3; camelev = pi/2; mousex = width/2; mousey = height/3; turncamera(); camf_rel = setvector(camdir, camelev); } void draw() { background(255); // camera & control operations mousex = constrain(mousex, 0, width); mousey = constrain(mousey, 0, height); setcamera(); camera(camp.x, camp.y, camp.z, camf_abs.x, camf_abs.y, camf_abs.z, 0, 0, -1); // draw environment // checkered plane fill(150,200,255); (int i=-10; i<10; i++) { (int j=-10; j<10; j++) { nostroke(); if ((i+j)%2 == 0) rect(i*50, j*50, 50, 50); } } // vertical line nofill(); stroke(100); box(1000); } // camera position , focus variables pvector camp = new pvector(0, 1200, 700); // camera position pvector camf_abs = new pvector(); // camera focus (absolute position) pvector camf_rel = new pvector(); // camera focus (relative vector) float camdir, camelev; // last camera bearing/elevation angles float mx, my; // last mouse x/y float mousex, mousey; // replicate inbuilt mouse variables float scaley; // scale mousey inputs vertical movement float scalex; // scale mousex inputs horizontal movement int direction = 0; // code controling movement float movespeed = 10; // overall controls responsiveness // main camera calculation operations void setcamera() { camf_rel = setvector(camdir, camelev); if (direction >= 1 & direction <= 4) movecamera(movespeed); if (direction >= 5 & direction <= 6) elevcamera(movespeed); camf_abs = camf_rel.get(); camf_abs.add(camp); } pvector setvector(float dir, float elev){ //generic function calculate pvector based on radial coordinates pvector v = new pvector(cos(dir), sin(dir), 0); float fz = -sin(elev); float fy = sqrt(1-pow(fz, 2)); v.mult(fy); v.z = fz; return(v); } void movecamera (float speed) { pvector moveto = new pvector(); // left / right movement if (direction%2 == 0) { float dir = 0; if (direction == 2) dir = camdir + pi/2; // right else dir = camdir - pi/2; // left pvector v = setvector(dir, 0); v.mult(speed); camp.add(v); } // forward / backward movement else { moveto = camf_rel.get(); if (direction == 1) moveto.mult(-1); // forward } moveto.normalize(); moveto.mult(speed); camp.sub(moveto); } void turncamera(){ float x = mousex - mx; float x_scaled = x * scalex; float y = mousey - my; float y_scaled = y * scaley; camdir += x_scaled; camelev += y_scaled; mx = mousex; = mousey; } void elevcamera (float speed) { if (direction == 5) { // lower camera camp.z -= speed; camf_abs.z -= speed; } else { // raise camera camp.z += speed; camf_abs.z += speed; } } void keypressed() { if (keycode == 38 | key == 'w') direction = 1; // move forward else if (keycode == 39 | key == 'd') direction = 2; // move right else if (keycode == 40 | key == 's') direction = 3; // move backward else if (keycode == 37 | key == 'a') direction = 4; // move left else if (key == 'z') direction = 5; // lower camera else if (key == 'x') direction = 6; // raise camera } void keyreleased() { direction = 0; } void mousemoved() { // turns camera turncamera(); }
you can control near , far clipping distances calling perspective() function in processing.
add these lines setup() , issue gone:
float fov = pi/3.0; float cameraz = (height/2.0) / tan(fov/2.0); float nearclippingdistance = 0.01; // default cameraz/10.0 perspective(fov, float(width)/float(height), nearclippingdistance, cameraz*10.0);
you can find out default near clipping distance in sketch adding line after previous ones:
println(cameraz/10.0); // output: 34.6410
Comments
Post a Comment