3

我正在处理中对 3D 对象进行建模,并且遇到一个问题,即当对象离相机太近时,它们的渲染会被剪裁(即对象最接近相机的位不会被渲染)。这在 P3D 和 OPENGL 模式下都会发生。下图右图说明:

在此处输入图像描述在此处输入图像描述

知道如何阻止这种情况发生吗?代码如下,可在openprocessing.org在线测试。提前谢谢了。

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();
}
4

1 回答 1

4

您可以通过调用Processing 中的perspective()函数来控制近剪裁距离和远剪裁距离。

将这些行添加到您的 setup() 中,问题就消失了:

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
于 2015-02-05T13:24:36.387 回答