1 /* 2 * Copyright (c) 2009-2010 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 33 package jme3test.post; 34 35 import com.jme3.app.SimpleApplication; 36 import com.jme3.input.KeyInput; 37 import com.jme3.input.controls.ActionListener; 38 import com.jme3.input.controls.KeyTrigger; 39 import com.jme3.material.Material; 40 import com.jme3.math.ColorRGBA; 41 import com.jme3.math.FastMath; 42 import com.jme3.math.Quaternion; 43 import com.jme3.math.Vector3f; 44 import com.jme3.renderer.Camera; 45 import com.jme3.renderer.ViewPort; 46 import com.jme3.scene.Geometry; 47 import com.jme3.scene.shape.Box; 48 import com.jme3.texture.FrameBuffer; 49 import com.jme3.texture.Image.Format; 50 import com.jme3.texture.Texture; 51 import com.jme3.texture.Texture2D; 52 53 /** 54 * This test renders a scene to a texture, then displays the texture on a cube. 55 */ 56 public class TestRenderToTexture extends SimpleApplication implements ActionListener { 57 58 private static final String TOGGLE_UPDATE = "Toggle Update"; 59 private Geometry offBox; 60 private float angle = 0; 61 private ViewPort offView; 62 main(String[] args)63 public static void main(String[] args){ 64 TestRenderToTexture app = new TestRenderToTexture(); 65 app.start(); 66 } 67 setupOffscreenView()68 public Texture setupOffscreenView(){ 69 Camera offCamera = new Camera(512, 512); 70 71 offView = renderManager.createPreView("Offscreen View", offCamera); 72 offView.setClearFlags(true, true, true); 73 offView.setBackgroundColor(ColorRGBA.DarkGray); 74 75 // create offscreen framebuffer 76 FrameBuffer offBuffer = new FrameBuffer(512, 512, 1); 77 78 //setup framebuffer's cam 79 offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f); 80 offCamera.setLocation(new Vector3f(0f, 0f, -5f)); 81 offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y); 82 83 //setup framebuffer's texture 84 Texture2D offTex = new Texture2D(512, 512, Format.RGBA8); 85 offTex.setMinFilter(Texture.MinFilter.Trilinear); 86 offTex.setMagFilter(Texture.MagFilter.Bilinear); 87 88 //setup framebuffer to use texture 89 offBuffer.setDepthBuffer(Format.Depth); 90 offBuffer.setColorTexture(offTex); 91 92 //set viewport to render to offscreen framebuffer 93 offView.setOutputFrameBuffer(offBuffer); 94 95 // setup framebuffer's scene 96 Box boxMesh = new Box(Vector3f.ZERO, 1,1,1); 97 Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m"); 98 offBox = new Geometry("box", boxMesh); 99 offBox.setMaterial(material); 100 101 // attach the scene to the viewport to be rendered 102 offView.attachScene(offBox); 103 104 return offTex; 105 } 106 107 @Override simpleInitApp()108 public void simpleInitApp() { 109 cam.setLocation(new Vector3f(3, 3, 3)); 110 cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y); 111 112 //setup main scene 113 Geometry quad = new Geometry("box", new Box(Vector3f.ZERO, 1,1,1)); 114 115 Texture offTex = setupOffscreenView(); 116 117 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 118 mat.setTexture("ColorMap", offTex); 119 quad.setMaterial(mat); 120 rootNode.attachChild(quad); 121 inputManager.addMapping(TOGGLE_UPDATE, new KeyTrigger(KeyInput.KEY_SPACE)); 122 inputManager.addListener(this, TOGGLE_UPDATE); 123 } 124 125 @Override simpleUpdate(float tpf)126 public void simpleUpdate(float tpf){ 127 Quaternion q = new Quaternion(); 128 129 if (offView.isEnabled()) { 130 angle += tpf; 131 angle %= FastMath.TWO_PI; 132 q.fromAngles(angle, 0, angle); 133 134 offBox.setLocalRotation(q); 135 offBox.updateLogicalState(tpf); 136 offBox.updateGeometricState(); 137 } 138 } 139 140 @Override onAction(String name, boolean isPressed, float tpf)141 public void onAction(String name, boolean isPressed, float tpf) { 142 if (name.equals(TOGGLE_UPDATE) && isPressed) { 143 offView.setEnabled(!offView.isEnabled()); 144 } 145 } 146 147 148 } 149