Sunday, February 24, 2013

Conway's Game of Life using JavaFX 3D

I didn't need to change much of the code to run it in 3D - just replace rectangles with boxes, add camera and light - that's it.

What you'll get when using 3D features (in any language) is increased complexity. You have to think about the camera's position, it's field of view, the light sources, their position, the materials, reflections, textures ...

But: What is really impressive concerning JavaFX is that from the programmers viewpoint everything stays the same - for example, you can register your mouseOver actions on a Box in 3D the same way you can use it in 2D with Rectangles. The timelining works the same no matter if you animate 3D or 2D objects etc.

Here is the code for the video above (JDK8 needed!):

JavaFX 3D Hello World

To compile JavaFX with 3D features you have to get the early access version of the JDK8.

At the moment as far as I know there is only a windows support for the 3D features, but a build for Mac and Linux will soon be released. (Luckily enough 3D support also works for a virtualized Windows running on Mac - this is how i got to the screeenshots.)

This blog entry is about a Scala version of the provided 3D examples

First, there is the class PhongMaterial, which defines some sort of "Phong shaded material". Basically you can create a material which can have a color or some texture. 

a red box and a blue sphere rendered with JavaFX

This is a screenshot of the same program, different colors, with a bumpmap applied:

example using a bump map
Here is the code:

With a little imagination you can surely think of many ways to use this features in your applications. At the moment PhongMaterial is the only implementation of the abstract Material class. 

In the above example, Sphere and Box classes are used to represent 3D shapes, but there are also other primitives provided, like Cylinder or MeshView.

You may also want to peek into the sources on the openjfx repository:

hg clone

Thursday, February 7, 2013

Conways Game of Life

I always wanted to implement Conways Game of Life, and by using JavaFX and Scala this happens to be possible with just some 140 lines of code.

Background information for the idea can be read on the wikipedia, I'll quote the important stuff here:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

This, expressed in Scala code, can be written as:

Conway's next generation algorithm

The funny thing is that there exist some starting configurations which produce some stable, self repeating patterns.

Here is the source:

Tuesday, February 5, 2013

Tree visualization Part 3

In this post I want to make the trees more realistic and give them some leaves.

The improved realism can be achieved with a technique called "midpoint replacement", which was also used to make the lightning bolts look somewhat chaotic yet surprisingly realistic.

Of course, in the case of visualizing natural trees there are many factors to consider, and by applying unspecific random based algorithms you only reach a limited realism. But compared to the first approach using straight lines the results are much better ;-)

During the implementation of the lightning article, I've developed a datastructure which I wanted to use also for the plant-some-trees repository. This datastructure, "Vec", is shown below:

Vec datastructure

It contains some helper methods, which make it easier to do 2D calculations. Using this datastructure the midpoint replacement can be implemented like this:

Midpoint replacement algorithm
First, I refactored the code to use the Vec class, secondly, I added midpoint replacement for the trees.

To make the visualization even more interesting, I also added some leaves to the trees. I used a simple approach: If a branch is thin enough, it will get some leaves. Only the  traverse function had to be enhanced for that.

Result: a tree with leaves
The code is available at github.