Skip to content

Commit

Permalink
Refactored some code from planet-render out into it's own library.
Browse files Browse the repository at this point in the history
  • Loading branch information
codeka committed Feb 11, 2013
1 parent 538621f commit da85744
Show file tree
Hide file tree
Showing 27 changed files with 202 additions and 87 deletions.
6 changes: 6 additions & 0 deletions common/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions common/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>common</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import au.com.codeka.common.ObjectPool;

/**
* Helper class that represents an ARGB colour.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

public class ObjectPool<T extends ObjectPool.Pooled> {
private PooledCreator mCreator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import java.util.Random;

/**
* This class generates perlin noise, which we can apply to various parts of the planet.
*/
public class PerlinNoise {
private double mPersistence;
private Interpolator mInterpolator;
private long mRawSeed;
private int mStartOctave;
private int mEndOctave;
private Random mRawRand;

public PerlinNoise(Template.PerlinNoiseTemplate tmpl, Random rand) {
mRawSeed = rand.nextLong();
mPersistence = tmpl.getPersistence();
mStartOctave = tmpl.getStartOctave();
mEndOctave = tmpl.getEndOctave();
protected double mPersistence;
protected Interpolator mInterpolator;
protected long mRawSeed;
protected int mStartOctave;
protected int mEndOctave;
protected Random mRawRand;

public PerlinNoise() {
mRawSeed = 0;
mPersistence = 0;
mStartOctave = 0;
mEndOctave = 0;
mRawRand = new Random();

if (tmpl.getInterpolation() == Template.PerlinNoiseTemplate.Interpolation.None) {
mInterpolator = new NoneInterpolator();
} else if (tmpl.getInterpolation() == Template.PerlinNoiseTemplate.Interpolation.Linear) {
mInterpolator = new LinearInterpolator();
} else if (tmpl.getInterpolation() == Template.PerlinNoiseTemplate.Interpolation.Cosine) {
mInterpolator = new CosineInterpolator();
} else {
mInterpolator = new NoneInterpolator(); // ??
}
mInterpolator = new NoneInterpolator();
}

/**
Expand Down Expand Up @@ -103,23 +94,29 @@ private double interpolatedNoise(double x, double y, int octave) {
return mInterpolator.interpolate(ny1, ny2, fy);
}

private interface Interpolator {
protected interface Interpolator {
double interpolate(double a, double b, double n);
}

private static class NoneInterpolator implements Interpolator {
protected static class NoneInterpolator implements Interpolator {
public NoneInterpolator() {}

public double interpolate(double a, double b, double n) {
return a;
}
}

private static class LinearInterpolator implements Interpolator {
protected static class LinearInterpolator implements Interpolator {
public LinearInterpolator() {}

public double interpolate(double a, double b, double n) {
return a + n * (b - a);
}
}

private static class CosineInterpolator implements Interpolator {
protected static class CosineInterpolator implements Interpolator {
public CosineInterpolator() {}

public double interpolate(double a, double b, double n) {
double radians = n * Math.PI;
n = (1 - Math.cos(radians)) * 0.5;
Expand Down
33 changes: 33 additions & 0 deletions common/src/au/com/codeka/common/PointCloud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package au.com.codeka.common;

import java.util.ArrayList;
import java.util.List;

/**
* A point cloud is, well, a cloud of points. We use it to generate a voroni/delauny mapping
* that is then use to generate planet textures.
*
* The points are always bound to the square (0,0), (1,1).
*/
public class PointCloud {
protected ArrayList<Vector2> mPoints;

public PointCloud() {
mPoints = new ArrayList<Vector2>();
}

public List<Vector2> getPoints() {
return mPoints;
}

/**
* Helper class to render this point cloud to the given \c Image (mostly for debugging).
*/
public void render(Image img) {
for (Vector2 p : mPoints) {
int x = (int)(img.getWidth() * p.x);
int y = (int)(img.getHeight() * p.y);
img.drawCircle(x, y, 5.0, Colour.RED);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import au.com.codeka.planetrender.ObjectPool.Pooled;
import au.com.codeka.common.ObjectPool;
import au.com.codeka.common.ObjectPool.Pooled;

/**
* Represents a 2-dimensional vector.
*/
class Vector2 implements ObjectPool.Pooled {
public class Vector2 implements ObjectPool.Pooled {
public static ObjectPool<Vector2> pool = new ObjectPool<Vector2>(250, new Vector2Creator());

public double x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import au.com.codeka.planetrender.ObjectPool.Pooled;
import au.com.codeka.common.ObjectPool;
import au.com.codeka.common.ObjectPool.Pooled;

/**
* Helper class that represents a 3-dimensional vector.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
* This class represents a Voronoi diagram (and corresponds Delaunay triangulation) of a
* \c PointCloud. It's used as the staring point of our texture generator.
*/
public class Voronoi {
private PointCloud mPointCloud;
private ArrayList<Triangle> mTriangles;
protected PointCloud mPointCloud;
protected ArrayList<Triangle> mTriangles;

// this mapping maps points in the point cloud to the triangles that share the
// point as a vertex
Expand All @@ -34,16 +33,10 @@ public Voronoi(PointCloud pc) {
generate();
}

public Voronoi(Template.VoronoiTemplate tmpl, Random rand) {
Template.PointCloudTemplate pcTmpl = tmpl.getParameter(Template.PointCloudTemplate.class);
mPointCloud = new PointCloud(pcTmpl, rand);
generate();
}

/**
* Generates the delaunay trianglation/voronoi diagram of the point cloud.
*/
private void generate() {
protected void generate() {
List<Triangle> triangles = new ArrayList<Triangle>();
List<Vector2> points = mPointCloud.getPoints();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package au.com.codeka.planetrender;
package au.com.codeka.common;

import java.util.Iterator;

Expand Down
6 changes: 6 additions & 0 deletions control-field/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions control-field/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>control-field</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
1 change: 1 addition & 0 deletions planet-render-android/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/planet-render"/>
<classpathentry combineaccessrules="false" kind="src" path="/common"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import au.com.codeka.planetrender.Image;
import au.com.codeka.common.Image;
import au.com.codeka.planetrender.PlanetRenderer;
import au.com.codeka.planetrender.Template;
import au.com.codeka.planetrender.TemplateException;
Expand Down
1 change: 1 addition & 0 deletions planet-render-test/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/planet-render"/>
<classpathentry combineaccessrules="false" kind="src" path="/common"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import javax.swing.JTextField;
import javax.swing.JToolBar;

import au.com.codeka.common.Colour;
import au.com.codeka.common.Image;
import au.com.codeka.common.PerlinNoise;
import au.com.codeka.common.PointCloud;
import au.com.codeka.common.Voronoi;
import au.com.codeka.planetrender.*;

/**
Expand Down Expand Up @@ -68,14 +73,14 @@ private void render() {
TextureGenerator texture = new TextureGenerator((Template.TextureTemplate) tmpl, getRandom());
texture.renderTexture(img);
} else if (tmpl instanceof Template.VoronoiTemplate) {
Voronoi v = new Voronoi((Template.VoronoiTemplate) tmpl, getRandom());
Voronoi v = new TemplatedVoronoi((Template.VoronoiTemplate) tmpl, getRandom());
v.renderDelaunay(img, Colour.GREEN);
v.renderVoronoi(img, Colour.BLUE);
} else if (tmpl instanceof Template.PointCloudTemplate) {
PointCloud pc = new PointCloud((Template.PointCloudTemplate) tmpl, getRandom());
PointCloud pc = new TemplatedPointCloud((Template.PointCloudTemplate) tmpl, getRandom());
pc.render(img);
} else if (tmpl instanceof Template.PerlinNoiseTemplate) {
PerlinNoise pn = new PerlinNoise((Template.PerlinNoiseTemplate) tmpl, getRandom());
PerlinNoise pn = new TemplatedPerlinNoise((Template.PerlinNoiseTemplate) tmpl, getRandom());
pn.render(img);
} else {
mStatus.setText("Unknown template kind: "+tmpl.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import javax.imageio.ImageIO;
import javax.swing.JPanel;

import au.com.codeka.planetrender.Colour;
import au.com.codeka.planetrender.Image;
import au.com.codeka.common.Colour;
import au.com.codeka.common.Image;

class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
Expand Down
1 change: 1 addition & 0 deletions planet-render/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/common"/>
<classpathentry kind="output" path="bin"/>
</classpath>
9 changes: 7 additions & 2 deletions planet-render/src/au/com/codeka/planetrender/Atmosphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import java.util.List;
import java.util.Random;

import au.com.codeka.common.Colour;
import au.com.codeka.common.ColourGradient;
import au.com.codeka.common.PerlinNoise;
import au.com.codeka.common.Vector3;

/**
* This class will generate an atmosphere around a planet.
*/
Expand Down Expand Up @@ -98,7 +103,7 @@ public InnerAtmosphere(Template.AtmosphereTemplate.InnerOuterTemplate tmpl, Rand
Template.PerlinNoiseTemplate perlinTemplate = tmpl.getParameter(
Template.PerlinNoiseTemplate.class);
if (perlinTemplate != null) {
mPerlin = new PerlinNoise(perlinTemplate, rand);
mPerlin = new TemplatedPerlinNoise(perlinTemplate, rand);
mNoisiness = tmpl.getNoisiness();
}
mBlendMode = tmpl.getBlendMode();
Expand Down Expand Up @@ -160,7 +165,7 @@ public OuterAtmosphere(Template.AtmosphereTemplate.InnerOuterTemplate tmpl, Rand
Template.PerlinNoiseTemplate perlinTemplate = tmpl.getParameter(
Template.PerlinNoiseTemplate.class);
if (perlinTemplate != null) {
mPerlin = new PerlinNoise(perlinTemplate, rand);
mPerlin = new TemplatedPerlinNoise(perlinTemplate, rand);
mNoisiness = tmpl.getNoisiness();
}
mBlendMode = tmpl.getBlendMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.util.List;
import java.util.Random;

import au.com.codeka.common.Colour;
import au.com.codeka.common.Image;
import au.com.codeka.common.Vector3;

/**
* This is actually a very simple ray-tracing engine. The simplicity comes from the fact that
* we assume there's only one object in the scene (the planet) and one light source (the sun).
Expand Down
5 changes: 5 additions & 0 deletions planet-render/src/au/com/codeka/planetrender/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import au.com.codeka.common.Colour;
import au.com.codeka.common.ColourGradient;
import au.com.codeka.common.Vector3;
import au.com.codeka.common.XmlIterator;

/**
* A template is used to create an image (usually a complete planet, but it doesn't have to be).
*/
Expand Down
Loading

0 comments on commit da85744

Please sign in to comment.