【发布时间】:2009-10-06 06:17:06
【问题描述】:
我正在学习使用Processing,并修改了其中一个示例以创建this applet。我有两个问题:
- 为什么这些球体是扁圆形的?我抄袭的示例中的球体又漂亮又圆。
- 当点光源位于球体之间时,为什么我会在球体的外侧边缘显示光?
这是这个小程序的源代码:
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();
}
}
【问题讨论】:
-
我看不出任何人在没有看到您的代码的情况下是如何分辨的。
-
@Bart:对不起,我还在习惯处理。小程序页面生成一个源代码链接,我认为它会显示源代码,并且我认为这是发布草图的惯例。我将编辑问题以将其包含在此处。
-
@Paul McGuire:谢谢。我不知道那里有指向源的链接。不幸的是,我没有尝试运行小程序,因为无法在我的 64 位 Linux 机器上使用 Java 的 IcedTea 插件运行处理小程序。稍后我会看一下代码。
-
当我运行代码时,您描述的问题都没有发生。我看到光线从球体内部反射回来,球体是球形的!
标签: 3d processing