Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: BurntPizza (posted 2014-06-15 02:03:08, viewed 68 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323  
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *  This time with a kd-tree!
 */
public class Worley3D {
   
   static List<BufferedImage> worley3D(int frames, int width, int height, float maxz, KDTree seeds) throws Exception {
      
      float z = 0;
      float zInc = maxz / frames;
      
      List<Proc> procs = new ArrayList<>();
      
      for (int frame = 0; frame < frames; frame++, z += zInc)
         procs.add(new Proc(width, height, z, frame, seeds));
      
      ExecutorService exec = Executors.newFixedThreadPool(Math.max(1, Runtime.getRuntime().availableProcessors()));
      
      List<Future<Frame>> results = exec.invokeAll(procs);
      
      exec.shutdown();
      
      List<Frame> frameList = new ArrayList<>();
      
      for (Future<Frame> future : results)
         frameList.add(future.get());
      
      Collections.sort(frameList, new Comparator<Frame>() {
         @Override
         public int compare(Frame o1, Frame o2) {
            return o1.number - o2.number;
         }
      });
      
      List<BufferedImage> images = new ArrayList<>();
      
      for (Frame f : frameList)
         images.add(f.image);
      
      return images;
   }
   
   static int loopCount = 0;
   
   static KDTree genSeeds(int number, float maxx, float maxy, float maxz) {
      List<Point3D> points = new ArrayList<Point3D>(number);
      
      for (int i = 0; i < number; i++) {
         points.add(new Point3D((float) Math.random() * maxx, (float) Math.random() * maxy, (float) Math.random() * maxz));
      }
      
      //return new Partition(points, (int) Math.ceil(maxx), (int) Math.ceil(maxy), (int) Math.ceil(maxz));
      return new KDTree(points);
   }
   
   @SuppressWarnings("serial")
   public static void main(String[] a) throws Exception {
      
      int NUM_FRAMES = 120;
      final int FPS = 30;
      
      final int width = 400, height = 400;
      float sweepZDepth = NUM_FRAMES; // defaulting to 1.0 z/frame sweep speed
      
      final List<BufferedImage> images = worley3D(NUM_FRAMES, width, height, sweepZDepth, genSeeds(NUM_FRAMES * 15, width, height, sweepZDepth));
      
      JFrame frame = new JFrame();
      frame.add(new JPanel() {
         int i = 0;
         {
            setSize(width, height);
            setPreferredSize(getSize());
         }
         
         @Override
         public void paint(Graphics g) {
            g.drawImage(images.get(i++ % images.size()), 0, 0, null);
            try {
               Thread.sleep(Math.round(1000f / FPS));
            } catch (InterruptedException e) {
            }
            repaint();
         }
      });
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation(d.width / 2 - width / 2, d.height / 2 - height / 2);
      frame.pack();
      frame.setVisible(true);
      
      // Write frames to PNGs, change the path to fit
      
      /*
      int i = 0;
      for (BufferedImage image : images) {
         ImageIO.write(image, "PNG", new File("3d/frame" + String.format("%04d", i++) + ".png"));
      }
      */
      
      // Write animated GIF; credit to Riven: http://riven8192.blogspot.com/2010/02/image-java-animated-gifs.html
      
      /*List<GifFrame> gifFrames = new ArrayList<GifFrame>();
      
      for (BufferedImage image : images) {
         int transparantColor = 0xFF00FF;
         BufferedImage gif = ImageUtil.convertRGBAToGIF(image, transparantColor);
         long delay = Math.round(1000f / FPS);
         String disposal = GifFrame.NONE;
         gifFrames.add(new GifFrame(gif, delay, disposal));
      }
      
      int loopCount = 0;
      FileOutputStream out = new FileOutputStream("noise3D.gif");
      ImageUtil.saveAnimatedGIF(out, gifFrames, loopCount);
      out.close();*/
   }
}

class Point3D {
   
   final float x, y, z;
   
   public Point3D(float x, float y, float z) {
      this.x = x;
      this.y = y;
      this.z = z;
   }
}

class Frame {
   final BufferedImage image;
   final int number;
   
   Frame(int num, BufferedImage img) {
      number = num;
      image = img;
   }
}

class Proc implements Callable<Frame> {
   
   final float z;
   
   final int width, height, num;
   
   final float[] dists;
   
   final KDTree seeds;
   
   Proc(int width, int height, float z, int num, KDTree seeds) {
      this.width = width;
      this.height = height;
      this.z = z;
      this.num = num;
      dists = new float[width * height];
      this.seeds = seeds;
   }
   
   @Override
   public Frame call() throws InterruptedException {
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      
      float maxDist = -1;
      long time = System.currentTimeMillis();
      
      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
            float dist = seeds.dNN(new Point3D(x, y, z));
            dists[x + y * width] = dist;
            maxDist = Math.max(maxDist, dist);
         }
      }
      
      float scale = 255 / maxDist;
      
      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
            float dist = dists[x + y * width];
            dist = Math.min(dist * scale, 255);
            int color = (int) dist;
            color = color << 16 | color << 8 | color;
            image.setRGB(x, y, color);
         }
      }
      
      time = System.currentTimeMillis() - time;
      System.out.println(time + " ms");
      if (num % 5 == 0)
         System.out.println("Rendered frame: " + num);
      return new Frame(num, image);
   }
}

