TestTwoSideLighting.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2009-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.light;
  33. import com.jme3.app.SimpleApplication;
  34. import com.jme3.light.PointLight;
  35. import com.jme3.material.Material;
  36. import com.jme3.material.RenderState;
  37. import com.jme3.material.TechniqueDef;
  38. import com.jme3.math.ColorRGBA;
  39. import com.jme3.math.FastMath;
  40. import com.jme3.math.Quaternion;
  41. import com.jme3.math.Vector3f;
  42. import com.jme3.scene.Geometry;
  43. import com.jme3.scene.shape.Quad;
  44. import com.jme3.scene.shape.Sphere;
  45. import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
  46. /**
  47. * Checks two-sided lighting capability.
  48. *
  49. * @author Kirill Vainer
  50. */
  51. public class TestTwoSideLighting extends SimpleApplication {
  52. private float angle;
  53. private PointLight pl;
  54. private Geometry lightMdl;
  55. public static void main(String[] args){
  56. TestTwoSideLighting app = new TestTwoSideLighting();
  57. app.start();
  58. }
  59. @Override
  60. public void simpleInitApp() {
  61. // Two-sided lighting requires single pass.
  62. renderManager.setPreferredLightMode(TechniqueDef.LightMode.SinglePass);
  63. renderManager.setSinglePassLightBatchSize(4);
  64. cam.setLocation(new Vector3f(5.936224f, 3.3759952f, -3.3202777f));
  65. cam.setRotation(new Quaternion(0.16265652f, -0.4811838f, 0.09137692f, 0.8565368f));
  66. Geometry quadGeom = new Geometry("quad", new Quad(1, 1));
  67. quadGeom.move(1, 0, 0);
  68. Material mat1 = assetManager.loadMaterial("Textures/BumpMapTest/SimpleBump.j3m");
  69. // Display both front and back faces.
  70. mat1.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
  71. quadGeom.setMaterial(mat1);
  72. // SimpleBump material requires tangents.
  73. MikktspaceTangentGenerator.generate(quadGeom);
  74. rootNode.attachChild(quadGeom);
  75. Geometry teapot = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
  76. teapot.move(-1, 0, 0);
  77. teapot.setLocalScale(2f);
  78. Material mat2 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  79. mat2.setFloat("Shininess", 25);
  80. mat2.setBoolean("UseMaterialColors", true);
  81. mat2.setColor("Ambient", ColorRGBA.Black);
  82. mat2.setColor("Diffuse", ColorRGBA.Gray);
  83. mat2.setColor("Specular", ColorRGBA.Gray);
  84. // Only display backfaces.
  85. mat2.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Front);
  86. teapot.setMaterial(mat2);
  87. rootNode.attachChild(teapot);
  88. lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
  89. lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
  90. lightMdl.getMesh().setStatic();
  91. rootNode.attachChild(lightMdl);
  92. pl = new PointLight();
  93. pl.setColor(ColorRGBA.White);
  94. pl.setRadius(4f);
  95. rootNode.addLight(pl);
  96. }
  97. @Override
  98. public void simpleUpdate(float tpf){
  99. angle += tpf;
  100. angle %= FastMath.TWO_PI;
  101. pl.setPosition(new Vector3f(FastMath.cos(angle) * 3f, 0.5f, FastMath.sin(angle) * 3f));
  102. lightMdl.setLocalTranslation(pl.getPosition());
  103. }
  104. }