Reset viewport size to current camera settings after reading the framebuffer.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9611 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
pot..om 13 years ago
parent bb631ab13a
commit a76f3a2cab
  1. 306
      engine/src/core/com/jme3/app/state/ScreenshotAppState.java

@ -1,148 +1,158 @@
package com.jme3.app.state; package com.jme3.app.state;
import com.jme3.app.Application; import com.jme3.app.Application;
import com.jme3.input.InputManager; import com.jme3.input.InputManager;
import com.jme3.input.KeyInput; import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener; import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger; import com.jme3.input.controls.KeyTrigger;
import com.jme3.post.SceneProcessor; import com.jme3.post.SceneProcessor;
import com.jme3.renderer.RenderManager; import com.jme3.renderer.Camera;
import com.jme3.renderer.Renderer; import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort; import com.jme3.renderer.Renderer;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.ViewPort;
import com.jme3.system.JmeSystem; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.FrameBuffer; import com.jme3.system.JmeSystem;
import com.jme3.util.BufferUtils; import com.jme3.texture.FrameBuffer;
import java.io.File; import com.jme3.util.BufferUtils;
import java.io.FileOutputStream; import java.io.File;
import java.io.IOException; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.IOException;
import java.nio.ByteBuffer; import java.io.OutputStream;
import java.util.List; import java.nio.ByteBuffer;
import java.util.logging.Level; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Level;
import java.util.logging.Logger;
public class ScreenshotAppState extends AbstractAppState implements ActionListener, SceneProcessor {
public class ScreenshotAppState extends AbstractAppState implements ActionListener, SceneProcessor {
private static final Logger logger = Logger.getLogger(ScreenshotAppState.class.getName());
private String filePath = null; private static final Logger logger = Logger.getLogger(ScreenshotAppState.class.getName());
private boolean capture = false; private String filePath = null;
private Renderer renderer; private boolean capture = false;
private ByteBuffer outBuf; private Renderer renderer;
private String appName; private RenderManager rm;
private int shotIndex = 0; private ByteBuffer outBuf;
private int width, height; private String appName;
private int shotIndex = 0;
/** private int width, height;
* Using this constructor, the screenshot files will be written sequentially to the system
* default storage folder. /**
*/ * Using this constructor, the screenshot files will be written sequentially to the system
public ScreenshotAppState() { * default storage folder.
this(null); */
} public ScreenshotAppState() {
this(null);
/** }
* This constructor allows you to specify the output file path of the screenshot.
* Include the seperator at the end of the path. /**
* Use an emptry string to use the application folder. Use NULL to use the system * This constructor allows you to specify the output file path of the screenshot.
* default storage folder. * Include the seperator at the end of the path.
* @param file The screenshot file path to use. Include the seperator at the end of the path. * Use an emptry string to use the application folder. Use NULL to use the system
*/ * default storage folder.
public ScreenshotAppState(String filePath) { * @param file The screenshot file path to use. Include the seperator at the end of the path.
this.filePath = filePath; */
} public ScreenshotAppState(String filePath) {
this.filePath = filePath;
/** }
* Set the file path to store the screenshot.
* Include the seperator at the end of the path. /**
* Use an emptry string to use the application folder. Use NULL to use the system * Set the file path to store the screenshot.
* default storage folder. * Include the seperator at the end of the path.
* @param file File path to use to store the screenshot. Include the seperator at the end of the path. * Use an emptry string to use the application folder. Use NULL to use the system
*/ * default storage folder.
public void setFilePath(String filePath) { * @param file File path to use to store the screenshot. Include the seperator at the end of the path.
this.filePath = filePath; */
} public void setFilePath(String filePath) {
this.filePath = filePath;
@Override }
public void initialize(AppStateManager stateManager, Application app) {
if (!super.isInitialized()){ @Override
InputManager inputManager = app.getInputManager(); public void initialize(AppStateManager stateManager, Application app) {
inputManager.addMapping("ScreenShot", new KeyTrigger(KeyInput.KEY_SYSRQ)); if (!super.isInitialized()){
inputManager.addListener(this, "ScreenShot"); InputManager inputManager = app.getInputManager();
inputManager.addMapping("ScreenShot", new KeyTrigger(KeyInput.KEY_SYSRQ));
List<ViewPort> vps = app.getRenderManager().getPostViews(); inputManager.addListener(this, "ScreenShot");
ViewPort last = vps.get(vps.size()-1);
last.addProcessor(this); List<ViewPort> vps = app.getRenderManager().getPostViews();
ViewPort last = vps.get(vps.size()-1);
appName = app.getClass().getSimpleName(); last.addProcessor(this);
}
appName = app.getClass().getSimpleName();
super.initialize(stateManager, app); }
}
super.initialize(stateManager, app);
public void onAction(String name, boolean value, float tpf) { }
if (value){
capture = true; public void onAction(String name, boolean value, float tpf) {
} if (value){
} capture = true;
}
public void takeScreenshot() { }
capture = true;
} public void takeScreenshot() {
capture = true;
public void initialize(RenderManager rm, ViewPort vp) { }
renderer = rm.getRenderer();
reshape(vp, vp.getCamera().getWidth(), vp.getCamera().getHeight()); public void initialize(RenderManager rm, ViewPort vp) {
} renderer = rm.getRenderer();
this.rm = rm;
@Override reshape(vp, vp.getCamera().getWidth(), vp.getCamera().getHeight());
public boolean isInitialized() { }
return super.isInitialized() && renderer != null;
} @Override
public boolean isInitialized() {
public void reshape(ViewPort vp, int w, int h) { return super.isInitialized() && renderer != null;
outBuf = BufferUtils.createByteBuffer(w * h * 4); }
width = w;
height = h; public void reshape(ViewPort vp, int w, int h) {
} outBuf = BufferUtils.createByteBuffer(w * h * 4);
width = w;
public void preFrame(float tpf) { height = h;
} }
public void postQueue(RenderQueue rq) { public void preFrame(float tpf) {
} }
public void postFrame(FrameBuffer out) { public void postQueue(RenderQueue rq) {
if (capture){ }
capture = false;
shotIndex++; public void postFrame(FrameBuffer out) {
if (capture){
renderer.setViewPort(0, 0, width, height); capture = false;
renderer.readFrameBuffer(out, outBuf); shotIndex++;
File file; Camera curCamera = rm.getCurrentCamera();
if (filePath == null) { int viewX = (int) (curCamera.getViewPortLeft() * curCamera.getWidth());
file = new File(JmeSystem.getStorageFolder() + File.separator + appName + shotIndex + ".png").getAbsoluteFile(); int viewY = (int) (curCamera.getViewPortBottom() * curCamera.getHeight());
} else { int viewWidth = (int) ((curCamera.getViewPortRight() - curCamera.getViewPortLeft()) * curCamera.getWidth());
file = new File(filePath + appName + shotIndex + ".png").getAbsoluteFile(); int viewHeight = (int) ((curCamera.getViewPortTop() - curCamera.getViewPortBottom()) * curCamera.getHeight());
}
logger.log(Level.INFO, "Saving ScreenShot to: {0}", file.getAbsolutePath()); renderer.setViewPort(0, 0, width, height);
renderer.readFrameBuffer(out, outBuf);
OutputStream outStream = null; renderer.setViewPort(viewX, viewY, viewWidth, viewHeight);
try {
outStream = new FileOutputStream(file); File file;
JmeSystem.writeImageFile(outStream, "png", outBuf, width, height); if (filePath == null) {
} catch (IOException ex) { file = new File(JmeSystem.getStorageFolder() + File.separator + appName + shotIndex + ".png").getAbsoluteFile();
logger.log(Level.SEVERE, "Error while saving screenshot", ex); } else {
} finally { file = new File(filePath + appName + shotIndex + ".png").getAbsoluteFile();
if (outStream != null){ }
try { logger.log(Level.INFO, "Saving ScreenShot to: {0}", file.getAbsolutePath());
outStream.close();
} catch (IOException ex) { OutputStream outStream = null;
logger.log(Level.SEVERE, "Error while saving screenshot", ex); try {
} outStream = new FileOutputStream(file);
} JmeSystem.writeImageFile(outStream, "png", outBuf, width, height);
} } catch (IOException ex) {
} logger.log(Level.SEVERE, "Error while saving screenshot", ex);
} } finally {
} if (outStream != null){
try {
outStream.close();
} catch (IOException ex) {
logger.log(Level.SEVERE, "Error while saving screenshot", ex);
}
}
}
}
}
}

Loading…
Cancel
Save