I whipped up this simple example, and I thought it could be included in the Processing distribution. It is an example of how to do 3D without a 3D renderer, for things that don't need a full blown 3D renderer. It's called Starmap because it reminds me of space themed RPG starmaps. If accepted, the code would be public domain. I think it should go under Basics->Transform, not under the 3D menu as it is not explaining how to use Processing's built in 3D code.
Code:/**
* Starmap
* by Jason Koenig.
*
* A rotatable view of a random starmap
* created using trigonometric functions
* to control projection. 3D effect with
* a 2D renderer. Shows how to click drag
* to rotate a 3D view.
*/
float theta = 0.0, phi = PI/4;//view variables: theta is vertical rotation around z-axis, while
//phi is angle between view and vertical.
float mapscale = 6.5;//the scale in screen space
Point origin = new Point(0,0,0); //the point the center the view on in world space
// origin = new Point(10,10,0);//try this instead
Point offset = new Point(100,100,0);//where on the screen to draw the screen.
Point[] points;//our star locations
void setup()
{
size(200,200);
points = new Point[20];//twenty stars
for (int i = 0; i < points.length; i++)
{
points[i] = new Point(random(-10,10),random(-10,10),random(-10,10));//generate a random map
}
ellipseMode(CENTER);
smooth();
noLoop();
}
void draw()
{
background(255);
for (int i = 0; i < points.length; i++)//for each point
{
Point p = project(points[i]);
stroke(0,0,0,50);
drawline(p,project(new Point(points[i].x,points[i].y,0)));//draw the drop down line
fill(0);
drawpoint(p);//draw the point itself
}
Point a = project(new Point(-10,10,0));
Point b = project(new Point(-10,-10,0));
Point c = project(new Point(10,-10,0));
Point d = project(new Point(10,10,0));
stroke(0,100,200,100);
drawline(a,b);//draw the base plane.
drawline(b,c);
drawline(c,d);
drawline(d,a);
}
int mouseXPrev, mouseYPrev;
void mouseMoved()//required for proper updating of the mouseXprev and mouseYPrev variables.
{
mouseXPrev = mouseX;
mouseYPrev = mouseY;
}
void mouseDragged()// handles rotation of the view
{
theta += -TWO_PI * float(mouseX-mouseXPrev)/width;
phi += PI * float(mouseY-mouseYPrev)/height;
mouseXPrev = mouseX;
mouseYPrev = mouseY;
redraw();//update the view
}
void drawpoint(Point p)
{
noStroke();
ellipse(p.x,p.y,4,4);
}
void drawline(Point a,Point b)
{
line(a.x,a.y,b.x,b.y);
}
Point project(Point in) //project from worldspace to screen space. simple ortho projection.
{
Point f = origin.to(in);
float xp = f.x*cos(theta) + f.y*sin(theta);
float yp = -f.x*sin(theta) + f.y*cos(theta);
yp *= cos(phi);
return new Point(offset.x+xp*mapscale,offset.y+yp*mapscale+f.z*mapscale*sin(phi),0.0);
}
class Point // simple container for 3D coordinates
{
public float x,y,z;
public Point() {x = y = z = 0.0;};
public Point(float _x, float _y, float _z) {x = _x; y = _y; z = _z;};
public Point to(Point p) {return new Point(p.x-x,p.y-y,p.z-z);};
}