The Easiest Way to Save and Share Code Snippets on the web

Untitled

processing

posted: Apr, 22nd 2012 | jump to bottom

import javax.media.opengl.*;
import processing.opengl.*;
 
float BASE_R = 180.0;
int MAX_DEPTH = 2;
float tx, ty;
 
void setup(){
  size( 640, 480, OPENGL);
  strokeWeight( 2 );
  smooth();
}
 
float[][] getSphereVerts(float r, float angOff, int lats, int longs) {
      float[][] ret = new float[lats * longs][3];
      for(int i = 0; i < lats; i++) {
         float lat = PI * (-0.5 + (float)i / lats) ;
 
         for(int j = 0; j < longs; j++) {
             float lng = 2 * PI * (float)j / longs + angOff;
             ret[i + longs * j ][0] = cos(lng) * cos(lat) * r;
             ret[i + longs * j ][1] = sin(lng) * cos(lat) * r;
             ret[i + longs * j ][2] = sin(lat) * r;
         }
     }
     return ret;
 }
 
void star( float xo, float yo, float zo, int count, float radius, float angOff, int depth ){
      float[][] verts = getSphereVerts( radius, angOff, count, count );
      stroke(0,0,0);
 
        for( int j = 0; j < count * count; j++ ){
          float xl = xo + verts[j][0];
          float yl = yo + verts[j][1];
          float zl = zo + verts[j][2];
 
          float cx = (xl / (BASE_R));
          float cy = (yl / (BASE_R));
          float cz = (zl / (BASE_R));
 
          stroke( 128 + cx * 127 - cz * 127, 128 - cx * 127 + cy * 127, 128 - cx * 127 - cy * 127 );
          beginShape( LINES );
          vertex( xo, yo, zo );
          vertex( xl, yl, zl);
          endShape();
 
          if( depth < MAX_DEPTH )
            star( xl, yl, zl, count, radius / 3,  angOff * 2, depth + 1 );
        }
}
 
float x = 0;
void draw(){
  background( 200 );
  translate( width / 2 , height / 2);
  rotateX( mouseY / 100.0 );
 rotateY( mouseX / 100.0 );
  star( 0.0, 0.0, 0.0, 5, BASE_R, x, 0 );  
  x+=0.01;
}
66 views