.. | .. |
---|
18 | 18 | import javax.imageio.ImageIO; |
---|
19 | 19 | import javax.imageio.ImageWriteParam; |
---|
20 | 20 | import javax.imageio.ImageWriter; |
---|
| 21 | +import javax.imageio.ImageReadParam; |
---|
| 22 | +import javax.imageio.ImageReader; |
---|
21 | 23 | import javax.imageio.plugins.jpeg.JPEGImageWriteParam; |
---|
| 24 | +import javax.imageio.stream.ImageInputStream; |
---|
22 | 25 | import javax.imageio.stream.ImageOutputStream; |
---|
23 | 26 | import javax.imageio.ImageWriteParam; |
---|
24 | 27 | |
---|
.. | .. |
---|
30 | 33 | import java.nio.*; |
---|
31 | 34 | |
---|
32 | 35 | import gleem.linalg.Mat4f; |
---|
| 36 | +import javax.imageio.ImageTypeSpecifier; |
---|
33 | 37 | |
---|
34 | 38 | class CameraPane extends GLCanvas implements iCameraPane, Runnable, GLEventListener, ActionListener, MouseWheelListener, MouseMotionListener, MouseListener, KeyListener |
---|
35 | 39 | { |
---|
.. | .. |
---|
44 | 48 | static boolean ABORTED = false; |
---|
45 | 49 | |
---|
46 | 50 | static int STEP = 1; |
---|
| 51 | + |
---|
| 52 | + private static BufferedImage CreateBim(byte[] bytes, int width, int height) |
---|
| 53 | + { |
---|
| 54 | + int[] pixels = new int[bytes.length/3]; |
---|
| 55 | + for (int i=pixels.length; --i>=0;) |
---|
| 56 | + { |
---|
| 57 | + int i3 = i*3; |
---|
| 58 | + pixels[i] = 0xFF; |
---|
| 59 | + pixels[i] <<= 8; |
---|
| 60 | + pixels[i] |= bytes[i3+2] & 0xFF; |
---|
| 61 | + pixels[i] <<= 8; |
---|
| 62 | + pixels[i] |= bytes[i3+1] & 0xFF; |
---|
| 63 | + pixels[i] <<= 8; |
---|
| 64 | + pixels[i] |= bytes[i3] & 0xFF; |
---|
| 65 | + } |
---|
| 66 | + /* |
---|
| 67 | + int r=0,g=0,b=0,a=0; |
---|
| 68 | + for (int i=0; i<width; i++) |
---|
| 69 | + for (int j=0; j<height; j++) |
---|
| 70 | + { |
---|
| 71 | + int index = j*width+i; |
---|
| 72 | + int p = pixels[index]; |
---|
| 73 | + a = ((p>>24) & 0xFF); |
---|
| 74 | + r = ((p>>16) & 0xFF); |
---|
| 75 | + g = ((p>>8) & 0xFF); |
---|
| 76 | + b = (p & 0xFF); |
---|
| 77 | + pixels[index] = (a<<24) | (b<<16) | (g<<8) | r; |
---|
| 78 | + } |
---|
| 79 | + /**/ |
---|
| 80 | + BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile); |
---|
| 81 | + rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width); |
---|
| 82 | + return rendImage; |
---|
| 83 | + } |
---|
47 | 84 | |
---|
48 | 85 | /*static*/ private boolean CULLFACE = false; // true; |
---|
49 | 86 | /*static*/ boolean NEAREST = false; // true; |
---|
.. | .. |
---|
60 | 97 | //boolean REDUCETEXTURE = true; |
---|
61 | 98 | boolean CACHETEXTURE = true; |
---|
62 | 99 | boolean CLEANCACHE = false; // true; |
---|
63 | | - boolean MIPMAP = true; // false; // true; |
---|
| 100 | + boolean MIPMAP = false; // true; // never works... |
---|
64 | 101 | boolean COMPRESSTEXTURE = false; |
---|
65 | 102 | boolean KOMPACTTEXTURE = false; // true; |
---|
66 | 103 | boolean RESIZETEXTURE = false; |
---|
.. | .. |
---|
2405 | 2442 | return currentGL; |
---|
2406 | 2443 | } |
---|
2407 | 2444 | |
---|
2408 | | - private BufferedImage CreateBim(TextureData texturedata) |
---|
| 2445 | + static private BufferedImage CreateBim(TextureData texturedata) |
---|
2409 | 2446 | { |
---|
2410 | | - // cache to disk |
---|
2411 | | - Buffer buffer = texturedata.getBuffer(); // getMipmapData(); |
---|
2412 | | - //buffers[0]. |
---|
2413 | | - ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer(); |
---|
2414 | | - int[] pixels = new int[bytebuf.capacity()/3]; |
---|
2415 | | - int width = texturedata.getWidth(); //(int)Math.sqrt(pixels.length); // squared |
---|
2416 | | - int height = width; |
---|
2417 | | - for (int i=pixels.length; --i>=0;) |
---|
2418 | | - { |
---|
2419 | | - int i3 = i*3; |
---|
2420 | | - pixels[i] = 0xFF; |
---|
2421 | | - pixels[i] <<= 8; |
---|
2422 | | - pixels[i] |= bytebuf.get(i3+2) & 0xFF; |
---|
2423 | | - pixels[i] <<= 8; |
---|
2424 | | - pixels[i] |= bytebuf.get(i3+1) & 0xFF; |
---|
2425 | | - pixels[i] <<= 8; |
---|
2426 | | - pixels[i] |= bytebuf.get(i3) & 0xFF; |
---|
2427 | | - } |
---|
2428 | | - /* |
---|
2429 | | - int r=0,g=0,b=0,a=0; |
---|
2430 | | - for (int i=0; i<width; i++) |
---|
2431 | | - for (int j=0; j<height; j++) |
---|
2432 | | - { |
---|
2433 | | - int index = j*width+i; |
---|
2434 | | - int p = pixels[index]; |
---|
2435 | | - a = ((p>>24) & 0xFF); |
---|
2436 | | - r = ((p>>16) & 0xFF); |
---|
2437 | | - g = ((p>>8) & 0xFF); |
---|
2438 | | - b = (p & 0xFF); |
---|
2439 | | - pixels[index] = (a<<24) | (b<<16) | (g<<8) | r; |
---|
2440 | | - } |
---|
2441 | | - /**/ |
---|
2442 | | - BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile); |
---|
2443 | | - rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width); |
---|
2444 | | - return rendImage; |
---|
| 2447 | + Grafreed.Assert(texturedata != null); |
---|
| 2448 | + |
---|
| 2449 | + int width = texturedata.getWidth(); |
---|
| 2450 | + int height = texturedata.getHeight(); |
---|
| 2451 | + |
---|
| 2452 | + Buffer buffer = texturedata.getBuffer(); |
---|
| 2453 | + ByteBuffer bytebuf = (ByteBuffer)buffer; |
---|
| 2454 | + |
---|
| 2455 | + byte[] bytes = bytebuf.array(); |
---|
| 2456 | + |
---|
| 2457 | + return CreateBim(bytes, width, height); |
---|
2445 | 2458 | } |
---|
2446 | 2459 | |
---|
2447 | 2460 | /**/ |
---|
.. | .. |
---|
2452 | 2465 | |
---|
2453 | 2466 | int resolution; |
---|
2454 | 2467 | |
---|
2455 | | - CacheTexture(com.sun.opengl.util.texture.Texture tex, int res) |
---|
| 2468 | + CacheTexture(com.sun.opengl.util.texture.TextureData texdata, int res) |
---|
2456 | 2469 | { |
---|
2457 | | - texture = tex; |
---|
| 2470 | + texture = com.sun.opengl.util.texture.TextureIO.newTexture(texdata); |
---|
| 2471 | + texturedata = texdata; |
---|
2458 | 2472 | resolution = res; |
---|
2459 | 2473 | } |
---|
2460 | 2474 | } |
---|
2461 | 2475 | /**/ |
---|
2462 | 2476 | |
---|
2463 | 2477 | // TEXTURE static Texture texture; |
---|
2464 | | - static public java.util.Hashtable<String, CacheTexture> textures = new java.util.Hashtable<String, CacheTexture>(); |
---|
2465 | | - static public java.util.Hashtable<BufferedImage, CacheTexture> bimtextures = new java.util.Hashtable<BufferedImage, CacheTexture>(); |
---|
2466 | | - static public java.util.HashSet<String> usedtextures = new java.util.HashSet<String>(); |
---|
| 2478 | + static public Hashtable<cTexture, CacheTexture> texturepigment = new Hashtable<cTexture, CacheTexture>(); |
---|
| 2479 | + static public Hashtable<cTexture, CacheTexture> texturebump = new Hashtable<cTexture, CacheTexture>(); |
---|
| 2480 | + static public Hashtable<byte[], CacheTexture> bimtextures = new Hashtable<byte[], CacheTexture>(); |
---|
| 2481 | + static public java.util.HashSet<cTexture> usedtextures = new java.util.HashSet<cTexture>(); |
---|
2467 | 2482 | |
---|
2468 | 2483 | int pigmentdepth = 0; |
---|
2469 | 2484 | public com.sun.opengl.util.texture.Texture[] pigmentstack = new com.sun.opengl.util.texture.Texture[65536]; |
---|
.. | .. |
---|
2471 | 2486 | public com.sun.opengl.util.texture.Texture[] bumpstack = new com.sun.opengl.util.texture.Texture[65536]; |
---|
2472 | 2487 | //public static String DEFAULT_TEXTURE = "DEFAULT_TEXTURE"; |
---|
2473 | 2488 | public static cTexture DEFAULT_TEXTURES = new cTexture("DEFAULT_TEXTURE" + ":" + "DEFAULT_TEXTURE_BUMP"); |
---|
2474 | | - public static String NOISE_TEXTURE = "WHITE_NOISE"; |
---|
| 2489 | + public static cTexture NOISE_TEXTURE = new cTexture("WHITE_NOISE:"); |
---|
2475 | 2490 | // public static cTexture IMMORTAL_TEXTURE = new cTexture("IMMORTAL"); |
---|
2476 | 2491 | |
---|
2477 | | - com.sun.opengl.util.texture.Texture GetResourceTexture(String name, boolean bump) |
---|
| 2492 | + com.sun.opengl.util.texture.TextureData GetResourceTexture(String name, boolean bump) |
---|
2478 | 2493 | { |
---|
2479 | 2494 | TextureData texturedata = null; |
---|
2480 | 2495 | |
---|
.. | .. |
---|
2493 | 2508 | if (bump) |
---|
2494 | 2509 | texturedata = ConvertBump(texturedata, false); |
---|
2495 | 2510 | |
---|
2496 | | - com.sun.opengl.util.texture.Texture texture = |
---|
2497 | | - com.sun.opengl.util.texture.TextureIO.newTexture(texturedata); |
---|
| 2511 | +// com.sun.opengl.util.texture.Texture texture = |
---|
| 2512 | +// com.sun.opengl.util.texture.TextureIO.newTexture(texturedata); |
---|
2498 | 2513 | |
---|
2499 | | - texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT); |
---|
2500 | | - texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_T, javax.media.opengl.GL.GL_REPEAT); |
---|
| 2514 | + //texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT); |
---|
| 2515 | + //texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_T, javax.media.opengl.GL.GL_REPEAT); |
---|
2501 | 2516 | |
---|
2502 | | - return texture; |
---|
| 2517 | + return texturedata; |
---|
2503 | 2518 | } |
---|
2504 | 2519 | |
---|
2505 | | - com.sun.opengl.util.texture.Texture GetBimTexture(BufferedImage name, boolean bump) |
---|
| 2520 | + com.sun.opengl.util.texture.TextureData GetBimTexture(BufferedImage bim, boolean bump) |
---|
2506 | 2521 | { |
---|
2507 | 2522 | TextureData texturedata = null; |
---|
2508 | 2523 | |
---|
.. | .. |
---|
2510 | 2525 | { |
---|
2511 | 2526 | texturedata = |
---|
2512 | 2527 | com.sun.opengl.util.texture.TextureIO.newTextureData( |
---|
2513 | | - name, |
---|
| 2528 | + bim, |
---|
2514 | 2529 | true); |
---|
2515 | 2530 | } catch (Exception e) |
---|
2516 | 2531 | { |
---|
.. | .. |
---|
2519 | 2534 | |
---|
2520 | 2535 | if (bump) |
---|
2521 | 2536 | texturedata = ConvertBump(texturedata, false); |
---|
2522 | | - |
---|
2523 | | - com.sun.opengl.util.texture.Texture texture = |
---|
2524 | | - com.sun.opengl.util.texture.TextureIO.newTexture(texturedata); |
---|
2525 | | - |
---|
2526 | | - texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT); |
---|
2527 | | - texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_T, javax.media.opengl.GL.GL_REPEAT); |
---|
2528 | | - |
---|
2529 | | - return texture; |
---|
| 2537 | + |
---|
| 2538 | + return texturedata; |
---|
2530 | 2539 | } |
---|
2531 | 2540 | |
---|
2532 | 2541 | boolean HUESMOOTH = true; // wrap around bug... true; |
---|
.. | .. |
---|
8014 | 8023 | pigment = null; |
---|
8015 | 8024 | } |
---|
8016 | 8025 | |
---|
8017 | | - ReleaseTexture(bump, true); |
---|
8018 | | - ReleaseTexture(pigment, false); |
---|
| 8026 | + ReleaseTexture(tex, true); |
---|
| 8027 | + ReleaseTexture(tex, false); |
---|
8019 | 8028 | } |
---|
8020 | 8029 | |
---|
8021 | 8030 | public void ReleasePigmentTexture(cTexture tex) // INTERFACE |
---|
.. | .. |
---|
8044 | 8053 | pigment = null; |
---|
8045 | 8054 | } |
---|
8046 | 8055 | |
---|
8047 | | - ReleaseTexture(pigment, false); |
---|
| 8056 | + ReleaseTexture(tex, false); |
---|
8048 | 8057 | } |
---|
8049 | 8058 | |
---|
8050 | 8059 | public void ReleaseBumpTexture(cTexture tex) // INTERFACE |
---|
.. | .. |
---|
8073 | 8082 | bump = null; |
---|
8074 | 8083 | } |
---|
8075 | 8084 | |
---|
8076 | | - ReleaseTexture(bump, true); |
---|
| 8085 | + ReleaseTexture(tex, true); |
---|
8077 | 8086 | } |
---|
8078 | 8087 | |
---|
8079 | | - void ReleaseTexture(String tex, boolean bump) |
---|
| 8088 | + void ReleaseTexture(cTexture tex, boolean bump) |
---|
8080 | 8089 | { |
---|
8081 | 8090 | if (// DrawMode() != 0 || /*tex == null ||*/ |
---|
8082 | 8091 | ambientOcclusion ) // || !textureon) |
---|
.. | .. |
---|
8087 | 8096 | CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null; |
---|
8088 | 8097 | |
---|
8089 | 8098 | if (tex != null) |
---|
8090 | | - texture = textures.get(tex); |
---|
| 8099 | + texture = bump ? texturebump.get(tex) : texturepigment.get(tex); |
---|
8091 | 8100 | |
---|
8092 | 8101 | // //assert( texture != null ); |
---|
8093 | 8102 | // if (texture == null) |
---|
.. | .. |
---|
8237 | 8246 | |
---|
8238 | 8247 | if (tex == null) |
---|
8239 | 8248 | { |
---|
8240 | | - BindTexture(null, null,false,resolution); |
---|
| 8249 | + BindTexture(null,false,resolution); |
---|
8241 | 8250 | return; |
---|
8242 | 8251 | } |
---|
8243 | 8252 | |
---|
8244 | 8253 | String pigment = Object3D.GetPigment(tex); |
---|
8245 | 8254 | |
---|
8246 | | - usedtextures.add(pigment); |
---|
| 8255 | + usedtextures.add(tex); |
---|
8247 | 8256 | |
---|
8248 | 8257 | //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) |
---|
8249 | 8258 | { |
---|
.. | .. |
---|
8257 | 8266 | } |
---|
8258 | 8267 | |
---|
8259 | 8268 | GetGL().glActiveTexture(GetGL().GL_TEXTURE0); |
---|
8260 | | - BindTexture(tex.pigmenttexture, pigment, false, resolution); |
---|
| 8269 | + BindTexture(tex, false, resolution); |
---|
8261 | 8270 | } |
---|
8262 | 8271 | |
---|
8263 | 8272 | /*boolean*/ public void BindBumpTexture(cTexture tex, int resolution) throws Exception // INTERFACE |
---|
.. | .. |
---|
8270 | 8279 | |
---|
8271 | 8280 | if (tex == null) |
---|
8272 | 8281 | { |
---|
8273 | | - BindTexture(null, null,true,resolution); |
---|
| 8282 | + BindTexture(null,true,resolution); |
---|
8274 | 8283 | return; |
---|
8275 | 8284 | } |
---|
8276 | 8285 | |
---|
8277 | 8286 | String bump = Object3D.GetBump(tex); |
---|
8278 | 8287 | |
---|
8279 | | - usedtextures.add(bump); |
---|
| 8288 | + usedtextures.add(tex); |
---|
8280 | 8289 | |
---|
8281 | 8290 | //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) |
---|
8282 | 8291 | { |
---|
.. | .. |
---|
8290 | 8299 | } |
---|
8291 | 8300 | |
---|
8292 | 8301 | GetGL().glActiveTexture(GetGL().GL_TEXTURE2); |
---|
8293 | | - BindTexture(tex.bumptexture, bump, true, resolution); |
---|
| 8302 | + BindTexture(tex, true, resolution); |
---|
8294 | 8303 | GetGL().glActiveTexture(GetGL().GL_TEXTURE0); |
---|
8295 | 8304 | } |
---|
8296 | 8305 | |
---|
.. | .. |
---|
8314 | 8323 | return fileExists; |
---|
8315 | 8324 | } |
---|
8316 | 8325 | |
---|
8317 | | - CacheTexture GetCacheTexture(java.awt.image.BufferedImage bim, String tex, boolean bump, int resolution) throws Exception |
---|
| 8326 | + CacheTexture GetCacheTexture(cTexture tex, boolean bump, int resolution) throws Exception |
---|
8318 | 8327 | { |
---|
8319 | 8328 | CacheTexture texturecache = null; |
---|
8320 | 8329 | |
---|
8321 | 8330 | if (tex != null) |
---|
8322 | 8331 | { |
---|
8323 | | - String texname = tex; |
---|
| 8332 | + String texname = bump ? Object3D.GetBump(tex) : Object3D.GetPigment(tex); |
---|
| 8333 | + byte[] texdata = bump ? tex.bumpdata : tex.pigmentdata; |
---|
| 8334 | + |
---|
| 8335 | + if (texname.equals("") && texdata == null) |
---|
| 8336 | + { |
---|
| 8337 | + return null; |
---|
| 8338 | + } |
---|
8324 | 8339 | |
---|
8325 | 8340 | String fallbackTextureName = defaultDirectory + "/Textures/" + texname; |
---|
8326 | 8341 | |
---|
.. | .. |
---|
8330 | 8345 | // else |
---|
8331 | 8346 | // if (!texname.startsWith("/")) |
---|
8332 | 8347 | // texname = "/Users/nbriere/Textures/" + texname; |
---|
8333 | | - if (!FileExists(tex)) |
---|
| 8348 | + if (!FileExists(texname)) |
---|
8334 | 8349 | { |
---|
8335 | 8350 | texname = fallbackTextureName; |
---|
8336 | 8351 | } |
---|
8337 | 8352 | |
---|
8338 | 8353 | if (CACHETEXTURE) |
---|
8339 | 8354 | { |
---|
8340 | | - if (bim == null) |
---|
8341 | | - texturecache = textures.get(texname); // TEXTURE CACHE |
---|
| 8355 | + if (texdata == null) |
---|
| 8356 | + texturecache = bump ? texturebump.get(tex) : texturepigment.get(tex); |
---|
8342 | 8357 | else |
---|
8343 | | - texturecache = bimtextures.get(bim); // TEXTURE CACHE |
---|
| 8358 | + texturecache = bimtextures.get(texdata); |
---|
8344 | 8359 | } |
---|
8345 | 8360 | |
---|
8346 | | - if (texturecache == null || texturecache.resolution < resolution) |
---|
| 8361 | + if (texturecache == null || texturecache.resolution != -1 && texturecache.resolution < resolution) |
---|
8347 | 8362 | { |
---|
8348 | 8363 | TextureData texturedata = null; |
---|
8349 | 8364 | |
---|
8350 | | - if (bim != null) |
---|
| 8365 | + if (texdata != null && textureon) |
---|
8351 | 8366 | { |
---|
| 8367 | + BufferedImage bim; // = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); |
---|
| 8368 | + |
---|
| 8369 | + try |
---|
| 8370 | + { |
---|
| 8371 | + bim = DecompressJPEG(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); |
---|
| 8372 | + } |
---|
| 8373 | + catch (Exception e) |
---|
| 8374 | + { |
---|
| 8375 | + bim = CreateBim(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); |
---|
| 8376 | + } |
---|
| 8377 | + |
---|
8352 | 8378 | texturecache = new CacheTexture(GetBimTexture(bim, bump), -1); |
---|
| 8379 | + bimtextures.put(texdata, texturecache); |
---|
| 8380 | + |
---|
| 8381 | + //BufferedImage bim3 = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); |
---|
| 8382 | + |
---|
| 8383 | + //Object bim2 = CreateBim(texturecache.texturedata); |
---|
| 8384 | + //bim2 = bim; |
---|
8353 | 8385 | } |
---|
8354 | 8386 | else |
---|
8355 | | - if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals("")) |
---|
| 8387 | + if (texname.endsWith("DEFAULT_TEXTURE")) // ||*/ tex.equals("")) |
---|
8356 | 8388 | { |
---|
8357 | 8389 | assert(!bump); |
---|
8358 | 8390 | // if (bump) |
---|
.. | .. |
---|
8363 | 8395 | // } |
---|
8364 | 8396 | // else |
---|
8365 | 8397 | // { |
---|
8366 | | - texturecache = textures.get(tex); |
---|
| 8398 | + // texturecache = textures.get(texname); // suspicious |
---|
8367 | 8399 | if (texturecache == null) |
---|
8368 | 8400 | { |
---|
8369 | 8401 | texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution); |
---|
.. | .. |
---|
8372 | 8404 | new Exception().printStackTrace(); |
---|
8373 | 8405 | // } |
---|
8374 | 8406 | } else |
---|
8375 | | - if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals("")) |
---|
| 8407 | + if (texname.endsWith("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals("")) |
---|
8376 | 8408 | { |
---|
8377 | 8409 | assert(bump); |
---|
8378 | | - texturecache = textures.get(tex); |
---|
| 8410 | + // texturecache = textures.get(texname); // suspicious |
---|
8379 | 8411 | if (texturecache == null) |
---|
8380 | 8412 | texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution); |
---|
8381 | 8413 | else |
---|
.. | .. |
---|
8387 | 8419 | // texture = GetResourceTexture("default.png"); |
---|
8388 | 8420 | //} else |
---|
8389 | 8421 | //{ |
---|
8390 | | - if (tex.equals("WHITE_NOISE")) |
---|
| 8422 | + if (texname.endsWith("WHITE_NOISE")) |
---|
8391 | 8423 | { |
---|
8392 | | - texturecache = textures.get(tex); |
---|
| 8424 | + // texturecache = textures.get(texname); // suspicious |
---|
8393 | 8425 | if (texturecache == null) |
---|
8394 | 8426 | texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution); |
---|
8395 | 8427 | else |
---|
.. | .. |
---|
8452 | 8484 | if (texturedata == null) |
---|
8453 | 8485 | throw new Exception(); |
---|
8454 | 8486 | |
---|
8455 | | - texturecache = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution); |
---|
| 8487 | + texturecache = new CacheTexture(texturedata,resolution); |
---|
8456 | 8488 | //texture = GetTexture(tex, bump); |
---|
8457 | 8489 | } |
---|
8458 | 8490 | } |
---|
8459 | 8491 | //} |
---|
8460 | 8492 | } |
---|
8461 | 8493 | |
---|
8462 | | - if (/*CACHETEXTURE &&*/ texturecache != null && textureon) |
---|
| 8494 | + if (texdata == null && /*CACHETEXTURE &&*/ texturecache != null && textureon) |
---|
8463 | 8495 | { |
---|
8464 | 8496 | //return false; |
---|
8465 | | - assert(bim == null); |
---|
8466 | 8497 | |
---|
8467 | 8498 | // System.out.println("CACHE +++++++++++++++ TEXTURE : " + texname + " (" + texture.getEstimatedMemorySize() + ")"); |
---|
8468 | 8499 | if (texturedata != null && texname.toLowerCase().endsWith(".jpg")) |
---|
.. | .. |
---|
8509 | 8540 | } |
---|
8510 | 8541 | } |
---|
8511 | 8542 | } |
---|
8512 | | - |
---|
| 8543 | + |
---|
| 8544 | + Hashtable<cTexture, CacheTexture> textures = bump ? texturebump : texturepigment; |
---|
| 8545 | + |
---|
8513 | 8546 | //System.out.println("Texture = " + tex); |
---|
8514 | | - if (textures.containsKey(texname)) |
---|
| 8547 | + if (textures.containsKey(tex)) |
---|
8515 | 8548 | { |
---|
8516 | | - CacheTexture thetex = textures.get(texname); |
---|
| 8549 | + CacheTexture thetex = textures.get(tex); |
---|
8517 | 8550 | thetex.texture.disable(); |
---|
8518 | 8551 | thetex.texture.dispose(); |
---|
8519 | | - textures.remove(texname); |
---|
| 8552 | + textures.remove(tex); |
---|
8520 | 8553 | } |
---|
8521 | 8554 | |
---|
8522 | 8555 | //texture.texturedata = texturedata; |
---|
8523 | | - textures.put(texname, texturecache); |
---|
| 8556 | + textures.put(tex, texturecache); |
---|
8524 | 8557 | |
---|
8525 | 8558 | // newtex = true; |
---|
8526 | 8559 | } |
---|
.. | .. |
---|
8541 | 8574 | |
---|
8542 | 8575 | static void EmbedTextures(cTexture tex) |
---|
8543 | 8576 | { |
---|
| 8577 | + if (tex.pigmentdata == null) |
---|
| 8578 | + { |
---|
| 8579 | + //String texname = Object3D.GetPigment(tex); |
---|
| 8580 | + |
---|
| 8581 | + CacheTexture texturecache = texturepigment.get(tex); |
---|
| 8582 | + |
---|
| 8583 | + if (texturecache != null) |
---|
| 8584 | + { |
---|
| 8585 | + tex.pw = texturecache.texturedata.getWidth(); |
---|
| 8586 | + tex.ph = texturecache.texturedata.getHeight(); |
---|
| 8587 | + tex.pigmentdata = //CompressJPEG(CreateBim |
---|
| 8588 | + ((ByteBuffer)texturecache.texturedata.getBuffer()).array() |
---|
| 8589 | + ; |
---|
| 8590 | + //, tex.pw, tex.ph), 0.5f); |
---|
| 8591 | + } |
---|
| 8592 | + } |
---|
8544 | 8593 | |
---|
| 8594 | + if (tex.bumpdata == null) |
---|
| 8595 | + { |
---|
| 8596 | + //String texname = Object3D.GetBump(tex); |
---|
| 8597 | + |
---|
| 8598 | + CacheTexture texturecache = texturebump.get(tex); |
---|
| 8599 | + |
---|
| 8600 | + if (texturecache != null) |
---|
| 8601 | + { |
---|
| 8602 | + tex.bw = texturecache.texturedata.getWidth(); |
---|
| 8603 | + tex.bh = texturecache.texturedata.getHeight(); |
---|
| 8604 | + tex.bumpdata = CompressJPEG(CreateBim(((ByteBuffer)texturecache.texturedata.getBuffer()).array(), tex.bw, tex.bh), 0.5f); |
---|
| 8605 | + } |
---|
| 8606 | + } |
---|
8545 | 8607 | } |
---|
8546 | 8608 | |
---|
8547 | | - com.sun.opengl.util.texture.Texture GetTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception |
---|
| 8609 | + com.sun.opengl.util.texture.Texture GetTexture(cTexture tex, boolean bump, int resolution) throws Exception |
---|
8548 | 8610 | { |
---|
8549 | | - CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution); |
---|
| 8611 | + CacheTexture texture = GetCacheTexture(tex, bump, resolution); |
---|
8550 | 8612 | |
---|
8551 | 8613 | if (bump) |
---|
8552 | 8614 | { |
---|
.. | .. |
---|
8562 | 8624 | return texture!=null?texture.texture:null; |
---|
8563 | 8625 | } |
---|
8564 | 8626 | |
---|
8565 | | - public com.sun.opengl.util.texture.TextureData GetTextureData(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception |
---|
| 8627 | + public com.sun.opengl.util.texture.TextureData GetTextureData(cTexture tex, boolean bump, int resolution) throws Exception |
---|
8566 | 8628 | { |
---|
8567 | | - CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution); |
---|
| 8629 | + CacheTexture texture = GetCacheTexture(tex, bump, resolution); |
---|
8568 | 8630 | |
---|
8569 | 8631 | return texture!=null?texture.texturedata:null; |
---|
8570 | 8632 | } |
---|
8571 | 8633 | |
---|
8572 | | - boolean BindTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception |
---|
| 8634 | + boolean BindTexture(cTexture tex, boolean bump, int resolution) throws Exception |
---|
8573 | 8635 | { |
---|
8574 | 8636 | if (/*tex == null ||*/ ambientOcclusion ) // || !textureon) |
---|
8575 | 8637 | { |
---|
.. | .. |
---|
8578 | 8640 | |
---|
8579 | 8641 | //boolean newtex = false; |
---|
8580 | 8642 | |
---|
8581 | | - com.sun.opengl.util.texture.Texture texture = GetTexture(stream, tex, bump, resolution); |
---|
| 8643 | + com.sun.opengl.util.texture.Texture texture = GetTexture(tex, bump, resolution); |
---|
8582 | 8644 | |
---|
8583 | 8645 | if (texture == null) |
---|
8584 | 8646 | return false; |
---|
.. | .. |
---|
8611 | 8673 | return true; // Warning: not used. |
---|
8612 | 8674 | } |
---|
8613 | 8675 | |
---|
| 8676 | + public static byte[] CompressJPEG(BufferedImage image, float quality) |
---|
| 8677 | + { |
---|
| 8678 | + try |
---|
| 8679 | + { |
---|
| 8680 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
---|
| 8681 | + Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
---|
| 8682 | + ImageWriter writer = writers.next(); |
---|
| 8683 | + |
---|
| 8684 | + ImageWriteParam param = writer.getDefaultWriteParam(); |
---|
| 8685 | + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
---|
| 8686 | + param.setCompressionQuality(quality); |
---|
| 8687 | + |
---|
| 8688 | + ImageOutputStream ios = ImageIO.createImageOutputStream(baos); |
---|
| 8689 | + writer.setOutput(ios); |
---|
| 8690 | + writer.write(null, new IIOImage(image, null, null), param); |
---|
| 8691 | + |
---|
| 8692 | + byte[] data = baos.toByteArray(); |
---|
| 8693 | + writer.dispose(); |
---|
| 8694 | + return data; |
---|
| 8695 | + } |
---|
| 8696 | + catch (Exception e) |
---|
| 8697 | + { |
---|
| 8698 | + e.printStackTrace(); |
---|
| 8699 | + return null; |
---|
| 8700 | + } |
---|
| 8701 | + } |
---|
| 8702 | + |
---|
| 8703 | + public static BufferedImage DecompressJPEG(byte[] image, int w, int h) throws IOException |
---|
| 8704 | + { |
---|
| 8705 | + ByteArrayInputStream baos = new ByteArrayInputStream(image); |
---|
| 8706 | + Iterator<ImageReader> writers = ImageIO.getImageReadersByFormatName("jpg"); |
---|
| 8707 | + ImageReader reader = writers.next(); |
---|
| 8708 | + |
---|
| 8709 | + BufferedImage bim = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); |
---|
| 8710 | + |
---|
| 8711 | + ImageReadParam param = reader.getDefaultReadParam(); |
---|
| 8712 | + param.setDestination(bim); |
---|
| 8713 | + //param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB)); |
---|
| 8714 | + |
---|
| 8715 | + ImageInputStream ios = ImageIO.createImageInputStream(baos); |
---|
| 8716 | + reader.setInput(ios); |
---|
| 8717 | + BufferedImage bim2 = reader.read(0, param); |
---|
| 8718 | + reader.dispose(); |
---|
| 8719 | + |
---|
| 8720 | +// WritableRaster raster = bim2.getRaster(); |
---|
| 8721 | +// DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); |
---|
| 8722 | +// byte[] bytes = data.getData(); |
---|
| 8723 | +// |
---|
| 8724 | +// int[] pixels = new int[bytes.length/3]; |
---|
| 8725 | +// for (int i=pixels.length; --i>=0;) |
---|
| 8726 | +// { |
---|
| 8727 | +// int i3 = i*3; |
---|
| 8728 | +// pixels[i] = 0xFF; |
---|
| 8729 | +// pixels[i] <<= 8; |
---|
| 8730 | +// pixels[i] |= bytes[i3+2] & 0xFF; |
---|
| 8731 | +// pixels[i] <<= 8; |
---|
| 8732 | +// pixels[i] |= bytes[i3+1] & 0xFF; |
---|
| 8733 | +// pixels[i] <<= 8; |
---|
| 8734 | +// pixels[i] |= bytes[i3] & 0xFF; |
---|
| 8735 | +// } |
---|
| 8736 | +// |
---|
| 8737 | +// bim.setRGB(0,0,w,h, pixels, w*(h-1),-w); |
---|
| 8738 | + |
---|
| 8739 | + return bim; |
---|
| 8740 | + } |
---|
| 8741 | + |
---|
8614 | 8742 | ShadowBuffer shadowPBuf; |
---|
8615 | 8743 | AntialiasBuffer antialiasPBuf; |
---|
8616 | 8744 | int MAXSTACK; |
---|
.. | .. |
---|
11065 | 11193 | |
---|
11066 | 11194 | try |
---|
11067 | 11195 | { |
---|
11068 | | - BindTexture(null, NOISE_TEXTURE, false, 2); |
---|
| 11196 | + BindTexture(NOISE_TEXTURE, false, 2); |
---|
11069 | 11197 | } |
---|
11070 | 11198 | catch (Exception e) |
---|
11071 | 11199 | { |
---|
.. | .. |
---|
11356 | 11484 | } |
---|
11357 | 11485 | } |
---|
11358 | 11486 | |
---|
11359 | | - if (false) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) |
---|
| 11487 | + if (false) //RENDERPROGRAM > 0 && DrawMode() == DEFAULT) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) |
---|
11360 | 11488 | { |
---|
11361 | 11489 | //gl.glDepthFunc(GL.GL_LEQUAL); |
---|
11362 | 11490 | //gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); |
---|
.. | .. |
---|
11364 | 11492 | |
---|
11365 | 11493 | boolean texon = textureon; |
---|
11366 | 11494 | |
---|
11367 | | - if (RENDERPROGRAM > 0) |
---|
11368 | | - { |
---|
11369 | | - gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
11370 | | - textureon = false; |
---|
11371 | | - } |
---|
| 11495 | + gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
| 11496 | + textureon = false; |
---|
| 11497 | + |
---|
11372 | 11498 | //gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB); |
---|
11373 | 11499 | //System.out.println("ALLO"); |
---|
11374 | 11500 | gl.glColorMask(false, false, false, false); |
---|
11375 | 11501 | DrawObject(gl); |
---|
11376 | | - if (RENDERPROGRAM > 0) |
---|
11377 | | - { |
---|
11378 | | - gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
11379 | | - textureon = texon; |
---|
11380 | | - } |
---|
| 11502 | + |
---|
| 11503 | + gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
| 11504 | + textureon = texon; |
---|
| 11505 | + |
---|
11381 | 11506 | gl.glColorMask(true, true, true, true); |
---|
11382 | 11507 | |
---|
11383 | 11508 | gl.glDepthFunc(GL.GL_EQUAL); |
---|
11384 | | - //gl.glDepthMask(false); |
---|
| 11509 | + gl.glDepthMask(false); |
---|
11385 | 11510 | } |
---|
11386 | 11511 | |
---|
11387 | 11512 | if (false) // DrawMode() == SHADOW) |
---|
.. | .. |
---|
11721 | 11846 | ReleaseTextures(DEFAULT_TEXTURES); |
---|
11722 | 11847 | |
---|
11723 | 11848 | if (CLEANCACHE) |
---|
11724 | | - for (java.util.Enumeration<String> e = textures.keys() ; e.hasMoreElements();) |
---|
| 11849 | + for (java.util.Enumeration<cTexture> e = texturepigment.keys() ; e.hasMoreElements();) |
---|
11725 | 11850 | { |
---|
11726 | | - String tex = e.nextElement(); |
---|
| 11851 | + cTexture tex = e.nextElement(); |
---|
11727 | 11852 | |
---|
11728 | 11853 | // System.out.println("Texture --------- " + tex); |
---|
11729 | 11854 | |
---|
11730 | | - if (tex.equals("WHITE_NOISE")) |
---|
| 11855 | + if (tex.equals("WHITE_NOISE:")) |
---|
11731 | 11856 | continue; |
---|
11732 | 11857 | |
---|
11733 | 11858 | if (!usedtextures.contains(tex)) |
---|
11734 | 11859 | { |
---|
| 11860 | + CacheTexture gettex = texturepigment.get(tex); |
---|
11735 | 11861 | // System.out.println("DISPOSE +++++++++++++++ " + tex); |
---|
11736 | | - textures.get(tex).texture.dispose(); |
---|
11737 | | - textures.remove(tex); |
---|
| 11862 | + if (gettex != null) |
---|
| 11863 | + { |
---|
| 11864 | + gettex.texture.dispose(); |
---|
| 11865 | + texturepigment.remove(tex); |
---|
| 11866 | + } |
---|
| 11867 | + |
---|
| 11868 | + gettex = texturebump.get(tex); |
---|
| 11869 | + // System.out.println("DISPOSE +++++++++++++++ " + tex); |
---|
| 11870 | + if (gettex != null) |
---|
| 11871 | + { |
---|
| 11872 | + gettex.texture.dispose(); |
---|
| 11873 | + texturebump.remove(tex); |
---|
| 11874 | + } |
---|
11738 | 11875 | } |
---|
11739 | 11876 | } |
---|
11740 | 11877 | } |
---|
.. | .. |
---|
12263 | 12400 | //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0); |
---|
12264 | 12401 | |
---|
12265 | 12402 | String program = |
---|
| 12403 | + // Min shader |
---|
12266 | 12404 | "!!ARBfp1.0\n" + |
---|
| 12405 | + "PARAM zero123 = { 0.0, 1.0, 2.0, 3.0 };" + |
---|
| 12406 | + "PARAM pow2 = { 0.5, 0.25, 0.125, 0.0 };" + |
---|
| 12407 | + "PARAM one = { 1.0, 1.0, 1.0, 1.0 };" + |
---|
| 12408 | + "PARAM eps = { 0.001, 0.001, 0.001, 1.0 };" + |
---|
| 12409 | + "PARAM infinity = { 100000000, 100000000, 100000000, 1.0 };" + |
---|
| 12410 | + "TEMP temp;" + |
---|
| 12411 | + "TEMP light;" + |
---|
| 12412 | + "TEMP ndotl;" + |
---|
| 12413 | + "TEMP normal;" + |
---|
| 12414 | + "TEMP depth;" + |
---|
| 12415 | + |
---|
| 12416 | + "MAD normal, fragment.color, zero123.z, -zero123.y;" + |
---|
| 12417 | + |
---|
| 12418 | + "MOV light, state.light[0].position;" + |
---|
| 12419 | + "DP3 ndotl.x, light, normal;" + |
---|
| 12420 | + |
---|
| 12421 | + // shadow |
---|
| 12422 | + "MOV temp, fragment.texcoord[1];" + |
---|
| 12423 | + TextureFetch("depth", "temp", "1") + |
---|
| 12424 | + //"TEX depth, fragment.texcoord[1], texture[1], 2D;" + |
---|
| 12425 | + "SLT temp.x, fragment.texcoord[1].z, depth.z;" + |
---|
| 12426 | + |
---|
| 12427 | + |
---|
| 12428 | + // No shadow when out of frustum |
---|
| 12429 | + //"SGE temp.y, depth.z, zero123.y;" + |
---|
| 12430 | + //"LRP temp.x, temp.y, zero123.y, temp.x;" + |
---|
| 12431 | + |
---|
| 12432 | + "MUL ndotl.x, ndotl.x, temp.x;" + |
---|
| 12433 | + "MAX ndotl.x, ndotl.x, pow2.y;" + |
---|
| 12434 | + |
---|
| 12435 | + "TEX temp, fragment.texcoord[0], texture[0], 2D;" + |
---|
| 12436 | + "MUL temp, temp, ndotl.x;" + |
---|
| 12437 | + |
---|
| 12438 | + "MUL temp, temp, zero123.z;" + |
---|
| 12439 | + |
---|
| 12440 | + "MOV temp.w, zero123.y;" + // reset alpha |
---|
| 12441 | + |
---|
| 12442 | + "MOV result.color, temp;" + |
---|
| 12443 | + "END"; |
---|
| 12444 | + |
---|
| 12445 | + String program2 = |
---|
| 12446 | + "!!ARBfp1.0\n" + |
---|
| 12447 | + |
---|
12267 | 12448 | //"OPTION ARB_fragment_program_shadow;" + |
---|
12268 | 12449 | "PARAM light2cam0 = program.env[10];" + |
---|
12269 | 12450 | "PARAM light2cam1 = program.env[11];" + |
---|
.. | .. |
---|
12378 | 12559 | "TEMP shininess;" + |
---|
12379 | 12560 | "\n" + |
---|
12380 | 12561 | "MOV texSamp, one;" + |
---|
12381 | | - //"TEX texSamp, fragment.texcoord[0], texture[0], 2D;" + |
---|
12382 | | - |
---|
| 12562 | + |
---|
12383 | 12563 | "MOV mapgrid.x, one2048th.x;" + |
---|
12384 | 12564 | "MOV temp, fragment.texcoord[1];" + |
---|
12385 | 12565 | /* |
---|
.. | .. |
---|
12784 | 12964 | "MAD shadow.x, buffer.x, frac.y, shadow.x;" + |
---|
12785 | 12965 | "") + |
---|
12786 | 12966 | |
---|
12787 | | - // display shadow only (bump == 0) |
---|
| 12967 | + // display shadow only (fakedepth == 0) |
---|
12788 | 12968 | "SUB temp.x, half.x, shadow.x;" + |
---|
12789 | 12969 | "MOV temp.y, -params5.z;" + // params6.x;" + |
---|
12790 | 12970 | "SLT temp.z, temp.y, -one2048th.x;" + |
---|
.. | .. |
---|
13276 | 13456 | "SLT temp.x, temp.x, zero.x;" + // shadoweps |
---|
13277 | 13457 | "LRP " + shadow + ", temp.x, one, " + shadow + ";" + |
---|
13278 | 13458 | |
---|
13279 | | - // No shadow when out of frustrum |
---|
| 13459 | + // No shadow when out of frustum |
---|
13280 | 13460 | "SGE temp.x, " + depth + ".z, one.z;" + |
---|
13281 | 13461 | "LRP " + shadow + ", temp.x, one, " + shadow + ";" + |
---|
13282 | 13462 | ""; |
---|
.. | .. |
---|
14927 | 15107 | // break; |
---|
14928 | 15108 | case 'T': |
---|
14929 | 15109 | CACHETEXTURE ^= true; |
---|
14930 | | - textures.clear(); |
---|
| 15110 | + texturepigment.clear(); |
---|
| 15111 | + texturebump.clear(); |
---|
14931 | 15112 | // repaint(); |
---|
14932 | 15113 | break; |
---|
14933 | 15114 | case 'Y': |
---|