1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package jme3test.batching;
7
8/*
9 * Copyright (c) 2009-2010 jMonkeyEngine
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are
14 * met:
15 *
16 * * Redistributions of source code must retain the above copyright
17 *   notice, this list of conditions and the following disclaimer.
18 *
19 * * Redistributions in binary form must reproduce the above copyright
20 *   notice, this list of conditions and the following disclaimer in the
21 *   documentation and/or other materials provided with the distribution.
22 *
23 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
24 *   may be used to endorse or promote products derived from this software
25 *   without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40import com.jme3.app.SimpleApplication;
41import com.jme3.asset.TextureKey;
42import com.jme3.bullet.BulletAppState;
43import com.jme3.bullet.PhysicsSpace;
44import com.jme3.bullet.collision.shapes.SphereCollisionShape;
45import com.jme3.bullet.control.RigidBodyControl;
46import com.jme3.font.BitmapText;
47import com.jme3.input.MouseInput;
48import com.jme3.input.controls.ActionListener;
49import com.jme3.input.controls.MouseButtonTrigger;
50import com.jme3.material.Material;
51import com.jme3.math.Vector2f;
52import com.jme3.math.Vector3f;
53import com.jme3.renderer.queue.RenderQueue.ShadowMode;
54import com.jme3.scene.BatchNode;
55import com.jme3.scene.Geometry;
56import com.jme3.scene.shape.Box;
57import com.jme3.scene.shape.Sphere;
58import com.jme3.scene.shape.Sphere.TextureMode;
59import com.jme3.shadow.PssmShadowRenderer;
60import com.jme3.shadow.PssmShadowRenderer.CompareMode;
61import com.jme3.shadow.PssmShadowRenderer.FilterMode;
62import com.jme3.system.AppSettings;
63import com.jme3.system.NanoTimer;
64import com.jme3.texture.Texture;
65import com.jme3.texture.Texture.WrapMode;
66import jme3test.bullet.BombControl;
67
68/**
69 *
70 * @author double1984 (tower mod by atom)
71 */
72public class TestBatchNodeTower extends SimpleApplication {
73
74    int bricksPerLayer = 8;
75    int brickLayers = 30;
76
77    static float brickWidth = .75f, brickHeight = .25f, brickDepth = .25f;
78    float radius = 3f;
79    float angle = 0;
80
81
82    Material mat;
83    Material mat2;
84    Material mat3;
85    PssmShadowRenderer bsr;
86    private Sphere bullet;
87    private Box brick;
88    private SphereCollisionShape bulletCollisionShape;
89
90    private BulletAppState bulletAppState;
91    BatchNode batchNode = new BatchNode("batch Node");
92
93    public static void main(String args[]) {
94        TestBatchNodeTower f = new TestBatchNodeTower();
95        AppSettings s = new AppSettings(true);
96        f.setSettings(s);
97        f.start();
98    }
99
100    @Override
101    public void simpleInitApp() {
102        timer = new NanoTimer();
103        bulletAppState = new BulletAppState();
104        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
105     //   bulletAppState.setEnabled(false);
106        stateManager.attach(bulletAppState);
107        bullet = new Sphere(32, 32, 0.4f, true, false);
108        bullet.setTextureMode(TextureMode.Projected);
109        bulletCollisionShape = new SphereCollisionShape(0.4f);
110
111        brick = new Box(Vector3f.ZERO, brickWidth, brickHeight, brickDepth);
112        brick.scaleTextureCoordinates(new Vector2f(1f, .5f));
113        //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
114        initMaterial();
115        initTower();
116        initFloor();
117        initCrossHairs();
118        this.cam.setLocation(new Vector3f(0, 25f, 8f));
119        cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));
120        cam.setFrustumFar(80);
121        inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
122        inputManager.addListener(actionListener, "shoot");
123        rootNode.setShadowMode(ShadowMode.Off);
124
125        batchNode.batch();
126        batchNode.setShadowMode(ShadowMode.CastAndReceive);
127        rootNode.attachChild(batchNode);
128
129
130        bsr = new PssmShadowRenderer(assetManager, 1024, 2);
131        bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
132        bsr.setLambda(0.55f);
133        bsr.setShadowIntensity(0.6f);
134        bsr.setCompareMode(CompareMode.Hardware);
135        bsr.setFilterMode(FilterMode.PCF4);
136        viewPort.addProcessor(bsr);
137    }
138
139    private PhysicsSpace getPhysicsSpace() {
140        return bulletAppState.getPhysicsSpace();
141    }
142    private ActionListener actionListener = new ActionListener() {
143
144        public void onAction(String name, boolean keyPressed, float tpf) {
145            if (name.equals("shoot") && !keyPressed) {
146                Geometry bulletg = new Geometry("bullet", bullet);
147                bulletg.setMaterial(mat2);
148                bulletg.setShadowMode(ShadowMode.CastAndReceive);
149                bulletg.setLocalTranslation(cam.getLocation());
150                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
151//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
152                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
153                bulletg.addControl(bulletNode);
154                rootNode.attachChild(bulletg);
155                getPhysicsSpace().add(bulletNode);
156            }
157        }
158    };
159
160    public void initTower() {
161        double tempX = 0;
162        double tempY = 0;
163        double tempZ = 0;
164        angle = 0f;
165        for (int i = 0; i < brickLayers; i++){
166            // Increment rows
167            if(i!=0)
168                tempY+=brickHeight*2;
169            else
170                tempY=brickHeight;
171            // Alternate brick seams
172            angle = 360.0f / bricksPerLayer * i/2f;
173            for (int j = 0; j < bricksPerLayer; j++){
174              tempZ = Math.cos(Math.toRadians(angle))*radius;
175              tempX = Math.sin(Math.toRadians(angle))*radius;
176              System.out.println("x="+((float)(tempX))+" y="+((float)(tempY))+" z="+(float)(tempZ));
177              Vector3f vt = new Vector3f((float)(tempX), (float)(tempY), (float)(tempZ));
178              // Add crenelation
179              if (i==brickLayers-1){
180                if (j%2 == 0){
181                    addBrick(vt);
182                }
183              }
184              // Create main tower
185              else {
186                addBrick(vt);
187              }
188              angle += 360.0/bricksPerLayer;
189            }
190          }
191
192    }
193
194    public void initFloor() {
195        Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
196        floorBox.scaleTextureCoordinates(new Vector2f(3, 6));
197
198        Geometry floor = new Geometry("floor", floorBox);
199        floor.setMaterial(mat3);
200        floor.setShadowMode(ShadowMode.Receive);
201        floor.setLocalTranslation(0, 0, 0);
202        floor.addControl(new RigidBodyControl(0));
203        this.rootNode.attachChild(floor);
204        this.getPhysicsSpace().add(floor);
205    }
206
207    public void initMaterial() {
208        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
209        TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
210        key.setGenerateMips(true);
211        Texture tex = assetManager.loadTexture(key);
212        mat.setTexture("ColorMap", tex);
213
214        mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
215        TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
216        key2.setGenerateMips(true);
217        Texture tex2 = assetManager.loadTexture(key2);
218        mat2.setTexture("ColorMap", tex2);
219
220        mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
221        TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
222        key3.setGenerateMips(true);
223        Texture tex3 = assetManager.loadTexture(key3);
224        tex3.setWrap(WrapMode.Repeat);
225        mat3.setTexture("ColorMap", tex3);
226    }
227int nbBrick =0;
228    public void addBrick(Vector3f ori) {
229        Geometry reBoxg = new Geometry("brick", brick);
230        reBoxg.setMaterial(mat);
231        reBoxg.setLocalTranslation(ori);
232        reBoxg.rotate(0f, (float)Math.toRadians(angle) , 0f );
233        reBoxg.addControl(new RigidBodyControl(1.5f));
234        reBoxg.setShadowMode(ShadowMode.CastAndReceive);
235        reBoxg.getControl(RigidBodyControl.class).setFriction(1.6f);
236        this.batchNode.attachChild(reBoxg);
237        this.getPhysicsSpace().add(reBoxg);
238        nbBrick++;
239    }
240
241    protected void initCrossHairs() {
242        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
243        BitmapText ch = new BitmapText(guiFont, false);
244        ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
245        ch.setText("+"); // crosshairs
246        ch.setLocalTranslation( // center
247                settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
248                settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
249        guiNode.attachChild(ch);
250    }
251}