From: <ma...@us...> - 2003-06-12 18:36:42
|
Update of /cvsroot/jrman/drafts/src/org/jrman/grid In directory sc8-pr-cvs1:/tmp/cvs-serv2346/src/org/jrman/grid Modified Files: Color3fGrid.java FloatGrid.java Log Message: Implemented mipmapped textures. Index: Color3fGrid.java =================================================================== RCS file: /cvsroot/jrman/drafts/src/org/jrman/grid/Color3fGrid.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Color3fGrid.java 6 Jun 2003 15:29:27 -0000 1.5 --- Color3fGrid.java 12 Jun 2003 18:36:08 -0000 1.6 *************** *** 21,64 **** import java.awt.image.BufferedImage; - import java.io.File; - import java.util.HashMap; - import java.util.Map; - import javax.imageio.ImageIO; import javax.vecmath.Color3f; ! import org.jrman.util.Calc; public class Color3fGrid extends Tuple3fGrid { - - private static Map textures = new HashMap(); - - private static BufferedImage getTexture(String name) { - BufferedImage texture = (BufferedImage) textures.get(name); - if (texture == null) { - try { - File file = new File(name); - texture = ImageIO.read(file); - textures.put(name, texture); - } catch (Exception e) { - System.err.println("Couldn't load texture " + name + ": " + e.toString()); - } - } - return texture; - } ! private static void getColor(BufferedImage texture, float s, float t, Color3f out) { ! int width = texture.getWidth(); ! int height = texture.getHeight(); ! int x = (int) Calc.clamp(s * width, 0f, width - 1); ! int y = (int) Calc.clamp(t * height, 0f, height -1); ! int argb = texture.getRGB(x, y); ! out.x = ((argb >> 16) & 0xff) / 255f; ! out.y = ((argb >> 8) & 0xff) / 255f; ! out.z = (argb & 0xff) / 255f; ! } ! public static void flush(String name) { ! textures.remove(name); } --- 21,45 ---- import java.awt.image.BufferedImage; import javax.vecmath.Color3f; ! import org.jrman.maps.MipMap; public class Color3fGrid extends Tuple3fGrid { ! private static float[] mmData = new float[4]; ! private static Color3f tmpc = new Color3f(); ! ! private static void getColor( ! MipMap texture, ! float s, ! float t, ! float area, ! Color3f out) { ! texture.getData(s, t, area, mmData); ! out.x = mmData[0]; ! out.y = mmData[1]; ! out.z = mmData[2]; } *************** *** 68,82 **** data[i] = new Color3f(); } ! public void texture(String textureName, FloatGrid s, FloatGrid t) { ! BufferedImage texture = getTexture(textureName); ! Color3f[] cdata = (Color3f[]) data; ! float[] sdata = s.data; ! float[] tdata = t.data; ! for (int i = 0; i < size; i++) { ! float ws = sdata[i] - (float) Math.floor(sdata[i]); ! float wt = tdata[i] - (float) Math.floor(tdata[i]); ! getColor(texture, ws, wt, cdata[i]); ! } } --- 49,73 ---- data[i] = new Color3f(); } ! ! /* ! * texture ! */ ! public void texture(String textureName, FloatGrid s, FloatGrid t) { ! MipMap texture = MipMap.getMipMap(textureName); ! for (int v = 0; v < vSize; v++) ! for (int u = 0; u < uSize; u++) { ! int u1 = u + 1; ! if (u1 == uSize) ! u1 = u - 1; ! int v1 = v + 1; ! if (v1 == vSize) ! v1 = v - 1; ! float area = ! Math.abs(s.get(u1, v1) - s.get(u, v)) ! * Math.abs(t.get(u1, v1) - t.get(u, v)); ! getColor(texture, s.get(u, v), t.get(u, v), area, tmpc); ! set(u, v, tmpc); ! } } Index: FloatGrid.java =================================================================== RCS file: /cvsroot/jrman/drafts/src/org/jrman/grid/FloatGrid.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** FloatGrid.java 6 Jun 2003 15:29:27 -0000 1.11 --- FloatGrid.java 12 Jun 2003 18:36:08 -0000 1.12 *************** *** 21,68 **** import java.awt.image.BufferedImage; - import java.io.File; - import java.util.HashMap; - import java.util.Map; - import javax.imageio.ImageIO; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; import org.jrman.util.Calc; public class FloatGrid extends Grid { ! public float[] data; ! ! private static Map textures = new HashMap(); ! private static BufferedImage getTexture(String name) { ! BufferedImage texture = (BufferedImage) textures.get(name); ! if (texture == null) { ! try { ! File file = new File(name); ! texture = ImageIO.read(file); ! textures.put(name, texture); ! } catch (Exception e) { ! System.err.println("Couldn't load texture " + name + ": " + e.toString()); ! } ! } ! return texture; ! } ! ! private static float getFloat(BufferedImage texture, float s, float t) { ! int width = texture.getWidth(); ! int height = texture.getHeight(); ! int x = (int) Calc.clamp(s * width, 0f, width - 1); ! int y = (int) Calc.clamp(t * height, 0f, height -1); ! int argb = texture.getRGB(x, y); ! float r = ((argb >> 16) & 0xff) / 255f; ! float g= ((argb >> 8) & 0xff) / 255f; ! float b = (argb & 0xff) / 255f; ! return (r + g + b) / 3f; ! } ! public static void flush(String name) { ! textures.remove(name); } --- 21,40 ---- import java.awt.image.BufferedImage; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; + import org.jrman.maps.MipMap; import org.jrman.util.Calc; public class FloatGrid extends Grid { ! private static float[] mmData = new float[4]; ! public float[] data; ! private static float getFloat(MipMap texture, float s, float t, float area) { ! texture.getData(s, t, area, mmData); ! return (mmData[0] + mmData[1] + mmData[2]) / 3f; } *************** *** 70,74 **** data = new float[maxSize]; } ! public float get(int i) { return data[i]; --- 42,46 ---- data = new float[maxSize]; } ! public float get(int i) { return data[i]; *************** *** 1210,1218 **** } ! public void depth( ! Point3fGrid p, ! float near, ! float far, ! BooleanGrid cond) { Point3f[] pdata = (Point3f[]) p.data; for (int i = 0; i < size; i++) --- 1182,1186 ---- } ! public void depth(Point3fGrid p, float near, float far, BooleanGrid cond) { Point3f[] pdata = (Point3f[]) p.data; for (int i = 0; i < size; i++) *************** *** 1220,1224 **** data[i] = Calc.depth(pdata[i], near, far); } ! /* * texture --- 1188,1192 ---- data[i] = Calc.depth(pdata[i], near, far); } ! /* * texture *************** *** 1226,1237 **** public void texture(String textureName, FloatGrid s, FloatGrid t) { ! BufferedImage texture = getTexture(textureName); ! float[] sdata = s.data; ! float[] tdata = t.data; ! for (int i = 0; i < size; i++) { ! float ws = sdata[i] - (float) Math.floor(sdata[i]); ! float wt = tdata[i] - (float) Math.floor(tdata[i]); ! data[i] = getFloat(texture, ws, wt); ! } } --- 1194,1211 ---- public void texture(String textureName, FloatGrid s, FloatGrid t) { ! MipMap texture = MipMap.getMipMap(textureName); ! for (int v = 0; v < vSize; v++) ! for (int u = 0; u < uSize; u++) { ! int u1 = u + 1; ! if (u1 == uSize) ! u1 = u - 1; ! int v1 = v + 1; ! if (v1 == vSize) ! v1 = v - 1; ! float area = ! Math.abs(s.get(u1, v) - s.get(u, v)) ! * Math.abs(t.get(u, v1) - t.get(u, v)); ! set(u, v, getFloat(texture, s.get(u, v), t.get(u, v), area)); ! } } |