.. | .. |
---|
18 | 18 | import javax.imageio.ImageIO; |
---|
19 | 19 | import javax.imageio.ImageWriteParam; |
---|
20 | 20 | import javax.imageio.ImageWriter; |
---|
| 21 | +import javax.imageio.ImageReadParam; |
---|
| 22 | +import javax.imageio.ImageReader; |
---|
21 | 23 | import javax.imageio.plugins.jpeg.JPEGImageWriteParam; |
---|
| 24 | +import javax.imageio.stream.ImageInputStream; |
---|
22 | 25 | import javax.imageio.stream.ImageOutputStream; |
---|
23 | 26 | import javax.imageio.ImageWriteParam; |
---|
24 | 27 | |
---|
.. | .. |
---|
30 | 33 | import java.nio.*; |
---|
31 | 34 | |
---|
32 | 35 | import gleem.linalg.Mat4f; |
---|
| 36 | +import javax.imageio.ImageTypeSpecifier; |
---|
33 | 37 | |
---|
34 | 38 | class CameraPane extends GLCanvas implements iCameraPane, Runnable, GLEventListener, ActionListener, MouseWheelListener, MouseMotionListener, MouseListener, KeyListener |
---|
35 | 39 | { |
---|
.. | .. |
---|
93 | 97 | //boolean REDUCETEXTURE = true; |
---|
94 | 98 | boolean CACHETEXTURE = true; |
---|
95 | 99 | boolean CLEANCACHE = false; // true; |
---|
96 | | - boolean MIPMAP = true; // false; // true; |
---|
| 100 | + boolean MIPMAP = false; // true; // never works... |
---|
97 | 101 | boolean COMPRESSTEXTURE = false; |
---|
98 | 102 | boolean KOMPACTTEXTURE = false; // true; |
---|
99 | 103 | boolean RESIZETEXTURE = false; |
---|
.. | .. |
---|
8358 | 8362 | { |
---|
8359 | 8363 | TextureData texturedata = null; |
---|
8360 | 8364 | |
---|
8361 | | - if (texdata != null) |
---|
| 8365 | + if (texdata != null && textureon) |
---|
8362 | 8366 | { |
---|
8363 | | - BufferedImage bim = //new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); |
---|
| 8367 | + BufferedImage bim; // = new BufferedImage(bump?tex.bw:tex.pw, bump?tex.bh:tex.ph, BufferedImage.TYPE_INT_RGB); |
---|
8364 | 8368 | |
---|
8365 | | - CreateBim(texdata, bump?tex.bw:tex.pw, bump?tex.bh:tex.ph); |
---|
| 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 | + } |
---|
8366 | 8377 | |
---|
8367 | 8378 | texturecache = new CacheTexture(GetBimTexture(bim, bump), -1); |
---|
8368 | 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; |
---|
8369 | 8385 | } |
---|
8370 | 8386 | else |
---|
8371 | 8387 | if (texname.endsWith("DEFAULT_TEXTURE")) // ||*/ tex.equals("")) |
---|
.. | .. |
---|
8560 | 8576 | { |
---|
8561 | 8577 | if (tex.pigmentdata == null) |
---|
8562 | 8578 | { |
---|
8563 | | - String texname = Object3D.GetPigment(tex); |
---|
| 8579 | + //String texname = Object3D.GetPigment(tex); |
---|
8564 | 8580 | |
---|
8565 | 8581 | CacheTexture texturecache = texturepigment.get(tex); |
---|
8566 | 8582 | |
---|
8567 | 8583 | if (texturecache != null) |
---|
8568 | 8584 | { |
---|
8569 | | - tex.pigmentdata = ((ByteBuffer)texturecache.texturedata.getBuffer()).array(); |
---|
8570 | 8585 | tex.pw = texturecache.texturedata.getWidth(); |
---|
8571 | 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); |
---|
8572 | 8591 | } |
---|
8573 | 8592 | } |
---|
8574 | 8593 | |
---|
8575 | 8594 | if (tex.bumpdata == null) |
---|
8576 | 8595 | { |
---|
8577 | | - String texname = Object3D.GetBump(tex); |
---|
| 8596 | + //String texname = Object3D.GetBump(tex); |
---|
8578 | 8597 | |
---|
8579 | 8598 | CacheTexture texturecache = texturebump.get(tex); |
---|
8580 | 8599 | |
---|
8581 | 8600 | if (texturecache != null) |
---|
8582 | 8601 | { |
---|
8583 | | - tex.bumpdata = ((ByteBuffer)texturecache.texturedata.getBuffer()).array(); |
---|
8584 | 8602 | tex.bw = texturecache.texturedata.getWidth(); |
---|
8585 | 8603 | tex.bh = texturecache.texturedata.getHeight(); |
---|
| 8604 | + tex.bumpdata = CompressJPEG(CreateBim(((ByteBuffer)texturecache.texturedata.getBuffer()).array(), tex.bw, tex.bh), 0.5f); |
---|
8586 | 8605 | } |
---|
8587 | 8606 | } |
---|
8588 | 8607 | } |
---|
.. | .. |
---|
8654 | 8673 | return true; // Warning: not used. |
---|
8655 | 8674 | } |
---|
8656 | 8675 | |
---|
| 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 | + |
---|
8657 | 8742 | ShadowBuffer shadowPBuf; |
---|
8658 | 8743 | AntialiasBuffer antialiasPBuf; |
---|
8659 | 8744 | int MAXSTACK; |
---|
.. | .. |
---|
11399 | 11484 | } |
---|
11400 | 11485 | } |
---|
11401 | 11486 | |
---|
11402 | | - if (false) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) |
---|
| 11487 | + if (false) //RENDERPROGRAM > 0 && DrawMode() == DEFAULT) // fast && !IsFreezed() && DrawMode() != SELECTION && !ambientOcclusion) |
---|
11403 | 11488 | { |
---|
11404 | 11489 | //gl.glDepthFunc(GL.GL_LEQUAL); |
---|
11405 | 11490 | //gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); |
---|
.. | .. |
---|
11407 | 11492 | |
---|
11408 | 11493 | boolean texon = textureon; |
---|
11409 | 11494 | |
---|
11410 | | - if (RENDERPROGRAM > 0) |
---|
11411 | | - { |
---|
11412 | | - gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
11413 | | - textureon = false; |
---|
11414 | | - } |
---|
| 11495 | + gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
| 11496 | + textureon = false; |
---|
| 11497 | + |
---|
11415 | 11498 | //gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB); |
---|
11416 | 11499 | //System.out.println("ALLO"); |
---|
11417 | 11500 | gl.glColorMask(false, false, false, false); |
---|
11418 | 11501 | DrawObject(gl); |
---|
11419 | | - if (RENDERPROGRAM > 0) |
---|
11420 | | - { |
---|
11421 | | - gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
11422 | | - textureon = texon; |
---|
11423 | | - } |
---|
| 11502 | + |
---|
| 11503 | + gl.glEnable(GL.GL_FRAGMENT_PROGRAM_ARB); |
---|
| 11504 | + textureon = texon; |
---|
| 11505 | + |
---|
11424 | 11506 | gl.glColorMask(true, true, true, true); |
---|
11425 | 11507 | |
---|
11426 | 11508 | gl.glDepthFunc(GL.GL_EQUAL); |
---|
11427 | | - //gl.glDepthMask(false); |
---|
| 11509 | + gl.glDepthMask(false); |
---|
11428 | 11510 | } |
---|
11429 | 11511 | |
---|
11430 | 11512 | if (false) // DrawMode() == SHADOW) |
---|
.. | .. |
---|
12318 | 12400 | //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0); |
---|
12319 | 12401 | |
---|
12320 | 12402 | String program = |
---|
| 12403 | + // Min shader |
---|
12321 | 12404 | "!!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 | + |
---|
12322 | 12449 | //"OPTION ARB_fragment_program_shadow;" + |
---|
12323 | 12450 | "PARAM light2cam0 = program.env[10];" + |
---|
12324 | 12451 | "PARAM light2cam1 = program.env[11];" + |
---|
.. | .. |
---|
12433 | 12560 | "TEMP shininess;" + |
---|
12434 | 12561 | "\n" + |
---|
12435 | 12562 | "MOV texSamp, one;" + |
---|
12436 | | - //"TEX texSamp, fragment.texcoord[0], texture[0], 2D;" + |
---|
12437 | | - |
---|
| 12563 | + |
---|
12438 | 12564 | "MOV mapgrid.x, one2048th.x;" + |
---|
12439 | 12565 | "MOV temp, fragment.texcoord[1];" + |
---|
12440 | 12566 | /* |
---|
.. | .. |
---|
12839 | 12965 | "MAD shadow.x, buffer.x, frac.y, shadow.x;" + |
---|
12840 | 12966 | "") + |
---|
12841 | 12967 | |
---|
12842 | | - // display shadow only (bump == 0) |
---|
| 12968 | + // display shadow only (fakedepth == 0) |
---|
12843 | 12969 | "SUB temp.x, half.x, shadow.x;" + |
---|
12844 | 12970 | "MOV temp.y, -params5.z;" + // params6.x;" + |
---|
12845 | 12971 | "SLT temp.z, temp.y, -one2048th.x;" + |
---|
.. | .. |
---|
13271 | 13397 | return "TEX " + dest + ", " + src + ", texture[" + unit + "], 2D;" + |
---|
13272 | 13398 | "SGE " + src + ".w, " + src + ".x, eps.x;" + |
---|
13273 | 13399 | "SGE " + src + ".z, " + src + ".y, eps.x;" + |
---|
| 13400 | + "SLT " + dest + ".x, " + src + ".x, one.x;" + |
---|
| 13401 | + "SLT " + dest + ".y, " + src + ".y, one.x;" + |
---|
13274 | 13402 | "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + |
---|
13275 | | - "SLT " + src + ".z, " + src + ".x, one.x;" + |
---|
13276 | | - "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + |
---|
13277 | | - "SLT " + src + ".z, " + src + ".y, one.x;" + |
---|
13278 | | - "MUL " + src + ".w, " + src + ".z, " + src + ".w;" + |
---|
| 13403 | + "MUL " + src + ".w, " + dest + ".x, " + src + ".w;" + |
---|
| 13404 | + "MUL " + src + ".w, " + dest + ".y, " + src + ".w;" + |
---|
13279 | 13405 | //"SWZ buffer, temp, w,w,w,w;"; |
---|
13280 | | - "MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" + |
---|
| 13406 | + //"MUL " + dest + ".z, " + dest + ".z, " + src + ".w;" + |
---|
13281 | 13407 | "SUB " + src + ".z, " + "one.x, " + src + ".w;" + |
---|
13282 | 13408 | //"MUL " + src + ".z, " + src + ".z, infinity.x;" + |
---|
13283 | 13409 | //"ADD " + dest + ".z, " + dest + ".z, " + src + ".z;"; |
---|
13284 | | - "MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;"; |
---|
| 13410 | + //"MAD " + dest + ".z, " + src + ".z, infinity.x," + dest + ".z;"; |
---|
13285 | 13411 | |
---|
13286 | | - //"LRP " + dest + ".z, " + src + ".w," + dest + ".z, infinity.x;"; |
---|
13287 | | - //"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;"; |
---|
13288 | 13414 | } |
---|
13289 | 13415 | |
---|
13290 | 13416 | String Shadow(String depth, String shadow) |
---|
.. | .. |
---|
13331 | 13457 | "SLT temp.x, temp.x, zero.x;" + // shadoweps |
---|
13332 | 13458 | "LRP " + shadow + ", temp.x, one, " + shadow + ";" + |
---|
13333 | 13459 | |
---|
13334 | | - // No shadow when out of frustrum |
---|
| 13460 | + // No shadow when out of frustum |
---|
13335 | 13461 | "SGE temp.x, " + depth + ".z, one.z;" + |
---|
13336 | 13462 | "LRP " + shadow + ", temp.x, one, " + shadow + ";" + |
---|
13337 | 13463 | ""; |
---|