mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Added new P3D examples
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Brick Tower
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* 3D castle tower constructed out of individual bricks.
|
||||
* Uses the PVector and Cube classes.
|
||||
*/
|
||||
|
||||
float bricksPerLayer = 16.0;
|
||||
float brickLayers = 18.0;
|
||||
Cube brick;
|
||||
float brickWidth = 60, brickHeight = 25, brickDepth = 25;
|
||||
float radius = 175.0;
|
||||
float angle = 0;
|
||||
|
||||
void setup(){
|
||||
size(640, 360, P3D);
|
||||
brick = new Cube(brickWidth, brickHeight, brickDepth);
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(0);
|
||||
float tempX = 0, tempY = 0, tempZ = 0;
|
||||
fill(182, 62, 29);
|
||||
noStroke();
|
||||
// Add basic light setup
|
||||
lights();
|
||||
translate(width/2, height*1.2, -380);
|
||||
// Tip tower to see inside
|
||||
rotateX(radians(-45));
|
||||
// Slowly rotate tower
|
||||
rotateY(frameCount * PI/600);
|
||||
for (int i = 0; i < brickLayers; i++){
|
||||
// Increment rows
|
||||
tempY-=brickHeight;
|
||||
// Alternate brick seams
|
||||
angle = 360.0 / bricksPerLayer * i/2;
|
||||
for (int j = 0; j < bricksPerLayer; j++){
|
||||
tempZ = cos(radians(angle))*radius;
|
||||
tempX = sin(radians(angle))*radius;
|
||||
pushMatrix();
|
||||
translate(tempX, tempY, tempZ);
|
||||
rotateY(radians(angle));
|
||||
// Add crenelation
|
||||
if (i==brickLayers-1){
|
||||
if (j%2 == 0){
|
||||
brick.create();
|
||||
}
|
||||
}
|
||||
// Create main tower
|
||||
else {
|
||||
brick.create();
|
||||
}
|
||||
popMatrix();
|
||||
angle += 360.0/bricksPerLayer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
class Cube {
|
||||
|
||||
PVector[] vertices = new PVector[24];
|
||||
float w, h, d;
|
||||
|
||||
Cube(){ }
|
||||
|
||||
Cube(float w, float h, float d){
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
this.d = d;
|
||||
|
||||
// Cube composed of 6 quads
|
||||
// Front
|
||||
vertices[0] = new PVector(-w/2, -h/2, d/2);
|
||||
vertices[1] = new PVector(w/2, -h/2, d/2);
|
||||
vertices[2] = new PVector(w/2, h/2, d/2);
|
||||
vertices[3] = new PVector(-w/2, h/2, d/2);
|
||||
|
||||
// Left
|
||||
vertices[4] = new PVector(-w/2, -h/2, d/2);
|
||||
vertices[5] = new PVector(-w/2, -h/2, -d/2);
|
||||
vertices[6] = new PVector(-w/2, h/2, -d/2);
|
||||
vertices[7] = new PVector(-w/2, h/2, d/2);
|
||||
|
||||
// Right
|
||||
vertices[8] = new PVector(w/2, -h/2, d/2);
|
||||
vertices[9] = new PVector(w/2, -h/2, -d/2);
|
||||
vertices[10] = new PVector(w/2, h/2, -d/2);
|
||||
vertices[11] = new PVector(w/2, h/2, d/2);
|
||||
|
||||
// Back
|
||||
vertices[12] = new PVector(-w/2, -h/2, -d/2);
|
||||
vertices[13] = new PVector(w/2, -h/2, -d/2);
|
||||
vertices[14] = new PVector(w/2, h/2, -d/2);
|
||||
vertices[15] = new PVector(-w/2, h/2, -d/2);
|
||||
|
||||
// Top
|
||||
vertices[16] = new PVector(-w/2, -h/2, d/2);
|
||||
vertices[17] = new PVector(-w/2, -h/2, -d/2);
|
||||
vertices[18] = new PVector(w/2, -h/2, -d/2);
|
||||
vertices[19] = new PVector(w/2, -h/2, d/2);
|
||||
|
||||
// Bottom
|
||||
vertices[20] = new PVector(-w/2, h/2, d/2);
|
||||
vertices[21] = new PVector(-w/2, h/2, -d/2);
|
||||
vertices[22] = new PVector(w/2, h/2, -d/2);
|
||||
vertices[23] = new PVector(w/2, h/2, d/2);
|
||||
}
|
||||
|
||||
void create(){
|
||||
for (int i=0; i<6; i++){
|
||||
beginShape(QUADS);
|
||||
for (int j = 0; j < 4; j++){
|
||||
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Cubic Grid
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* 3D translucent colored grid uses nested pushMatrix()
|
||||
* and popMatrix() functions.
|
||||
*/
|
||||
|
||||
float boxSize = 40;
|
||||
float margin = boxSize*2;
|
||||
float depth = 400;
|
||||
color boxFill;
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
// Center and spin grid
|
||||
translate(width/2, height/2, -depth);
|
||||
rotateY(frameCount * 0.01);
|
||||
rotateX(frameCount * 0.01);
|
||||
|
||||
// Build grid using multiple translations
|
||||
for (float i =- depth/2+margin; i <= depth/2-margin; i += boxSize){
|
||||
pushMatrix();
|
||||
for (float j =- height+margin; j <= height-margin; j += boxSize){
|
||||
pushMatrix();
|
||||
for (float k =- width+margin; k <= width-margin; k += boxSize){
|
||||
// Base fill color on counter values, abs function
|
||||
// ensures values stay within legal range
|
||||
boxFill = color(abs(i), abs(j), abs(k), 50);
|
||||
pushMatrix();
|
||||
translate(k, j, i);
|
||||
fill(boxFill);
|
||||
box(boxSize, boxSize, boxSize);
|
||||
popMatrix();
|
||||
}
|
||||
popMatrix();
|
||||
}
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Geometry
|
||||
* by Marius Watz.
|
||||
*
|
||||
* Using sin/cos lookup tables, blends colors, and draws a series of
|
||||
* rotating arcs on the screen.
|
||||
*/
|
||||
|
||||
// Trig lookup tables borrowed from Toxi; cryptic but effective.
|
||||
float sinLUT[];
|
||||
float cosLUT[];
|
||||
float SINCOS_PRECISION=1.0;
|
||||
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
|
||||
|
||||
// System data
|
||||
boolean dosave=false;
|
||||
int num;
|
||||
float pt[];
|
||||
int style[];
|
||||
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, P3D);
|
||||
background(255);
|
||||
|
||||
// Fill the tables
|
||||
sinLUT=new float[SINCOS_LENGTH];
|
||||
cosLUT=new float[SINCOS_LENGTH];
|
||||
for (int i = 0; i < SINCOS_LENGTH; i++) {
|
||||
sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
|
||||
cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
|
||||
}
|
||||
|
||||
num = 150;
|
||||
pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
|
||||
style = new int[2*num]; // color, render style
|
||||
|
||||
// Set up arc shapes
|
||||
int index=0;
|
||||
float prob;
|
||||
for (int i=0; i<num; i++) {
|
||||
pt[index++] = random(PI*2); // Random X axis rotation
|
||||
pt[index++] = random(PI*2); // Random Y axis rotation
|
||||
|
||||
pt[index++] = random(60,80); // Short to quarter-circle arcs
|
||||
if(random(100)>90) pt[index]=(int)random(8,27)*10;
|
||||
|
||||
pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely
|
||||
|
||||
pt[index++] = random(4,32); // Width of band
|
||||
if(random(100)>90) pt[index]=random(40,60); // Width of band
|
||||
|
||||
pt[index++] = radians(random(5,30))/5; // Speed of rotation
|
||||
|
||||
// get colors
|
||||
prob = random(100);
|
||||
if(prob<30) style[i*2]=colorBlended(random(1), 255,0,100, 255,0,0, 210);
|
||||
else if(prob<70) style[i*2]=colorBlended(random(1), 0,153,255, 170,225,255, 210);
|
||||
else if(prob<90) style[i*2]=colorBlended(random(1), 200,255,0, 150,255,0, 210);
|
||||
else style[i*2]=color(255,255,255, 220);
|
||||
|
||||
if(prob<50) style[i*2]=colorBlended(random(1), 200,255,0, 50,120,0, 210);
|
||||
else if(prob<90) style[i*2]=colorBlended(random(1), 255,100,0, 255,255,0, 210);
|
||||
else style[i*2]=color(255,255,255, 220);
|
||||
|
||||
style[i*2+1]=(int)(random(100))%3;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
background(0);
|
||||
|
||||
int index=0;
|
||||
translate(width/2, height/2, 0);
|
||||
rotateX(PI/6);
|
||||
rotateY(PI/6);
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
pushMatrix();
|
||||
|
||||
rotateX(pt[index++]);
|
||||
rotateY(pt[index++]);
|
||||
|
||||
if(style[i*2+1]==0) {
|
||||
stroke(style[i*2]);
|
||||
noFill();
|
||||
strokeWeight(1);
|
||||
arcLine(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
else if(style[i*2+1]==1) {
|
||||
fill(style[i*2]);
|
||||
noStroke();
|
||||
arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
else {
|
||||
fill(style[i*2]);
|
||||
noStroke();
|
||||
arc(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
|
||||
// increase rotation
|
||||
pt[index-5]+=pt[index]/10;
|
||||
pt[index-4]+=pt[index++]/20;
|
||||
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get blend of two colors
|
||||
int colorBlended(float fract,
|
||||
float r, float g, float b,
|
||||
float r2, float g2, float b2, float a) {
|
||||
|
||||
r2 = (r2 - r);
|
||||
g2 = (g2 - g);
|
||||
b2 = (b2 - b);
|
||||
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
|
||||
}
|
||||
|
||||
|
||||
// Draw arc line
|
||||
void arcLine(float x,float y,float deg,float rad,float w) {
|
||||
int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
|
||||
int numlines=(int)(w/2);
|
||||
|
||||
for (int j=0; j<numlines; j++) {
|
||||
beginShape();
|
||||
for (int i=0; i<a; i++) {
|
||||
vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
|
||||
}
|
||||
endShape();
|
||||
rad += 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw arc line with bars
|
||||
void arcLineBars(float x,float y,float deg,float rad,float w) {
|
||||
int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
|
||||
a /= 4;
|
||||
|
||||
beginShape(QUADS);
|
||||
for (int i=0; i<a; i+=4) {
|
||||
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
|
||||
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
|
||||
vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
|
||||
vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
|
||||
// Draw solid arc
|
||||
void arc(float x,float y,float deg,float rad,float w) {
|
||||
int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
|
||||
beginShape(QUAD_STRIP);
|
||||
for (int i = 0; i < a; i++) {
|
||||
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
|
||||
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Dimension3D{
|
||||
float w, h, d;
|
||||
|
||||
Dimension3D(float w, float h, float d){
|
||||
this.w=w;
|
||||
this.h=h;
|
||||
this.d=d;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* I Like Icosahedra
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* This example plots icosahedra. The Icosahdron is a regular
|
||||
* polyhedron composed of twenty equalateral triangles.
|
||||
*/
|
||||
|
||||
Icosahedron ico1;
|
||||
Icosahedron ico2;
|
||||
Icosahedron ico3;
|
||||
|
||||
void setup(){
|
||||
size(640, 360, P3D);
|
||||
ico1 = new Icosahedron(75);
|
||||
ico2 = new Icosahedron(75);
|
||||
ico3 = new Icosahedron(75);
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(0);
|
||||
lights();
|
||||
translate(width/2, height/2);
|
||||
|
||||
pushMatrix();
|
||||
translate(-width/3.5, 0);
|
||||
rotateX(frameCount*PI/185);
|
||||
rotateY(frameCount*PI/-200);
|
||||
stroke(170, 0, 0);
|
||||
noFill();
|
||||
ico1.create();
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
rotateX(frameCount*PI/200);
|
||||
rotateY(frameCount*PI/300);
|
||||
stroke(150, 0, 180);
|
||||
fill(170, 170, 0);
|
||||
ico2.create();
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
translate(width/3.5, 0);
|
||||
rotateX(frameCount*PI/-200);
|
||||
rotateY(frameCount*PI/200);
|
||||
noStroke();
|
||||
fill(0, 0, 185);
|
||||
ico3.create();
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
class Icosahedron extends Shape3D{
|
||||
|
||||
// icosahedron
|
||||
PVector topPoint;
|
||||
PVector[] topPent = new PVector[5];
|
||||
PVector bottomPoint;
|
||||
PVector[] bottomPent = new PVector[5];
|
||||
float angle = 0, radius = 150;
|
||||
float triDist;
|
||||
float triHt;
|
||||
float a, b, c;
|
||||
|
||||
// constructor
|
||||
Icosahedron(float radius){
|
||||
this.radius = radius;
|
||||
init();
|
||||
}
|
||||
|
||||
Icosahedron(PVector v, float radius){
|
||||
super(v);
|
||||
this.radius = radius;
|
||||
init();
|
||||
}
|
||||
|
||||
// calculate geometry
|
||||
void init(){
|
||||
c = dist(cos(0)*radius, sin(0)*radius, cos(radians(72))*radius, sin(radians(72))*radius);
|
||||
b = radius;
|
||||
a = (float)(Math.sqrt(((c*c)-(b*b))));
|
||||
|
||||
triHt = (float)(Math.sqrt((c*c)-((c/2)*(c/2))));
|
||||
|
||||
for (int i=0; i<topPent.length; i++){
|
||||
topPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, triHt/2.0f);
|
||||
angle+=radians(72);
|
||||
}
|
||||
topPoint = new PVector(0, 0, triHt/2.0f+a);
|
||||
angle = 72.0f/2.0f;
|
||||
for (int i=0; i<topPent.length; i++){
|
||||
bottomPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, -triHt/2.0f);
|
||||
angle+=radians(72);
|
||||
}
|
||||
bottomPoint = new PVector(0, 0, -(triHt/2.0f+a));
|
||||
}
|
||||
|
||||
// draws icosahedron
|
||||
void create(){
|
||||
for (int i=0; i<topPent.length; i++){
|
||||
// icosahedron top
|
||||
beginShape();
|
||||
if (i<topPent.length-1){
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
|
||||
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
|
||||
}
|
||||
else {
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
|
||||
vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
|
||||
// icosahedron bottom
|
||||
beginShape();
|
||||
if (i<bottomPent.length-1){
|
||||
vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
|
||||
vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
|
||||
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
|
||||
}
|
||||
else {
|
||||
vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
|
||||
vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
|
||||
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
}
|
||||
|
||||
// icosahedron body
|
||||
for (int i=0; i<topPent.length; i++){
|
||||
if (i<topPent.length-2){
|
||||
beginShape();
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
|
||||
vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
|
||||
endShape(CLOSE);
|
||||
|
||||
beginShape();
|
||||
vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
|
||||
endShape(CLOSE);
|
||||
}
|
||||
else if (i==topPent.length-2){
|
||||
beginShape();
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
|
||||
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
|
||||
endShape(CLOSE);
|
||||
|
||||
beginShape();
|
||||
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
|
||||
endShape(CLOSE);
|
||||
}
|
||||
else if (i==topPent.length-1){
|
||||
beginShape();
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
|
||||
vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
|
||||
endShape(CLOSE);
|
||||
|
||||
beginShape();
|
||||
vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
|
||||
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
|
||||
vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
|
||||
endShape(CLOSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// overrided methods fom Shape3D
|
||||
void rotZ(float theta){
|
||||
float tx=0, ty=0, tz=0;
|
||||
// top point
|
||||
tx = cos(theta)*topPoint.x+sin(theta)*topPoint.y;
|
||||
ty = sin(theta)*topPoint.x-cos(theta)*topPoint.y;
|
||||
topPoint.x = tx;
|
||||
topPoint.y = ty;
|
||||
|
||||
// bottom point
|
||||
tx = cos(theta)*bottomPoint.x+sin(theta)*bottomPoint.y;
|
||||
ty = sin(theta)*bottomPoint.x-cos(theta)*bottomPoint.y;
|
||||
bottomPoint.x = tx;
|
||||
bottomPoint.y = ty;
|
||||
|
||||
// top and bottom pentagons
|
||||
for (int i=0; i<topPent.length; i++){
|
||||
tx = cos(theta)*topPent[i].x+sin(theta)*topPent[i].y;
|
||||
ty = sin(theta)*topPent[i].x-cos(theta)*topPent[i].y;
|
||||
topPent[i].x = tx;
|
||||
topPent[i].y = ty;
|
||||
|
||||
tx = cos(theta)*bottomPent[i].x+sin(theta)*bottomPent[i].y;
|
||||
ty = sin(theta)*bottomPent[i].x-cos(theta)*bottomPent[i].y;
|
||||
bottomPent[i].x = tx;
|
||||
bottomPent[i].y = ty;
|
||||
}
|
||||
}
|
||||
|
||||
void rotX(float theta){
|
||||
}
|
||||
|
||||
void rotY(float theta){
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
abstract class Shape3D{
|
||||
float x, y, z;
|
||||
float w, h, d;
|
||||
|
||||
Shape3D(){
|
||||
}
|
||||
|
||||
Shape3D(float x, float y, float z){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
Shape3D(PVector p){
|
||||
x = p.x;
|
||||
y = p.y;
|
||||
z = p.z;
|
||||
}
|
||||
|
||||
|
||||
Shape3D(Dimension3D dim){
|
||||
w = dim.w;
|
||||
h = dim.h;
|
||||
d = dim.d;
|
||||
}
|
||||
|
||||
Shape3D(float x, float y, float z, float w, float h, float d){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
Shape3D(float x, float y, float z, Dimension3D dim){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
w = dim.w;
|
||||
h = dim.h;
|
||||
d = dim.d;
|
||||
}
|
||||
|
||||
Shape3D(PVector p, Dimension3D dim){
|
||||
x = p.x;
|
||||
y = p.y;
|
||||
z = p.z;
|
||||
w = dim.w;
|
||||
h = dim.h;
|
||||
d = dim.d;
|
||||
}
|
||||
|
||||
void setLoc(PVector p){
|
||||
x=p.x;
|
||||
y=p.y;
|
||||
z=p.z;
|
||||
}
|
||||
|
||||
void setLoc(float x, float y, float z){
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
this.z=z;
|
||||
}
|
||||
|
||||
|
||||
// override if you need these
|
||||
void rotX(float theta){
|
||||
}
|
||||
|
||||
void rotY(float theta){
|
||||
}
|
||||
|
||||
void rotZ(float theta){
|
||||
}
|
||||
|
||||
|
||||
// must be implemented in subclasses
|
||||
abstract void init();
|
||||
abstract void create();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Primitives 3D.
|
||||
*
|
||||
* Placing mathematically 3D objects in synthetic space.
|
||||
* The lights() method reveals their imagined dimension.
|
||||
* The box() and sphere() functions each have one parameter
|
||||
* which is used to specify their size. These shapes are
|
||||
* positioned using the translate() function.
|
||||
*/
|
||||
|
||||
size(640, 360, P3D);
|
||||
background(0);
|
||||
lights();
|
||||
|
||||
noStroke();
|
||||
pushMatrix();
|
||||
translate(130, height/2, 0);
|
||||
rotateY(1.25);
|
||||
rotateX(-0.4);
|
||||
box(100);
|
||||
popMatrix();
|
||||
|
||||
noFill();
|
||||
stroke(255);
|
||||
pushMatrix();
|
||||
translate(500, height*0.35, -200);
|
||||
sphere(280);
|
||||
popMatrix();
|
||||
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* RGB Cube.
|
||||
*
|
||||
* The three primary colors of the additive color model are red, green, and blue.
|
||||
* This RGB color cube displays smooth transitions between these colors.
|
||||
*/
|
||||
|
||||
float xmag, ymag = 0;
|
||||
float newXmag, newYmag = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 360, P3D);
|
||||
noStroke();
|
||||
colorMode(RGB, 1);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0.5);
|
||||
|
||||
pushMatrix();
|
||||
|
||||
translate(width/2, height/2, -30);
|
||||
|
||||
newXmag = mouseX/float(width) * TWO_PI;
|
||||
newYmag = mouseY/float(height) * TWO_PI;
|
||||
|
||||
float diff = xmag-newXmag;
|
||||
if (abs(diff) > 0.01) { xmag -= diff/4.0; }
|
||||
|
||||
diff = ymag-newYmag;
|
||||
if (abs(diff) > 0.01) { ymag -= diff/4.0; }
|
||||
|
||||
rotateX(-ymag);
|
||||
rotateY(-xmag);
|
||||
|
||||
scale(90);
|
||||
beginShape(QUADS);
|
||||
|
||||
fill(0, 1, 1); vertex(-1, 1, 1);
|
||||
fill(1, 1, 1); vertex( 1, 1, 1);
|
||||
fill(1, 0, 1); vertex( 1, -1, 1);
|
||||
fill(0, 0, 1); vertex(-1, -1, 1);
|
||||
|
||||
fill(1, 1, 1); vertex( 1, 1, 1);
|
||||
fill(1, 1, 0); vertex( 1, 1, -1);
|
||||
fill(1, 0, 0); vertex( 1, -1, -1);
|
||||
fill(1, 0, 1); vertex( 1, -1, 1);
|
||||
|
||||
fill(1, 1, 0); vertex( 1, 1, -1);
|
||||
fill(0, 1, 0); vertex(-1, 1, -1);
|
||||
fill(0, 0, 0); vertex(-1, -1, -1);
|
||||
fill(1, 0, 0); vertex( 1, -1, -1);
|
||||
|
||||
fill(0, 1, 0); vertex(-1, 1, -1);
|
||||
fill(0, 1, 1); vertex(-1, 1, 1);
|
||||
fill(0, 0, 1); vertex(-1, -1, 1);
|
||||
fill(0, 0, 0); vertex(-1, -1, -1);
|
||||
|
||||
fill(0, 1, 0); vertex(-1, 1, -1);
|
||||
fill(1, 1, 0); vertex( 1, 1, -1);
|
||||
fill(1, 1, 1); vertex( 1, 1, 1);
|
||||
fill(0, 1, 1); vertex(-1, 1, 1);
|
||||
|
||||
fill(0, 0, 0); vertex(-1, -1, -1);
|
||||
fill(1, 0, 0); vertex( 1, -1, -1);
|
||||
fill(1, 0, 1); vertex( 1, -1, 1);
|
||||
fill(0, 0, 1); vertex(-1, -1, 1);
|
||||
|
||||
endShape();
|
||||
|
||||
popMatrix();
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Legs class
|
||||
* By Ira Greenberg <br />
|
||||
* Processing for Flash Developers,
|
||||
* Friends of ED, 2009
|
||||
*/
|
||||
|
||||
class Legs {
|
||||
// Instance properties with default values
|
||||
float x = 0, y = 0, z = 0, w = 150, ht = 125;
|
||||
color col = #77AA22;
|
||||
// Advanced properties
|
||||
float detailW = w/6.0;
|
||||
float detailHt = ht/8.0;
|
||||
float shoeBulge = detailHt*2.0;
|
||||
float legGap = w/7.0;
|
||||
|
||||
// Dynamics properties
|
||||
float velocity = .02, stepL, stepR, stepRate = random(10, 50);
|
||||
float speedX = 1.0, speedZ, spring, damping = .5, theta;
|
||||
|
||||
// Default constructor
|
||||
Legs() {
|
||||
}
|
||||
|
||||
// Standard constructor
|
||||
Legs(float x, float z, float w, float ht, color col) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.ht = ht;
|
||||
this.col = col;
|
||||
fill(col);
|
||||
detailW = w/6.0;
|
||||
detailHt = ht/8.0;
|
||||
shoeBulge = detailHt*2.0;
|
||||
legGap = w/7.0;
|
||||
speedX = random(-speedX, speedX);
|
||||
}
|
||||
|
||||
// Advanced constructor
|
||||
Legs(float x, float z, float w, float ht, color col, float detailW,
|
||||
float detailHt, float shoeBulge, float legGap) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.ht = ht;
|
||||
this.col = col;
|
||||
this.detailW = detailW;
|
||||
this.detailHt = detailHt;
|
||||
this.shoeBulge = shoeBulge;
|
||||
this.legGap = legGap;
|
||||
speedX = random(-speedX, speedX);
|
||||
}
|
||||
|
||||
// Draw legs
|
||||
void create() {
|
||||
fill(col);
|
||||
float footWidth = (w - legGap)/2;
|
||||
beginShape();
|
||||
vertex(x - w/2, y - ht, z);
|
||||
vertex(x - w/2, y - ht + detailHt, z);
|
||||
vertex(x - w/2 + detailW, y - ht + detailHt, z);
|
||||
// left foot
|
||||
vertex(x - w/2 + detailW, y + stepL, z);
|
||||
curveVertex(x - w/2 + detailW, y + stepL, z);
|
||||
curveVertex(x - w/2 + detailW, y + stepL, z);
|
||||
curveVertex(x - w/2 + detailW - shoeBulge, y + detailHt/2 + stepL, z);
|
||||
curveVertex(x - w/2, y + detailHt + stepL, z);
|
||||
curveVertex(x - w/2, y + detailHt + stepL, z);
|
||||
vertex(x - w/2 + footWidth, y + detailHt + stepL*.9, z);
|
||||
// end left foot
|
||||
vertex(x - w/2 + footWidth + legGap/2, y - ht + detailHt, z);
|
||||
vertex(x - w/2 + footWidth + legGap/2, y - ht + detailHt, z);
|
||||
// right foot
|
||||
vertex(x - w/2 + footWidth + legGap, y + detailHt + stepR*.9, z);
|
||||
vertex(x + w/2, y + detailHt + stepR, z);
|
||||
curveVertex(x + w/2, y + detailHt + stepR, z);
|
||||
curveVertex(x + w/2, y + detailHt + stepR, z);
|
||||
curveVertex(x + w/2 - detailW + shoeBulge, y + detailHt/2 + stepR, z);
|
||||
curveVertex(x + w/2 - detailW, y + stepR, z);
|
||||
vertex(x + w/2 - detailW, y + stepR, z);
|
||||
// end right foot
|
||||
vertex(x + w/2 - detailW, y - ht + detailHt, z);
|
||||
vertex(x + w/2, y - ht + detailHt, z);
|
||||
vertex(x + w/2, y - ht, z);
|
||||
endShape(CLOSE);
|
||||
}
|
||||
|
||||
// Set advanced property values
|
||||
void setDetails(float detailW, float detailHt, float shoeBulge, float legGap) {
|
||||
this.detailW = detailW;
|
||||
this.detailHt = detailHt;
|
||||
this.shoeBulge = shoeBulge;
|
||||
this.legGap = legGap;
|
||||
}
|
||||
|
||||
// Make the legs step
|
||||
void step(float stepRate) {
|
||||
this.stepRate = stepRate;
|
||||
spring = ht/2.0;
|
||||
stepL = sin(theta)*spring;
|
||||
stepR = cos(theta)*spring;
|
||||
theta += radians(stepRate);
|
||||
}
|
||||
|
||||
// Alternative overloaded step method
|
||||
void step() {
|
||||
spring = ht/2.0;
|
||||
stepL = sin(theta)*spring;
|
||||
stepR = cos(theta)*spring;
|
||||
theta += radians(stepRate);
|
||||
}
|
||||
|
||||
|
||||
// Moves legs along x, y, z axes
|
||||
void move() {
|
||||
// Move legs along y-axis
|
||||
y = stepR*damping;
|
||||
|
||||
// Move legs along x-axis and
|
||||
// check for collision against frame edge
|
||||
x += speedX;
|
||||
if (screenX(x, y, z) > width) {
|
||||
speedX *= -1;
|
||||
}
|
||||
if (screenX(x, y, z) < 0) {
|
||||
speedX *= -1;
|
||||
}
|
||||
|
||||
// Move legs along z-axis based on speed of stepping
|
||||
// and check for collision against extremes
|
||||
speedZ = (stepRate*velocity);
|
||||
z += speedZ;
|
||||
if (z > 400) {
|
||||
z = 400;
|
||||
velocity *= -1;
|
||||
}
|
||||
if (z < -100) {
|
||||
z = -100;
|
||||
velocity *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
void setDynamics(float speedX, float spring, float damping) {
|
||||
this.speedX = speedX;
|
||||
this.spring = spring;
|
||||
this.damping = damping;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Run-Amuck
|
||||
* By Ira Greenberg <br />
|
||||
* Processing for Flash Developers,
|
||||
* Friends of ED, 2009
|
||||
*/
|
||||
|
||||
int count = 250;
|
||||
Legs[] legs = new Legs[count];
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
noStroke();
|
||||
for (int i = 0; i < legs.length; i++) {
|
||||
legs[i] = new Legs(random(-10, 10), random(-50, 150), random(.5, 5),
|
||||
random(.5, 5), color(random(255), random(255), random(255)));
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width/2, height/2);
|
||||
noStroke();
|
||||
fill(35);
|
||||
|
||||
// Draw ground plane
|
||||
beginShape();
|
||||
vertex(-width*2, 0, -1000);
|
||||
vertex(width*2, 0, -1000);
|
||||
vertex(width/2, height/2, 400);
|
||||
vertex(-width/2, height/2, 400);
|
||||
endShape(CLOSE);
|
||||
|
||||
// Update and draw the legs
|
||||
for (int i = 0; i < legs.length; i++) {
|
||||
legs[i].create();
|
||||
// Set foot step rate
|
||||
legs[i].step(random(10, 50));
|
||||
// Move legs along x, y, z axes
|
||||
// z-movement dependent upon step rate
|
||||
legs[i].move();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* Shape Transform
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Illustrates the geometric relationship
|
||||
* between Cube, Pyramid, Cone and
|
||||
* Cylinder 3D primitives.
|
||||
*
|
||||
* Instructions:<br />
|
||||
* Up Arrow - increases points<br />
|
||||
* Down Arrow - decreases points<br />
|
||||
* 'p' key toggles between cube/pyramid<br />
|
||||
*/
|
||||
|
||||
int pts = 4;
|
||||
float angle = 0;
|
||||
float radius = 99;
|
||||
float cylinderLength = 95;
|
||||
|
||||
//vertices
|
||||
PVector vertices[][];
|
||||
boolean isPyramid = false;
|
||||
|
||||
float angleInc;
|
||||
|
||||
void setup(){
|
||||
size(640, 360, P3D);
|
||||
noStroke();
|
||||
angleInc = PI/300.0;
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(170, 95, 95);
|
||||
lights();
|
||||
fill(255, 200, 200);
|
||||
translate(width/2, height/2);
|
||||
rotateX(frameCount * angleInc);
|
||||
rotateY(frameCount * angleInc);
|
||||
rotateZ(frameCount * angleInc);
|
||||
|
||||
// initialize vertex arrays
|
||||
vertices = new PVector[2][pts+1];
|
||||
|
||||
// fill arrays
|
||||
for (int i = 0; i < 2; i++){
|
||||
angle = 0;
|
||||
for(int j = 0; j <= pts; j++){
|
||||
vertices[i][j] = new PVector();
|
||||
if (isPyramid){
|
||||
if (i==1){
|
||||
vertices[i][j].x = 0;
|
||||
vertices[i][j].y = 0;
|
||||
}
|
||||
else {
|
||||
vertices[i][j].x = cos(radians(angle)) * radius;
|
||||
vertices[i][j].y = sin(radians(angle)) * radius;
|
||||
}
|
||||
}
|
||||
else {
|
||||
vertices[i][j].x = cos(radians(angle)) * radius;
|
||||
vertices[i][j].y = sin(radians(angle)) * radius;
|
||||
}
|
||||
vertices[i][j].z = cylinderLength;
|
||||
// the .0 after the 360 is critical
|
||||
angle += 360.0/pts;
|
||||
}
|
||||
cylinderLength *= -1;
|
||||
}
|
||||
|
||||
// draw cylinder tube
|
||||
beginShape(QUAD_STRIP);
|
||||
for(int j = 0; j <= pts; j++){
|
||||
vertex(vertices[0][j].x, vertices[0][j].y, vertices[0][j].z);
|
||||
vertex(vertices[1][j].x, vertices[1][j].y, vertices[1][j].z);
|
||||
}
|
||||
endShape();
|
||||
|
||||
//draw cylinder ends
|
||||
for (int i = 0; i < 2; i++){
|
||||
beginShape();
|
||||
for(int j = 0; j < pts; j++){
|
||||
vertex(vertices[i][j].x, vertices[i][j].y, vertices[i][j].z);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
up/down arrow keys control
|
||||
polygon detail.
|
||||
*/
|
||||
void keyPressed(){
|
||||
if(key == CODED) {
|
||||
// pts
|
||||
if (keyCode == UP) {
|
||||
if (pts < 90){
|
||||
pts++;
|
||||
}
|
||||
}
|
||||
else if (keyCode == DOWN) {
|
||||
if (pts > 4){
|
||||
pts--;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (key =='p'){
|
||||
if (isPyramid){
|
||||
isPyramid = false;
|
||||
}
|
||||
else {
|
||||
isPyramid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
|
||||
class Cube {
|
||||
|
||||
// Properties
|
||||
int w, h, d;
|
||||
int shiftX, shiftY, shiftZ;
|
||||
|
||||
// Constructor
|
||||
Cube(int w, int h, int d, int shiftX, int shiftY, int shiftZ){
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
this.d = d;
|
||||
this.shiftX = shiftX;
|
||||
this.shiftY = shiftY;
|
||||
this.shiftZ = shiftZ;
|
||||
}
|
||||
|
||||
// Main cube drawing method, which looks
|
||||
// more confusing than it really is. It's
|
||||
// just a bunch of rectangles drawn for
|
||||
// each cube face
|
||||
void drawCube(){
|
||||
beginShape(QUADS);
|
||||
// Front face
|
||||
normal(0, 0, 1);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Back face
|
||||
normal(0, 0, -1);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
|
||||
// Left face
|
||||
normal(1, 0, 0);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Right face
|
||||
normal(-1, 0, 0);
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Top face
|
||||
normal(0, 1, 0);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
|
||||
// Bottom face
|
||||
normal(0, -1, 0);
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
|
||||
endShape();
|
||||
|
||||
// Add some rotation to each box for pizazz.
|
||||
rotateY(radians(1));
|
||||
rotateX(radians(1));
|
||||
rotateZ(radians(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Space Junk
|
||||
* by Ira Greenberg.
|
||||
* Zoom suggestion
|
||||
* by Danny Greenberg.
|
||||
*
|
||||
* Rotating cubes in space using a custom Cube class.
|
||||
* Color controlled by light sources. Move the mouse left
|
||||
* and right to zoom.
|
||||
*/
|
||||
|
||||
// Used for oveall rotation
|
||||
float ang;
|
||||
|
||||
// Cube count-lower/raise to test P3D/OPENGL performance
|
||||
int limit = 500;
|
||||
|
||||
// Array for all cubes
|
||||
Cube[]cubes = new Cube[limit];
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, P3D);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Instantiate cubes, passing in random vals for size and postion
|
||||
for (int i = 0; i< cubes.length; i++){
|
||||
cubes[i] = new Cube(int(random(-10, 10)), int(random(-10, 10)),
|
||||
int(random(-10, 10)), int(random(-140, 140)), int(random(-140, 140)),
|
||||
int(random(-140, 140)));
|
||||
}
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(0);
|
||||
fill(200);
|
||||
|
||||
// Set up some different colored lights
|
||||
pointLight(51, 102, 255, 65, 60, 100);
|
||||
pointLight(200, 40, 60, -65, -60, -150);
|
||||
|
||||
// Raise overall light in scene
|
||||
ambientLight(70, 70, 10);
|
||||
|
||||
// Center geometry in display windwow.
|
||||
// you can change 3rd argument ('0')
|
||||
// to move block group closer(+)/further(-)
|
||||
translate(width/2, height/2, -200 + mouseX * 0.65);
|
||||
|
||||
// Rotate around y and x axes
|
||||
rotateY(radians(ang));
|
||||
rotateX(radians(ang));
|
||||
|
||||
// Draw cubes
|
||||
for (int i = 0; i < cubes.length; i++){
|
||||
cubes[i].drawCube();
|
||||
}
|
||||
|
||||
// Used in rotate function calls above
|
||||
ang++;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* Textured Sphere
|
||||
* by Mike 'Flux' Chang (cleaned up by Aaron Koblin).
|
||||
* Based on code by Toxi.
|
||||
*
|
||||
* A 3D textured sphere with simple rotation control.
|
||||
* Note: Controls will be inverted when sphere is upside down.
|
||||
* Use an "arc ball" to deal with this appropriately.
|
||||
*/
|
||||
|
||||
PImage bg;
|
||||
PImage texmap;
|
||||
|
||||
int sDetail = 35; // Sphere detail setting
|
||||
float rotationX = 0;
|
||||
float rotationY = 0;
|
||||
float velocityX = 0;
|
||||
float velocityY = 0;
|
||||
float globeRadius = 450;
|
||||
float pushBack = 0;
|
||||
|
||||
float[] cx, cz, sphereX, sphereY, sphereZ;
|
||||
float sinLUT[];
|
||||
float cosLUT[];
|
||||
float SINCOS_PRECISION = 0.5;
|
||||
int SINCOS_LENGTH = int(360.0 / SINCOS_PRECISION);
|
||||
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, P3D);
|
||||
texmap = loadImage("world32k.jpg");
|
||||
initializeSphere(sDetail);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
renderGlobe();
|
||||
}
|
||||
|
||||
void renderGlobe() {
|
||||
pushMatrix();
|
||||
translate(width/2.0, height/2.0, pushBack);
|
||||
pushMatrix();
|
||||
noFill();
|
||||
stroke(255,200);
|
||||
strokeWeight(2);
|
||||
smooth();
|
||||
popMatrix();
|
||||
lights();
|
||||
pushMatrix();
|
||||
rotateX( radians(-rotationX) );
|
||||
rotateY( radians(270 - rotationY) );
|
||||
fill(200);
|
||||
noStroke();
|
||||
textureMode(IMAGE);
|
||||
texturedSphere(globeRadius, texmap);
|
||||
popMatrix();
|
||||
popMatrix();
|
||||
rotationX += velocityX;
|
||||
rotationY += velocityY;
|
||||
velocityX *= 0.95;
|
||||
velocityY *= 0.95;
|
||||
|
||||
// Implements mouse control (interaction will be inverse when sphere is upside down)
|
||||
if(mousePressed){
|
||||
velocityX += (mouseY-pmouseY) * 0.01;
|
||||
velocityY -= (mouseX-pmouseX) * 0.01;
|
||||
}
|
||||
}
|
||||
|
||||
void initializeSphere(int res)
|
||||
{
|
||||
sinLUT = new float[SINCOS_LENGTH];
|
||||
cosLUT = new float[SINCOS_LENGTH];
|
||||
|
||||
for (int i = 0; i < SINCOS_LENGTH; i++) {
|
||||
sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
|
||||
cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
|
||||
}
|
||||
|
||||
float delta = (float)SINCOS_LENGTH/res;
|
||||
float[] cx = new float[res];
|
||||
float[] cz = new float[res];
|
||||
|
||||
// Calc unit circle in XZ plane
|
||||
for (int i = 0; i < res; i++) {
|
||||
cx[i] = -cosLUT[(int) (i*delta) % SINCOS_LENGTH];
|
||||
cz[i] = sinLUT[(int) (i*delta) % SINCOS_LENGTH];
|
||||
}
|
||||
|
||||
// Computing vertexlist vertexlist starts at south pole
|
||||
int vertCount = res * (res-1) + 2;
|
||||
int currVert = 0;
|
||||
|
||||
// Re-init arrays to store vertices
|
||||
sphereX = new float[vertCount];
|
||||
sphereY = new float[vertCount];
|
||||
sphereZ = new float[vertCount];
|
||||
float angle_step = (SINCOS_LENGTH*0.5f)/res;
|
||||
float angle = angle_step;
|
||||
|
||||
// Step along Y axis
|
||||
for (int i = 1; i < res; i++) {
|
||||
float curradius = sinLUT[(int) angle % SINCOS_LENGTH];
|
||||
float currY = -cosLUT[(int) angle % SINCOS_LENGTH];
|
||||
for (int j = 0; j < res; j++) {
|
||||
sphereX[currVert] = cx[j] * curradius;
|
||||
sphereY[currVert] = currY;
|
||||
sphereZ[currVert++] = cz[j] * curradius;
|
||||
}
|
||||
angle += angle_step;
|
||||
}
|
||||
sDetail = res;
|
||||
}
|
||||
|
||||
// Generic routine to draw textured sphere
|
||||
void texturedSphere(float r, PImage t)
|
||||
{
|
||||
int v1,v11,v2;
|
||||
r = (r + 240 ) * 0.33;
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
float iu=(float)(t.width-1)/(sDetail);
|
||||
float iv=(float)(t.height-1)/(sDetail);
|
||||
float u=0,v=iv;
|
||||
for (int i = 0; i < sDetail; i++) {
|
||||
vertex(0, -r, 0,u,0);
|
||||
vertex(sphereX[i]*r, sphereY[i]*r, sphereZ[i]*r, u, v);
|
||||
u+=iu;
|
||||
}
|
||||
vertex(0, -r, 0,u,0);
|
||||
vertex(sphereX[0]*r, sphereY[0]*r, sphereZ[0]*r, u, v);
|
||||
endShape();
|
||||
|
||||
// Middle rings
|
||||
int voff = 0;
|
||||
for(int i = 2; i < sDetail; i++) {
|
||||
v1=v11=voff;
|
||||
voff += sDetail;
|
||||
v2=voff;
|
||||
u=0;
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
for (int j = 0; j < sDetail; j++) {
|
||||
vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1++]*r, u, v);
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2++]*r, u, v+iv);
|
||||
u+=iu;
|
||||
}
|
||||
|
||||
// Close each ring
|
||||
v1=v11;
|
||||
v2=voff;
|
||||
vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1]*r, u, v);
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v+iv);
|
||||
endShape();
|
||||
v+=iv;
|
||||
}
|
||||
u=0;
|
||||
|
||||
// Add the northern cap
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
for (int i = 0; i < sDetail; i++) {
|
||||
v2 = voff + i;
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v);
|
||||
vertex(0, r, 0,u,v+iv);
|
||||
u+=iu;
|
||||
}
|
||||
vertex(sphereX[voff]*r, sphereY[voff]*r, sphereZ[voff]*r, u, v);
|
||||
endShape();
|
||||
|
||||
}
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* Interactive Toroid
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Illustrates the geometric relationship between Toroid, Sphere, and Helix
|
||||
* 3D primitives, as well as lathing principal.
|
||||
*
|
||||
* Instructions: <br />
|
||||
* UP arrow key pts++ <br />
|
||||
* DOWN arrow key pts-- <br />
|
||||
* LEFT arrow key segments-- <br />
|
||||
* RIGHT arrow key segments++ <br />
|
||||
* 'a' key toroid radius-- <br />
|
||||
* 's' key toroid radius++ <br />
|
||||
* 'z' key initial polygon radius-- <br />
|
||||
* 'x' key initial polygon radius++ <br />
|
||||
* 'w' key toggle wireframe/solid shading <br />
|
||||
* 'h' key toggle sphere/helix <br />
|
||||
*/
|
||||
|
||||
int pts = 40;
|
||||
float angle = 0;
|
||||
float radius = 60.0;
|
||||
|
||||
// lathe segments
|
||||
int segments = 60;
|
||||
float latheAngle = 0;
|
||||
float latheRadius = 100.0;
|
||||
|
||||
//vertices
|
||||
PVector vertices[], vertices2[];
|
||||
|
||||
// for shaded or wireframe rendering
|
||||
boolean isWireFrame = false;
|
||||
|
||||
// for optional helix
|
||||
boolean isHelix = false;
|
||||
float helixOffset = 5.0;
|
||||
|
||||
void setup(){
|
||||
size(640, 360, P3D);
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(50, 64, 42);
|
||||
// basic lighting setup
|
||||
lights();
|
||||
// 2 rendering styles
|
||||
// wireframe or solid
|
||||
if (isWireFrame){
|
||||
stroke(255, 255, 150);
|
||||
noFill();
|
||||
}
|
||||
else {
|
||||
noStroke();
|
||||
fill(150, 195, 125);
|
||||
}
|
||||
//center and spin toroid
|
||||
translate(width/2, height/2, -100);
|
||||
|
||||
rotateX(frameCount*PI/150);
|
||||
rotateY(frameCount*PI/170);
|
||||
rotateZ(frameCount*PI/90);
|
||||
|
||||
// initialize point arrays
|
||||
vertices = new PVector[pts+1];
|
||||
vertices2 = new PVector[pts+1];
|
||||
|
||||
// fill arrays
|
||||
for(int i=0; i<=pts; i++){
|
||||
vertices[i] = new PVector();
|
||||
vertices2[i] = new PVector();
|
||||
vertices[i].x = latheRadius + sin(radians(angle))*radius;
|
||||
if (isHelix){
|
||||
vertices[i].z = cos(radians(angle))*radius-(helixOffset*
|
||||
segments)/2;
|
||||
}
|
||||
else{
|
||||
vertices[i].z = cos(radians(angle))*radius;
|
||||
}
|
||||
angle+=360.0/pts;
|
||||
}
|
||||
|
||||
// draw toroid
|
||||
latheAngle = 0;
|
||||
for(int i=0; i<=segments; i++){
|
||||
beginShape(QUAD_STRIP);
|
||||
for(int j=0; j<=pts; j++){
|
||||
if (i>0){
|
||||
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
|
||||
}
|
||||
vertices2[j].x = cos(radians(latheAngle))*vertices[j].x;
|
||||
vertices2[j].y = sin(radians(latheAngle))*vertices[j].x;
|
||||
vertices2[j].z = vertices[j].z;
|
||||
// optional helix offset
|
||||
if (isHelix){
|
||||
vertices[j].z+=helixOffset;
|
||||
}
|
||||
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
|
||||
}
|
||||
// create extra rotation for helix
|
||||
if (isHelix){
|
||||
latheAngle+=720.0/segments;
|
||||
}
|
||||
else {
|
||||
latheAngle+=360.0/segments;
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
left/right arrow keys control ellipse detail
|
||||
up/down arrow keys control segment detail.
|
||||
'a','s' keys control lathe radius
|
||||
'z','x' keys control ellipse radius
|
||||
'w' key toggles between wireframe and solid
|
||||
'h' key toggles between toroid and helix
|
||||
*/
|
||||
void keyPressed(){
|
||||
if(key == CODED) {
|
||||
// pts
|
||||
if (keyCode == UP) {
|
||||
if (pts<40){
|
||||
pts++;
|
||||
}
|
||||
}
|
||||
else if (keyCode == DOWN) {
|
||||
if (pts>3){
|
||||
pts--;
|
||||
}
|
||||
}
|
||||
// extrusion length
|
||||
if (keyCode == LEFT) {
|
||||
if (segments>3){
|
||||
segments--;
|
||||
}
|
||||
}
|
||||
else if (keyCode == RIGHT) {
|
||||
if (segments<80){
|
||||
segments++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// lathe radius
|
||||
if (key =='a'){
|
||||
if (latheRadius>0){
|
||||
latheRadius--;
|
||||
}
|
||||
}
|
||||
else if (key == 's'){
|
||||
latheRadius++;
|
||||
}
|
||||
// ellipse radius
|
||||
if (key =='z'){
|
||||
if (radius>10){
|
||||
radius--;
|
||||
}
|
||||
}
|
||||
else if (key == 'x'){
|
||||
radius++;
|
||||
}
|
||||
// wireframe
|
||||
if (key =='w'){
|
||||
if (isWireFrame){
|
||||
isWireFrame=false;
|
||||
}
|
||||
else {
|
||||
isWireFrame=true;
|
||||
}
|
||||
}
|
||||
// helix
|
||||
if (key =='h'){
|
||||
if (isHelix){
|
||||
isHelix=false;
|
||||
}
|
||||
else {
|
||||
isHelix=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Vertices
|
||||
* by Simon Greenwold.
|
||||
*
|
||||
* Draw a cylinder centered on the y-axis, going down
|
||||
* from y=0 to y=height. The radius at the top can be
|
||||
* different from the radius at the bottom, and the
|
||||
* number of sides drawn is variable.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
lights();
|
||||
translate(width / 2, height / 2);
|
||||
rotateY(map(mouseX, 0, width, 0, PI));
|
||||
rotateZ(map(mouseY, 0, height, 0, -PI));
|
||||
noStroke();
|
||||
fill(255, 255, 255);
|
||||
translate(0, -40, 0);
|
||||
drawCylinder(10, 180, 200, 16); // Draw a mix between a cylinder and a cone
|
||||
//drawCylinder(70, 70, 120, 64); // Draw a cylinder
|
||||
//drawCylinder(0, 180, 200, 4); // Draw a pyramid
|
||||
}
|
||||
|
||||
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
|
||||
float angle = 0;
|
||||
float angleIncrement = TWO_PI / sides;
|
||||
beginShape(QUAD_STRIP);
|
||||
for (int i = 0; i < sides + 1; ++i) {
|
||||
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
|
||||
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
|
||||
// If it is not a cone, draw the circular top cap
|
||||
if (topRadius != 0) {
|
||||
angle = 0;
|
||||
beginShape(TRIANGLE_FAN);
|
||||
|
||||
// Center point
|
||||
vertex(0, 0, 0);
|
||||
for (int i = 0; i < sides + 1; i++) {
|
||||
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
|
||||
// If it is not a cone, draw the circular bottom cap
|
||||
if (bottomRadius != 0) {
|
||||
angle = 0;
|
||||
beginShape(TRIANGLE_FAN);
|
||||
|
||||
// Center point
|
||||
vertex(0, tall, 0);
|
||||
for (int i = 0; i < sides + 1; i++) {
|
||||
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user