1/*
2 * To change this template, choose Tools | Templates and open the template in
3 * the editor.
4 */
5package jme3test.batching;
6
7import com.jme3.app.SimpleApplication;
8import com.jme3.input.KeyInput;
9import com.jme3.input.controls.ActionListener;
10import com.jme3.input.controls.KeyTrigger;
11import com.jme3.material.Material;
12import com.jme3.math.ColorRGBA;
13import com.jme3.math.FastMath;
14import com.jme3.math.Quaternion;
15import com.jme3.math.Vector3f;
16import com.jme3.post.FilterPostProcessor;
17import com.jme3.post.filters.BloomFilter;
18import com.jme3.scene.*;
19import com.jme3.scene.debug.Arrow;
20import com.jme3.scene.shape.Box;
21import com.jme3.system.AppSettings;
22import com.jme3.system.NanoTimer;
23import java.util.ArrayList;
24import java.util.Random;
25
26public class TestBatchNodeCluster extends SimpleApplication {
27
28    public static void main(String[] args) {
29        TestBatchNodeCluster app = new TestBatchNodeCluster();
30        settingst = new AppSettings(true);
31        //settingst.setFrameRate(75);
32        settingst.setResolution(640, 480);
33        settingst.setVSync(false);
34        settingst.setFullscreen(false);
35        app.setSettings(settingst);
36        app.setShowSettings(false);
37        app.start();
38    }
39    private ActionListener al = new ActionListener() {
40
41        public void onAction(String name, boolean isPressed, float tpf) {
42            if (name.equals("Start Game")) {
43//              randomGenerator();
44            }
45        }
46    };
47    protected Random rand = new Random();
48    protected int maxCubes = 2000;
49    protected int startAt = 0;
50    protected static int xPositions = 0, yPositions = 0, zPositions = 0;
51    protected int returner = 0;
52    protected ArrayList<Integer> xPosition = new ArrayList<Integer>();
53    protected ArrayList<Integer> yPosition = new ArrayList<Integer>();
54    protected ArrayList<Integer> zPosition = new ArrayList<Integer>();
55    protected int xLimitf = 60, xLimits = -60, yLimitf = 60, yLimits = -20, zLimitf = 60, zLimits = -60;
56    protected int circ = 8;//increases by 8 every time.
57    protected int dynamic = 4;
58    protected static AppSettings settingst;
59    protected boolean isTrue = true;
60    private int lineLength = 50;
61    protected BatchNode batchNode;
62    Material mat1;
63    Material mat2;
64    Material mat3;
65    Material mat4;
66    Node terrain;
67    //protected
68//    protected Geometry player;
69
70    @Override
71    public void simpleInitApp() {
72        timer = new NanoTimer();
73
74        batchNode = new SimpleBatchNode("BatchNode");
75
76
77        xPosition.add(0);
78        yPosition.add(0);
79        zPosition.add(0);
80
81        mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
82        mat1.setColor("Color", ColorRGBA.White);
83        mat1.setColor("GlowColor", ColorRGBA.Blue.mult(10));
84
85        mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
86        mat2.setColor("Color", ColorRGBA.White);
87        mat2.setColor("GlowColor", ColorRGBA.Red.mult(10));
88
89        mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
90        mat3.setColor("Color", ColorRGBA.White);
91        mat3.setColor("GlowColor", ColorRGBA.Yellow.mult(10));
92
93        mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
94        mat4.setColor("Color", ColorRGBA.White);
95        mat4.setColor("GlowColor", ColorRGBA.Orange.mult(10));
96
97        randomGenerator();
98
99        //rootNode.attachChild(SkyFactory.createSky(
100        //  assetManager, "Textures/SKY02.zip", false));
101        inputManager.addMapping("Start Game", new KeyTrigger(KeyInput.KEY_J));
102        inputManager.addListener(al, new String[]{"Start Game"});
103
104
105        cam.setLocation(new Vector3f(-34.403286f, 126.65158f, 434.791f));
106        cam.setRotation(new Quaternion(0.022630932f, 0.9749435f, -0.18736298f, 0.11776358f));
107
108
109        batchNode.batch();
110
111
112        terrain = new Node("terrain");
113        terrain.setLocalTranslation(50, 0, 50);
114        terrain.attachChild(batchNode);
115
116        flyCam.setMoveSpeed(100);
117        rootNode.attachChild(terrain);
118        Vector3f pos = new Vector3f(-40, 0, -40);
119        batchNode.setLocalTranslation(pos);
120
121
122        Arrow a = new Arrow(new Vector3f(0, 50, 0));
123        Geometry g = new Geometry("a", a);
124        g.setLocalTranslation(terrain.getLocalTranslation());
125        Material m = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
126        m.setColor("Color", ColorRGBA.Blue);
127        g.setMaterial(m);
128
129
130
131        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
132        fpp.addFilter(new BloomFilter(BloomFilter.GlowMode.Objects));
133//        SSAOFilter ssao = new SSAOFilter(8.630104f,22.970434f,2.9299977f,0.2999997f);
134//        fpp.addFilter(ssao);
135        viewPort.addProcessor(fpp);
136        //   viewPort.setBackgroundColor(ColorRGBA.DarkGray);
137    }
138
139    public void randomGenerator() {
140        for (int i = startAt; i < maxCubes - 1; i++) {
141            randomize();
142            Geometry box = new Geometry("Box" + i, new Box(Vector3f.ZERO, 1, 1, 1));
143            box.setLocalTranslation(new Vector3f(xPosition.get(xPosition.size() - 1),
144                    yPosition.get(yPosition.size() - 1),
145                    zPosition.get(zPosition.size() - 1)));
146            batchNode.attachChild(box);
147            if (i < 500) {
148                box.setMaterial(mat1);
149            } else if (i < 1000) {
150
151                box.setMaterial(mat2);
152            } else if (i < 1500) {
153
154                box.setMaterial(mat3);
155            } else {
156
157                box.setMaterial(mat4);
158            }
159
160        }
161    }
162
163//    public BatchNode randomBatch() {
164//
165//        int randomn = rand.nextInt(4);
166//        if (randomn == 0) {
167//            return blue;
168//        } else if (randomn == 1) {
169//            return brown;
170//        } else if (randomn == 2) {
171//            return pink;
172//        } else if (randomn == 3) {
173//            return orange;
174//        }
175//        return null;
176//    }
177    public ColorRGBA randomColor() {
178        ColorRGBA color = ColorRGBA.Black;
179        int randomn = rand.nextInt(4);
180        if (randomn == 0) {
181            color = ColorRGBA.Orange;
182        } else if (randomn == 1) {
183            color = ColorRGBA.Blue;
184        } else if (randomn == 2) {
185            color = ColorRGBA.Brown;
186        } else if (randomn == 3) {
187            color = ColorRGBA.Magenta;
188        }
189        return color;
190    }
191
192    public void randomize() {
193        int xpos = xPosition.get(xPosition.size() - 1);
194        int ypos = yPosition.get(yPosition.size() - 1);
195        int zpos = zPosition.get(zPosition.size() - 1);
196        int x = 0;
197        int y = 0;
198        int z = 0;
199        boolean unTrue = true;
200        while (unTrue) {
201            unTrue = false;
202            boolean xChanged = false;
203            x = 0;
204            y = 0;
205            z = 0;
206            if (xpos >= lineLength * 2) {
207                x = 2;
208                xChanged = true;
209            } else {
210                x = xPosition.get(xPosition.size() - 1) + 2;
211            }
212            if (xChanged) {
213                //y = yPosition.get(yPosition.size() - lineLength) + 2;
214            } else {
215                y = rand.nextInt(3);
216                if (yPosition.size() > lineLength) {
217                    if (yPosition.size() > 51) {
218                        if (y == 0 && ypos < yLimitf && getym(lineLength) > ypos - 2) {
219                            y = ypos + 2;
220                        } else if (y == 1 && ypos > yLimits && getym(lineLength) < ypos + 2) {
221                            y = ypos - 2;
222                        } else if (y == 2 && getym(lineLength) > ypos - 2 && getym(lineLength) < ypos + 2) {
223                            y = ypos;
224                        } else {
225                            if (ypos >= yLimitf) {
226                                y = ypos - 2;
227                            } else if (ypos <= yLimits) {
228                                y = ypos + 2;
229                            } else if (y == 0 && getym(lineLength) >= ypos - 4) {
230                                y = ypos - 2;
231                            } else if (y == 0 && getym(lineLength) >= ypos - 2) {
232                                y = ypos;
233                            } else if (y == 1 && getym(lineLength) >= ypos + 4) {
234                                y = ypos + 2;
235                            } else if (y == 1 && getym(lineLength) >= ypos + 2) {
236                                y = ypos;
237                            } else if (y == 2 && getym(lineLength) <= ypos - 2) {
238                                y = ypos - 2;
239                            } else if (y == 2 && getym(lineLength) >= ypos + 2) {
240                                y = ypos + 2;
241                            } else {
242                                System.out.println("wtf");
243                            }
244                        }
245                    } else if (yPosition.size() == lineLength) {
246                        if (y == 0 && ypos < yLimitf) {
247                            y = getym(lineLength) + 2;
248                        } else if (y == 1 && ypos > yLimits) {
249                            y = getym(lineLength) - 2;
250                        }
251                    }
252                } else {
253                    if (y == 0 && ypos < yLimitf) {
254                        y = ypos + 2;
255                    } else if (y == 1 && ypos > yLimits) {
256                        y = ypos - 2;
257                    } else if (y == 2) {
258                        y = ypos;
259                    } else if (y == 0 && ypos >= yLimitf) {
260                        y = ypos - 2;
261                    } else if (y == 1 && ypos <= yLimits) {
262                        y = ypos + 2;
263                    }
264                }
265            }
266            if (xChanged) {
267                z = zpos + 2;
268            } else {
269                z = zpos;
270            }
271//          for (int i = 0; i < xPosition.size(); i++)
272//          {
273//              if (x - xPosition.get(i) <= 1 && x - xPosition.get(i) >= -1 &&
274//                      y - yPosition.get(i) <= 1 && y - yPosition.get(i) >= -1
275//                      &&z - zPosition.get(i) <= 1 && z - zPosition.get(i) >=
276//                      -1)
277//              {
278//                  unTrue = true;
279//              }
280//          }
281        }
282        xPosition.add(x);
283        yPosition.add(y);
284        zPosition.add(z);
285    }
286
287    public int getxm(int i) {
288        return xPosition.get(xPosition.size() - i);
289    }
290
291    public int getym(int i) {
292        return yPosition.get(yPosition.size() - i);
293    }
294
295    public int getzm(int i) {
296        return zPosition.get(zPosition.size() - i);
297    }
298
299    public int getx(int i) {
300        return xPosition.get(i);
301    }
302
303    public int gety(int i) {
304        return yPosition.get(i);
305    }
306
307    public int getz(int i) {
308        return zPosition.get(i);
309    }
310    long nbFrames = 0;
311    long cullTime = 0;
312    float time = 0;
313    Vector3f lookAtPos = new Vector3f(0, 0, 0);
314    float xpos = 0;
315    Spatial box;
316
317    @Override
318    public void simpleUpdate(float tpf) {
319        time += tpf;
320        int random = rand.nextInt(2000);
321        float mult1 = 1.0f;
322        float mult2 = 1.0f;
323        if (random < 500) {
324            mult1 = 1.0f;
325            mult2 = 1.0f;
326        } else if (random < 1000) {
327            mult1 = -1.0f;
328            mult2 = 1.0f;
329        } else if (random < 1500) {
330            mult1 = 1.0f;
331            mult2 = -1.0f;
332        } else if (random <= 2000) {
333            mult1 = -1.0f;
334            mult2 = -1.0f;
335        }
336        box = batchNode.getChild("Box" + random);
337        if (box != null) {
338            Vector3f v = box.getLocalTranslation();
339            box.setLocalTranslation(v.x + FastMath.sin(time * mult1) * 20, v.y + (FastMath.sin(time * mult1) * FastMath.cos(time * mult1) * 20), v.z + FastMath.cos(time * mult2) * 20);
340        }
341        terrain.setLocalRotation(new Quaternion().fromAngleAxis(time, Vector3f.UNIT_Y));
342
343
344    }
345}
346