Scene.java
java · 121 lines · part of White Outline (Cartoon Shading) · raw
import java.util.Vector; // http://java.sun.com/products/jdk/1.1/docs/api/java.util.Vector.html
//---- Scene
public class Scene
{
//---- Data
//- Camera
private double camera_x=0, camera_y=0, camera_z=-8;
private double camera_rx=0, camera_ry=0;
//- Output buffer
private int[] buf = null;
private int w = 0, h = 0;
//- Shapes in this scene
private Vector shapes;
//- Transformation stack
private Transform[] trans;
private int numTransforms;
//---- Main code
public Scene()
{
shapes = new Vector();
trans = new Transform[32];
numTransforms = 0;
}
//---- addShape
public void addShape(Shape s)
{
shapes.addElement(s);
}
//---- setGraphics
public void setGraphics(int[] buf, int w, int h)
{
this.buf = buf; this.w = w; this.h = h;
}
//---- setCamera
public void setCamera(double x, double y, double z, double rx, double ry)
{
camera_x = x; camera_y = y; camera_z = z;
camera_rx = rx; camera_ry = ry;
}
//---- plotPoint
public void plotPoint(double x, double y, double z)
{
// Transform point
double[] t = {x, y, z, 1};
t = trans[numTransforms-1].multiply(t);
// Screen space point
double sx = w*0.5 + 150.0*t[0]/(t[2]);
double sy = h*0.5 + 150.0*t[1]/(t[2]);
// Plot
buf[(int)sx + w*(int)sy] = 0xFFFFFF;
}
//---- transform
public void transform(Transform t)
{
trans[numTransforms] = new Transform(trans[numTransforms-1]);
trans[numTransforms].transform(t);
numTransforms ++;
}
//---- untransform
public void untransform()
{
numTransforms --;
}
//---- render
public void render()
{
//- Set up transformation stack
trans[0] = new Transform();
trans[0].translate(-camera_x, -camera_y, -camera_z);
trans[0].rotateY(-camera_ry);
trans[0].rotateX(-camera_rx);
numTransforms = 1;
//- Render shapes
for(int i=0; i<shapes.size(); i++) ((Shape)shapes.elementAt(i)).render();
//- Done
}
}