gpt4 book ai didi

processing - 在处理中渲染近多边形对象的剪裁

转载 作者:行者123 更新时间:2023-12-04 02:56:46 27 4
gpt4 key购买 nike

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

enter image description here enter image description here

知道如何阻止这种情况发生吗?代码如下,可在 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();
}

最佳答案

您可以通过调用 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

关于processing - 在处理中渲染近多边形对象的剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16544258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com