Normand Briere
2019-07-21 d32f24f147068e6cbecb31c7f98047f68bc8b58a
CameraPane.java
....@@ -60,7 +60,7 @@
6060 //boolean REDUCETEXTURE = true;
6161 boolean CACHETEXTURE = true;
6262 boolean CLEANCACHE = false; // true;
63
- boolean MIPMAP = false; // true;
63
+ boolean MIPMAP = true; // false; // true;
6464 boolean COMPRESSTEXTURE = false;
6565 boolean KOMPACTTEXTURE = false; // true;
6666 boolean RESIZETEXTURE = false;
....@@ -2405,6 +2405,45 @@
24052405 return currentGL;
24062406 }
24072407
2408
+ private BufferedImage CreateBim(TextureData texturedata)
2409
+ {
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;
2445
+ }
2446
+
24082447 /**/
24092448 class CacheTexture
24102449 {
....@@ -2422,9 +2461,10 @@
24222461 /**/
24232462
24242463 // TEXTURE static Texture texture;
2425
- static public java.util.Hashtable<String, CacheTexture/*com.sun.opengl.util.texture.Texture*/> textures
2426
- = new java.util.Hashtable<String, CacheTexture/*com.sun.opengl.util.texture.Texture*/>();
2427
- static public java.util.Hashtable<String, String> usedtextures = new java.util.Hashtable<String, String>();
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>();
2467
+
24282468 int pigmentdepth = 0;
24292469 public com.sun.opengl.util.texture.Texture[] pigmentstack = new com.sun.opengl.util.texture.Texture[65536];
24302470 int bumpdepth = 0;
....@@ -2446,6 +2486,33 @@
24462486 true,
24472487 com.sun.opengl.util.texture.TextureIO.PNG);
24482488 } catch (java.io.IOException e)
2489
+ {
2490
+ throw new javax.media.opengl.GLException(e);
2491
+ }
2492
+
2493
+ if (bump)
2494
+ texturedata = ConvertBump(texturedata, false);
2495
+
2496
+ com.sun.opengl.util.texture.Texture texture =
2497
+ com.sun.opengl.util.texture.TextureIO.newTexture(texturedata);
2498
+
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);
2501
+
2502
+ return texture;
2503
+ }
2504
+
2505
+ com.sun.opengl.util.texture.Texture GetBimTexture(BufferedImage name, boolean bump)
2506
+ {
2507
+ TextureData texturedata = null;
2508
+
2509
+ try
2510
+ {
2511
+ texturedata =
2512
+ com.sun.opengl.util.texture.TextureIO.newTextureData(
2513
+ name,
2514
+ true);
2515
+ } catch (Exception e)
24492516 {
24502517 throw new javax.media.opengl.GLException(e);
24512518 }
....@@ -3532,6 +3599,8 @@
35323599
35333600 System.out.println("LOADING TEXTURE : " + name);
35343601
3602
+ Object x = texturedata.getMipmapData(); // .getBuffer();
3603
+
35353604 //
35363605 if (false) // compressbit > 0)
35373606 {
....@@ -8112,47 +8181,50 @@
81128181
81138182 /*boolean*/ public void BindTextures(cTexture tex, int resolution) throws Exception // INTERFACE
81148183 {
8115
- if (// DrawMode() != 0 || /*tex == null ||*/
8116
- ambientOcclusion ) // || !textureon)
8117
- {
8118
- return; // false;
8119
- }
8120
-
8121
- if (tex == null)
8122
- {
8123
- BindTexture(null,false,resolution);
8124
- BindTexture(null,true,resolution);
8125
- return;
8126
- }
8184
+// if (// DrawMode() != 0 || /*tex == null ||*/
8185
+// ambientOcclusion ) // || !textureon)
8186
+// {
8187
+// return; // false;
8188
+// }
8189
+//
8190
+// if (tex == null)
8191
+// {
8192
+// BindTexture(null,false,resolution);
8193
+// BindTexture(null,true,resolution);
8194
+// return;
8195
+// }
8196
+//
8197
+// String pigment = Object3D.GetPigment(tex);
8198
+// String bump = Object3D.GetBump(tex);
8199
+//
8200
+// usedtextures.add(pigment);
8201
+// usedtextures.add(bump);
8202
+//
8203
+// //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
8204
+// {
8205
+// // System.out.print("BIND +++++++++++++++ pigment = " + pigment);
8206
+// // System.out.println("; bump = " + bump);
8207
+// }
8208
+//
8209
+// if (bump.equals(""))
8210
+// {
8211
+// bump = null;
8212
+// }
8213
+// if (pigment.equals(""))
8214
+// {
8215
+// pigment = null;
8216
+// }
8217
+//
8218
+// GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8219
+// BindTexture(pigment, false, resolution);
8220
+// GetGL().glActiveTexture(GetGL().GL_TEXTURE2);
8221
+// BindTexture(bump, true, resolution);
8222
+// GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8223
+//
8224
+// return; // true;
81278225
8128
- String pigment = Object3D.GetPigment(tex);
8129
- String bump = Object3D.GetBump(tex);
8130
-
8131
- usedtextures.put(pigment, pigment);
8132
- usedtextures.put(bump, bump);
8133
-
8134
- //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
8135
- {
8136
- // System.out.print("BIND +++++++++++++++ pigment = " + pigment);
8137
- // System.out.println("; bump = " + bump);
8138
- }
8139
-
8140
- if (bump.equals(""))
8141
- {
8142
- bump = null;
8143
- }
8144
- if (pigment.equals(""))
8145
- {
8146
- pigment = null;
8147
- }
8148
-
8149
- GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8150
- BindTexture(pigment, false, resolution);
8151
- GetGL().glActiveTexture(GetGL().GL_TEXTURE2);
8152
- BindTexture(bump, true, resolution);
8153
- GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8154
-
8155
- return; // true;
8226
+ BindPigmentTexture(tex, resolution);
8227
+ BindBumpTexture(tex, resolution);
81568228 }
81578229
81588230 /*boolean*/ public void BindPigmentTexture(cTexture tex, int resolution) throws Exception // INTERFACE
....@@ -8165,13 +8237,13 @@
81658237
81668238 if (tex == null)
81678239 {
8168
- BindTexture(null,false,resolution);
8240
+ BindTexture(null, null,false,resolution);
81698241 return;
81708242 }
81718243
81728244 String pigment = Object3D.GetPigment(tex);
81738245
8174
- usedtextures.put(pigment, pigment);
8246
+ usedtextures.add(pigment);
81758247
81768248 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
81778249 {
....@@ -8185,7 +8257,7 @@
81858257 }
81868258
81878259 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
8188
- BindTexture(pigment, false, resolution);
8260
+ BindTexture(tex.pigmenttexture, pigment, false, resolution);
81898261 }
81908262
81918263 /*boolean*/ public void BindBumpTexture(cTexture tex, int resolution) throws Exception // INTERFACE
....@@ -8198,13 +8270,13 @@
81988270
81998271 if (tex == null)
82008272 {
8201
- BindTexture(null,true,resolution);
8273
+ BindTexture(null, null,true,resolution);
82028274 return;
82038275 }
82048276
82058277 String bump = Object3D.GetBump(tex);
82068278
8207
- usedtextures.put(bump, bump);
8279
+ usedtextures.add(bump);
82088280
82098281 //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES))
82108282 {
....@@ -8218,7 +8290,7 @@
82188290 }
82198291
82208292 GetGL().glActiveTexture(GetGL().GL_TEXTURE2);
8221
- BindTexture(bump, true, resolution);
8293
+ BindTexture(tex.bumptexture, bump, true, resolution);
82228294 GetGL().glActiveTexture(GetGL().GL_TEXTURE0);
82238295 }
82248296
....@@ -8242,9 +8314,9 @@
82428314 return fileExists;
82438315 }
82448316
8245
- CacheTexture GetCacheTexture(String tex, boolean bump, int resolution) throws Exception
8317
+ CacheTexture GetCacheTexture(java.awt.image.BufferedImage bim, String tex, boolean bump, int resolution) throws Exception
82468318 {
8247
- CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null;
8319
+ CacheTexture texturecache = null;
82488320
82498321 if (tex != null)
82508322 {
....@@ -8264,12 +8336,22 @@
82648336 }
82658337
82668338 if (CACHETEXTURE)
8267
- texture = textures.get(texname); // TEXTURE CACHE
8268
-
8269
- TextureData texturedata = null;
8270
-
8271
- if (texture == null || texture.resolution < resolution)
82728339 {
8340
+ if (bim == null)
8341
+ texturecache = textures.get(texname); // TEXTURE CACHE
8342
+ else
8343
+ texturecache = bimtextures.get(bim); // TEXTURE CACHE
8344
+ }
8345
+
8346
+ if (texturecache == null || texturecache.resolution < resolution)
8347
+ {
8348
+ TextureData texturedata = null;
8349
+
8350
+ if (bim != null)
8351
+ {
8352
+ texturecache = new CacheTexture(GetBimTexture(bim, bump), -1);
8353
+ }
8354
+ else
82738355 if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
82748356 {
82758357 assert(!bump);
....@@ -8281,19 +8363,23 @@
82818363 // }
82828364 // else
82838365 // {
8284
- texture = textures.get(tex);
8285
- if (texture == null)
8366
+ texturecache = textures.get(tex);
8367
+ if (texturecache == null)
82868368 {
8287
- texture = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
8369
+ texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
82888370 }
8371
+ else
8372
+ new Exception().printStackTrace();
82898373 // }
82908374 } else
82918375 if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
82928376 {
82938377 assert(bump);
8294
- texture = textures.get(tex);
8295
- if (texture == null)
8296
- texture = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
8378
+ texturecache = textures.get(tex);
8379
+ if (texturecache == null)
8380
+ texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
8381
+ else
8382
+ new Exception().printStackTrace();
82978383 } else
82988384 {
82998385 //if (tex.equals("IMMORTAL"))
....@@ -8303,9 +8389,11 @@
83038389 //{
83048390 if (tex.equals("WHITE_NOISE"))
83058391 {
8306
- texture = textures.get(tex);
8307
- if (texture == null)
8308
- texture = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution);
8392
+ texturecache = textures.get(tex);
8393
+ if (texturecache == null)
8394
+ texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution);
8395
+ else
8396
+ new Exception().printStackTrace();
83098397 } else
83108398 {
83118399 if (textureon)
....@@ -8364,19 +8452,20 @@
83648452 if (texturedata == null)
83658453 throw new Exception();
83668454
8367
- texture = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution);
8455
+ texturecache = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution);
83688456 //texture = GetTexture(tex, bump);
83698457 }
83708458 }
83718459 //}
83728460 }
83738461
8374
- if (/*CACHETEXTURE &&*/ texture != null && textureon)
8462
+ if (/*CACHETEXTURE &&*/ texturecache != null && textureon)
83758463 {
83768464 //return false;
8465
+ assert(bim == null);
83778466
83788467 // System.out.println("CACHE +++++++++++++++ TEXTURE : " + texname + " (" + texture.getEstimatedMemorySize() + ")");
8379
- if (texturedata != null && (texname.endsWith(".jpg") || texname.endsWith(".JPG")))
8468
+ if (texturedata != null && texname.toLowerCase().endsWith(".jpg"))
83808469 {
83818470 // String ext = "_highres";
83828471 // if (REDUCETEXTURE)
....@@ -8393,52 +8482,17 @@
83938482 File cachefile = new File(texname.substring(0, texname.length()-4)+ext+".jpg");
83948483 if (!cachefile.exists())
83958484 {
8396
- // cache to disk
8397
- Buffer buffer = texturedata.getBuffer(); // getMipmapData();
8398
- //buffers[0].
8399
-
8400
- ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer();
8401
- int[] pixels = new int[bytebuf.capacity()/3];
8402
-
8403
- // squared size heuristic...
8404
- if ((int)Math.sqrt(pixels.length) == Math.sqrt(pixels.length))
8485
+ //if (texturedata.getWidth() == texturedata.getHeight())
84058486 {
8406
- for (int i=pixels.length; --i>=0;)
8407
- {
8408
- int i3 = i*3;
8409
- pixels[i] = 0xFF;
8410
- pixels[i] <<= 8;
8411
- pixels[i] |= bytebuf.get(i3+2) & 0xFF;
8412
- pixels[i] <<= 8;
8413
- pixels[i] |= bytebuf.get(i3+1) & 0xFF;
8414
- pixels[i] <<= 8;
8415
- pixels[i] |= bytebuf.get(i3) & 0xFF;
8416
- }
8417
-
8418
- /*
8419
- int r=0,g=0,b=0,a=0;
8420
- for (int i=0; i<width; i++)
8421
- for (int j=0; j<height; j++)
8422
- {
8423
- int index = j*width+i;
8424
- int p = pixels[index];
8425
- a = ((p>>24) & 0xFF);
8426
- r = ((p>>16) & 0xFF);
8427
- g = ((p>>8) & 0xFF);
8428
- b = (p & 0xFF);
8429
- pixels[index] = (a<<24) | (b<<16) | (g<<8) | r;
8430
- }
8431
- /**/
8432
- int width = (int)Math.sqrt(pixels.length); // squared
8433
- int height = width;
8434
- BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile);
8435
- rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width);
8487
+ BufferedImage rendImage = CreateBim(texturedata);
8488
+
84368489 ImageWriter writer = null;
84378490 Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
84388491 if (iter.hasNext()) {
84398492 writer = (ImageWriter)iter.next();
84408493 }
8441
- float compressionQuality = 0.9f;
8494
+
8495
+ float compressionQuality = 0.85f;
84428496 try
84438497 {
84448498 ImageOutputStream ios = ImageIO.createImageOutputStream(cachefile);
....@@ -8465,8 +8519,8 @@
84658519 textures.remove(texname);
84668520 }
84678521
8468
- texture.texturedata = texturedata;
8469
- textures.put(texname, texture);
8522
+ //texture.texturedata = texturedata;
8523
+ textures.put(texname, texturecache);
84708524
84718525 // newtex = true;
84728526 }
....@@ -8482,12 +8536,17 @@
84828536 }
84838537 }
84848538
8485
- return texture;
8539
+ return texturecache;
84868540 }
84878541
8488
- com.sun.opengl.util.texture.Texture GetTexture(String tex, boolean bump, int resolution) throws Exception
8542
+ static void EmbedTextures(cTexture tex)
84898543 {
8490
- CacheTexture texture = GetCacheTexture(tex, bump, resolution);
8544
+
8545
+ }
8546
+
8547
+ com.sun.opengl.util.texture.Texture GetTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception
8548
+ {
8549
+ CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
84918550
84928551 if (bump)
84938552 {
....@@ -8503,14 +8562,14 @@
85038562 return texture!=null?texture.texture:null;
85048563 }
85058564
8506
- public com.sun.opengl.util.texture.TextureData GetTextureData(String tex, boolean bump, int resolution) throws Exception
8565
+ public com.sun.opengl.util.texture.TextureData GetTextureData(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception
85078566 {
8508
- CacheTexture texture = GetCacheTexture(tex, bump, resolution);
8567
+ CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);
85098568
85108569 return texture!=null?texture.texturedata:null;
85118570 }
85128571
8513
- boolean BindTexture(String tex, boolean bump, int resolution) throws Exception
8572
+ boolean BindTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception
85148573 {
85158574 if (/*tex == null ||*/ ambientOcclusion ) // || !textureon)
85168575 {
....@@ -8519,7 +8578,7 @@
85198578
85208579 //boolean newtex = false;
85218580
8522
- com.sun.opengl.util.texture.Texture texture = GetTexture(tex, bump, resolution);
8581
+ com.sun.opengl.util.texture.Texture texture = GetTexture(stream, tex, bump, resolution);
85238582
85248583 if (texture == null)
85258584 return false;
....@@ -11006,7 +11065,7 @@
1100611065
1100711066 try
1100811067 {
11009
- BindTexture(NOISE_TEXTURE, false, 2);
11068
+ BindTexture(null, NOISE_TEXTURE, false, 2);
1101011069 }
1101111070 catch (Exception e)
1101211071 {
....@@ -11671,7 +11730,7 @@
1167111730 if (tex.equals("WHITE_NOISE"))
1167211731 continue;
1167311732
11674
- if (!usedtextures.containsKey(tex))
11733
+ if (!usedtextures.contains(tex))
1167511734 {
1167611735 // System.out.println("DISPOSE +++++++++++++++ " + tex);
1167711736 textures.get(tex).texture.dispose();