Home | History | Annotate | Download | only in light
      1 /*
      2  * Copyright (c) 2009-2010 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 
     33 package jme3test.light;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.light.DirectionalLight;
     37 import com.jme3.light.PointLight;
     38 import com.jme3.material.Material;
     39 import com.jme3.math.ColorRGBA;
     40 import com.jme3.math.Vector3f;
     41 import com.jme3.scene.Geometry;
     42 import com.jme3.scene.Mesh;
     43 import com.jme3.scene.Mesh.Mode;
     44 import com.jme3.scene.Spatial;
     45 import com.jme3.scene.VertexBuffer.Type;
     46 import com.jme3.scene.shape.Quad;
     47 import com.jme3.scene.shape.Sphere;
     48 import com.jme3.util.BufferUtils;
     49 import com.jme3.util.TangentBinormalGenerator;
     50 import java.nio.FloatBuffer;
     51 import java.nio.IntBuffer;
     52 
     53 
     54 public class TestTangentGen extends SimpleApplication {
     55 
     56     float angle;
     57     PointLight pl;
     58     Geometry lightMdl;
     59 
     60     public static void main(String[] args){
     61         TestTangentGen app = new TestTangentGen();
     62         app.start();
     63     }
     64 
     65     @Override
     66     public void simpleInitApp() {
     67         flyCam.setMoveSpeed(20);
     68         Sphere sphereMesh = new Sphere(32, 32, 1);
     69         sphereMesh.setTextureMode(Sphere.TextureMode.Projected);
     70         sphereMesh.updateGeometry(32, 32, 1, false, false);
     71         addMesh("Sphere", sphereMesh, new Vector3f(-1, 0, 0));
     72 
     73         Quad quadMesh = new Quad(1, 1);
     74         quadMesh.updateGeometry(1, 1);
     75         addMesh("Quad", quadMesh, new Vector3f(1, 0, 0));
     76 
     77         Mesh strip = createTriangleStripMesh();
     78         addMesh("strip", strip, new Vector3f(0, -3, 0));
     79 
     80         DirectionalLight dl = new DirectionalLight();
     81         dl.setDirection(new Vector3f(1, -1, -1).normalizeLocal());
     82         dl.setColor(ColorRGBA.White);
     83         rootNode.addLight(dl);
     84     }
     85 
     86     private void addMesh(String name, Mesh mesh, Vector3f translation) {
     87         TangentBinormalGenerator.generate(mesh);
     88 
     89         Geometry testGeom = new Geometry(name, mesh);
     90         Material mat = assetManager.loadMaterial("Textures/BumpMapTest/Tangent.j3m");
     91         testGeom.setMaterial(mat);
     92         testGeom.getLocalTranslation().set(translation);
     93         rootNode.attachChild(testGeom);
     94 
     95         Geometry debug = new Geometry(
     96                 "Debug " + name,
     97                 TangentBinormalGenerator.genTbnLines(mesh, 0.08f)
     98         );
     99         Material debugMat = assetManager.loadMaterial("Common/Materials/VertexColor.j3m");
    100         debug.setMaterial(debugMat);
    101         debug.setCullHint(Spatial.CullHint.Never);
    102         debug.getLocalTranslation().set(translation);
    103         rootNode.attachChild(debug);
    104     }
    105 
    106     @Override
    107     public void simpleUpdate(float tpf){
    108     }
    109 
    110     private Mesh createTriangleStripMesh() {
    111         Mesh strip = new Mesh();
    112         strip.setMode(Mode.TriangleStrip);
    113         FloatBuffer vb = BufferUtils.createFloatBuffer(3*3*3); // 3 rows * 3 columns * 3 floats
    114         vb.rewind();
    115         vb.put(new float[]{0,2,0}); vb.put(new float[]{1,2,0}); vb.put(new float[]{2,2,0});
    116         vb.put(new float[]{0,1,0}); vb.put(new float[]{1,1,0}); vb.put(new float[]{2,1,0});
    117         vb.put(new float[]{0,0,0}); vb.put(new float[]{1,0,0}); vb.put(new float[]{2,0,0});
    118         FloatBuffer nb = BufferUtils.createFloatBuffer(3*3*3);
    119         nb.rewind();
    120         nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1});
    121         nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1});
    122         nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1});
    123         FloatBuffer tb = BufferUtils.createFloatBuffer(3*3*2);
    124         tb.rewind();
    125         tb.put(new float[]{0,0}); tb.put(new float[]{0.5f,0}); tb.put(new float[]{1,0});
    126         tb.put(new float[]{0,0.5f}); tb.put(new float[]{0.5f,0.5f}); tb.put(new float[]{1,0.5f});
    127         tb.put(new float[]{0,1}); tb.put(new float[]{0.5f,1}); tb.put(new float[]{1,1});
    128         int[] indexes = new int[]{0,3,1,4,2,5, 5,3, 3,6,4,7,5,8};
    129         IntBuffer ib = BufferUtils.createIntBuffer(indexes.length);
    130         ib.put(indexes);
    131         strip.setBuffer(Type.Position, 3, vb);
    132 		strip.setBuffer(Type.Normal, 3, nb);
    133 		strip.setBuffer(Type.TexCoord, 2, tb);
    134 		strip.setBuffer(Type.Index, 3, ib);
    135         strip.updateBound();
    136         return strip;
    137     }
    138 
    139 }
    140