Thursday 9 April 2009

Second Program completed

Just completed the 2nd program. It just kinda came together by trial and error really. So now just need to proof-read my section, and I'm done!







/**
* Mouse event (chunk 82) example. Creates a set of orbiting spheres that can
* be controlled in several ways:
* - clicking: clicking a mouse button will change the hue (left button), brightness (right button) or saturation (other button)
* - dragging: dragging the mouse pointer changes the sphere by:
* - dragging horizontally changes the number of facets in the sphere (left = less)
* - dragging vertically changes the size of the spheres (upwards = smaller)
* @author Antony Lees
*/

// preset the sphere values
float angle = 0;
int sphereSize = 60;
int resolution = 10;
int numberOfOrbitingSpheres = 10;
// preset the colour values
int hue = 0;
int saturation = 51;
int brightness = 102;

ArrayList spheres;

void setup() {
// full screen 3D mode (chunk 32)
size(screen.width, screen.height, P3D);
// set the line colour to a blue
stroke(25, 25, 112);
spheres = new ArrayList();
// always create the static one in the middle
Sphere sphere = new Sphere(width / 2, height / 2, 0);
spheres.add(sphere);
// create the given number of orbiting spheres
// for statement (chunk 12)
for (int i = 0; i < numberOfOrbitingSpheres; i++) {
Sphere orbiter = new Sphere((int) random(width / 4), (int) random(height / 4), 0);
spheres.add(orbiter);
}

}

void mouseDragged() {
// set the resolution to the mouse x coordinate
resolution = mouseX / 50;
// set the size to the mouse y cooridinate
sphereSize = mouseY / 5;
}

void mouseClicked() {
// change the colour values according to the mouse x coordinate
switch (mouseButton) {
// set the values based on which button is clicked
// switch statement (chunk 11)
case (LEFT):
hue = mouseX % 255;
break;
case (RIGHT):
brightness = mouseX % 255;
break;
default:
saturation = mouseX % 255;
break;
}

}

// continous mode (chunk 30)
void draw() {
// set the screen environment
lights();
background(0);
// set the sphere colouring values
fill(hue, saturation, brightness);
lightSpecular(255, 255, 255);
directionalLight(204, 204, 204, 0, 0, -1);

// draw every sphere
for (int i = 0; i < spheres.size(); i ++) {
Sphere sphere = (Sphere) spheres.get(i);
sphere.drawSphere();
}

// change the angle for the orbiting sphere
angle++;
}

/**
* Defines each sphere
*
* @author Antony Lees
*/
class Sphere {

// sphere coordinates
int x;
int y;
int d;

// create a sphere by setting the coordinates
// Spehere constructor (chunk 62)
Sphere(int x, int y, int d) {
this.x = x;
this.y = y;
this.d = d;
}

// draw the sphere
// user-defined function (chunk 14)
void drawSphere() {
// draw at the coordinates
translate(x, y, d);
// rotate
rotateY(radians(angle));
rotateX(radians(angle));
rotateZ(radians(angle));
// set the resolution
sphereDetail(resolution);
// draw the sphere
sphere(sphereSize);
}
}

2 comments:

Anonymous said...

Worked/looked even better with OPENGL for me.

Anonymous said...

Looke even better with a dark blue backgroun image, strange quir with opengl you can't use background(img);, instead use image(img, 0, 0, width, height);