TestCrossHatch.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.input.KeyInput;
  35. import com.jme3.input.controls.ActionListener;
  36. import com.jme3.input.controls.KeyTrigger;
  37. import com.jme3.light.DirectionalLight;
  38. import com.jme3.material.Material;
  39. import com.jme3.math.ColorRGBA;
  40. import com.jme3.math.Quaternion;
  41. import com.jme3.math.Vector3f;
  42. import com.jme3.post.FilterPostProcessor;
  43. import com.jme3.post.filters.CrossHatchFilter;
  44. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  45. import com.jme3.scene.Geometry;
  46. import com.jme3.scene.Spatial;
  47. import com.jme3.scene.debug.WireFrustum;
  48. import com.jme3.scene.shape.Box;
  49. import com.jme3.util.SkyFactory;
  50. public class TestCrossHatch extends SimpleApplication {
  51. float angle;
  52. Spatial lightMdl;
  53. Spatial teapot;
  54. Geometry frustumMdl;
  55. WireFrustum frustum;
  56. boolean active=true;
  57. FilterPostProcessor fpp;
  58. public static void main(String[] args){
  59. TestCrossHatch app = new TestCrossHatch();
  60. app.start();
  61. }
  62. @Override
  63. public void simpleInitApp() {
  64. // put the camera in a bad position
  65. cam.setLocation(new Vector3f(-2.336393f, 11.91392f, -7.139601f));
  66. cam.setRotation(new Quaternion(0.23602544f, 0.11321983f, -0.027698677f, 0.96473104f));
  67. //cam.setFrustumFar(1000);
  68. Material mat = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
  69. mat.setFloat("Shininess", 15f);
  70. mat.setBoolean("UseMaterialColors", true);
  71. mat.setColor("Ambient", ColorRGBA.Yellow.mult(0.2f));
  72. mat.setColor("Diffuse", ColorRGBA.Yellow.mult(0.2f));
  73. mat.setColor("Specular", ColorRGBA.Yellow.mult(0.8f));
  74. Material matSoil = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
  75. matSoil.setFloat("Shininess", 15f);
  76. matSoil.setBoolean("UseMaterialColors", true);
  77. matSoil.setColor("Ambient", ColorRGBA.Gray);
  78. matSoil.setColor("Diffuse", ColorRGBA.Black);
  79. matSoil.setColor("Specular", ColorRGBA.Gray);
  80. teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
  81. teapot.setLocalTranslation(0,0,10);
  82. teapot.setMaterial(mat);
  83. teapot.setShadowMode(ShadowMode.CastAndReceive);
  84. teapot.setLocalScale(10.0f);
  85. rootNode.attachChild(teapot);
  86. Geometry soil=new Geometry("soil", new Box(new Vector3f(0, -13, 550), 800, 10, 700));
  87. soil.setMaterial(matSoil);
  88. soil.setShadowMode(ShadowMode.CastAndReceive);
  89. rootNode.attachChild(soil);
  90. DirectionalLight light=new DirectionalLight();
  91. light.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
  92. light.setColor(ColorRGBA.White.mult(1.5f));
  93. rootNode.addLight(light);
  94. // load sky
  95. Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Bright/FullskiesBlueClear03.dds", false);
  96. sky.setCullHint(Spatial.CullHint.Never);
  97. rootNode.attachChild(sky);
  98. fpp=new FilterPostProcessor(assetManager);
  99. int numSamples = getContext().getSettings().getSamples();
  100. if( numSamples > 0 ) {
  101. fpp.setNumSamples(numSamples);
  102. }
  103. CrossHatchFilter chf=new CrossHatchFilter();
  104. viewPort.addProcessor(fpp);
  105. fpp.addFilter(chf);
  106. initInputs();
  107. }
  108. private void initInputs() {
  109. inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
  110. ActionListener acl = new ActionListener() {
  111. public void onAction(String name, boolean keyPressed, float tpf) {
  112. if (name.equals("toggle") && keyPressed) {
  113. if(active){
  114. active=false;
  115. viewPort.removeProcessor(fpp);
  116. }else{
  117. active=true;
  118. viewPort.addProcessor(fpp);
  119. }
  120. }
  121. }
  122. };
  123. inputManager.addListener(acl, "toggle");
  124. }
  125. }