Colorful Spheres

2008 August 07

I had my first taste of 3D in Processing dur­ing our last class, and now I’m hooked. Here’s my first attempt at gen­er­at­ing a bunch of spheres, each one a dif­fer­ent ran­dom color.

2 comments. »

  1. Colorful spheres are cool but I am think­ing what you really want to make is a 3D chicken! Can you post your code?

    Comment by jason — 2008 August 08 @ 1:32 pm

  2. Sure thing -- I should have done that before. Here you go:

    //Libraries
    import processing.opengl.*;
    
    //Objects
    Sphere[] spheres = new Sphere[500];
    
    //Variables
    int outerBounds = 300;
    
    
    
    void setup() {
      size(600, 400, OPENGL);
      for (int i = 0; i < spheres.length; i++) {
        spheres[i] = new Sphere();
      }
    }
    
    
    
    void draw() {
      background(0);
      smooth();
      pushMatrix();
      translate(width/2, height/2, 0);
    
      //Prep lighting for drawing spheres
      noStroke();
      lights();
      ambientLight(255, 255, 255);
    
      //Loop through each sphere object and draw it
      for(int i = 0; i < spheres.length; i++) {
        spheres[i].display();
      }  
      popMatrix();
    }
    
    
    
    class Sphere {
      int x;
      int y;
      int z;
      int radius;
    
      Sphere() {
        x = int(random(-1*outerBounds, outerBounds));
        y = int(random(-1*outerBounds, outerBounds));
        z = int(random(-1*outerBounds, outerBounds));
        radius = 10;
      }
    
      void display() {
        pushMatrix();
        translate(x, y, z);
        int r = int(random(0,255));
        int g = int(random(0,255));
        int b = int(random(0,255));
        ambient(r, g, b);
        sphere(radius);
        popMatrix();
      }
    }
    

    Comment by Scott — 2008 August 09 @ 5:07 pm

RSS feed for comments on this post.

Leave a comment

Site content and design © copyright 2006–2008 Scott Murray.