The Actual Ray Tracer
Thread raytracer;
public void start()
{
if (raytracer == null) {
raytracer = new Thread(this);
raytracer.start();
} else {
raytracer.resume();
}
}
public void stop()
{
if (raytracer != null) {
raytracer.suspend();
}
}
private void renderPixel(int i, int j)
{
Vector3D dir = new Vector3D(
i*Du.x + j*Dv.x + Vp.x,
i*Du.y + j*Dv.y + Vp.y,
i*Du.z + j*Dv.z + Vp.z);
Ray ray = new Ray(eye, dir);
if (ray.trace(objectList)) {
gc.setColor(ray.Shade(lightList, objectList, background));
} else {
gc.setColor(background);
}
gc.drawLine(i, j, i, j); // oh well, it works.
}
public void run()
{
Graphics g = getGraphics();
long time = System.currentTimeMillis();
for (int j = 0; j < height; j++) {
showStatus("Scanline "+j);
for (int i = 0; i < width; i++) {
renderPixel(i, j);
}
g.drawImage(screen, 0, 0, this); // doing this less often speeds things up a bit
}
g.drawImage(screen, 0, 0, this);
time = System.currentTimeMillis() - time;
showStatus("Rendered in "+(time/60000)+":"+((time%60000)*0.001));
finished = true;
}
|