From: <ma...@us...> - 2003-08-21 22:53:53
|
Update of /cvsroot/jrman/drafts/src/org/jrman/maps In directory sc8-pr-cvs1:/tmp/cvs-serv8189/src/org/jrman/maps Modified Files: ShadowMap.java Log Message: Improved shadows implementation Index: ShadowMap.java =================================================================== RCS file: /cvsroot/jrman/drafts/src/org/jrman/maps/ShadowMap.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ShadowMap.java 20 Aug 2003 20:41:21 -0000 1.2 --- ShadowMap.java 21 Aug 2003 22:53:46 -0000 1.3 *************** *** 37,53 **** import org.jrman.geom.PerspectiveTransform; import org.jrman.geom.Transform; public class ShadowMap { ! ! private static Point3f ptmp = new Point3f(); ! private static Map map = new HashMap(); ! private FloatBuffer data; ! private int width; ! private int height; ! private Transform worldToRaster; --- 37,56 ---- import org.jrman.geom.PerspectiveTransform; import org.jrman.geom.Transform; + import org.jrman.util.Calc; public class ShadowMap { ! ! private static Point3f ptmp1 = new Point3f(); ! ! private static Point3f ptmp2 = new Point3f(); ! private static Map map = new HashMap(); ! private FloatBuffer data; ! private int width; ! private int height; ! private Transform worldToRaster; *************** *** 109,149 **** int width, int height, ! float[] depths) throws IOException { ! Transform worldToRaster = cameraToRaster.concat(worldToCamera); ! FileOutputStream fos = new FileOutputStream(filename); ! BufferedOutputStream bos = new BufferedOutputStream(fos); ! DataOutputStream dos = new DataOutputStream(bos); ! if (worldToRaster.isPerspective()) ! dos.writeByte(Projection.getNamed("perspective").getCode()); ! else ! dos.writeByte(Projection.getNamed("orthographic").getCode()); ! worldToRaster.writeToFile(dos); ! dos.writeInt(width); ! dos.writeInt(height); ! for (int i = 0; i < depths.length; i++) ! dos.writeFloat(depths[i]); ! dos.flush(); ! dos.close(); ! fos.close(); ! } ! ! public static ShadowMap getShadowMap(String filename) { ! ShadowMap result = (ShadowMap) map.get(filename); ! if (result == null) ! try { ! result = new ShadowMap(filename); ! map.put(filename, result); ! } catch (Exception e) { ! System.err.println("Can't load shadow map: " + filename); ! } ! return result; ! } ! ! public static void flushShadowMap(String filename) { ! map.remove(filename); ! } ! ! private ShadowMap(String filename) throws IOException { try { FileInputStream fis = new FileInputStream(filename); DataInputStream dis = new DataInputStream(fis); --- 112,153 ---- int width, int height, ! float[] depths) ! throws IOException { ! Transform worldToRaster = cameraToRaster.concat(worldToCamera); ! FileOutputStream fos = new FileOutputStream(filename); ! BufferedOutputStream bos = new BufferedOutputStream(fos); ! DataOutputStream dos = new DataOutputStream(bos); ! if (worldToRaster.isPerspective()) ! dos.writeByte(Projection.getNamed("perspective").getCode()); ! else ! dos.writeByte(Projection.getNamed("orthographic").getCode()); ! worldToRaster.writeToFile(dos); ! dos.writeInt(width); ! dos.writeInt(height); ! for (int i = 0; i < depths.length; i++) ! dos.writeFloat(depths[i]); ! dos.flush(); ! dos.close(); ! fos.close(); ! } ! ! public static ShadowMap getShadowMap(String filename) { ! ShadowMap result = (ShadowMap) map.get(filename); ! if (result == null) try { + result = new ShadowMap(filename); + map.put(filename, result); + } catch (Exception e) { + System.err.println("Can't load shadow map: " + filename); + } + return result; + } + + public static void flushShadowMap(String filename) { + map.remove(filename); + } + + private ShadowMap(String filename) throws IOException { + try { FileInputStream fis = new FileInputStream(filename); DataInputStream dis = new DataInputStream(fis); *************** *** 162,182 **** ByteBuffer bf = fc.map(FileChannel.MapMode.READ_ONLY, offset, size); data = bf.asFloatBuffer(); ! } catch (IOException e) { ! e.printStackTrace(); ! throw e; ! } ! } ! ! private float get(int x, int y, float dist) { ! if (x < 0 || x >= width || y < 0 || y >= height) ! return 0f; ! float z = data.get(y * width + x); ! return (z < dist) ? 1f : 0f; } ! ! public float get(Point3f p, float bias) { ! worldToRaster.transformPoint(p, ptmp); ! return get((int) ptmp.x, (int) ptmp.y, ptmp.z - bias); } ! } --- 166,199 ---- ByteBuffer bf = fc.map(FileChannel.MapMode.READ_ONLY, offset, size); data = bf.asFloatBuffer(); ! } catch (IOException e) { ! e.printStackTrace(); ! throw e; } ! } ! ! private float get(int x, int y, float dist) { ! if (x < 0 || x >= width || y < 0 || y >= height) ! return 0f; ! float z = data.get(y * width + x); ! return (z < dist) ? 1f : 0f; ! } ! ! public float get(Point3f p1, Point3f p2, float bias, int samples, float blur) { ! worldToRaster.transformPoint(p1, ptmp1); ! worldToRaster.transformPoint(p2, ptmp2); ! float total = 0f; ! for (int i = 0; i < samples; i++) { ! int x = ! Math.round( ! Calc.interpolate(ptmp1.x, ptmp2.x, (float) Math.random()) ! + ((float) Math.random() - .5f) * blur); ! int y = ! Math.round( ! Calc.interpolate(ptmp1.y, ptmp2.y, (float) Math.random()) ! + ((float) Math.random() - .5f) * blur); ! total += get(x, y, ptmp1.z - bias); } ! return total / samples; } + + } |