home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-12 | 10.4 KB | 364 lines |
- /*
-
- Standalone Java program to create a VRML 2.0 fractal height field surface
- You need that Java Dev. Kit to compile and run this program.
- // 10.06. changed tom VRML 2.0 draft 2 syntax
-
-
-
- Basic algorithm from
-
- H. Peitgen, D. Saupe
- "The Science of Fractal Images"
- Springer Verlag,
- Page 100
-
-
-
- Steps to exectue:
- javac Landscape.java
- set CLASSPATH = .;%CLASSPATH%
- java Landscape
- glview Landscape.wrl
-
-
- Startet from an applet from
- C. Thornborrow (Silicon Graphics (UK) )
- http://www.sgi.com/Fun/free/java/chris-thornborrow/index.html
-
-
- hg@berlin.snafu.de
- http://www.snafu.de/~hg
-
-
- */
-
- import java.applet.Applet;
- import java.awt.Color;
- import java.lang.Math;
- import java.util.Random;
- import java.io.*;
- import java.lang.Integer;
-
-
- public class Landscape extends java.applet.Applet implements Runnable
-
- {
-
- int scape[][]; // height field
- Color colors[][]; // height field colors
-
-
- // actual amplitude for level
- double delta;
-
- // options
- int size; // size of landscape = 2^power
- int power; // size of landscape as 2 power
- boolean zeroBorder; // start with 0 at outer first level
- // to have a landscape embedded in water
- long seed; // random seed
- boolean outputColor; // output a VRML file with colors (makes file much larger)
-
-
- int red,green;
-
- Color c;
- Random rng;
-
- double sigma,fracdim;
- boolean addition;
-
-
- public void init(){
- //size = 512; power = 9; sigma = 90.0;
- //size = 256; power = 8; sigma = 64.0; // will be 1.3 Meg
- size = 128; power = 7; sigma = 64.0; //
- fracdim = 0.85-0.1;
- zeroBorder = true;
- outputColor = true;
- addition = true;
-
- if (zeroBorder) sigma*=2;
-
- seed = 1;
-
- if (seed != 0) rng = new Random(seed);
- else rng = new Random(); // seed based on time
-
- scape = new int[size+1][size+1];
-
- colors = new Color[size+1][size+1];
-
- int i,j;
-
- for (i=0; i<size; i++)
- for (j=0; j<size; j++)
- scape[i][j] = 0;
-
-
- }
-
-
-
-
-
-
- // application entry point
- public static void main(String argv[]){
-
-
- Landscape l;
- l = new Landscape();
-
- l.init();
- l.compute();
- try {
- l.save();
- } catch (java.io.IOException e) { }
-
- }
-
-
- public void run(){
- compute();
- }
-
- // MIdPointFM2D
- // H. Peitgen, D. Saupe The Science of Fractal Images
- // Springer Verlag Page 100
-
- public void compute(){
-
- int i,j,scale;
- int step,hstep;
- delta = sigma;
-
- step = size;
- hstep = step/2;
-
- if (!zeroBorder) {
- // set the four corner points
- scape[0][0] = (int) ((rng.nextGaussian())*delta);
- scape[0][size] =(int) ((rng.nextGaussian())*delta);
- scape[size][0] =(int) ((rng.nextGaussian())*delta);
- scape[size][size] =(int) ((rng.nextGaussian())*delta);
- }
-
- System.out.print("##computing Size = ");
- System.out.print(size);
- System.out.print(" Fracdim= ");
- System.out.print(fracdim);
- System.out.println(" ...");
-
-
- // create the landscape
- for (scale=1; scale<=power; scale++) {
-
- delta *= Math.pow (0.5, 0.5*fracdim);
-
- // set the internal point in square
- for (i=hstep; i<=size-hstep; i+=step)
- for (j=hstep; j<=size-hstep; j+=step)
- this.set4point (i,j, scale,
- scape[i+hstep][j+hstep],
- scape[i+hstep][j-hstep],
- scape[i-hstep][j+hstep],
- scape[i-hstep][j-hstep]);
-
- // displace other points
- if (addition)
- for (i=0; i<=size; i+=step)
- for (j=0; j<=size; j+=step)
- scape[i][j] +=(int) ((rng.nextGaussian())*delta);
-
-
-
- delta *= Math.pow (0.5, 0.5*fracdim);
-
- // set boundary points
- for (i=hstep; i<=size-hstep; i+=step) {
- this.set3point (i, 0, scale-1,scape[i+hstep][0],
- scape[i-hstep][0], scape[i][hstep]);
- this.set3point (i,size,scale -1, scape[i+hstep][size],
- scape[i-hstep][size], scape[i][size-hstep]);
- this.set3point (0,i, scale -1, scape[0][i+hstep],
- scape[0][i-hstep],scape[hstep][i]);
- this.set3point (size,i, scale-1, scape[size][i+hstep],
- scape[size][i-hstep],scape[size-hstep][i]);
- }
-
- // set interior grid points
- for (i=hstep; i<=size-hstep; i+=step)
- for (j=step; j<=size-hstep; j+=step)
- this.set4point (i,j,scale-1,
- scape[i][j+hstep], scape[i][j-hstep],
- scape[i+hstep][j], scape[i-hstep][j]);
-
- for (i=step; i<=size-hstep; i+=step)
- for (j=hstep; j<=size-hstep; j+=step)
- this.set4point (i,j, scale-1,
- scape[i][j+hstep], scape[i][j-hstep],
- scape[i+hstep][j],scape[i-hstep][j] );
-
- // displace other points
- if (addition) {
- for (i=0; i<=size; i+=step)
- for (j=0; j<=size; j+=step)
- scape[i][j] +=(int) ((rng.nextGaussian())*delta);
-
- for (i=hstep; i<=size-hstep; i+=step)
- for (j=hstep; j<=size-hstep; j+=step)
- scape[i][j] +=(int) ((rng.nextGaussian())*delta);
-
- }
-
- if (zeroBorder)
- for (i=0; i<=size; i+=step)
- {
- scape[i][0]=0;
- scape[i][size]=0;
- scape[0][i]=0;
- scape[size][i]=0;
- }
-
-
- // change the size of the grid we are working on
- step /=2;
- hstep /=2;
- }
-
-
- System.out.println("computing colors ..");
-
- for (i=0; i<=size; i++)
- for (j=size; j>=0; j--) {
-
-
- computeColor(i,j,scape[i][j]);
-
- if (scape[i][j] < 0) scape[i][j] = (int) (scape[i][j]*0.1);
-
- }
-
- System.out.println("computing done ..");
-
-
-
-
- }
-
- private void set4point (int i, int j, int s, int a, int b, int c, int d) {
-
- scape[i][j] = (a + b +c +d )/4;
- scape[i][j] += (int)(rng.nextGaussian() * delta);
- }
-
- private void set3point (int i, int j, int s, int a, int b, int c) {
-
- scape[i][j] = (a + b + c )/3;
- scape[i][j] += (int)(rng.nextGaussian() * delta);
- }
-
-
- // save Heighfield as VRML file
- public void save()
- throws java.io.IOException
- {
- FileOutputStream outFile;
- PrintStream data;
- int i,j;
-
- System.out.println("saving ...");
-
- // first open the file
-
- outFile = new FileOutputStream("Landscape.wrl");
- // create a data stream
- data = new PrintStream( outFile );
-
- data.println("#VRML V1.0 ascii");
- data.println("#GLView heightfield demo created by Landscape.java V 0");
- data.println("Separator {");
-
- double f = 1.0/ 255.0;
-
- // ********* write per vertex color
- if (outputColor) {
-
- data.println("Material {");
- data.println("diffuseColor [ ");
-
-
- for (i=0; i<=size; i++) {
- for (j=size; j>=0; j--) {
- Color c= colors[i][j];
- data.print(c.getRed() *f); data.print(" ");
- data.print(c.getGreen() *f); data.print(" ");
- data.print(c.getBlue() *f); data.print(" ");
- data.print(",");
- }
- data.println();
- }
-
- data.println("]");
- data.println("}");
-
- data.println("MaterialBinding {value PER_VERTEX }");
- }
-
- // VRML convention Y is Z
- f = 0.15;
- //data.println("Scale { scaleFactor 1.0 "); data.print(f); data.println(" 1.0}");
-
-
- // ********** output the grid
- data.println("ElevationGrid {");
- data.print("xDimension "); data.print(size+1); data.println();
- data.print("zDimension "); data.print(size+1); data.println();
- data.print("#fracdim "); data.print(fracdim); data.println();
- data.println("height [ ");
-
- for (i=0; i<=size; i++) {
- for (j=size; j>=0; j--) {
- data.print(scape[i][j]*f);
- data.print(",");
- }
- data.println();
- }
- data.println("]");
- data.println("}");
-
- data.println("}");
-
- outFile.close();
- }
-
-
- // compute a color for grid point i,j based on h
- private void computeColor (int i, int j, int h) {
-
- if (j==0)
- c = (Color.black);
- else if (h<=0)
- c = (new Color (0,0,200+h));
- else if (h<5)
- c = (new Color (218,195,112));
- else if (h<110) {
- red = 0;
- green = 50+h/2;
- if (h>30) red = h-10;
- c = (new Color(red,green,0));
- }
- else if (h>=110) {
- red = h+100;
- if (red>255) red = 255;
- c = (new Color(red-10,red-10,red));
- }
-
- colors[i][j]= c;
-
- }
-
- }
-