rayTrace.java
package com.gwief.jTrace;
/* rayTrace by Damian Newport
* version 0.5
*/
import java.awt.*;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
import java.util.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class rayTrace extends javax.swing.JApplet implements MouseListener {
/* constants */
final static double INFINITY = Double.POSITIVE_INFINITY;
final static double NINFINITY = Double.NEGATIVE_INFINITY;
final static double MAX = 2000;
final static double MIN = -2000;
final static double TINY = 0.00000001;
final static double NTINY = -0.0000001;
final static int DEPTH = 10; /* max Ray recursion depth */
/* flags */
static boolean bugmode = false;
boolean render = false;
boolean redraw = true;
/* model */
Scene3D scene;
/* rendering */
Image picture;
int width;
int height;
/* strings */
String shape = "";
String rendertime = "";
static String error = "";
public void init () {
String input = getParameter("file");
if (input == null){
input = "none";
error = "no imput file specified";
}
width = getSize().width;
height = getSize().height;
scene = new Scene3D();
CSGparser parser = new CSGparser(scene, width, height);
if (parser.parse(input)){
rerender();
render = true;
addMouseListener(this);
}else{
error = parser.error;
}
/* debug mode */
String debug = getParameter("debug");
if (debug != null){
bugmode = true;
}
}
public void rerender(){
Date date1 = new Date();
picture = render();
Date date2 = new Date();
rendertime = "render took " + (date2.getTime()-date1.getTime()) + " ms";
redraw = false;
}
public void paint (Graphics screen){
super.paint(screen);
Graphics2D screen2D = (Graphics2D)screen;
if (render == true){
screen2D.drawImage(picture, 0, 0, width, height, this);
screen2D.setColor(Color.white);
screen2D.drawString(rendertime,5,(height - 5));
screen2D.drawString(shape,5,15);
}else{
screen2D.setBackground(Color.black);
screen2D.clearRect(0,0,width,height);
screen2D.setColor(Color.white);
screen2D.drawString(error,5,15);
}
}
public void update(Graphics screen) {
if (redraw){
rerender();
}
paint(screen);
}
Image render(){
int pixels[] = new int[width * height];
int color[] = new int[4];
color[3] = 255;
int index = 0;
Color3D eek = new Color3D();
Ray RayA = new Ray();
scene.build();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
RayA = scene.camera.getRay(x,y);
eek = RayA.trace(scene,DEPTH);
/* DEBUG */
/*
if ((RayA.direction.x == 0) && (RayA.direction.y == 0)){
eek = new Color3D(1,1,1);
}
*/
color[0] = (int)(255 * eek.r);
color[1] = (int)(255 * eek.g);
color[2] = (int)(255 * eek.b);
pixels[index++] = ((color[3] << 24) |
(color[0] << 16) |
(color[1] << 8) |
(color[2] << 0));
}
}
return createImage(new MemoryImageSource(width, height,
ColorModel.getRGBdefault(), pixels, 0, width));
}
public void destroy() {
removeMouseListener(this);
}
public void mouseReleased(final java.awt.event.MouseEvent p1) {
}
public void mouseEntered(final java.awt.event.MouseEvent p1) {
}
public void mouseClicked(final java.awt.event.MouseEvent p1) {
}
public void mouseExited(final java.awt.event.MouseEvent p1) {
}
public void mousePressed(final java.awt.event.MouseEvent p1) {
if (bugmode){
Ray RayB = scene.camera.getRay(p1.getX(), p1.getY());
System.out.println("=================================================================================");
System.out.println("============================ DEBUG =============================");
System.out.println("=================================================================================");
Color3D eek = RayB.debug(scene, DEPTH);
System.out.println("color = " + eek.out());
}else{
Ray RayB = scene.camera.getRay(p1.getX(), p1.getY());
Intersection thisint = RayB.traceRay(scene);
if (thisint != null){
shape = thisint.hit.name;
Color3D thiscolor = thisint.hit.material.color;
if (thiscolor.r == 1){
thiscolor.setcolor(0,1,0);
}else{
thiscolor.setcolor(1,0,0);
}
redraw = true;
}else{
shape = "none";
}
repaint();
}
}
}