From 062507e23e91b4f8d2d81318bbd498d2a3435866 Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Sun, 4 Sep 2011 23:41:46 +0000 Subject: [PATCH] * Render off screen surfaces even if main screen isn't visible * AWTLoader now supports 16-bit grayscale PNG loading as Luminance16 * AWTLoader uses try/finally on stream * Fix exception in MaterialLoader (was it really that hard to fix, normen?) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8185 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../core/com/jme3/app/SimpleApplication.java | 4 +- .../core/com/jme3/renderer/RenderManager.java | 17 +++-- .../com/jme3/texture/plugins/AWTLoader.java | 71 +++++++++++++++---- .../scene/plugins/ogre/MaterialLoader.java | 2 + 4 files changed, 72 insertions(+), 22 deletions(-) diff --git a/engine/src/core/com/jme3/app/SimpleApplication.java b/engine/src/core/com/jme3/app/SimpleApplication.java index 5c875919d..16a91cc92 100644 --- a/engine/src/core/com/jme3/app/SimpleApplication.java +++ b/engine/src/core/com/jme3/app/SimpleApplication.java @@ -260,9 +260,7 @@ public abstract class SimpleApplication extends Application { // render states stateManager.render(renderManager); - if (context.isRenderable()){ - renderManager.render(tpf); - } + renderManager.render(tpf, context.isRenderable()); simpleRender(renderManager); stateManager.postRender(); } diff --git a/engine/src/core/com/jme3/renderer/RenderManager.java b/engine/src/core/com/jme3/renderer/RenderManager.java index 692cef024..a2e7d125c 100644 --- a/engine/src/core/com/jme3/renderer/RenderManager.java +++ b/engine/src/core/com/jme3/renderer/RenderManager.java @@ -1148,7 +1148,7 @@ public class RenderManager { * * @param tpf Time per frame value */ - public void render(float tpf) { + public void render(float tpf, boolean mainFrameBufferActive) { if (renderer instanceof NullRenderer) { return; } @@ -1156,13 +1156,22 @@ public class RenderManager { this.shader = renderer.getCaps().contains(Caps.GLSL100); for (int i = 0; i < preViewPorts.size(); i++) { - renderViewPort(preViewPorts.get(i), tpf); + ViewPort vp = preViewPorts.get(i); + if (vp.getOutputFrameBuffer() != null || mainFrameBufferActive){ + renderViewPort(vp, tpf); + } } for (int i = 0; i < viewPorts.size(); i++) { - renderViewPort(viewPorts.get(i), tpf); + ViewPort vp = viewPorts.get(i); + if (vp.getOutputFrameBuffer() != null || mainFrameBufferActive){ + renderViewPort(vp, tpf); + } } for (int i = 0; i < postViewPorts.size(); i++) { - renderViewPort(postViewPorts.get(i), tpf); + ViewPort vp = postViewPorts.get(i); + if (vp.getOutputFrameBuffer() != null || mainFrameBufferActive){ + renderViewPort(vp, tpf); + } } } } diff --git a/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java b/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java index 0db07f06d..ff944f56d 100644 --- a/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java +++ b/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java @@ -43,6 +43,7 @@ import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; +import java.awt.image.DataBufferUShort; import java.awt.image.DirectColorModel; import java.io.IOException; import java.io.InputStream; @@ -65,12 +66,15 @@ public class AWTLoader implements AssetLoader { Transparency.BITMASK, DataBuffer.TYPE_BYTE); - private byte[] extractImageData(BufferedImage img){ + private Object extractImageData(BufferedImage img){ DataBuffer buf = img.getRaster().getDataBuffer(); switch (buf.getDataType()){ case DataBuffer.TYPE_BYTE: DataBufferByte byteBuf = (DataBufferByte) buf; return byteBuf.getData(); + case DataBuffer.TYPE_USHORT: + DataBufferUShort shortBuf = (DataBufferUShort) buf; + return shortBuf.getData(); } return null; } @@ -86,26 +90,56 @@ public class AWTLoader implements AssetLoader { System.arraycopy(sln, 0, img, y2 * scSz, scSz); } } + + private void flipImage(short[] img, int width, int height, int bpp){ + int scSz = (width * bpp) / 8; + scSz /= 2; // Because shorts are 2 bytes + byte[] sln = new byte[scSz]; + int y2 = 0; + for (int y1 = 0; y1 < height / 2; y1++){ + y2 = height - y1 - 1; + System.arraycopy(img, y1 * scSz, sln, 0, scSz); + System.arraycopy(img, y2 * scSz, img, y1 * scSz, scSz); + System.arraycopy(sln, 0, img, y2 * scSz, scSz); + } + } public Image load(BufferedImage img, boolean flipY){ int width = img.getWidth(); int height = img.getHeight(); switch (img.getType()){ + case BufferedImage.TYPE_4BYTE_ABGR: // most common in PNG images w/ alpha + byte[] dataBuf1 = (byte[]) extractImageData(img); + if (flipY) + flipImage(dataBuf1, width, height, 32); + + ByteBuffer data1 = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()*4); + data1.put(dataBuf1); + return new Image(Format.ABGR8, width, height, data1); case BufferedImage.TYPE_3BYTE_BGR: // most common in JPEG images - byte[] dataBuf = extractImageData(img); + byte[] dataBuf2 = (byte[]) extractImageData(img); if (flipY) - flipImage(dataBuf, width, height, 24); - ByteBuffer data = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()*3); - data.put(dataBuf); - return new Image(Format.BGR8, width, height, data); + flipImage(dataBuf2, width, height, 24); + + ByteBuffer data2 = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()*3); + data2.put(dataBuf2); + return new Image(Format.BGR8, width, height, data2); case BufferedImage.TYPE_BYTE_GRAY: // grayscale fonts - byte[] dataBuf2 = extractImageData(img); + byte[] dataBuf3 = (byte[]) extractImageData(img); if (flipY) - flipImage(dataBuf2, width, height, 8); - ByteBuffer data2 = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()); - data2.put(dataBuf2); - return new Image(Format.Luminance8, width, height, data2); + flipImage(dataBuf3, width, height, 8); + ByteBuffer data3 = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()); + data3.put(dataBuf3); + return new Image(Format.Luminance8, width, height, data3); + case BufferedImage.TYPE_USHORT_GRAY: // grayscale heightmap + short[] dataBuf4 = (short[]) extractImageData(img); + if (flipY) + flipImage(dataBuf4, width, height, 16); + + ByteBuffer data4 = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()*2); + data4.asShortBuffer().put(dataBuf4); + return new Image(Format.Luminance16, width, height, data4); default: break; } @@ -163,11 +197,18 @@ public class AWTLoader implements AssetLoader { public Object load(AssetInfo info) throws IOException { if (ImageIO.getImageWritersBySuffix(info.getKey().getExtension()) != null){ - InputStream in = info.openStream(); + boolean flip = ((TextureKey) info.getKey()).isFlipY(); - Image img = load(in, flip); - in.close(); - return img; + InputStream in = null; + try { + in = info.openStream(); + Image img = load(in, flip); + return img; + } finally { + if (in != null){ + in.close(); + } + } } return null; } diff --git a/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java b/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java index 39dc8d276..dbeaa2d33 100644 --- a/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java +++ b/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java @@ -458,6 +458,8 @@ public class MaterialLoader implements AssetLoader { list.put(matName, mat); } } + + return list; } public Object load(AssetInfo info) throws IOException {