@SuppressWarnings("unchecked")
class KDTree {
   
   private static final Comparator<Point3D>[] comparators = new Comparator[] {
         new Comparator<Point3D>() {
            @Override
            public int compare(Point3D o1, Point3D o2) {
               return Float.compare(o1.x, o2.x);
            }
         },
         new Comparator<Point3D>() {
            @Override
            public int compare(Point3D o1, Point3D o2) {
               return Float.compare(o1.y, o2.y);
            }
         },
         new Comparator<Point3D>() {
            @Override
            public int compare(Point3D o1, Point3D o2) {
               return Float.compare(o1.z, o2.z);
            }
         }
   };
   
   final KDTree.Node root;
   
   public KDTree(List<Point3D> points) {
      Point3D[] p = new Point3D[points.size()];
      p = points.toArray(p);
      root = new Node(0);
      build(root, p);
   }
   
   private void build(KDTree.Node node, Point3D[] points) {
      
      final int end = points.length;
      final int median = end / 2;
      
      if (end > 1) {
         int depth = node.depth;
         Arrays.sort(points, comparators[depth++ % 3]);
         build((node.l = new Node(depth)), copy(points, 0, median));
         build((node.r = new Node(depth)), copy(points, median, end));
      }
      Point3D m = points[median];
      node.x = m.x;
      node.y = m.y;
      node.z = m.z;
   }
   
   private final static Point3D[] copy(Point3D[] src, int a, int b) {
      Point3D[] dst = new Point3D[b - a];
      System.arraycopy(src, a, dst, 0, dst.length);
      return dst;
   }
   
   public float dNN(final Point3D point) {
      NN nn = new NN(point);
      getNN(nn, root);
      return (float) Math.sqrt(nn.minDistSq);
   }
   
   private void getNN(final NN nn, final KDTree.Node node) {
      if (node.isLeaf()) {
         nn.update(node.x, node.y, node.z);
      } else {
         float pDist = planeDistance(node, nn.tx, nn.ty, nn.tz);
         getNN(nn, (pDist < 0) ? node.l : node.r);
         if ((pDist * pDist) < nn.minDistSq) {
            getNN(nn, (pDist < 0) ? node.r : node.l);
         }
      }
   }
   
   private final float planeDistance(final KDTree.Node node, final float tx, final float ty, final float tz) {
      switch (node.depth % 3) {
         case 0:
            return tx - node.x;
         case 1:
            return ty - node.y;
         default:
            return tz - node.z;
      }
   }
   
   private static class Node {
      final int depth;
      float x, y, z;
      Node l, r;
      
      public Node(int depth) {
         this.depth = depth;
      }
      
      boolean isLeaf() {
         return (l == null) | (r == null);
      }
   }
   
   private static class NN {
      final float tx, ty, tz;
      float minDistSq = Float.MAX_VALUE;
      
      NN(Point3D target) {
         tx = target.x;
         ty = target.y;
         tz = target.z;
      }
      
      void update(final float nx, final float ny, final float nz) {
         float dx = nx - tx;
         float dy = ny - ty;
         float dz = nz - tz;
         minDistSq = Math.min(minDistSq, dx * dx + dy * dy + dz * dz);
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!