Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (746)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (828)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Perlin Noise?  (Read 2015 times)
0 Members and 1 Guest are viewing this topic.
Offline minecraftguy1337

Junior Newbie


Exp: 4-6 months



« Posted 2015-05-18 19:48:28 »

Hello, and I am currently working on yet another minecraft clone (yes, I know Cranky) and I was wondering, how could I implement Perlin Noise in my game? I already have the code for the Perlin Noise, but I don't know how to generate it.

code:

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  
public class Noise {
   private static Random random = new Random(new Random().nextLong());

   private static float[][] generateWhiteNoise(int width, int height) {
       float[][] noise = new float[width][height];

       for (int i = 0; i < width; i++) {
           for (int j = 0; j < height; j++) {
               noise[i][j] = (float) random.nextDouble() % 1;
           }
       }

       return noise;
   }

   private static float[][] generateSmoothNoise(float[][] baseNoise, int octave) {
       baseNoise = new float[octave][octave];
      int width = baseNoise.length;
       int height = baseNoise.length;

       float[][] smoothNoise = new float[width][height];

       int samplePeriod = 1 << octave; // calculates 2 ^ k
       float sampleFrequency = 1.0f / samplePeriod;

       for (int i = 0; i < width; i++) {
           // calculate the horizontal sampling indices
           int sample_i0 = (i / samplePeriod) * samplePeriod;
           int sample_i1 = (sample_i0 + samplePeriod) % width; // wrap around
           float horizontal_blend = (i - sample_i0) * sampleFrequency;

           for (int j = 0; j < height; j++) {
               // calculate the vertical sampling indices
               int sample_j0 = (j / samplePeriod) * samplePeriod;
               int sample_j1 = (sample_j0 + samplePeriod) % height; // wrap
                                                                       // around
               float vertical_blend = (j - sample_j0) * sampleFrequency;

               // blend the top two corners
               float top = interpolate(baseNoise[sample_i0][sample_j0],
                       baseNoise[sample_i1][sample_j0], horizontal_blend);

               // blend the bottom two corners
               float bottom = interpolate(baseNoise[sample_i0][sample_j1],
                       baseNoise[sample_i1][sample_j1], horizontal_blend);

               // final blend
               smoothNoise[i][j] = interpolate(top, bottom, vertical_blend);
           }
       }

       return smoothNoise;
   }

   private static float interpolate(float x0, float x1, float alpha) {
       return x0 * (1 - alpha) + alpha * x1;
   }

   private static float[][] generatePerlinNoise(float[][] baseNoise,
           int octaveCount) {
       int width = baseNoise.length;
       int height = baseNoise[0].length;

       float[][][] smoothNoise = new float[octaveCount][][]; // an array of 2D
                                                               // arrays
                                                               // containing

       float persistance = 0.5f;

       // generate smooth noise
       for (int i = 0; i < octaveCount; i++) {
           smoothNoise[i] = generateSmoothNoise(baseNoise, i);
       }

       float[][] perlinNoise = new float[width][height];
       float amplitude = 0.0f; // the bigger, the more big mountains
       float totalAmplitude = 0.0f;

       // blend noise together
       for (int octave = octaveCount - 1; octave >= 0; octave--) {
           amplitude *= persistance;
           totalAmplitude += amplitude;

           for (int i = 0; i < width; i++) {
               for (int j = 0; j < height; j++) {
                   perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
               }
           }
       }

       for (int i = 0; i < width; i++) {
           for (int j = 0; j < height; j++) {
               perlinNoise[i][j] /= totalAmplitude;
               perlinNoise[i][j] = (float) (Math.floor(perlinNoise[i][j] * 25));
           }
       }

       return perlinNoise;
   }
   
   public static void generate(int width, int length) {
      float[][] noise = generateSmoothNoise(generateWhiteNoise(width, length), 5);
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public class World {
   public static final int CHUNK_SIZE = 16;
   private Block[][][] blocks;
   
   
   public void update() {
     
   }
   
   public World() {
      blocks = new Block[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
      for(int x=0; x<CHUNK_SIZE;x++) {
         for(int y=0;y<CHUNK_SIZE;y++) {
            for(int z=0;z<CHUNK_SIZE;z++) {
               blocks[x][y][z] = new Block(BlockType.BlockType_Default);
               blocks[x][y][z].render();
            }
            GL11.glTranslatef(0, 2, 0);
         }
      }
   }
   
}
Offline Sabomoth

Junior Devvie


Medals: 4
Exp: 3 years



« Reply #1 - Posted 2015-05-18 20:19:37 »

http://accidentalnoise.sourceforge.net/minecraftworlds.html

If you are just going to copy something, copy something that will make it easy to use instead.
Offline philfrei
« Reply #2 - Posted 2015-05-19 00:44:15 »

Quote
I already have the code for the Perlin Noise, but I don't know how to generate it.

Are you asking how to make a Perlin noise function?
Are you asking how to figure out what values to pass into the Perlin noise function call?
Are you asking what to do with the values that are returned by the calls?

music and music apps: http://adonax.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen

JGO Kernel


Medals: 516



« Reply #3 - Posted 2015-05-19 07:56:33 »

...and there's a search box in your upper right hand frame of this forum.  Your question has probably been answered.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (106 views)
2017-12-31 17:17:51

xxMrPHDxx (41 views)
2017-12-31 17:15:51

xxMrPHDxx (49 views)
2017-12-28 18:11:33

Ecumene (215 views)
2017-09-30 02:57:34

theagentd (324 views)
2017-09-26 18:23:31

cybrmynd (370 views)
2017-08-02 12:28:51

cybrmynd (406 views)
2017-08-02 12:19:43

cybrmynd (374 views)
2017-08-02 12:18:09

Sralse (345 views)
2017-07-25 17:13:48

Archive (1104 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!