From d32f24f147068e6cbecb31c7f98047f68bc8b58a Mon Sep 17 00:00:00 2001 From: Normand Briere <nbriere@noware.ca> Date: Sun, 21 Jul 2019 19:03:28 -0400 Subject: [PATCH] Embed texture. --- CameraPane.java | 299 +++++++++++++++++++++++++++++++++++------------------------ 1 files changed, 179 insertions(+), 120 deletions(-) diff --git a/CameraPane.java b/CameraPane.java index b55d184..acc49b8 100644 --- a/CameraPane.java +++ b/CameraPane.java @@ -60,7 +60,7 @@ //boolean REDUCETEXTURE = true; boolean CACHETEXTURE = true; boolean CLEANCACHE = false; // true; - boolean MIPMAP = false; // true; + boolean MIPMAP = true; // false; // true; boolean COMPRESSTEXTURE = false; boolean KOMPACTTEXTURE = false; // true; boolean RESIZETEXTURE = false; @@ -2405,6 +2405,45 @@ return currentGL; } + private BufferedImage CreateBim(TextureData texturedata) + { + // cache to disk + Buffer buffer = texturedata.getBuffer(); // getMipmapData(); + //buffers[0]. + ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer(); + int[] pixels = new int[bytebuf.capacity()/3]; + int width = texturedata.getWidth(); //(int)Math.sqrt(pixels.length); // squared + int height = width; + for (int i=pixels.length; --i>=0;) + { + int i3 = i*3; + pixels[i] = 0xFF; + pixels[i] <<= 8; + pixels[i] |= bytebuf.get(i3+2) & 0xFF; + pixels[i] <<= 8; + pixels[i] |= bytebuf.get(i3+1) & 0xFF; + pixels[i] <<= 8; + pixels[i] |= bytebuf.get(i3) & 0xFF; + } + /* + int r=0,g=0,b=0,a=0; + for (int i=0; i<width; i++) + for (int j=0; j<height; j++) + { + int index = j*width+i; + int p = pixels[index]; + a = ((p>>24) & 0xFF); + r = ((p>>16) & 0xFF); + g = ((p>>8) & 0xFF); + b = (p & 0xFF); + pixels[index] = (a<<24) | (b<<16) | (g<<8) | r; + } + /**/ + BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile); + rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width); + return rendImage; + } + /**/ class CacheTexture { @@ -2422,9 +2461,10 @@ /**/ // TEXTURE static Texture texture; - static public java.util.Hashtable<String, CacheTexture/*com.sun.opengl.util.texture.Texture*/> textures - = new java.util.Hashtable<String, CacheTexture/*com.sun.opengl.util.texture.Texture*/>(); - static public java.util.Hashtable<String, String> usedtextures = new java.util.Hashtable<String, String>(); + static public java.util.Hashtable<String, CacheTexture> textures = new java.util.Hashtable<String, CacheTexture>(); + static public java.util.Hashtable<BufferedImage, CacheTexture> bimtextures = new java.util.Hashtable<BufferedImage, CacheTexture>(); + static public java.util.HashSet<String> usedtextures = new java.util.HashSet<String>(); + int pigmentdepth = 0; public com.sun.opengl.util.texture.Texture[] pigmentstack = new com.sun.opengl.util.texture.Texture[65536]; int bumpdepth = 0; @@ -2446,6 +2486,33 @@ true, com.sun.opengl.util.texture.TextureIO.PNG); } catch (java.io.IOException e) + { + throw new javax.media.opengl.GLException(e); + } + + if (bump) + texturedata = ConvertBump(texturedata, false); + + com.sun.opengl.util.texture.Texture texture = + com.sun.opengl.util.texture.TextureIO.newTexture(texturedata); + + texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_S, javax.media.opengl.GL.GL_REPEAT); + texture.setTexParameteri(javax.media.opengl.GL.GL_TEXTURE_WRAP_T, javax.media.opengl.GL.GL_REPEAT); + + return texture; + } + + com.sun.opengl.util.texture.Texture GetBimTexture(BufferedImage name, boolean bump) + { + TextureData texturedata = null; + + try + { + texturedata = + com.sun.opengl.util.texture.TextureIO.newTextureData( + name, + true); + } catch (Exception e) { throw new javax.media.opengl.GLException(e); } @@ -3532,6 +3599,8 @@ System.out.println("LOADING TEXTURE : " + name); + Object x = texturedata.getMipmapData(); // .getBuffer(); + // if (false) // compressbit > 0) { @@ -8112,47 +8181,50 @@ /*boolean*/ public void BindTextures(cTexture tex, int resolution) throws Exception // INTERFACE { - if (// DrawMode() != 0 || /*tex == null ||*/ - ambientOcclusion ) // || !textureon) - { - return; // false; - } - - if (tex == null) - { - BindTexture(null,false,resolution); - BindTexture(null,true,resolution); - return; - } +// if (// DrawMode() != 0 || /*tex == null ||*/ +// ambientOcclusion ) // || !textureon) +// { +// return; // false; +// } +// +// if (tex == null) +// { +// BindTexture(null,false,resolution); +// BindTexture(null,true,resolution); +// return; +// } +// +// String pigment = Object3D.GetPigment(tex); +// String bump = Object3D.GetBump(tex); +// +// usedtextures.add(pigment); +// usedtextures.add(bump); +// +// //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) +// { +// // System.out.print("BIND +++++++++++++++ pigment = " + pigment); +// // System.out.println("; bump = " + bump); +// } +// +// if (bump.equals("")) +// { +// bump = null; +// } +// if (pigment.equals("")) +// { +// pigment = null; +// } +// +// GetGL().glActiveTexture(GetGL().GL_TEXTURE0); +// BindTexture(pigment, false, resolution); +// GetGL().glActiveTexture(GetGL().GL_TEXTURE2); +// BindTexture(bump, true, resolution); +// GetGL().glActiveTexture(GetGL().GL_TEXTURE0); +// +// return; // true; - String pigment = Object3D.GetPigment(tex); - String bump = Object3D.GetBump(tex); - - usedtextures.put(pigment, pigment); - usedtextures.put(bump, bump); - - //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) - { - // System.out.print("BIND +++++++++++++++ pigment = " + pigment); - // System.out.println("; bump = " + bump); - } - - if (bump.equals("")) - { - bump = null; - } - if (pigment.equals("")) - { - pigment = null; - } - - GetGL().glActiveTexture(GetGL().GL_TEXTURE0); - BindTexture(pigment, false, resolution); - GetGL().glActiveTexture(GetGL().GL_TEXTURE2); - BindTexture(bump, true, resolution); - GetGL().glActiveTexture(GetGL().GL_TEXTURE0); - - return; // true; + BindPigmentTexture(tex, resolution); + BindBumpTexture(tex, resolution); } /*boolean*/ public void BindPigmentTexture(cTexture tex, int resolution) throws Exception // INTERFACE @@ -8165,13 +8237,13 @@ if (tex == null) { - BindTexture(null,false,resolution); + BindTexture(null, null,false,resolution); return; } String pigment = Object3D.GetPigment(tex); - usedtextures.put(pigment, pigment); + usedtextures.add(pigment); //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) { @@ -8185,7 +8257,7 @@ } GetGL().glActiveTexture(GetGL().GL_TEXTURE0); - BindTexture(pigment, false, resolution); + BindTexture(tex.pigmenttexture, pigment, false, resolution); } /*boolean*/ public void BindBumpTexture(cTexture tex, int resolution) throws Exception // INTERFACE @@ -8198,13 +8270,13 @@ if (tex == null) { - BindTexture(null,true,resolution); + BindTexture(null, null,true,resolution); return; } String bump = Object3D.GetBump(tex); - usedtextures.put(bump, bump); + usedtextures.add(bump); //if (!tex.equals(":") && !tex.equals(DEFAULT_TEXTURES)) { @@ -8218,7 +8290,7 @@ } GetGL().glActiveTexture(GetGL().GL_TEXTURE2); - BindTexture(bump, true, resolution); + BindTexture(tex.bumptexture, bump, true, resolution); GetGL().glActiveTexture(GetGL().GL_TEXTURE0); } @@ -8242,9 +8314,9 @@ return fileExists; } - CacheTexture GetCacheTexture(String tex, boolean bump, int resolution) throws Exception + CacheTexture GetCacheTexture(java.awt.image.BufferedImage bim, String tex, boolean bump, int resolution) throws Exception { - CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null; + CacheTexture texturecache = null; if (tex != null) { @@ -8264,12 +8336,22 @@ } if (CACHETEXTURE) - texture = textures.get(texname); // TEXTURE CACHE - - TextureData texturedata = null; - - if (texture == null || texture.resolution < resolution) { + if (bim == null) + texturecache = textures.get(texname); // TEXTURE CACHE + else + texturecache = bimtextures.get(bim); // TEXTURE CACHE + } + + if (texturecache == null || texturecache.resolution < resolution) + { + TextureData texturedata = null; + + if (bim != null) + { + texturecache = new CacheTexture(GetBimTexture(bim, bump), -1); + } + else if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals("")) { assert(!bump); @@ -8281,19 +8363,23 @@ // } // else // { - texture = textures.get(tex); - if (texture == null) + texturecache = textures.get(tex); + if (texturecache == null) { - texture = new CacheTexture(GetResourceTexture("default.png", bump),resolution); + texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution); } + else + new Exception().printStackTrace(); // } } else if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals("")) { assert(bump); - texture = textures.get(tex); - if (texture == null) - texture = new CacheTexture(GetResourceTexture("default.png", bump),resolution); + texturecache = textures.get(tex); + if (texturecache == null) + texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution); + else + new Exception().printStackTrace(); } else { //if (tex.equals("IMMORTAL")) @@ -8303,9 +8389,11 @@ //{ if (tex.equals("WHITE_NOISE")) { - texture = textures.get(tex); - if (texture == null) - texture = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution); + texturecache = textures.get(tex); + if (texturecache == null) + texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution); + else + new Exception().printStackTrace(); } else { if (textureon) @@ -8364,19 +8452,20 @@ if (texturedata == null) throw new Exception(); - texture = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution); + texturecache = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution); //texture = GetTexture(tex, bump); } } //} } - if (/*CACHETEXTURE &&*/ texture != null && textureon) + if (/*CACHETEXTURE &&*/ texturecache != null && textureon) { //return false; + assert(bim == null); // System.out.println("CACHE +++++++++++++++ TEXTURE : " + texname + " (" + texture.getEstimatedMemorySize() + ")"); - if (texturedata != null && (texname.endsWith(".jpg") || texname.endsWith(".JPG"))) + if (texturedata != null && texname.toLowerCase().endsWith(".jpg")) { // String ext = "_highres"; // if (REDUCETEXTURE) @@ -8393,52 +8482,17 @@ File cachefile = new File(texname.substring(0, texname.length()-4)+ext+".jpg"); if (!cachefile.exists()) { - // cache to disk - Buffer buffer = texturedata.getBuffer(); // getMipmapData(); - //buffers[0]. - - ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer(); - int[] pixels = new int[bytebuf.capacity()/3]; - - // squared size heuristic... - if ((int)Math.sqrt(pixels.length) == Math.sqrt(pixels.length)) + //if (texturedata.getWidth() == texturedata.getHeight()) { - for (int i=pixels.length; --i>=0;) - { - int i3 = i*3; - pixels[i] = 0xFF; - pixels[i] <<= 8; - pixels[i] |= bytebuf.get(i3+2) & 0xFF; - pixels[i] <<= 8; - pixels[i] |= bytebuf.get(i3+1) & 0xFF; - pixels[i] <<= 8; - pixels[i] |= bytebuf.get(i3) & 0xFF; - } - - /* - int r=0,g=0,b=0,a=0; - for (int i=0; i<width; i++) - for (int j=0; j<height; j++) - { - int index = j*width+i; - int p = pixels[index]; - a = ((p>>24) & 0xFF); - r = ((p>>16) & 0xFF); - g = ((p>>8) & 0xFF); - b = (p & 0xFF); - pixels[index] = (a<<24) | (b<<16) | (g<<8) | r; - } - /**/ - int width = (int)Math.sqrt(pixels.length); // squared - int height = width; - BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile); - rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width); + BufferedImage rendImage = CreateBim(texturedata); + ImageWriter writer = null; Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = (ImageWriter)iter.next(); } - float compressionQuality = 0.9f; + + float compressionQuality = 0.85f; try { ImageOutputStream ios = ImageIO.createImageOutputStream(cachefile); @@ -8465,8 +8519,8 @@ textures.remove(texname); } - texture.texturedata = texturedata; - textures.put(texname, texture); + //texture.texturedata = texturedata; + textures.put(texname, texturecache); // newtex = true; } @@ -8482,12 +8536,17 @@ } } - return texture; + return texturecache; } - com.sun.opengl.util.texture.Texture GetTexture(String tex, boolean bump, int resolution) throws Exception + static void EmbedTextures(cTexture tex) { - CacheTexture texture = GetCacheTexture(tex, bump, resolution); + + } + + com.sun.opengl.util.texture.Texture GetTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception + { + CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution); if (bump) { @@ -8503,14 +8562,14 @@ return texture!=null?texture.texture:null; } - public com.sun.opengl.util.texture.TextureData GetTextureData(String tex, boolean bump, int resolution) throws Exception + public com.sun.opengl.util.texture.TextureData GetTextureData(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception { - CacheTexture texture = GetCacheTexture(tex, bump, resolution); + CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution); return texture!=null?texture.texturedata:null; } - boolean BindTexture(String tex, boolean bump, int resolution) throws Exception + boolean BindTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception { if (/*tex == null ||*/ ambientOcclusion ) // || !textureon) { @@ -8519,7 +8578,7 @@ //boolean newtex = false; - com.sun.opengl.util.texture.Texture texture = GetTexture(tex, bump, resolution); + com.sun.opengl.util.texture.Texture texture = GetTexture(stream, tex, bump, resolution); if (texture == null) return false; @@ -11006,7 +11065,7 @@ try { - BindTexture(NOISE_TEXTURE, false, 2); + BindTexture(null, NOISE_TEXTURE, false, 2); } catch (Exception e) { @@ -11671,7 +11730,7 @@ if (tex.equals("WHITE_NOISE")) continue; - if (!usedtextures.containsKey(tex)) + if (!usedtextures.contains(tex)) { // System.out.println("DISPOSE +++++++++++++++ " + tex); textures.get(tex).texture.dispose(); -- Gitblit v1.6.2