作者 主题: [屏保][starfield]怎么把这个box()换成线。。。  (阅读 3278 次)

ww

  • Newbie
  • *
  • 帖子: 19
  • twitter.com/ww1way
    • 怪比软件学习笔记
[屏保][starfield]怎么把这个box()换成线。。。
« 于: 十一月 08, 2011, 05:34:15 下午 »
以前老windows的屏保:starfiled,大家都应该看过。

程序代码
int num = 500;
Star stars[] = new Star[num];

void setup() {
  size(800, 600, P3D);
  for (int i=0; i< num; i++) {
    stars[i] = new Star();
  }
  frameRate(25);
}

void draw() {
  background(0);
  for (int i=0; i< num; i++) {
    stars[i].display();
  }
}

class Star {
  float x = random(width);
  float y = random(height);
  float z = random(600);
  int a = 0;
  float d = 5;

  void display() {
    fill(255, a);
    pushMatrix();
    translate(x, y, z-100);
    box(d);
    popMatrix();

    z+=random(10.0, 25.0);
    //change alpha with z position
    a+=z/20;

    if (z>600) {
      z=0;
      a=0;
      x = random(width);
      y = random(height);
    }
  }
}


现在暂时用box()混着,,,但是不知道如何将这些box用一条一条的线代替。。。
« 最后编辑时间: 十一月 08, 2011, 06:41:22 下午 作者 vinjn »

ww

  • Newbie
  • *
  • 帖子: 19
  • twitter.com/ww1way
    • 怪比软件学习笔记
Re: 怎么把这个box()换成线。。。
« 回复 #1 于: 十一月 08, 2011, 06:10:33 下午 »
ok,现在搞出来了,但有个疑问是,如果去掉下边代码中的box(0),效果就会错,,,求高手解答,,

程序代码
int num = 4500; 
Star stars[] = new Star[num];

void setup() {
  size(800, 600, P3D);
  smooth();
  for (int i=0; i< num; i++) {
    stars[i] = new Star();
  }
  frameRate(25);
}

void draw() {
  background(0);
  for (int i=0; i< num; i++) {
    stars[i].display();
  }
}

class Star {
  float x = random(width);
  float y = random(height);
  float z = random(600);
  int a = 0;

  void display() {
    pushMatrix();
    translate(0, 0, z-100);
    stroke(0);
    box(0);
    stroke(255, a);
    line(x, y, z, x, y, z+45);
    popMatrix();

    z+=random(10.0, 25.0);
    //change alpha with z position
    a+=z/20;

    if (z>600) {
      z=0;
      a=0;
      x = random(width);
      y = random(height);
    }
  }
}

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
Re: 怎么把这个box()换成线。。。
« 回复 #2 于: 十一月 08, 2011, 06:40:50 下午 »
那啥,P3D模式sucks
换成opengl即可

程序代码
import processing.opengl.*;

int num = 4500;
Star stars[] = new Star[num];

void setup() {
  size(800, 600, OPENGL);
  smooth();
  for (int i=0; i< num; i++) {
    stars[i] = new Star();
  }
  frameRate(25);
}

void draw() {
  background(0);
  for (int i=0; i< num; i++) {
    stars[i].display();
  }
}

class Star {
  float x = random(width);
  float y = random(height);
  float z = random(600);
  int a = 0;

  void display() {
    stroke(255, a);
    line(x, y, z, x, y, z+45);

    z+=random(10.0, 25.0);
    //change alpha with z position
    a+=z/20;

    if (z>600) {
      z=0;
      a=0;
      x = random(width);
      y = random(height);
    }
  }
}

Tags: