Normand Briere
2019-07-23 0f4fa9dc4191aaee9661c1e6d73725436ae64ac2
CameraPane.java
....@@ -18,7 +18,10 @@
1818 import javax.imageio.ImageIO;
1919 import javax.imageio.ImageWriteParam;
2020 import javax.imageio.ImageWriter;
21
+import javax.imageio.ImageReadParam;
22
+import javax.imageio.ImageReader;
2123 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
24
+import javax.imageio.stream.ImageInputStream;
2225 import javax.imageio.stream.ImageOutputStream;
2326 import javax.imageio.ImageWriteParam;
2427
....@@ -30,6 +33,7 @@
3033 import java.nio.*;
3134
3235 import gleem.linalg.Mat4f;
36
+import javax.imageio.ImageTypeSpecifier;
3337
3438 class CameraPane extends GLCanvas implements iCameraPane, Runnable, GLEventListener, ActionListener, MouseWheelListener, MouseMotionListener, MouseListener, KeyListener
3539 {
....@@ -44,6 +48,39 @@
4448 static boolean ABORTED = false;
4549
4650 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
+ }
4784
4885 /*static*/ private boolean CULLFACE = false; // true;
4986 /*static*/ boolean NEAREST = false; // true;
....@@ -60,7 +97,7 @@
6097 //boolean REDUCETEXTURE = true;
6198 boolean CACHETEXTURE = true;
6299 boolean CLEANCACHE = false; // true;
63
- boolean MIPMAP = true; // false; // true;
100
+ boolean MIPMAP = false; // true; // never works...
64101 boolean COMPRESSTEXTURE = false;
65102 boolean KOMPACTTEXTURE = false; // true;
66103 boolean RESIZETEXTURE = false;
....@@ -2405,43 +2442,19 @@
24052442 return currentGL;
24062443 }
24072444
2408
- private BufferedImage CreateBim(TextureData texturedata)
2445
+ static private BufferedImage CreateBim(TextureData texturedata)
24092446 {
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);
24452458 }
24462459
24472460 /**/
....@@ -2452,18 +2465,20 @@
24522465
24532466 int resolution;
24542467
2455
- CacheTexture(com.sun.opengl.util.texture.Texture tex, int res)
2468
+ CacheTexture(com.sun.opengl.util.texture.TextureData texdata, int res)
24562469 {
2457
- texture = tex;
2470
+ texture = com.sun.opengl.util.texture.TextureIO.newTexture(texdata);
2471
+ texturedata = texdata;
24582472 resolution = res;
24592473 }
24602474 }
24612475 /**/
24622476
24632477 // 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>();
24672482
24682483 int pigmentdepth = 0;
24692484 public com.sun.opengl.util.texture.Texture[] pigmentstack = new com.sun.opengl.util.texture.Texture[65536];
....@@ -2471,10 +2486,10 @@
24712486 public com.sun.opengl.util.texture.Texture[] bumpstack = new com.sun.opengl.util.texture.Texture[65536];
24722487 //public static String DEFAULT_TEXTURE = "DEFAULT_TEXTURE";
24732488 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:");
24752490 // public static cTexture IMMORTAL_TEXTURE = new cTexture("IMMORTAL");
24762491
2477
- com.sun.opengl.util.texture.Texture GetResourceTexture(String name, boolean bump)
2492
+ com.sun.opengl.util.texture.TextureData GetResourceTexture(String name, boolean bump)
24782493 {
24792494 TextureData texturedata = null;
24802495
....@@ -2493,16 +2508,16 @@
24932508 if (bump)
24942509 texturedata = ConvertBump(texturedata, false);
24952510
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);
24982513
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);
25012516
2502
- return texture;
2517
+ return texturedata;
25032518 }
25042519
2505
- com.sun.opengl.util.texture.Texture GetBimTexture(BufferedImage name, boolean bump)
2520
+ com.sun.opengl.util.texture.TextureData GetBimTexture(BufferedImage bim, boolean bump)
25062521 {
25072522 TextureData texturedata = null;
25082523
....@@ -2510,7 +2525,7 @@
25102525 {
25112526 texturedata =
25122527 com.sun.opengl.util.texture.TextureIO.newTextureData(
2513
- name,
2528
+ bim,
25142529 true);
25152530 } catch (Exception e)
25162531 {
....@@ -2519,14 +2534,8 @@
25192534
25202535 if (bump)
25212536 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;
25302539 }
25312540
25322541 boolean HUESMOOTH = true; // wrap around bug... true;
....@@ -8014,8 +8023,8 @@
80148023 pigment = null;
80158024 }
80168025
8017
- ReleaseTexture(bump, true);
8018
- ReleaseTexture(pigment, false);
8026
+ ReleaseTexture(tex, true);
8027
+ ReleaseTexture(tex, false);
80198028 }
80208029
80218030 public void ReleasePigmentTexture(cTexture tex) // INTERFACE
....@@ -8044,7 +8053,7 @@
80448053 pigment = null;
80458054 }
80468055
8047
- ReleaseTexture(pigment, false);
8056
+ ReleaseTexture(tex, false);
80488057 }
80498058
80508059 public void ReleaseBumpTexture(cTexture tex) // INTERFACE
....@@ -8073,10 +8082,10 @@
80738082 bump = null;
80748083 }
80758084
8076
- ReleaseTexture(bump, true);
8085
+ ReleaseTexture(tex, true);
80778086 }
80788087
8079
- void ReleaseTexture(String tex, boolean bump)
8088
+ void ReleaseTexture(cTexture tex, boolean bump)
80808089 {
80818090 if (// DrawMode() != 0 || /*tex == null ||*/
80828091 ambientOcclusion ) // || !textureon)
....@@ -8087,7 +8096,7 @@
80878096 CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null;
80888097
80898098 if (tex != null)
8090
- texture = textures.get(tex);
8099
+ texture = bump ? texturebump.get(tex) : texturepigment.get(tex);
80918100
80928101 // //assert( texture != null );
80938102 // if (texture == null)
....@@ -8237,13 +8246,13 @@
82378246
82388247 if (tex == null)
82398248 {
8240
- BindTexture(null, null,false,resolution);
8249
+ BindTexture(null,false,resolution);
82418250 return;
82428251 }
82438252
82448253 String pigment = Object3D.GetPigment(tex);
82458254
8246
- usedtextures.add(pigment);
8255
+ usedtextures.add(tex);
82478256
82488257 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
82498258 {
....@@ -8257,7 +8266,7 @@
82578266 }
82588267
82598268 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8260
- BindTexture(tex.pigmenttexture, pigment, false, resolution);
8269
+ BindTexture(tex, false, resolution);
82618270 }
82628271
82638272 /*boolean*/ public void BindBumpTexture(cTexture tex, int resolution) throws Exception // INTERFACE
....@@ -8270,13 +8279,13 @@
82708279
82718280 if (tex == null)
82728281 {
8273
- BindTexture(null, null,true,resolution);
8282
+ BindTexture(null,true,resolution);
82748283 return;
82758284 }
82768285
82778286 String bump = Object3D.GetBump(tex);
82788287
8279
- usedtextures.add(bump);
8288
+ usedtextures.add(tex);
82808289
82818290 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
82828291 {
....@@ -8290,7 +8299,7 @@
82908299 }
82918300
82928301 GetGL().glActiveTexture(GetGL().GL_TEXTURE2);
8293
- BindTexture(tex.bumptexture, bump, true, resolution);
8302
+ BindTexture(tex, true, resolution);
82948303 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
82958304 }
82968305
....@@ -8314,13 +8323,19 @@
83148323 return fileExists;
83158324 }
83168325
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
83188327 {
83198328 CacheTexture texturecache = null;
83208329
83218330 if (tex != null)
83228331 {
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
+ }
83248339
83258340 String fallbackTextureName = defaultDirectory + "/Textures/" + texname;
83268341
....@@ -8330,29 +8345,46 @@
83308345 // else
83318346 // if (!texname.startsWith("/"))
83328347 // texname = "/Users/nbriere/Textures/" + texname;
8333
- if (!FileExists(tex))
8348
+ if (!FileExists(texname))
83348349 {
83358350 texname = fallbackTextureName;
83368351 }
83378352
83388353 if (CACHETEXTURE)
83398354 {
8340
- if (bim == null)
8341
- texturecache = textures.get(texname); // TEXTURE CACHE
8355
+ if (texdata == null)
8356
+ texturecache = bump ? texturebump.get(tex) : texturepigment.get(tex);
83428357 else
8343
- texturecache = bimtextures.get(bim); // TEXTURE CACHE
8358
+ texturecache = bimtextures.get(texdata);
83448359 }
83458360
8346
- if (texturecache == null || texturecache.resolution < resolution)
8361
+ if (texturecache == null || texturecache.resolution != -1 && texturecache.resolution < resolution)
83478362 {
83488363 TextureData texturedata = null;
83498364
8350
- if (bim != null)
8365
+ if (texdata != null && textureon)
83518366 {
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
+
83528378 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;
83538385 }
83548386 else
8355
- if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
8387
+ if (texname.endsWith("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
83568388 {
83578389 assert(!bump);
83588390 // if (bump)
....@@ -8363,7 +8395,7 @@
83638395 // }
83648396 // else
83658397 // {
8366
- texturecache = textures.get(tex);
8398
+ // texturecache = textures.get(texname); // suspicious
83678399 if (texturecache == null)
83688400 {
83698401 texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
....@@ -8372,10 +8404,10 @@
83728404 new Exception().printStackTrace();
83738405 // }
83748406 } else
8375
- if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
8407
+ if (texname.endsWith("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
83768408 {
83778409 assert(bump);
8378
- texturecache = textures.get(tex);
8410
+ // texturecache = textures.get(texname); // suspicious
83798411 if (texturecache == null)
83808412 texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
83818413 else
....@@ -8387,9 +8419,9 @@
83878419 // texture = GetResourceTexture("default.png");
83888420 //} else
83898421 //{
8390
- if (tex.equals("WHITE_NOISE"))
8422
+ if (texname.endsWith("WHITE_NOISE"))
83918423 {
8392
- texturecache = textures.get(tex);
8424
+ // texturecache = textures.get(texname); // suspicious
83938425 if (texturecache == null)
83948426 texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution);
83958427 else
....@@ -8452,17 +8484,16 @@
84528484 if (texturedata == null)
84538485 throw new Exception();
84548486
8455
- texturecache = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution);
8487
+ texturecache = new CacheTexture(texturedata,resolution);
84568488 //texture = GetTexture(tex, bump);
84578489 }
84588490 }
84598491 //}
84608492 }
84618493
8462
- if (/*CACHETEXTURE &&*/ texturecache != null && textureon)
8494
+ if (texdata == null && /*CACHETEXTURE &&*/ texturecache != null && textureon)
84638495 {
84648496 //return false;
8465
- assert(bim == null);
84668497
84678498 // System.out.println("CACHE +++++++++++++++ TEXTURE : " + texname + " (" + texture.getEstimatedMemorySize() + ")");
84688499 if (texturedata != null && texname.toLowerCase().endsWith(".jpg"))
....@@ -8509,18 +8540,20 @@
85098540 }
85108541 }
85118542 }
8512
-
8543
+
8544
+ Hashtable<cTexture, CacheTexture> textures = bump ? texturebump : texturepigment;
8545
+
85138546 //System.out.println("Texture = " + tex);
8514
- if (textures.containsKey(texname))
8547
+ if (textures.containsKey(tex))
85158548 {
8516
- CacheTexture thetex = textures.get(texname);
8549
+ CacheTexture thetex = textures.get(tex);
85178550 thetex.texture.disable();
85188551 thetex.texture.dispose();
8519
- textures.remove(texname);
8552
+ textures.remove(tex);
85208553 }
85218554
85228555 //texture.texturedata = texturedata;
8523
- textures.put(texname, texturecache);
8556
+ textures.put(tex, texturecache);
85248557
85258558 // newtex = true;
85268559 }
....@@ -8541,12 +8574,41 @@
85418574
85428575 static void EmbedTextures(cTexture tex)
85438576 {
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
+ }
85448593
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
+ }
85458607 }
85468608
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
85488610 {
8549
- CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
8611
+ CacheTexture texture = GetCacheTexture(tex, bump, resolution);
85508612
85518613 if (bump)
85528614 {
....@@ -8562,14 +8624,14 @@
85628624 return texture!=null?texture.texture:null;
85638625 }
85648626
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
85668628 {
8567
- CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
8629
+ CacheTexture texture = GetCacheTexture(tex, bump, resolution);
85688630
85698631 return texture!=null?texture.texturedata:null;
85708632 }
85718633
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
85738635 {
85748636 if (/*tex == null ||*/ ambientOcclusion ) // || !textureon)
85758637 {
....@@ -8578,7 +8640,7 @@
85788640
85798641 //boolean newtex = false;
85808642
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);
85828644
85838645 if (texture == null)
85848646 return false;
....@@ -8611,6 +8673,72 @@
86118673 return true; // Warning: not used.
86128674 }
86138675
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
+
86148742 ShadowBuffer shadowPBuf;
86158743 AntialiasBuffer antialiasPBuf;
86168744 int MAXSTACK;
....@@ -11065,7 +11193,7 @@
1106511193
1106611194 try
1106711195 {
11068
- BindTexture(null, NOISE_TEXTURE, false, 2);
11196
+ BindTexture(NOISE_TEXTURE, false, 2);
1106911197 }
1107011198 catch (Exception e)
1107111199 {
....@@ -11356,7 +11484,7 @@
1135611484 }
1135711485 }
1135811486
11359
- if (false) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion)
11487
+ if (false) //RENDERPROGRAM > 0 && DrawMode() == DEFAULT) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion)
1136011488 {
1136111489 //gl.glDepthFunc(GL.GL_LEQUAL);
1136211490 //gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
....@@ -11364,24 +11492,21 @@
1136411492
1136511493 boolean texon = textureon;
1136611494
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
+
1137211498 //gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB);
1137311499 //System.out.println("ALLO");
1137411500 gl.glColorMask(false, false, false, false);
1137511501 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
+
1138111506 gl.glColorMask(true, true, true, true);
1138211507
1138311508 gl.glDepthFunc(GL.GL_EQUAL);
11384
- //gl.glDepthMask(false);
11509
+ gl.glDepthMask(false);
1138511510 }
1138611511
1138711512 if (false) // DrawMode() == SHADOW)
....@@ -11721,20 +11846,32 @@
1172111846 ReleaseTextures(DEFAULT_TEXTURES);
1172211847
1172311848 if (CLEANCACHE)
11724
- for (java.util.Enumeration<String> e = textures.keys() ; e.hasMoreElements();)
11849
+ for (java.util.Enumeration<cTexture> e = texturepigment.keys() ; e.hasMoreElements();)
1172511850 {
11726
- String tex = e.nextElement();
11851
+ cTexture tex = e.nextElement();
1172711852
1172811853 // System.out.println("Texture --------- " + tex);
1172911854
11730
- if (tex.equals("WHITE_NOISE"))
11855
+ if (tex.equals("WHITE_NOISE:"))
1173111856 continue;
1173211857
1173311858 if (!usedtextures.contains(tex))
1173411859 {
11860
+ CacheTexture gettex = texturepigment.get(tex);
1173511861 // 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
+ }
1173811875 }
1173911876 }
1174011877 }
....@@ -12263,7 +12400,52 @@
1226312400 //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0);
1226412401
1226512402 String program =
12403
+ // Min shader
1226612404 "!!ARBfp1.0\n" +
12405
+ "PARAM zero123 = { 0.0, 1.0, 2.0, 1.25 };" +
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
+ "LRP temp, zero123.w, temp, one;" + // texture proportion
12437
+ "MUL temp, temp, ndotl.x;" +
12438
+
12439
+ "MUL temp, temp, zero123.z;" +
12440
+
12441
+ "MOV temp.w, zero123.y;" + // reset alpha
12442
+
12443
+ "MOV result.color, temp;" +
12444
+ "END";
12445
+
12446
+ String program2 =
12447
+ "!!ARBfp1.0\n" +
12448
+
1226712449 //"OPTION ARB_fragment_program_shadow;" +
1226812450 "PARAM light2cam0 = program.env[10];" +
1226912451 "PARAM light2cam1 = program.env[11];" +
....@@ -12378,8 +12560,7 @@
1237812560 "TEMP shininess;" +
1237912561 "\n" +
1238012562 "MOV texSamp, one;" +
12381
- //"TEX texSamp, fragment.texcoord[0], texture[0], 2D;" +
12382
-
12563
+
1238312564 "MOV mapgrid.x, one2048th.x;" +
1238412565 "MOV temp, fragment.texcoord[1];" +
1238512566 /*
....@@ -12784,7 +12965,7 @@
1278412965 "MAD shadow.x, buffer.x, frac.y, shadow.x;" +
1278512966 "") +
1278612967
12787
- // display shadow only (bump == 0)
12968
+ // display shadow only (fakedepth == 0)
1278812969 "SUB temp.x, half.x, shadow.x;" +
1278912970 "MOV temp.y, -params5.z;" + // params6.x;" +
1279012971 "SLT temp.z, temp.y, -one2048th.x;" +
....@@ -13216,20 +13397,20 @@
1321613397 return "TEX " + dest + ", " + src + ", texture[" + unit + "], 2D;" +
1321713398 "SGE " + src + ".w, " + src + ".x, eps.x;" +
1321813399 "SGE " + src + ".z, " + src + ".y, eps.x;" +
13400
+ "SLT " + dest + ".x, " + src + ".x, one.x;" +
13401
+ "SLT " + dest + ".y, " + src + ".y, one.x;" +
1321913402 "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13220
- "SLT " + src + ".z, " + src + ".x, one.x;" +
13221
- "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13222
- "SLT " + src + ".z, " + src + ".y, one.x;" +
13223
- "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13403
+ "MUL " + src + ".w, " + dest + ".x, " + src + ".w;" +
13404
+ "MUL " + src + ".w, " + dest + ".y, " + src + ".w;" +
1322413405 //"SWZ buffer, temp, w,w,w,w;";
13225
- "MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" +
13406
+ //"MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" +
1322613407 "SUB " + src + ".z, " + "one.x, " + src + ".w;" +
1322713408 //"MUL " + src + ".z, " + src + ".z, infinity.x;" +
1322813409 //"ADD " + dest + ".z, " + dest + ".z, " + src + ".z;";
13229
- "MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;";
13410
+ //"MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;";
1323013411
13231
- //"LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;";
13232
- //"LRP " + dest + ".z" + ", " + src + ".w, infinity.x," + dest + ".z;";
13412
+ //?? "LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;";
13413
+ "LRP " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;";
1323313414 }
1323413415
1323513416 String Shadow(String depth, String shadow)
....@@ -13276,7 +13457,7 @@
1327613457 "SLT temp.x, temp.x, zero.x;" + // shadoweps
1327713458 "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
1327813459
13279
- // No shadow when out of frustrum
13460
+ // No shadow when out of frustum
1328013461 "SGE temp.x, " + depth + ".z, one.z;" +
1328113462 "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
1328213463 "";
....@@ -14927,7 +15108,8 @@
1492715108 // break;
1492815109 case 'T':
1492915110 CACHETEXTURE ^= true;
14930
- textures.clear();
15111
+ texturepigment.clear();
15112
+ texturebump.clear();
1493115113 // repaint();
1493215114 break;
1493315115 case 'Y':