Normand Briere
2019-07-24 f555e2cacc4470c5b2217a14d40d2b39c4a57ba2
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,6 +2442,21 @@
24052442 return currentGL;
24062443 }
24072444
2445
+ static private BufferedImage CreateBim(TextureData texturedata)
2446
+ {
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);
2458
+ }
2459
+
24082460 /**/
24092461 class CacheTexture
24102462 {
....@@ -2413,18 +2465,20 @@
24132465
24142466 int resolution;
24152467
2416
- CacheTexture(com.sun.opengl.util.texture.Texture tex, int res)
2468
+ CacheTexture(com.sun.opengl.util.texture.TextureData texdata, int res)
24172469 {
2418
- texture = tex;
2470
+ texture = com.sun.opengl.util.texture.TextureIO.newTexture(texdata);
2471
+ texturedata = texdata;
24192472 resolution = res;
24202473 }
24212474 }
24222475 /**/
24232476
24242477 // TEXTURE static Texture texture;
2425
- static public java.util.Hashtable<String, CacheTexture> textures = new java.util.Hashtable<String, CacheTexture>();
2426
- static public java.util.Hashtable<BufferedImage, CacheTexture> bimtextures = new java.util.Hashtable<BufferedImage, CacheTexture>();
2427
- 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>();
24282482
24292483 int pigmentdepth = 0;
24302484 public com.sun.opengl.util.texture.Texture[] pigmentstack = new com.sun.opengl.util.texture.Texture[65536];
....@@ -2432,10 +2486,10 @@
24322486 public com.sun.opengl.util.texture.Texture[] bumpstack = new com.sun.opengl.util.texture.Texture[65536];
24332487 //public static String DEFAULT_TEXTURE = "DEFAULT_TEXTURE";
24342488 public static cTexture DEFAULT_TEXTURES = new cTexture("DEFAULT_TEXTURE" + ":" + "DEFAULT_TEXTURE_BUMP");
2435
- public static String NOISE_TEXTURE = "WHITE_NOISE";
2489
+ public static cTexture NOISE_TEXTURE = new cTexture("WHITE_NOISE:");
24362490 // public static cTexture IMMORTAL_TEXTURE = new cTexture("IMMORTAL");
24372491
2438
- com.sun.opengl.util.texture.Texture GetResourceTexture(String name, boolean bump)
2492
+ com.sun.opengl.util.texture.TextureData GetResourceTexture(String name, boolean bump)
24392493 {
24402494 TextureData texturedata = null;
24412495
....@@ -2454,16 +2508,16 @@
24542508 if (bump)
24552509 texturedata = ConvertBump(texturedata, false);
24562510
2457
- com.sun.opengl.util.texture.Texture texture =
2458
- 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);
24592513
2460
- texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT);
2461
- 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);
24622516
2463
- return texture;
2517
+ return texturedata;
24642518 }
24652519
2466
- com.sun.opengl.util.texture.Texture GetBimTexture(BufferedImage name, boolean bump)
2520
+ com.sun.opengl.util.texture.TextureData GetBimTexture(BufferedImage bim, boolean bump)
24672521 {
24682522 TextureData texturedata = null;
24692523
....@@ -2471,7 +2525,7 @@
24712525 {
24722526 texturedata =
24732527 com.sun.opengl.util.texture.TextureIO.newTextureData(
2474
- name,
2528
+ bim,
24752529 true);
24762530 } catch (Exception e)
24772531 {
....@@ -2480,14 +2534,8 @@
24802534
24812535 if (bump)
24822536 texturedata = ConvertBump(texturedata, false);
2483
-
2484
- com.sun.opengl.util.texture.Texture texture =
2485
- com.sun.opengl.util.texture.TextureIO.newTexture(texturedata);
2486
-
2487
- texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT);
2488
- texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_T, javax.media.opengl.GL.GL_REPEAT);
2489
-
2490
- return texture;
2537
+
2538
+ return texturedata;
24912539 }
24922540
24932541 boolean HUESMOOTH = true; // wrap around bug... true;
....@@ -7975,8 +8023,8 @@
79758023 pigment = null;
79768024 }
79778025
7978
- ReleaseTexture(bump, true);
7979
- ReleaseTexture(pigment, false);
8026
+ ReleaseTexture(tex, true);
8027
+ ReleaseTexture(tex, false);
79808028 }
79818029
79828030 public void ReleasePigmentTexture(cTexture tex) // INTERFACE
....@@ -8005,7 +8053,7 @@
80058053 pigment = null;
80068054 }
80078055
8008
- ReleaseTexture(pigment, false);
8056
+ ReleaseTexture(tex, false);
80098057 }
80108058
80118059 public void ReleaseBumpTexture(cTexture tex) // INTERFACE
....@@ -8034,10 +8082,10 @@
80348082 bump = null;
80358083 }
80368084
8037
- ReleaseTexture(bump, true);
8085
+ ReleaseTexture(tex, true);
80388086 }
80398087
8040
- void ReleaseTexture(String tex, boolean bump)
8088
+ void ReleaseTexture(cTexture tex, boolean bump)
80418089 {
80428090 if (// DrawMode() != 0 || /*tex == null ||*/
80438091 ambientOcclusion ) // || !textureon)
....@@ -8048,7 +8096,7 @@
80488096 CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null;
80498097
80508098 if (tex != null)
8051
- texture = textures.get(tex);
8099
+ texture = bump ? texturebump.get(tex) : texturepigment.get(tex);
80528100
80538101 // //assert( texture != null );
80548102 // if (texture == null)
....@@ -8198,13 +8246,13 @@
81988246
81998247 if (tex == null)
82008248 {
8201
- BindTexture(null, null,false,resolution);
8249
+ BindTexture(null,false,resolution);
82028250 return;
82038251 }
82048252
82058253 String pigment = Object3D.GetPigment(tex);
82068254
8207
- usedtextures.add(pigment);
8255
+ usedtextures.add(tex);
82088256
82098257 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
82108258 {
....@@ -8218,7 +8266,7 @@
82188266 }
82198267
82208268 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8221
- BindTexture(tex.pigmenttexture, pigment, false, resolution);
8269
+ BindTexture(tex, false, resolution);
82228270 }
82238271
82248272 /*boolean*/ public void BindBumpTexture(cTexture tex, int resolution) throws Exception // INTERFACE
....@@ -8231,13 +8279,13 @@
82318279
82328280 if (tex == null)
82338281 {
8234
- BindTexture(null, null,true,resolution);
8282
+ BindTexture(null,true,resolution);
82358283 return;
82368284 }
82378285
82388286 String bump = Object3D.GetBump(tex);
82398287
8240
- usedtextures.add(bump);
8288
+ usedtextures.add(tex);
82418289
82428290 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
82438291 {
....@@ -8251,7 +8299,7 @@
82518299 }
82528300
82538301 GetGL().glActiveTexture(GetGL().GL_TEXTURE2);
8254
- BindTexture(tex.bumptexture, bump, true, resolution);
8302
+ BindTexture(tex, true, resolution);
82558303 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
82568304 }
82578305
....@@ -8275,13 +8323,19 @@
82758323 return fileExists;
82768324 }
82778325
8278
- 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
82798327 {
82808328 CacheTexture texturecache = null;
82818329
82828330 if (tex != null)
82838331 {
8284
- 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
+ }
82858339
82868340 String fallbackTextureName = defaultDirectory + "/Textures/" + texname;
82878341
....@@ -8291,29 +8345,46 @@
82918345 // else
82928346 // if (!texname.startsWith("/"))
82938347 // texname = "/Users/nbriere/Textures/" + texname;
8294
- if (!FileExists(tex))
8348
+ if (!FileExists(texname))
82958349 {
82968350 texname = fallbackTextureName;
82978351 }
82988352
82998353 if (CACHETEXTURE)
83008354 {
8301
- if (bim == null)
8302
- texturecache = textures.get(texname); // TEXTURE CACHE
8355
+ if (texdata == null)
8356
+ texturecache = bump ? texturebump.get(tex) : texturepigment.get(tex);
83038357 else
8304
- texturecache = bimtextures.get(bim); // TEXTURE CACHE
8358
+ texturecache = bimtextures.get(texdata);
83058359 }
83068360
8307
- if (texturecache == null || texturecache.resolution < resolution)
8361
+ if (texturecache == null || texturecache.resolution != -1 && texturecache.resolution < resolution)
83088362 {
83098363 TextureData texturedata = null;
83108364
8311
- if (bim == null)
8365
+ if (texdata != null && textureon)
83128366 {
8367
+ BufferedImage bim; // = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB);
83138368
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
+
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;
83148385 }
83158386 else
8316
- if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
8387
+ if (texname.endsWith("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
83178388 {
83188389 assert(!bump);
83198390 // if (bump)
....@@ -8324,19 +8395,23 @@
83248395 // }
83258396 // else
83268397 // {
8327
- texturecache = textures.get(tex);
8398
+ // texturecache = textures.get(texname); // suspicious
83288399 if (texturecache == null)
83298400 {
83308401 texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
83318402 }
8403
+ else
8404
+ new Exception().printStackTrace();
83328405 // }
83338406 } else
8334
- if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
8407
+ if (texname.endsWith("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
83358408 {
83368409 assert(bump);
8337
- texturecache = textures.get(tex);
8410
+ // texturecache = textures.get(texname); // suspicious
83388411 if (texturecache == null)
83398412 texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
8413
+ else
8414
+ new Exception().printStackTrace();
83408415 } else
83418416 {
83428417 //if (tex.equals("IMMORTAL"))
....@@ -8344,11 +8419,13 @@
83448419 // texture = GetResourceTexture("default.png");
83458420 //} else
83468421 //{
8347
- if (tex.equals("WHITE_NOISE"))
8422
+ if (texname.endsWith("WHITE_NOISE"))
83488423 {
8349
- texturecache = textures.get(tex);
8424
+ // texturecache = textures.get(texname); // suspicious
83508425 if (texturecache == null)
83518426 texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution);
8427
+ else
8428
+ new Exception().printStackTrace();
83528429 } else
83538430 {
83548431 if (textureon)
....@@ -8407,14 +8484,14 @@
84078484 if (texturedata == null)
84088485 throw new Exception();
84098486
8410
- texturecache = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution);
8487
+ texturecache = new CacheTexture(texturedata,resolution);
84118488 //texture = GetTexture(tex, bump);
84128489 }
84138490 }
84148491 //}
84158492 }
84168493
8417
- if (/*CACHETEXTURE &&*/ texturecache != null && textureon)
8494
+ if (texdata == null && /*CACHETEXTURE &&*/ texturecache != null && textureon)
84188495 {
84198496 //return false;
84208497
....@@ -8436,52 +8513,17 @@
84368513 File cachefile = new File(texname.substring(0, texname.length()-4)+ext+".jpg");
84378514 if (!cachefile.exists())
84388515 {
8439
- // cache to disk
8440
- Buffer buffer = texturedata.getBuffer(); // getMipmapData();
8441
- //buffers[0].
8442
-
8443
- ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer();
8444
- int[] pixels = new int[bytebuf.capacity()/3];
8445
-
8446
- // squared size heuristic...
8447
- if ((int)Math.sqrt(pixels.length) == Math.sqrt(pixels.length))
8516
+ //if (texturedata.getWidth() == texturedata.getHeight())
84488517 {
8449
- for (int i=pixels.length; --i>=0;)
8450
- {
8451
- int i3 = i*3;
8452
- pixels[i] = 0xFF;
8453
- pixels[i] <<= 8;
8454
- pixels[i] |= bytebuf.get(i3+2) & 0xFF;
8455
- pixels[i] <<= 8;
8456
- pixels[i] |= bytebuf.get(i3+1) & 0xFF;
8457
- pixels[i] <<= 8;
8458
- pixels[i] |= bytebuf.get(i3) & 0xFF;
8459
- }
8460
-
8461
- /*
8462
- int r=0,g=0,b=0,a=0;
8463
- for (int i=0; i<width; i++)
8464
- for (int j=0; j<height; j++)
8465
- {
8466
- int index = j*width+i;
8467
- int p = pixels[index];
8468
- a = ((p>>24) & 0xFF);
8469
- r = ((p>>16) & 0xFF);
8470
- g = ((p>>8) & 0xFF);
8471
- b = (p & 0xFF);
8472
- pixels[index] = (a<<24) | (b<<16) | (g<<8) | r;
8473
- }
8474
- /**/
8475
- int width = (int)Math.sqrt(pixels.length); // squared
8476
- int height = width;
8477
- BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile);
8478
- rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width);
8518
+ BufferedImage rendImage = CreateBim(texturedata);
8519
+
84798520 ImageWriter writer = null;
84808521 Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
84818522 if (iter.hasNext()) {
84828523 writer = (ImageWriter)iter.next();
84838524 }
8484
- float compressionQuality = 0.9f;
8525
+
8526
+ float compressionQuality = 0.85f;
84858527 try
84868528 {
84878529 ImageOutputStream ios = ImageIO.createImageOutputStream(cachefile);
....@@ -8498,18 +8540,20 @@
84988540 }
84998541 }
85008542 }
8501
-
8543
+
8544
+ Hashtable<cTexture, CacheTexture> textures = bump ? texturebump : texturepigment;
8545
+
85028546 //System.out.println("Texture = " + tex);
8503
- if (textures.containsKey(texname))
8547
+ if (textures.containsKey(tex))
85048548 {
8505
- CacheTexture thetex = textures.get(texname);
8549
+ CacheTexture thetex = textures.get(tex);
85068550 thetex.texture.disable();
85078551 thetex.texture.dispose();
8508
- textures.remove(texname);
8552
+ textures.remove(tex);
85098553 }
85108554
85118555 //texture.texturedata = texturedata;
8512
- textures.put(texname, texturecache);
8556
+ textures.put(tex, texturecache);
85138557
85148558 // newtex = true;
85158559 }
....@@ -8528,9 +8572,43 @@
85288572 return texturecache;
85298573 }
85308574
8531
- com.sun.opengl.util.texture.Texture GetTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception
8575
+ static void EmbedTextures(cTexture tex)
85328576 {
8533
- CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
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
+ }
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
+ }
8607
+ }
8608
+
8609
+ com.sun.opengl.util.texture.Texture GetTexture(cTexture tex, boolean bump, int resolution) throws Exception
8610
+ {
8611
+ CacheTexture texture = GetCacheTexture(tex, bump, resolution);
85348612
85358613 if (bump)
85368614 {
....@@ -8546,14 +8624,14 @@
85468624 return texture!=null?texture.texture:null;
85478625 }
85488626
8549
- 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
85508628 {
8551
- CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
8629
+ CacheTexture texture = GetCacheTexture(tex, bump, resolution);
85528630
85538631 return texture!=null?texture.texturedata:null;
85548632 }
85558633
8556
- 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
85578635 {
85588636 if (/*tex == null ||*/ ambientOcclusion ) // || !textureon)
85598637 {
....@@ -8562,7 +8640,7 @@
85628640
85638641 //boolean newtex = false;
85648642
8565
- com.sun.opengl.util.texture.Texture texture = GetTexture(stream, tex, bump, resolution);
8643
+ com.sun.opengl.util.texture.Texture texture = GetTexture(tex, bump, resolution);
85668644
85678645 if (texture == null)
85688646 return false;
....@@ -8595,6 +8673,72 @@
85958673 return true; // Warning: not used.
85968674 }
85978675
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
+
85988742 ShadowBuffer shadowPBuf;
85998743 AntialiasBuffer antialiasPBuf;
86008744 int MAXSTACK;
....@@ -11049,7 +11193,7 @@
1104911193
1105011194 try
1105111195 {
11052
- BindTexture(null, NOISE_TEXTURE, false, 2);
11196
+ BindTexture(NOISE_TEXTURE, false, 2);
1105311197 }
1105411198 catch (Exception e)
1105511199 {
....@@ -11340,7 +11484,7 @@
1134011484 }
1134111485 }
1134211486
11343
- if (false) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion)
11487
+ if (false) //RENDERPROGRAM > 0 && DrawMode() == DEFAULT) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion)
1134411488 {
1134511489 //gl.glDepthFunc(GL.GL_LEQUAL);
1134611490 //gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
....@@ -11348,24 +11492,21 @@
1134811492
1134911493 boolean texon = textureon;
1135011494
11351
- if (RENDERPROGRAM > 0)
11352
- {
11353
- gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB);
11354
- textureon = false;
11355
- }
11495
+ gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB);
11496
+ textureon = false;
11497
+
1135611498 //gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB);
1135711499 //System.out.println("ALLO");
1135811500 gl.glColorMask(false, false, false, false);
1135911501 DrawObject(gl);
11360
- if (RENDERPROGRAM > 0)
11361
- {
11362
- gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB);
11363
- textureon = texon;
11364
- }
11502
+
11503
+ gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB);
11504
+ textureon = texon;
11505
+
1136511506 gl.glColorMask(true, true, true, true);
1136611507
1136711508 gl.glDepthFunc(GL.GL_EQUAL);
11368
- //gl.glDepthMask(false);
11509
+ gl.glDepthMask(false);
1136911510 }
1137011511
1137111512 if (false) // DrawMode() == SHADOW)
....@@ -11705,20 +11846,32 @@
1170511846 ReleaseTextures(DEFAULT_TEXTURES);
1170611847
1170711848 if (CLEANCACHE)
11708
- for (java.util.Enumeration<String> e = textures.keys() ; e.hasMoreElements();)
11849
+ for (java.util.Enumeration<cTexture> e = texturepigment.keys() ; e.hasMoreElements();)
1170911850 {
11710
- String tex = e.nextElement();
11851
+ cTexture tex = e.nextElement();
1171111852
1171211853 // System.out.println("Texture --------- " + tex);
1171311854
11714
- if (tex.equals("WHITE_NOISE"))
11855
+ if (tex.equals("WHITE_NOISE:"))
1171511856 continue;
1171611857
1171711858 if (!usedtextures.contains(tex))
1171811859 {
11860
+ CacheTexture gettex = texturepigment.get(tex);
1171911861 // System.out.println("DISPOSE +++++++++++++++ " + tex);
11720
- textures.get(tex).texture.dispose();
11721
- 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
+ }
1172211875 }
1172311876 }
1172411877 }
....@@ -12247,7 +12400,52 @@
1224712400 //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0);
1224812401
1224912402 String program =
12403
+ // Min shader
1225012404 "!!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
+
1225112449 //"OPTION ARB_fragment_program_shadow;" +
1225212450 "PARAM light2cam0 = program.env[10];" +
1225312451 "PARAM light2cam1 = program.env[11];" +
....@@ -12362,8 +12560,7 @@
1236212560 "TEMP shininess;" +
1236312561 "\n" +
1236412562 "MOV texSamp, one;" +
12365
- //"TEX texSamp, fragment.texcoord[0], texture[0], 2D;" +
12366
-
12563
+
1236712564 "MOV mapgrid.x, one2048th.x;" +
1236812565 "MOV temp, fragment.texcoord[1];" +
1236912566 /*
....@@ -12768,7 +12965,7 @@
1276812965 "MAD shadow.x, buffer.x, frac.y, shadow.x;" +
1276912966 "") +
1277012967
12771
- // display shadow only (bump == 0)
12968
+ // display shadow only (fakedepth == 0)
1277212969 "SUB temp.x, half.x, shadow.x;" +
1277312970 "MOV temp.y, -params5.z;" + // params6.x;" +
1277412971 "SLT temp.z, temp.y, -one2048th.x;" +
....@@ -13200,20 +13397,20 @@
1320013397 return "TEX " + dest + ", " + src + ", texture[" + unit + "], 2D;" +
1320113398 "SGE " + src + ".w, " + src + ".x, eps.x;" +
1320213399 "SGE " + src + ".z, " + src + ".y, eps.x;" +
13400
+ "SLT " + dest + ".x, " + src + ".x, one.x;" +
13401
+ "SLT " + dest + ".y, " + src + ".y, one.x;" +
1320313402 "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13204
- "SLT " + src + ".z, " + src + ".x, one.x;" +
13205
- "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13206
- "SLT " + src + ".z, " + src + ".y, one.x;" +
13207
- "MUL " + src + ".w, " + src + ".z, " + src + ".w;" +
13403
+ "MUL " + src + ".w, " + dest + ".x, " + src + ".w;" +
13404
+ "MUL " + src + ".w, " + dest + ".y, " + src + ".w;" +
1320813405 //"SWZ buffer, temp, w,w,w,w;";
13209
- "MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" +
13406
+ //"MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" +
1321013407 "SUB " + src + ".z, " + "one.x, " + src + ".w;" +
1321113408 //"MUL " + src + ".z, " + src + ".z, infinity.x;" +
1321213409 //"ADD " + dest + ".z, " + dest + ".z, " + src + ".z;";
13213
- "MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;";
13410
+ //"MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;";
1321413411
13215
- //"LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;";
13216
- //"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;";
1321713414 }
1321813415
1321913416 String Shadow(String depth, String shadow)
....@@ -13260,7 +13457,7 @@
1326013457 "SLT temp.x, temp.x, zero.x;" + // shadoweps
1326113458 "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
1326213459
13263
- // No shadow when out of frustrum
13460
+ // No shadow when out of frustum
1326413461 "SGE temp.x, " + depth + ".z, one.z;" +
1326513462 "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
1326613463 "";
....@@ -14058,14 +14255,15 @@
1405814255 drag = false;
1405914256 //System.out.println("Mouse DOWN");
1406014257 editObj = false;
14061
- ClickInfo info = new ClickInfo();
14062
- info.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom));
14063
- info.pane = this;
14064
- info.camera = renderCamera;
14065
- info.x = x;
14066
- info.y = y;
14067
- info.modifiers = modifiersex;
14068
- editObj = object.doEditClick(info, 0);
14258
+ //ClickInfo info = new ClickInfo();
14259
+ object.clickInfo.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom));
14260
+ object.clickInfo.pane = this;
14261
+ object.clickInfo.camera = renderCamera;
14262
+ object.clickInfo.x = x;
14263
+ object.clickInfo.y = y;
14264
+ object.clickInfo.modifiers = modifiersex;
14265
+ editObj = object.doEditClick(//info,
14266
+ 0);
1406914267 if (!editObj)
1407014268 {
1407114269 hasMarquee = true;
....@@ -14465,15 +14663,16 @@
1446514663 if (editObj)
1446614664 {
1446714665 drag = true;
14468
- ClickInfo info = new ClickInfo();
14469
- info.bounds.setBounds(0, 0,
14666
+ //ClickInfo info = new ClickInfo();
14667
+ object.clickInfo.bounds.setBounds(0, 0,
1447014668 (int) (getBounds().width * zoom), (int) (getBounds().height * zoom));
14471
- info.pane = this;
14472
- info.camera = renderCamera;
14473
- info.x = x;
14474
- info.y = y;
14475
- object.GetWindow().copy
14476
- .doEditDrag(info, (modifiers & MouseEvent.BUTTON3_MASK) != 0);
14669
+ object.clickInfo.pane = this;
14670
+ object.clickInfo.camera = renderCamera;
14671
+ object.clickInfo.x = x;
14672
+ object.clickInfo.y = y;
14673
+ object //.GetWindow().copy
14674
+ .doEditDrag(//info,
14675
+ (modifiers & MouseEvent.BUTTON3_MASK) != 0);
1447714676 } else
1447814677 {
1447914678 if (x < startX)
....@@ -14622,24 +14821,27 @@
1462214821 }
1462314822 }
1462414823
14824
+// ClickInfo clickInfo = new ClickInfo();
14825
+
1462514826 public void mouseMoved(MouseEvent e)
1462614827 {
1462714828 //System.out.println("mouseMoved: " + e);
1462814829 if (isRenderer)
1462914830 return;
1463014831
14631
- ClickInfo ci = new ClickInfo();
14632
- ci.x = e.getX();
14633
- ci.y = e.getY();
14634
- ci.modifiers = e.getModifiersEx();
14635
- ci.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom));
14636
- ci.pane = this;
14637
- ci.camera = renderCamera;
14832
+ // Mouse cursor feedback
14833
+ object.clickInfo.x = e.getX();
14834
+ object.clickInfo.y = e.getY();
14835
+ object.clickInfo.modifiers = e.getModifiersEx();
14836
+ object.clickInfo.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom));
14837
+ object.clickInfo.pane = this;
14838
+ object.clickInfo.camera = renderCamera;
1463814839 if (!isRenderer)
1463914840 {
1464014841 //ObjEditor editWindow = object.editWindow;
1464114842 //Object3D copy = editWindow.copy;
14642
- if (object.doEditClick(ci, 0))
14843
+ if (object.doEditClick(//clickInfo,
14844
+ 0))
1464314845 {
1464414846 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
1464514847 } else
....@@ -14911,7 +15113,8 @@
1491115113 // break;
1491215114 case 'T':
1491315115 CACHETEXTURE ^= true;
14914
- textures.clear();
15116
+ texturepigment.clear();
15117
+ texturebump.clear();
1491515118 // repaint();
1491615119 break;
1491715120 case 'Y':
....@@ -15664,8 +15867,6 @@
1566415867
1566515868 int width = getBounds().width;
1566615869 int height = getBounds().height;
15667
- ClickInfo info = new ClickInfo();
15668
- info.bounds.setBounds(0, 0, (int) (width * zoom), (int) (height * zoom));
1566915870 //Image img = CreateImage(width, height);
1567015871 //System.out.println("width = " + width + "; height = " + height + "\n");
1567115872
....@@ -15742,31 +15943,37 @@
1574215943 }
1574315944 if (object != null && !hasMarquee)
1574415945 {
15946
+ if (object.clickInfo == null)
15947
+ object.clickInfo = new ClickInfo();
15948
+ ClickInfo info = object.clickInfo;
15949
+ //ClickInfo info = new ClickInfo();
15950
+ info.bounds.setBounds(0, 0, (int) (width * zoom), (int) (height * zoom));
15951
+
1574515952 if (isRenderer)
1574615953 {
15747
- info.flags++;
15954
+ object.clickInfo.flags++;
1574815955 double frameAspect = (double) width / (double) height;
1574915956 if (frameAspect > renderCamera.aspect)
1575015957 {
1575115958 int desired = (int) ((double) height * renderCamera.aspect);
15752
- info.bounds.width -= width - desired;
15753
- info.bounds.x += (width - desired) / 2;
15959
+ object.clickInfo.bounds.width -= width - desired;
15960
+ object.clickInfo.bounds.x += (width - desired) / 2;
1575415961 } else
1575515962 {
1575615963 int desired = (int) ((double) width / renderCamera.aspect);
15757
- info.bounds.height -= height - desired;
15758
- info.bounds.y += (height - desired) / 2;
15964
+ object.clickInfo.bounds.height -= height - desired;
15965
+ object.clickInfo.bounds.y += (height - desired) / 2;
1575915966 }
1576015967 }
1576115968
15762
- info.g = gr;
15763
- info.camera = renderCamera;
15969
+ object.clickInfo.g = gr;
15970
+ object.clickInfo.camera = renderCamera;
1576415971 /*
1576515972 // Memory intensive (brep.verticescopy)
1576615973 if (!(object instanceof Composite))
1576715974 object.draw(info, 0, false); // SLOW :
1576815975 */
15769
- if (!isRenderer)
15976
+ if (!isRenderer) // && drag)
1577015977 {
1577115978 Grafreed.Assert(object != null);
1577215979 Grafreed.Assert(object.selection != null);
....@@ -15774,9 +15981,9 @@
1577415981 {
1577515982 int hitSomething = object.selection.get(0).hitSomething;
1577615983
15777
- info.DX = 0;
15778
- info.DY = 0;
15779
- info.W = 1;
15984
+ object.clickInfo.DX = 0;
15985
+ object.clickInfo.DY = 0;
15986
+ object.clickInfo.W = 1;
1578015987 if (hitSomething == Object3D.hitCenter)
1578115988 {
1578215989 info.DX = X;
....@@ -15788,7 +15995,8 @@
1578815995 info.DY -= info.bounds.height/2;
1578915996 }
1579015997
15791
- object.drawEditHandles(info, 0);
15998
+ object.drawEditHandles(//info,
15999
+ 0);
1579216000
1579316001 if (drag && (X != 0 || Y != 0))
1579416002 {
....@@ -15801,7 +16009,7 @@
1580116009 gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2);
1580216010 break;
1580316011 case Object3D.hitScale: gr.setColor(Color.cyan);
15804
- gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2);
16012
+ gr.drawLine(X, Y, 0, 0);
1580516013 break;
1580616014 }
1580716015