From f2b6a33fdf84a06b958f9cb9d667a2eff3063d8b Mon Sep 17 00:00:00 2001 From: Normand Briere <nbriere@noware.ca> Date: Wed, 24 Jul 2019 23:50:40 -0400 Subject: [PATCH] Camera global inverse. --- CameraPane.java | 286 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 211 insertions(+), 75 deletions(-) diff --git a/CameraPane.java b/CameraPane.java index cef7cdb..07290d0 100644 --- a/CameraPane.java +++ b/CameraPane.java @@ -18,7 +18,10 @@ import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; +import javax.imageio.ImageReadParam; +import javax.imageio.ImageReader; import javax.imageio.plugins.jpeg.JPEGImageWriteParam; +import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import javax.imageio.ImageWriteParam; @@ -30,6 +33,7 @@ import java.nio.*; import gleem.linalg.Mat4f; +import javax.imageio.ImageTypeSpecifier; class CameraPane extends GLCanvas implements iCameraPane, Runnable, GLEventListener, ActionListener, MouseWheelListener, MouseMotionListener, MouseListener, KeyListener { @@ -93,7 +97,7 @@ //boolean REDUCETEXTURE = true; boolean CACHETEXTURE = true; boolean CLEANCACHE = false; // true; - boolean MIPMAP = true; // false; // true; + boolean MIPMAP = false; // true; // never works... boolean COMPRESSTEXTURE = false; boolean KOMPACTTEXTURE = false; // true; boolean RESIZETEXTURE = false; @@ -8358,14 +8362,26 @@ { TextureData texturedata = null; - if (texdata != null) + if (texdata != null && textureon) { - BufferedImage bim = //new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); + BufferedImage bim; // = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); - CreateBim(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); + try + { + bim = DecompressJPEG(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); + } + catch (Exception e) + { + bim = CreateBim(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); + } texturecache = new CacheTexture(GetBimTexture(bim, bump), -1); bimtextures.put(texdata, texturecache); + + //BufferedImage bim3 = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); + + //Object bim2 = CreateBim(texturecache.texturedata); + //bim2 = bim; } else if (texname.endsWith("DEFAULT_TEXTURE")) // ||*/ tex.equals("")) @@ -8560,29 +8576,32 @@ { if (tex.pigmentdata == null) { - String texname = Object3D.GetPigment(tex); + //String texname = Object3D.GetPigment(tex); CacheTexture texturecache = texturepigment.get(tex); if (texturecache != null) { - tex.pigmentdata = ((ByteBuffer)texturecache.texturedata.getBuffer()).array(); tex.pw = texturecache.texturedata.getWidth(); tex.ph = texturecache.texturedata.getHeight(); + tex.pigmentdata = //CompressJPEG(CreateBim + ((ByteBuffer)texturecache.texturedata.getBuffer()).array() + ; + //, tex.pw, tex.ph), 0.5f); } } if (tex.bumpdata == null) { - String texname = Object3D.GetBump(tex); + //String texname = Object3D.GetBump(tex); CacheTexture texturecache = texturebump.get(tex); if (texturecache != null) { - tex.bumpdata = ((ByteBuffer)texturecache.texturedata.getBuffer()).array(); tex.bw = texturecache.texturedata.getWidth(); tex.bh = texturecache.texturedata.getHeight(); + tex.bumpdata = CompressJPEG(CreateBim(((ByteBuffer)texturecache.texturedata.getBuffer()).array(), tex.bw, tex.bh), 0.5f); } } } @@ -8654,6 +8673,72 @@ return true; // Warning: not used. } + public static byte[] CompressJPEG(BufferedImage image, float quality) + { + try + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); + ImageWriter writer = writers.next(); + + ImageWriteParam param = writer.getDefaultWriteParam(); + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + param.setCompressionQuality(quality); + + ImageOutputStream ios = ImageIO.createImageOutputStream(baos); + writer.setOutput(ios); + writer.write(null, new IIOImage(image, null, null), param); + + byte[] data = baos.toByteArray(); + writer.dispose(); + return data; + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } + } + + public static BufferedImage DecompressJPEG(byte[] image, int w, int h) throws IOException + { + ByteArrayInputStream baos = new ByteArrayInputStream(image); + Iterator<ImageReader> writers = ImageIO.getImageReadersByFormatName("jpg"); + ImageReader reader = writers.next(); + + BufferedImage bim = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); + + ImageReadParam param = reader.getDefaultReadParam(); + param.setDestination(bim); + //param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB)); + + ImageInputStream ios = ImageIO.createImageInputStream(baos); + reader.setInput(ios); + BufferedImage bim2 = reader.read(0, param); + reader.dispose(); + +// WritableRaster raster = bim2.getRaster(); +// DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); +// byte[] bytes = data.getData(); +// +// int[] pixels = new int[bytes.length/3]; +// for (int i=pixels.length; --i>=0;) +// { +// int i3 = i*3; +// pixels[i] = 0xFF; +// pixels[i] <<= 8; +// pixels[i] |= bytes[i3+2] & 0xFF; +// pixels[i] <<= 8; +// pixels[i] |= bytes[i3+1] & 0xFF; +// pixels[i] <<= 8; +// pixels[i] |= bytes[i3] & 0xFF; +// } +// +// bim.setRGB(0,0,w,h, pixels, w*(h-1),-w); + + return bim; + } + ShadowBuffer shadowPBuf; AntialiasBuffer antialiasPBuf; int MAXSTACK; @@ -9614,7 +9699,7 @@ if (renderCamera != lightCamera) //for (int count = parentcam.GetTransformCount(); --count>=0;) - LA.matConcat(matrix, parentcam.GlobalTransform(), matrix); + LA.matConcat(matrix, parentcam.GlobalTransformInv(), matrix); // LA.matConcat(renderCamera.toScreen, renderCamera.toParent, matrix); @@ -9630,7 +9715,7 @@ if (renderCamera != lightCamera) //for (int count = parentcam.GetTransformCount(); --count>=0;) - LA.matConcat(parentcam.GlobalTransformInv(), matrix, matrix); + LA.matConcat(parentcam.GlobalTransform(), matrix, matrix); // LA.matConcat(renderCamera.fromParent, renderCamera.fromScreen, matrix); @@ -10831,7 +10916,7 @@ // if (parentcam != renderCamera) // not a light if (cam != lightCamera) //for (int count = parentcam.GetTransformCount(); --count>=0;) - LA.matConcat(matrix, parentcam.GlobalTransform(), matrix); + LA.matConcat(matrix, parentcam.GlobalTransformInv(), matrix); for (int j = 0; j < 4; j++) { @@ -10846,7 +10931,7 @@ // if (parentcam != renderCamera) // not a light if (cam != lightCamera) //for (int count = parentcam.GetTransformCount(); --count>=0;) - LA.matConcat(parentcam.GlobalTransformInv(), matrix, matrix); + LA.matConcat(parentcam.GlobalTransform(), matrix, matrix); //LA.matConcat(cam.fromScreen, parentcam.fromParent, matrix); @@ -11399,7 +11484,7 @@ } } - if (false) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) + if (false) //RENDERPROGRAM > 0 && DrawMode() == DEFAULT) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) { //gl.glDepthFunc(GL.GL_LEQUAL); //gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); @@ -11407,24 +11492,21 @@ boolean texon = textureon; - if (RENDERPROGRAM > 0) - { - gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); - textureon = false; - } + gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); + textureon = false; + //gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB); //System.out.println("ALLO"); gl.glColorMask(false, false, false, false); DrawObject(gl); - if (RENDERPROGRAM > 0) - { - gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); - textureon = texon; - } + + gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); + textureon = texon; + gl.glColorMask(true, true, true, true); gl.glDepthFunc(GL.GL_EQUAL); - //gl.glDepthMask(false); + gl.glDepthMask(false); } if (false) // DrawMode() == SHADOW) @@ -12318,7 +12400,52 @@ //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0); String program = + // Min shader "!!ARBfp1.0\n" + + "PARAM zero123 = { 0.0, 1.0, 2.0, 1.25 };" + + "PARAM pow2 = { 0.5, 0.25, 0.125, 0.0 };" + + "PARAM one = { 1.0, 1.0, 1.0, 1.0 };" + + "PARAM eps = { 0.001, 0.001, 0.001, 1.0 };" + + "PARAM infinity = { 100000000, 100000000, 100000000, 1.0 };" + + "TEMP temp;" + + "TEMP light;" + + "TEMP ndotl;" + + "TEMP normal;" + + "TEMP depth;" + + + "MAD normal, fragment.color, zero123.z, -zero123.y;" + + + "MOV light, state.light[0].position;" + + "DP3 ndotl.x, light, normal;" + + + // shadow + "MOV temp, fragment.texcoord[1];" + + TextureFetch("depth", "temp", "1") + + //"TEX depth, fragment.texcoord[1], texture[1], 2D;" + + "SLT temp.x, fragment.texcoord[1].z, depth.z;" + + + + // No shadow when out of frustum + //"SGE temp.y, depth.z, zero123.y;" + + //"LRP temp.x, temp.y, zero123.y, temp.x;" + + + "MUL ndotl.x, ndotl.x, temp.x;" + + "MAX ndotl.x, ndotl.x, pow2.y;" + + + "TEX temp, fragment.texcoord[0], texture[0], 2D;" + + "LRP temp, zero123.w, temp, one;" + // texture proportion + "MUL temp, temp, ndotl.x;" + + + "MUL temp, temp, zero123.z;" + + + "MOV temp.w, zero123.y;" + // reset alpha + + "MOV result.color, temp;" + + "END"; + + String program2 = + "!!ARBfp1.0\n" + + //"OPTION ARB_fragment_program_shadow;" + "PARAM light2cam0 = program.env[10];" + "PARAM light2cam1 = program.env[11];" + @@ -12433,8 +12560,7 @@ "TEMP shininess;" + "\n" + "MOV texSamp, one;" + - //"TEX texSamp, fragment.texcoord[0], texture[0], 2D;" + - + "MOV mapgrid.x, one2048th.x;" + "MOV temp, fragment.texcoord[1];" + /* @@ -12839,7 +12965,7 @@ "MAD shadow.x, buffer.x, frac.y, shadow.x;" + "") + - // display shadow only (bump == 0) + // display shadow only (fakedepth == 0) "SUB temp.x, half.x, shadow.x;" + "MOV temp.y, -params5.z;" + // params6.x;" + "SLT temp.z, temp.y, -one2048th.x;" + @@ -13271,20 +13397,20 @@ return "TEX " + dest + ", " + src + ", texture[" + unit + "], 2D;" + "SGE " + src + ".w, " + src + ".x, eps.x;" + "SGE " + src + ".z, " + src + ".y, eps.x;" + + "SLT " + dest + ".x, " + src + ".x, one.x;" + + "SLT " + dest + ".y, " + src + ".y, one.x;" + "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + - "SLT " + src + ".z, " + src + ".x, one.x;" + - "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + - "SLT " + src + ".z, " + src + ".y, one.x;" + - "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + + "MUL " + src + ".w, " + dest + ".x, " + src + ".w;" + + "MUL " + src + ".w, " + dest + ".y, " + src + ".w;" + //"SWZ buffer, temp, w,w,w,w;"; - "MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" + + //"MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" + "SUB " + src + ".z, " + "one.x, " + src + ".w;" + //"MUL " + src + ".z, " + src + ".z, infinity.x;" + //"ADD " + dest + ".z, " + dest + ".z, " + src + ".z;"; - "MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;"; + //"MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;"; - //"LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;"; - //"LRP " + dest + ".z" + ", " + src + ".w, infinity.x," + dest + ".z;"; + //?? "LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;"; + "LRP " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;"; } String Shadow(String depth, String shadow) @@ -13331,7 +13457,7 @@ "SLT temp.x, temp.x, zero.x;" + // shadoweps "LRP " + shadow + ", temp.x, one, " + shadow + ";" + - // No shadow when out of frustrum + // No shadow when out of frustum "SGE temp.x, " + depth + ".z, one.z;" + "LRP " + shadow + ", temp.x, one, " + shadow + ";" + ""; @@ -14129,14 +14255,15 @@ drag = false; //System.out.println("Mouse DOWN"); editObj = false; - ClickInfo info = new ClickInfo(); - info.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom)); - info.pane = this; - info.camera = renderCamera; - info.x = x; - info.y = y; - info.modifiers = modifiersex; - editObj = object.doEditClick(info, 0); + //ClickInfo info = new ClickInfo(); + object.clickInfo.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom)); + object.clickInfo.pane = this; + object.clickInfo.camera = renderCamera; + object.clickInfo.x = x; + object.clickInfo.y = y; + object.clickInfo.modifiers = modifiersex; + editObj = object.doEditClick(//info, + 0); if (!editObj) { hasMarquee = true; @@ -14536,15 +14663,16 @@ if (editObj) { drag = true; - ClickInfo info = new ClickInfo(); - info.bounds.setBounds(0, 0, + //ClickInfo info = new ClickInfo(); + object.clickInfo.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom)); - info.pane = this; - info.camera = renderCamera; - info.x = x; - info.y = y; - object.GetWindow().copy - .doEditDrag(info, (modifiers & MouseEvent.BUTTON3_MASK) != 0); + object.clickInfo.pane = this; + object.clickInfo.camera = renderCamera; + object.clickInfo.x = x; + object.clickInfo.y = y; + object //.GetWindow().copy + .doEditDrag(//info, + (modifiers & MouseEvent.BUTTON3_MASK) != 0); } else { if (x < startX) @@ -14693,24 +14821,27 @@ } } +// ClickInfo clickInfo = new ClickInfo(); + public void mouseMoved(MouseEvent e) { //System.out.println("mouseMoved: " + e); if (isRenderer) return; - ClickInfo ci = new ClickInfo(); - ci.x = e.getX(); - ci.y = e.getY(); - ci.modifiers = e.getModifiersEx(); - ci.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom)); - ci.pane = this; - ci.camera = renderCamera; + // Mouse cursor feedback + object.clickInfo.x = e.getX(); + object.clickInfo.y = e.getY(); + object.clickInfo.modifiers = e.getModifiersEx(); + object.clickInfo.bounds.setBounds(0, 0, (int) (getBounds().width * zoom), (int) (getBounds().height * zoom)); + object.clickInfo.pane = this; + object.clickInfo.camera = renderCamera; if (!isRenderer) { //ObjEditor editWindow = object.editWindow; //Object3D copy = editWindow.copy; - if (object.doEditClick(ci, 0)) + if (object.doEditClick(//clickInfo, + 0)) { setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } else @@ -15736,8 +15867,6 @@ int width = getBounds().width; int height = getBounds().height; - ClickInfo info = new ClickInfo(); - info.bounds.setBounds(0, 0, (int) (width * zoom), (int) (height * zoom)); //Image img = CreateImage(width, height); //System.out.println("width = " + width + "; height = " + height + "\n"); @@ -15814,31 +15943,37 @@ } if (object != null && !hasMarquee) { + if (object.clickInfo == null) + object.clickInfo = new ClickInfo(); + ClickInfo info = object.clickInfo; + //ClickInfo info = new ClickInfo(); + info.bounds.setBounds(0, 0, (int) (width * zoom), (int) (height * zoom)); + if (isRenderer) { - info.flags++; + object.clickInfo.flags++; double frameAspect = (double) width / (double) height; if (frameAspect > renderCamera.aspect) { int desired = (int) ((double) height * renderCamera.aspect); - info.bounds.width -= width - desired; - info.bounds.x += (width - desired) / 2; + object.clickInfo.bounds.width -= width - desired; + object.clickInfo.bounds.x += (width - desired) / 2; } else { int desired = (int) ((double) width / renderCamera.aspect); - info.bounds.height -= height - desired; - info.bounds.y += (height - desired) / 2; + object.clickInfo.bounds.height -= height - desired; + object.clickInfo.bounds.y += (height - desired) / 2; } } - info.g = gr; - info.camera = renderCamera; + object.clickInfo.g = gr; + object.clickInfo.camera = renderCamera; /* // Memory intensive (brep.verticescopy) if (!(object instanceof Composite)) object.draw(info, 0, false); // SLOW : */ - if (!isRenderer) + if (!isRenderer) // && drag) { Grafreed.Assert(object != null); Grafreed.Assert(object.selection != null); @@ -15846,9 +15981,9 @@ { int hitSomething = object.selection.get(0).hitSomething; - info.DX = 0; - info.DY = 0; - info.W = 1; + object.clickInfo.DX = 0; + object.clickInfo.DY = 0; + object.clickInfo.W = 1; if (hitSomething == Object3D.hitCenter) { info.DX = X; @@ -15860,7 +15995,8 @@ info.DY -= info.bounds.height/2; } - object.drawEditHandles(info, 0); + object.drawEditHandles(//info, + 0); if (drag && (X != 0 || Y != 0)) { @@ -15873,7 +16009,7 @@ gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2); break; case Object3D.hitScale: gr.setColor(Color.cyan); - gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2); + gr.drawLine(X, Y, 0, 0); break; } -- Gitblit v1.6.2