2

我正在学习使用Processing,并修改了其中一个示例来创建这个小程序。我有两个问题:

  1. 为什么球体是扁圆形的?我抄袭的例子中的球体又漂亮又圆。
  2. 当点光源位于球体之间时,为什么我会在球体的外侧边缘显示光?

这是这个小程序的源代码:

int radius = 40;
int spheredist = 320;
int maxlevel = 7;
float ecc = 0.28;
int x1, x2, y1, y2;

void setup() {
  size(640, 360, P3D);
  fill(204);
  //smooth();  // makes spheres ugly
  translate(width/2, height/2, 0);
  x1 = -spheredist/2+radius;
  x2 = spheredist/2-radius;
  y1 =
  y2 = 0;
}

void drawLightning(int x1_,int y1_,int x2_,int y2_,int lvl){
   if (lvl < maxlevel){
     int midx = (x1_ + x2_)/2;
     int midy = (y1_ + y2_)/2;
     float d = dist(x1_, y1_, x2_, y2_);
     d *= ecc;
     midx += random(-d, d);
     midy += random(-d, d);
     drawLightning(x1_, y1_, midx, midy, lvl+1);
     drawLightning(midx, midy, x2_, y2_, lvl+1);
   } else {
     strokeWeight(10);
     stroke(60,100,255,100);
     line(x1_,y1_,x2_,y2_);
     strokeWeight(1);
     stroke(255);
     line(x1_,y1_,x2_,y2_);
   }
}

void draw() {
  background(0); 
  noStroke(); 
  int brt = 200;
  pointLight(brt/2, brt/2, brt/2, spheredist/2, -spheredist, spheredist); 
  ambientLight(brt/8,brt/8,brt/8);


  if ((mouseX > width/4 && mouseX < width*3/4) &&
      (mouseY > height/2-radius && mouseY < height/2+radius)) {
    pushMatrix();
    translate(width/2, height/2, 0);
    pointLight(100, 100, 255, 0, 0, 0); 
    popMatrix();
  }

  pushMatrix();
  translate(width/2 - spheredist/2, height/2, 0); 
  sphere(radius); 
  translate(spheredist, 0, 0); 
  sphere(radius); 
  popMatrix();

  if ((mouseX > width/4 && mouseX < width*3/4) &&
      (mouseY > height/2-radius && mouseY < height/2+radius)) {
    pushMatrix();
    translate(width/2, height/2, 0);  
    drawLightning(x1,y1,x2,y2,0);
    popMatrix();
  }
}
4

2 回答 2

3

在这种情况下,平滑函数不能很好地工作,因为您有邻接的多边形。在没有图片的情况下跟随有点棘手,但想想两个多边形之间的边缘上的像素。例如,假设背景是 (0,0,0),多边形是 (255,255,255)。第一个多边形绘制并命中该像素。它覆盖了一半,因此它计算 0.5*(0,0,0) + 0.5*(255,255,255) 并将 (126,126,126) 保存为该像素的新值。第二个多边形绘制。它还覆盖了一半像素,因此它计算 0.5*(126,126,126) + 0.5*(255,255,255) 并保存 (190,190,190)。这是不正确的,因为这两个多边形应该分别覆盖不同的一半并导致颜色为 (255,255,255)。但是,如果您单独绘制每个多边形并且不这样做,您将无法弄清楚

现代显卡支持称为多重采样抗锯齿的东西。这保存了一些关于第一个多边形覆盖的像素部分的信息。处理试图在没有硬件支持的情况下模拟这一点,所以它使用了一个技巧,当你没有邻接的原语时效果很好,但当你有时就会崩溃。

至于扁率。默认情况下,Processing 会填充整个窗口,并且您的尺寸不是正方形的。解决这个问题的最简单方法是使用 ortho 函数使相机的纵横比为正方形。尝试将此添加到您的设置中:

邻(-360,360,-180,180,-10,10);

于 2009-12-05T03:53:43.457 回答
0

关于漂亮的圆形边缘,请尝试在 setup() 中调用方法 smooth():

void setup() {
  // ...
  smooth();
  // ...
}
于 2009-10-06T11:21:44.523 回答