dcsimg
December 4, 2016
Hot Topics:

3D Graphics in JavaFX

  • May 1, 2015
  • By Manoj Debnath
  • Send Email »
  • More Articles »

JavaFX 8 provides some intuitive 3D graphics capabilities in its API library. An interesting aspect is that the 3D API structure of JavaFX has maintained the same lineage of simplicity when compared with its the core API structure. The learning curve is almost flat. If one has dealt with 2D APIs of JavaFX, working with 3D APIs will be almost a cakewalk. The use of functions and objects in 3D programming does not make it look like a hurdle while coding. However, it would be too much of an expectation, at least for now, to have all the function and features that are available in any established 3D API library. However, the progress of JavaFX in the 3D arena is commendably decent, keeping sight of the complexities involved in creating a full-fledged 3D API library. The article shall focus on the simplicity of 3D programming in JavaFX with some detail.

Simplifying Complexity

It is obvious that 3D programming is complex with, an involvement of multiple parameters in action with due consideration of functions such as projection, camera angle, color, lighting and shading effects, and so forth. They are the primary ingredient of a 3D scene that works synchronously to project 3D objects on a computer screen. Another perspective to ratiocinate is that a computer screen is inherently two-dimensional. The mathematics of three-dimensional computing creates an illusion of an object having height, width, and depth, when actually there is not. Only the parameters like light, shading, and angle of projection make it so. As I said earlier, it's complex in the sense that a lot of mathematics is involved. As with any library, JavaFX allays a considerable complexity by providing ready-to-use functions and objects. Let's first see a simple yet complete 3D programming code in JavaFX.

package demojavafx3d1;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

public class DemoJavaFX3D1 extends Application {

   @Override
   public void start(Stage primaryStage) {

      Sphere sphere=new Sphere(100);
      Group root=new Group(sphere);

      Scene scene = new Scene(root, 600, 600);

      primaryStage.setTitle("3D JavaFX");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

   public static void main(String[] args) {
      Application.launch(args);
   }

}

Although it may seem a simple JavaFX program, observe the following line of code closely:

Sphere sphere=new Sphere(100);

The only 3D API we have used in this context represents a sphere looming large in the window center without any fuss over lights, camera, color and so forth, and by all means it is ridiculously a simple yet complete 3D program. What the JavaFX runtime system does internally is that it adds default camera, light, and material quality to the scene objects without active intervention of the programmer. However, complete it may seem, it is actually a dumb 3D code in JavaFX. Let's make it a little livelier.

Setting Up Rendering Material

Shading material basically mixes light, color, and shade to give an illusion of a 3D effect. We can add Phong Shading material to the 3D object. Phong shading is a technique used to render 3D objects so that they have more realistic highlights on the surface. It reflects light on the basis of a diffuse and specular component along with ambience and self-illumination. Mathematically, it determines the average unit normal vector at each polygon vertex and then linearly interpolates the vertex normal over the surface of the polygon. After that, an illumination model is applied along each scan line to calculate projected pixel intensities for the surface points. In code, we do it as follows.

Sphere sphere = new Sphere(100);
PhongMaterial material1 = new PhongMaterial();
material1.setDiffuseColor(Color.BLUE);
material1.setSpecularColor(Color.LIGHTBLUE);
material1.setSpecularPower(10.0);
sphere.setMaterial(material1);

Setting Up Light

As shading material creates the effect of internal light of 3D object, ambient and point light represents the light pouring from external sources. The javafx.scene.LightBase abstract provides two concrete implementation classes, such as AmbientLight and PointLight. LightBase defines source of light that illuminates the 3D object. AmbientLight defines the light source that seems to come from all directions. PointLight defines light that is coming from a fixed point in space and radiates in all directions equally. We can add light source in code, as shown in the next code segment.

AmbientLight light=new AmbientLight(Color.AQUA);
light.setTranslateX(-180);
light.setTranslateY(-90);
light.setTranslateZ(-120);
light.getScope().addAll(box,sphere);

PointLight light2=new PointLight(Color.AQUA);
light2.setTranslateX(180);
light2.setTranslateY(190);
light2.setTranslateZ(180);
light2.getScope().addAll(box,sphere);

Group group = new Group(sphere, box);
...
group.getChildren().addAll(light,light2);

Setting Up Camera

Camera sets the viewing perspective of having a third dimension. Setting up of camera in JavaFX creates a separate scene graph node and defines our viewing perspective of the 3D object.

Scene scene = new Scene(root, 600, 600);
scene.setFill(Color.BLACK);
PerspectiveCamera camera =
   new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(1000.0);
camera.setTranslateZ(-1000);
scene.setCamera(camera);

The boolean parameter of the PerspectiveCamera instance defines the 3D layout to the origin, the center of the screen. If we provide a false value, the 3D layout origin is located at the upper-left corner of the screen. The parameter value of the functions setNearClip and setFarclip specifies the distance range between which the object will be displayed: an object very near (to the eye coordinate space) or too far beyond the visual range. The value determines the clipping region or the visual range beyond which the object will be not drawn or displayed.

Problems with JavaFX

The main problem associated with working with the JavaFX 3D API library is that to what extent and way the API can utilize native graphics hardware for performance gain. At present, the picture is rather gloomy. JavaFX crawls with complicated 3D API animation. Java inherently works in layers and number crunching in graphics processing require first-hand access to the underlying hardware. This is the main reason why Java is not much of a choice when working with low-level, graphics-intensive programs such as games. This actually is a double-edged problem. On one hand, Java has to be platform independent by being in the lap of JVM; on the other hand, 3D API demands low-level access. A compromise of either Java-philosophy or performance degradation due to 3D API is clearly not acceptable. A third alternative must be sought to leverage JavaFX 3D API as a full-fledged 3D library in the near future.

Conclusion

Camera, light, projection, and rendering material are the building blocks of a 3D scene in JavaFX. JavaFX lacks extensive 3D functionalities, yet to begin with this is the best library, especially for the people who want to initiate 3D programming. This ease of use is not due to its limited functionality, but due its intuitive API structure. Programmers like me always wanted a core 3D corner (not a third-party library) in Java. Now, JavaFX has provided one and we can play with the new toy and have fun!


Tags: JavaFX, camera, 3D graphics API, 3D programming, Boolean, 2D graphics, Phong Shading




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel