/** * File: CheckerboardGenerator.java * Author: Marsette Vona * Created: 4/7/10 **/ import java.io.*; import static java.lang.Math.*; /** *

This program generates a checkerboard base plane in a format compatible * with CS4300 Homework 6: a ray tracer.

* *

The output is a white plane through the world frame origin perpendicular * to the world frame y axis, overlaid by a black checkerboard pattern. The * checkerboard pattern extends from -width/2 to +width/2 in world frame units * along the x axis, and from -height/2 to +height/2 in world frame units along * the z axis. There are nx columns and nz rows.

* *

To compile: javac CheckerboardGenerator.java

* *

To generate doc: javadoc -d doc CheckerboardGenerator.java

* *

To run: java CheckerboardGenerator nx nz width height * [firstVertexIndex]

* *

Note: this code may require Java 1.6 or greater.

* *

See the * assignment for the output format.

**/ public class CheckerboardGenerator { /** base plane material color greylevels **/ public static final double BASE_AI = 0.8, BASE_DI = 0.5, BASE_SI = 0.1; /** base plane material colors **/ public static final double[] BASE_AM = grey(BASE_AI), BASE_DM = grey(BASE_DI), BASE_SM = grey(BASE_SI); /** phong exponent for base plane **/ public static final int BASE_PHONG_EXP = 1; /** check material color greylevels **/ public static final double CHECK_AI = 0.0, CHECK_DI = 0.0, CHECK_SI = 0.1; /** check material colors **/ public static final double[] CHECK_AM = grey(CHECK_AI), CHECK_DM = grey(CHECK_DI), CHECK_SM = grey(CHECK_SI); /** phong exponent for checks **/ public static final int CHECK_PHONG_EXP = 1; /** y offset of checks **/ public static final double CHECK_OFFSET = 0.001; /** vertex component indices **/ public static final int X = 0, Y = 1, Z = 2; /** next vertex index to be printed **/ protected static int nextVertex = 0; /** see class header doc **/ public static void main(String[] arg) throws NumberFormatException { if (arg.length < 4) { System.err.println( "usage: java CheckerboardGenerator nx nz width height "+ "[firstVertexIndex]"); System.exit(1); } int nx = Integer.parseInt(arg[0]); int nz = Integer.parseInt(arg[1]); double w = Double.parseDouble(arg[2]); double h = Double.parseDouble(arg[3]); if (arg.length > 4) nextVertex = Integer.parseInt(arg[4]); //the white plane System.out.println("am "+string(BASE_AM)); System.out.println("dm "+string(BASE_DM)); System.out.println("sm "+string(BASE_SM)+" "+BASE_PHONG_EXP); System.out.println("ps "+printVertex(new double[] {0.0, 0.0, 0.0})); //the checks System.out.println("am "+string(CHECK_AM)); System.out.println("dm "+string(CHECK_DM)); System.out.println("sm "+string(CHECK_SM)+" "+CHECK_PHONG_EXP); double cw = w/((double) nx), ch = h/((double) nz); double[] v = new double[3]; v[Y] = CHECK_OFFSET; int a, b, c; for (int i = 0; i < nz; i++) { for (int j = 0; j < nx; j++) { if ((i%2)==(j%2)) { v[X] = -w/2+j*cw; v[Z] = -h/2+i*ch; a = printVertex(v); v[Z] += ch; b = printVertex(v); v[X] += cw; c = printVertex(v); System.out.println("ts "+a+" "+b+" "+c); b = a; a = c; c = b; //swap a and c v[Z] -= ch; b = printVertex(v); System.out.println("ts "+a+" "+b+" "+c); } } } } /** returns a grey color **/ public static double[] grey(double intensity) { return new double[] {intensity, intensity, intensity}; } /** converts v to a string **/ public static String string(double[] v) { return v[X]+" "+v[Y]+" "+v[Z]; } /** print a vertex **/ public static int printVertex(double[] v) { System.out.println("vv "+string(v)+" 0 1 0"); return nextVertex++; } }