代码之家  ›  专栏  ›  技术社区  ›  PaulMcG

为什么我的处理应用程序无法正常运行?

  •  2
  • PaulMcG  · 技术社区  · 16 年前

    我正在学习使用 Processing ,并修改了其中一个示例以创建 this applet . 我有两个问题:

    1. 当点光源位于球体之间时,为什么要在球体的外边缘显示灯光?

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

    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();
      }
    }
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   MPG    16 年前

    在这种情况下,“平滑”函数不能很好地工作,因为有邻接的多边形。在没有图片的情况下进行跟踪有点棘手,但请考虑位于两个多边形之间边缘的像素。例如,假设背景为(0,0,0),多边形为(255255)。第一个多边形绘制并命中该像素。它覆盖了它的一半,因此它计算0.5*(0,0,0)+0.5*(255255)并将(126126)保存为该像素的新值。第二个多边形绘制。它还覆盖了一半像素,因此它计算0.5*(126126)+0.5*(255255)并保存(190190)。这是不正确的,因为这两个多边形应该各自覆盖不同的一半,并产生(255255)的颜色。但是,如果您单独绘制每个多边形,并且不在其间保存任何覆盖信息,则无法确定这一点。

    现代图形卡支持一种叫做 multisample antialiasing

    至于扁率。默认情况下,处理填充整个窗口,并且您的大小不是正方形。处理此问题的最简单方法是使用正交函数使相机的纵横比为正方形。尝试将其添加到您的设置中:

    正交(-360360,-180180,-10,10);

        2
  •  0
  •   Bart Kiers    16 年前

    void setup() {
      // ...
      smooth();
      // ...
    }
    
    推荐文章