Advertisement
Guest User

Untitled

a guest
Dec 26th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.material.Material;
  5. import com.jme3.math.Vector3f;
  6. import com.jme3.scene.Geometry;
  7. import com.jme3.scene.shape.Box;
  8. import com.jme3.math.ColorRGBA;
  9. import com.jme3.input.KeyInput;
  10. import com.jme3.input.MouseInput;
  11. import com.jme3.input.controls.ActionListener;
  12. import com.jme3.input.controls.AnalogListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.input.controls.MouseButtonTrigger;
  15. import com.jme3.light.AmbientLight;
  16. import com.jme3.light.DirectionalLight;
  17. import com.jme3.light.PointLight;
  18. import com.jme3.math.FastMath;
  19. import com.jme3.math.Quaternion;
  20. import com.jme3.scene.Node;
  21. import com.jme3.scene.Spatial;
  22. import com.jme3.scene.shape.Sphere;
  23. import com.jme3.texture.Texture;
  24. import com.jme3.util.TangentBinormalGenerator;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27.  
  28. /** Sample 5 - how to map keys and mousebuttons to actions */
  29. public class Main extends SimpleA {
  30.  
  31.     public static void main(String[] args) {
  32.         Main app = new Main();
  33.         app.start();
  34.     }
  35.     //protected Geometry player;
  36.     Boolean isRunning = true;
  37.     float angle;
  38.     Geometry lightMdl;
  39.     Vector3f posLuz = new Vector3f(0, -2, -2);
  40.     private PointLight pl;
  41.  
  42.     @Override
  43.     public void simpleInitApp() {
  44.  
  45.         flyCam.setMoveSpeed(5f);
  46.         pl = new PointLight();
  47.         pl.setColor(ColorRGBA.White);
  48.         pl.setPosition(posLuz);
  49.         pl.setRadius(4f);
  50.         rootNode.addLight(pl);
  51.  
  52.         AmbientLight al = new AmbientLight();
  53.         al.setColor(ColorRGBA.White);
  54.         rootNode.addLight(al);
  55.  
  56.  
  57.         //rootNode.attachChild(fazBloco(0,0,0));
  58.         this.lightMdl = miniBloco(posLuz);
  59.         Geometry bloco1 = fazBloco(0,0,0);
  60.        // Geometry bloco2 = fazBlocoToon(2,2,0);
  61.         rootNode.attachChild(bloco1);
  62.        // rootNode.attachChild(bloco2);
  63.         //rootNode.attachChild(fazBloco(6,0,0));
  64.  
  65.         rootNode.attachChild(lightMdl);
  66.  
  67.         DirectionalLight sun = new DirectionalLight();
  68.         sun.setDirection(posLuz);
  69.         sun.setColor(ColorRGBA.White);
  70.         rootNode.addLight(sun);
  71.  
  72.         initKeys(); // load my custom keybinding
  73.     }
  74.  
  75.     public Geometry fazBlocoToon(int x, int y, int z) {
  76.         Box boxshape1 = new Box(new Vector3f(x, y, z), 3, 0.5f, 1);
  77.         Geometry g = new Geometry("BoxToon", boxshape1);
  78.         Material m = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  79.         Texture t = assetManager.loadTexture("Textures/bloco.png");
  80.         m.setTexture("ColorRamp", t);
  81.         m.setBoolean("UseMaterialColors", true);
  82.         m.setColor("Specular", ColorRGBA.Black);
  83.         m.setColor("Diffuse", ColorRGBA.White);
  84.         m.setBoolean("VertexLighting", true);
  85.         g.setMaterial(m);
  86.         return g;
  87.     }
  88.  
  89.     public Geometry fazBloco(int x, int y, int z) {
  90.         Box boxshape1 = new Box(new Vector3f(x, y, z), 3, 0.5f, 1);
  91.         Geometry cube = new Geometry("My Textured Box", boxshape1);
  92.         //TangentBinormalGenerator.generate(boxshape1);  
  93.         Material mat_stl = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  94.         Texture tex_ml = assetManager.loadTexture("Textures/bloco.png");
  95.         mat_stl.setTexture("DiffuseMap", tex_ml);
  96.       //  mat_stl.setFloat("m_Shininess", 12);
  97.         mat_stl.setBoolean("m_UseMaterialColors", true);
  98.  
  99.         //mat_stl.setTexture("m_ColorRamp", assetManager.loadTexture("Textures/ColorRamp/cloudy.png"));
  100. //
  101. //        mat.setBoolean("m_VTangent", true);
  102. //        mat.setBoolean("m_Minnaert", true);
  103. //        mat.setBoolean("m_WardIso", true);
  104.         mat_stl.setBoolean("m_VertexLighting", true);
  105. //        mat.setBoolean("m_LowQuality", true);
  106.         mat_stl.setBoolean("m_HighQuality", true);
  107.  
  108.         mat_stl.setColor("m_Ambient", ColorRGBA.Black);
  109.         mat_stl.setColor("m_Diffuse", ColorRGBA.Gray);
  110.         mat_stl.setColor("m_Specular", ColorRGBA.Gray);
  111.         cube.setMaterial(mat_stl);
  112.         return cube;
  113.  
  114.     }
  115.  
  116.     public Geometry miniBloco(Vector3f v) {
  117.  
  118.         Box boxshape1 = new Box(v, 0.5f, 0.5f, 0.5f);
  119.         Geometry cube = new Geometry("My Textured Box", boxshape1);
  120.         //TangentBinormalGenerator.generate(boxshape1);  
  121.         Material mat_stl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  122.         Texture tex_ml = assetManager.loadTexture("Textures/bloco.png");
  123.         mat_stl.setTexture("m_ColorMap", tex_ml);
  124.  
  125.  
  126.  
  127.         cube.setMaterial(mat_stl);
  128.         this.lightMdl = cube;
  129.         return cube;
  130.  
  131.     }
  132.  
  133.     /** Custom Keybinding: Map named actions to inputs. */
  134.     private void initKeys() {
  135.         // You can map one or several inputs to one named action
  136.         inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
  137.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
  138.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
  139.         inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
  140.                 new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  141.         // Add the names to the action listener.
  142.         inputManager.addListener(actionListener, new String[]{"Pause"});
  143.         // inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate"});
  144.  
  145.     }
  146.     private ActionListener actionListener = new ActionListener() {
  147.  
  148.         public void onAction(String name, boolean keyPressed, float tpf) {
  149.             if (name.equals("Pause") && !keyPressed) {
  150.                 isRunning = !isRunning;
  151.             }
  152.         }
  153.     };
  154.  
  155.     @Override
  156.     public void simpleUpdate(float tpf) {
  157.         // cam.setLocation(new Vector3f(2.0632997f, 1.9493936f, 2.6885238f));
  158.         // cam.setRotation(new Quaternion(-0.053555284f, 0.9407851f, -0.17754152f, -0.28378546f));
  159.          angle += tpf;
  160.         angle %= FastMath.TWO_PI;
  161.          pl.setPosition(new Vector3f(FastMath.cos(angle) * 2f, 1, FastMath.sin(angle) * 2f));
  162.         posLuz = pl.getPosition();
  163.         this.lightMdl.setLocalTranslation(posLuz);
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement