Home | History | Annotate | Download | only in light
      1 package jme3test.light;
      2 
      3 import com.jme3.app.SimpleApplication;
      4 import com.jme3.font.BitmapText;
      5 import com.jme3.input.KeyInput;
      6 import com.jme3.input.controls.ActionListener;
      7 import com.jme3.input.controls.KeyTrigger;
      8 import com.jme3.light.DirectionalLight;
      9 import com.jme3.light.PointLight;
     10 import com.jme3.material.Material;
     11 import com.jme3.math.ColorRGBA;
     12 import com.jme3.math.FastMath;
     13 import com.jme3.math.Vector3f;
     14 import com.jme3.renderer.queue.RenderQueue.Bucket;
     15 import com.jme3.scene.*;
     16 import com.jme3.scene.Spatial.CullHint;
     17 import com.jme3.scene.shape.Sphere;
     18 import com.jme3.util.TangentBinormalGenerator;
     19 
     20 /**
     21  *
     22  * @author Kirusha
     23  */
     24 public class TestTangentGenBadModels extends SimpleApplication {
     25 
     26     float angle;
     27     PointLight pl;
     28     Geometry lightMdl;
     29 
     30     public static void main(String[] args){
     31         TestTangentGenBadModels app = new TestTangentGenBadModels();
     32         app.start();
     33     }
     34 
     35     @Override
     36     public void simpleInitApp() {
     37 //        assetManager.registerLocator("http://jme-glsl-shaders.googlecode.com/hg/assets/Models/LightBlow/", UrlLocator.class);
     38 //        assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/", UrlLocator.class);
     39 
     40         final Spatial badModel = assetManager.loadModel("Models/TangentBugs/test.blend");
     41 //        badModel.setLocalScale(1f);
     42 
     43         Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
     44         mat.setTexture("NormalMap", assetManager.loadTexture("Models/TangentBugs/test_normal.png"));
     45 //        Material mat = assetManager.loadMaterial("Textures/BumpMapTest/Tangent.j3m");
     46         badModel.setMaterial(mat);
     47         rootNode.attachChild(badModel);
     48 
     49         // TODO: For some reason blender loader fails to load this.
     50         // need to check it
     51 //        Spatial model = assetManager.loadModel("test.blend");
     52 //        rootNode.attachChild(model);
     53 
     54         final Node debugTangents = new Node("debug tangents");
     55         debugTangents.setCullHint(CullHint.Always);
     56         rootNode.attachChild(debugTangents);
     57 
     58         final Material debugMat = assetManager.loadMaterial("Common/Materials/VertexColor.j3m");
     59 
     60         badModel.depthFirstTraversal(new SceneGraphVisitorAdapter(){
     61             @Override
     62             public void visit(Geometry g){
     63                 Mesh m = g.getMesh();
     64                 Material mat = g.getMaterial();
     65 
     66 //                if (mat.getParam("DiffuseMap") != null){
     67 //                    mat.setTexture("DiffuseMap", null);
     68 //                }
     69                 TangentBinormalGenerator.generate(m);
     70 
     71                 Geometry debug = new Geometry(
     72                     "debug tangents geom",
     73                     TangentBinormalGenerator.genTbnLines(g.getMesh(), 0.2f)
     74                 );
     75                 debug.getMesh().setLineWidth(1);
     76                 debug.setMaterial(debugMat);
     77                 debug.setCullHint(Spatial.CullHint.Never);
     78                 debug.setLocalTransform(g.getWorldTransform());
     79                 debugTangents.attachChild(debug);
     80             }
     81         });
     82 
     83         DirectionalLight dl = new DirectionalLight();
     84         dl.setDirection(new Vector3f(-0.8f, -0.6f, -0.08f).normalizeLocal());
     85         dl.setColor(new ColorRGBA(1,1,1,1));
     86         rootNode.addLight(dl);
     87 
     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 
     93         pl = new PointLight();
     94         pl.setColor(ColorRGBA.White);
     95 //        rootNode.addLight(pl);
     96 
     97 
     98         BitmapText info = new BitmapText(guiFont);
     99         info.setText("Press SPACE to switch between lighting and tangent display");
    100         info.setQueueBucket(Bucket.Gui);
    101         info.move(0, settings.getHeight() - info.getLineHeight(), 0);
    102         rootNode.attachChild(info);
    103 
    104         inputManager.addMapping("space", new KeyTrigger(KeyInput.KEY_SPACE));
    105         inputManager.addListener(new ActionListener() {
    106 
    107             private boolean isLit = true;
    108 
    109             public void onAction(String name, boolean isPressed, float tpf) {
    110                 if (isPressed) return;
    111                 Material mat;
    112                 if (isLit){
    113                     mat = assetManager.loadMaterial("Textures/BumpMapTest/Tangent.j3m");
    114                     debugTangents.setCullHint(CullHint.Inherit);
    115                 }else{
    116                     mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    117                     mat.setTexture("NormalMap", assetManager.loadTexture("Models/TangentBugs/test_normal.png"));
    118                     debugTangents.setCullHint(CullHint.Always);
    119                 }
    120                 isLit = !isLit;
    121                 badModel.setMaterial(mat);
    122             }
    123         }, "space");
    124     }
    125 
    126     @Override
    127     public void simpleUpdate(float tpf){
    128         angle += tpf;
    129         angle %= FastMath.TWO_PI;
    130 
    131         pl.setPosition(new Vector3f(FastMath.cos(angle) * 2f, 2f, FastMath.sin(angle) * 2f));
    132         lightMdl.setLocalTranslation(pl.getPosition());
    133     }
    134 
    135 
    136 }
    137