TestBackgroundImage.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2021 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.renderer;
  33. import com.jme3.anim.util.AnimMigrationUtils;
  34. import com.jme3.app.SimpleApplication;
  35. import com.jme3.light.AmbientLight;
  36. import com.jme3.light.DirectionalLight;
  37. import com.jme3.material.Material;
  38. import com.jme3.material.Materials;
  39. import com.jme3.math.ColorRGBA;
  40. import com.jme3.math.Vector3f;
  41. import com.jme3.renderer.Camera;
  42. import com.jme3.renderer.ViewPort;
  43. import com.jme3.renderer.queue.RenderQueue;
  44. import com.jme3.scene.Geometry;
  45. import com.jme3.scene.Mesh;
  46. import com.jme3.scene.Node;
  47. import com.jme3.scene.Spatial;
  48. import com.jme3.scene.shape.Quad;
  49. import com.jme3.texture.Texture;
  50. import java.util.List;
  51. /**
  52. * Demonstrates how to render an non-moving background image using a pre
  53. * ViewPort.
  54. *
  55. * @author Stephen Gold [email protected]
  56. */
  57. public class TestBackgroundImage extends SimpleApplication {
  58. private static ViewPort backgroundViewport;
  59. public static void main(String[] args) {
  60. new TestBackgroundImage().start();
  61. }
  62. @Override
  63. public void simpleInitApp() {
  64. /*
  65. * SimpleApplication creates 2 viewports:
  66. * 1. the default viewport (rendered first, after clearing all buffers)
  67. * 2. the GUI viewport (rendered last, without clearing any buffers)
  68. *
  69. * Create a 3rd ViewPort, named "background viewport",
  70. * to be rendered BEFORE the default viewport.
  71. */
  72. Camera backgroundCamera = guiViewPort.getCamera().clone();
  73. backgroundViewport = renderManager.createPreView(
  74. "background viewport", backgroundCamera);
  75. /*
  76. * Don't clear the color buffer before drawing the main viewport.
  77. * Clearing the color buffer would hide the background.
  78. */
  79. boolean clearColorBuffer = false;
  80. viewPort.setClearFlags(clearColorBuffer, true, true);
  81. /*
  82. * Create a quad to display the JMonkeyEngine logo,
  83. * assign it to the Gui bucket,
  84. * and attach it to the background viewport.
  85. */
  86. Texture quadTexture
  87. = assetManager.loadTexture("Interface/Logo/Monkey.png");
  88. Material quadMaterial = new Material(assetManager, Materials.UNSHADED);
  89. quadMaterial.setTexture("ColorMap", quadTexture);
  90. float quadHeight = backgroundCamera.getHeight();
  91. float quadWidth = backgroundCamera.getWidth();
  92. Mesh quadMesh = new Quad(quadWidth, quadHeight);
  93. Spatial quadGeometry = new Geometry("quad geometry", quadMesh);
  94. quadGeometry.setMaterial(quadMaterial);
  95. quadGeometry.setQueueBucket(RenderQueue.Bucket.Gui);
  96. backgroundViewport.attachScene(quadGeometry);
  97. /*
  98. * Add Jaime model and lighting to the default scene.
  99. */
  100. loadModel();
  101. setupLights();
  102. /*
  103. * Speed up camera motion for convenience.
  104. */
  105. flyCam.setMoveSpeed(8f);
  106. }
  107. @Override
  108. public void simpleUpdate(float timePerFrame) {
  109. /*
  110. * Since SimpleApplication is unaware of the background viewport,
  111. * the application must explictly update its scenes.
  112. */
  113. List<Spatial> scenes = backgroundViewport.getScenes();
  114. for (Spatial scene : scenes) {
  115. scene.updateLogicalState(timePerFrame);
  116. scene.updateGeometricState();
  117. }
  118. }
  119. private void loadModel() {
  120. Node jaime = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
  121. AnimMigrationUtils.migrate(jaime);
  122. jaime.scale(3f);
  123. rootNode.attachChild(jaime);
  124. }
  125. private void setupLights() {
  126. AmbientLight ambient = new AmbientLight();
  127. ambient.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
  128. rootNode.addLight(ambient);
  129. DirectionalLight sun = new DirectionalLight();
  130. sun.setDirection(new Vector3f(0f, -1f, -1f).normalizeLocal());
  131. sun.setColor(ColorRGBA.White.mult(1.5f));
  132. rootNode.addLight(sun);
  133. }
  134. }