TestRenderToCubemap.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.renderer.Camera;
  40. import com.jme3.renderer.ViewPort;
  41. import com.jme3.scene.Geometry;
  42. import com.jme3.scene.shape.Box;
  43. import com.jme3.texture.FrameBuffer;
  44. import com.jme3.texture.Image.Format;
  45. import com.jme3.texture.Texture;
  46. import com.jme3.texture.TextureCubeMap;
  47. import com.jme3.util.SkyFactory;
  48. /**
  49. * Renders a rotating box to a cubemap texture, then applies the cubemap
  50. * texture as a sky.
  51. */
  52. public class TestRenderToCubemap extends SimpleApplication {
  53. private Geometry offBox;
  54. private float angle = 0;
  55. private ViewPort offView;
  56. public static void main(String[] args){
  57. TestRenderToCubemap app = new TestRenderToCubemap();
  58. app.start();
  59. }
  60. public Texture setupOffscreenView(){
  61. Camera offCamera = new Camera(512, 512);
  62. offView = renderManager.createPreView("Offscreen View", offCamera);
  63. offView.setClearFlags(true, true, true);
  64. offView.setBackgroundColor(ColorRGBA.DarkGray);
  65. // create offscreen framebuffer
  66. FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);
  67. //setup framebuffer's cam
  68. offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
  69. offCamera.setLocation(new Vector3f(0f, 0f, -5f));
  70. offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
  71. //setup framebuffer's texture
  72. TextureCubeMap offTex = new TextureCubeMap(512, 512, Format.RGBA8);
  73. offTex.setMinFilter(Texture.MinFilter.Trilinear);
  74. offTex.setMagFilter(Texture.MagFilter.Bilinear);
  75. //setup framebuffer to use texture
  76. offBuffer.setDepthBuffer(Format.Depth);
  77. offBuffer.setMultiTarget(true);
  78. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeX);
  79. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveX);
  80. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeY);
  81. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveY);
  82. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeZ);
  83. offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveZ);
  84. //set viewport to render to offscreen framebuffer
  85. offView.setOutputFrameBuffer(offBuffer);
  86. // setup framebuffer's scene
  87. Box boxMesh = new Box(Vector3f.ZERO, 1,1,1);
  88. Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m");
  89. offBox = new Geometry("box", boxMesh);
  90. offBox.setMaterial(material);
  91. // attach the scene to the viewport to be rendered
  92. offView.attachScene(offBox);
  93. return offTex;
  94. }
  95. @Override
  96. public void simpleInitApp() {
  97. cam.setLocation(new Vector3f(3, 3, 3));
  98. cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
  99. Texture offTex = setupOffscreenView();
  100. rootNode.attachChild(SkyFactory.createSky(assetManager, offTex, false));
  101. }
  102. @Override
  103. public void simpleUpdate(float tpf){
  104. Quaternion q = new Quaternion();
  105. angle += tpf;
  106. angle %= FastMath.TWO_PI;
  107. q.fromAngles(angle, 0, angle);
  108. offBox.setLocalRotation(q);
  109. offBox.updateLogicalState(tpf);
  110. offBox.updateGeometricState();
  111. }
  112. }