Tuesday, 17 December 2013

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 abuild 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:
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereAndBox extends Application {
double anchorX, anchorY, anchorAngle;
Image bumpMap = new Image(getClass().getResourceAsStream("panchayt-map.jpg"));
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("SphereAndBox");
System.out.println("@@@@@@@@@@@@@");
PhongMaterial boxMaterial = new PhongMaterial();
boxMaterial.setDiffuseColor(Color.GREEN);
boxMaterial.setSpecularColor(Color.WHITESMOKE);
// BOX
Box box = new Box(400, 400, 400);
box.setMaterial(boxMaterial);
box.setTranslateX(250);
box.setTranslateY(250);
box.setTranslateZ(450);
// Sphere
PhongMaterial sphereMaterial = new PhongMaterial();
sphereMaterial.setDiffuseColor(Color.WHITE);
sphereMaterial.setSpecularColor(Color.LIGHTBLUE);
sphereMaterial.setBumpMap(bumpMap);
Sphere sphere = new Sphere(200);
sphere.setMaterial(sphereMaterial);
sphere.setTranslateX(250);
sphere.setTranslateY(250);
sphere.setTranslateZ(50);
final Group parent = new Group(box, sphere);
parent.setTranslateZ(500);
parent.setRotationAxis(new Point3D(1, 1, 1));
Group root = new Group(parent);
Scene scene = new Scene(root, 500, 500, true);
scene.setCamera(new PerspectiveCamera(false));
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
anchorX = event.getSceneX();
anchorY = event.getSceneY();
anchorAngle = parent.getRotate();
}
});
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
parent.setRotate(anchorAngle + anchorX - event.getSceneX());
}
});
PointLight pointLight = new PointLight(Color.ANTIQUEWHITE);
pointLight.setTranslateX(15);
pointLight.setTranslateY(-10);
pointLight.setTranslateZ(-100);
root.getChildren().add(pointLight);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
System.setProperty("prism.dirtyopts", "false");
launch(args);
}
}
view raw SphereAndBox hosted with ❤ by GitHub

No comments: