Processing 处理中近多边形对象的渲染剪裁
我在处理过程中对3D对象建模,遇到一个问题,即当对象离摄影机太近时,其渲染会被剪裁(即,距离摄影机最近的对象位不会被渲染)。这在P3D和OPENGL模式下都会发生。下面的右图显示了: 你知道如何阻止这种事情发生吗?代码如下所示,可在进行在线测试。非常感谢Processing 处理中近多边形对象的渲染剪裁,processing,Processing,我在处理过程中对3D对象建模,遇到一个问题,即当对象离摄影机太近时,其渲染会被剪裁(即,距离摄影机最近的对象位不会被渲染)。这在P3D和OPENGL模式下都会发生。下面的右图显示了: 你知道如何阻止这种事情发生吗?代码如下所示,可在进行在线测试。非常感谢 void setup() { size(600, 400, P3D); stroke(0); rectMode(CENTER); fill(200); // initialise camera variables s
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);
for (int i=-10; i<10; i++) {
for (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 and 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 to vertical movement
float scaleX; // scale mouseX inputs to horizontal movement
int direction = 0; // code for 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 to calculate the 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;
my = 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 the camera
turnCamera();
}
void setup(){
尺寸(600、400、P3D);
冲程(0);
矩形模式(中心);
填充(200);
//初始化相机变量
scaleY=PI/高度;
scaleX=2*PI/宽度;
camDir=PI/3;
camElev=PI/2;
MouseX=宽度/2;
鼠标=高度/3;
旋转摄像机();
camF_rel=设置向量(camDir,camElev);
}
作废提款(){
背景(255);
//摄像机和控制操作
MouseX=约束(MouseX,0,宽度);
MouseY=约束(MouseY,0,高度);
设置摄像机();
摄像机(camP.x,camP.y,camP.z,camF_abs.x,camF_abs.y,camF_abs.z,0,0,-1);
//绘图环境
//方格面
填充(150200255);
对于(inti=-10;i您可以通过调用Processing中的函数来控制近剪裁距离和远剪裁距离
将这些行添加到设置()中,问题就消失了:
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
float nearClippingDistance = 0.01; // default is cameraZ/10.0
perspective(fov, float(width)/float(height), nearClippingDistance, cameraZ*10.0);
通过在上一条线之后添加此线,可以在草图中找到默认的近剪裁距离:
println(cameraZ/10.0); // output: 34.6410
可能会有帮助。不确定您是否已经这样做了,但将您的问题放在处理论坛上会更快地得到您的某种帮助。处理似乎在这里并不流行。谢谢@nickecarlo,该页面逃过了我之前的搜索。