TestRenderToMemory.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package jme3test.post;
  33. import com.jme3.app.SimpleApplication;
  34. import com.jme3.material.Material;
  35. import com.jme3.math.ColorRGBA;
  36. import com.jme3.math.FastMath;
  37. import com.jme3.math.Quaternion;
  38. import com.jme3.math.Vector3f;
  39. import com.jme3.post.SceneProcessor;
  40. import com.jme3.renderer.Camera;
  41. import com.jme3.renderer.RenderManager;
  42. import com.jme3.renderer.ViewPort;
  43. import com.jme3.renderer.queue.RenderQueue;
  44. import com.jme3.scene.Geometry;
  45. import com.jme3.scene.shape.Box;
  46. import com.jme3.system.AppSettings;
  47. import com.jme3.system.JmeContext.Type;
  48. import com.jme3.texture.FrameBuffer;
  49. import com.jme3.texture.Image;
  50. import com.jme3.texture.Image.Format;
  51. import com.jme3.texture.Texture2D;
  52. import com.jme3.util.BufferUtils;
  53. import com.jme3.util.Screenshots;
  54. import java.awt.Color;
  55. import java.awt.Dimension;
  56. import java.awt.Graphics;
  57. import java.awt.Graphics2D;
  58. import java.awt.event.WindowAdapter;
  59. import java.awt.event.WindowEvent;
  60. import java.awt.image.BufferedImage;
  61. import java.nio.ByteBuffer;
  62. import javax.swing.JFrame;
  63. import javax.swing.JPanel;
  64. import javax.swing.SwingUtilities;
  65. /**
  66. * This test renders a scene to an offscreen framebuffer, then copies
  67. * the contents to a Swing JFrame. Note that some parts are done inefficently,
  68. * this is done to make the code more readable.
  69. */
  70. public class TestRenderToMemory extends SimpleApplication implements SceneProcessor {
  71. private Geometry offBox;
  72. private float angle = 0;
  73. private FrameBuffer offBuffer;
  74. private ViewPort offView;
  75. private Texture2D offTex;
  76. private Camera offCamera;
  77. private ImageDisplay display;
  78. private static final int width = 800, height = 600;
  79. private final ByteBuffer cpuBuf = BufferUtils.createByteBuffer(width * height * 4);
  80. private final byte[] cpuArray = new byte[width * height * 4];
  81. private final BufferedImage image = new BufferedImage(width, height,
  82. BufferedImage.TYPE_INT_BGR);
  83. private class ImageDisplay extends JPanel {
  84. private long t;
  85. private long total;
  86. private int frames;
  87. private int fps;
  88. @Override
  89. public void paintComponent(Graphics gfx) {
  90. super.paintComponent(gfx);
  91. Graphics2D g2d = (Graphics2D) gfx;
  92. if (t == 0)
  93. t = timer.getTime();
  94. // g2d.setBackground(Color.BLACK);
  95. // g2d.clearRect(0,0,width,height);
  96. synchronized (image){
  97. g2d.drawImage(image, null, 0, 0);
  98. }
  99. long t2 = timer.getTime();
  100. long dt = t2 - t;
  101. total += dt;
  102. frames ++;
  103. t = t2;
  104. if (total > timer.getResolution()) {
  105. fps = frames;
  106. total = 0;
  107. frames = 0;
  108. }
  109. g2d.setColor(Color.white);
  110. g2d.drawString("FPS: "+fps, 0, getHeight() - 100);
  111. }
  112. }
  113. public static void main(String[] args){
  114. TestRenderToMemory app = new TestRenderToMemory();
  115. app.setPauseOnLostFocus(false);
  116. AppSettings settings = new AppSettings(true);
  117. settings.setResolution(1, 1);
  118. app.setSettings(settings);
  119. app.start(Type.OffscreenSurface);
  120. }
  121. public void createDisplayFrame(){
  122. SwingUtilities.invokeLater(new Runnable(){
  123. public void run(){
  124. JFrame frame = new JFrame("Render Display");
  125. display = new ImageDisplay();
  126. display.setPreferredSize(new Dimension(width, height));
  127. frame.getContentPane().add(display);
  128. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  129. frame.addWindowListener(new WindowAdapter(){
  130. public void windowClosed(WindowEvent e){
  131. stop();
  132. }
  133. });
  134. frame.pack();
  135. frame.setLocationRelativeTo(null);
  136. frame.setResizable(false);
  137. frame.setVisible(true);
  138. }
  139. });
  140. }
  141. public void updateImageContents(){
  142. cpuBuf.clear();
  143. renderer.readFrameBuffer(offBuffer, cpuBuf);
  144. synchronized (image) {
  145. Screenshots.convertScreenShot2(cpuBuf.asIntBuffer(), image);
  146. }
  147. if (display != null)
  148. display.repaint();
  149. }
  150. public void setupOffscreenView(){
  151. offCamera = new Camera(width, height);
  152. // create a pre-view. a view that is rendered before the main view
  153. offView = renderManager.createPreView("Offscreen View", offCamera);
  154. offView.setBackgroundColor(ColorRGBA.DarkGray);
  155. offView.setClearFlags(true, true, true);
  156. // this will let us know when the scene has been rendered to the
  157. // frame buffer
  158. offView.addProcessor(this);
  159. // create offscreen framebuffer
  160. offBuffer = new FrameBuffer(width, height, 1);
  161. //setup framebuffer's cam
  162. offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
  163. offCamera.setLocation(new Vector3f(0f, 0f, -5f));
  164. offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
  165. //setup framebuffer's texture
  166. // offTex = new Texture2D(width, height, Format.RGBA8);
  167. //setup framebuffer to use renderbuffer
  168. // this is faster for gpu -> cpu copies
  169. offBuffer.setDepthBuffer(Format.Depth);
  170. offBuffer.setColorBuffer(Format.RGBA8);
  171. // offBuffer.setColorTexture(offTex);
  172. //set viewport to render to offscreen framebuffer
  173. offView.setOutputFrameBuffer(offBuffer);
  174. // setup framebuffer's scene
  175. Box boxMesh = new Box(1, 1, 1);
  176. Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m");
  177. offBox = new Geometry("box", boxMesh);
  178. offBox.setMaterial(material);
  179. // attach the scene to the viewport to be rendered
  180. offView.attachScene(offBox);
  181. }
  182. @Override
  183. public void simpleInitApp() {
  184. setupOffscreenView();
  185. createDisplayFrame();
  186. }
  187. @Override
  188. public void simpleUpdate(float tpf){
  189. Quaternion q = new Quaternion();
  190. angle += tpf;
  191. angle %= FastMath.TWO_PI;
  192. q.fromAngles(angle, 0, angle);
  193. offBox.setLocalRotation(q);
  194. offBox.updateLogicalState(tpf);
  195. offBox.updateGeometricState();
  196. }
  197. public void initialize(RenderManager rm, ViewPort vp) {
  198. }
  199. public void reshape(ViewPort vp, int w, int h) {
  200. }
  201. public boolean isInitialized() {
  202. return true;
  203. }
  204. public void preFrame(float tpf) {
  205. }
  206. public void postQueue(RenderQueue rq) {
  207. }
  208. /**
  209. * Update the CPU image's contents after the scene has
  210. * been rendered to the framebuffer.
  211. */
  212. public void postFrame(FrameBuffer out) {
  213. updateImageContents();
  214. }
  215. public void cleanup() {
  216. }
  217. }