3

I want to simulate perspective distortion of boxes and rectangles. My goal is to warp the box and image, as if the camera is being tilted and moved. But I don't really follow how to use the PerspectiveCamera.

When I set the fixedEyeAtCameraZero to false, I can see the box and image on screen. But changing the window size causes weird orthographic perspective changes that aren't realistic.

On the other hand when I set fixedEyeAtCameraZero to true, all I see is a blank window.

False: false

True: true

Here's the code, with the offending flag at line 51.

package sample;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class Example_Box extends Application {
    @Override
    public void start(Stage stage) {

        //Drawing a Box
        Box box2 = new Box();

        //Setting the properties of the Box
        box2.setWidth(100.0);
        box2.setHeight(100.0);
        box2.setDepth(100.0);

        //Setting the position of the box
        box2.setTranslateX(30); //450
        box2.setTranslateY(90);//150
        box2.setTranslateZ(300);

        //Setting the drawing mode of the box
        box2.setDrawMode(DrawMode.LINE);

        //Drawing an Image
        Image image = new Image("Lenna.png");
        ImageView imageView = new ImageView(image);
        imageView.setTranslateX(200);
        imageView.setTranslateY(150);
        imageView.setTranslateZ(200);

        //imageView.getTransforms().add(new Rotate(30, 50, 30));

        //Creating a Group object
        Group root = new Group(box2, imageView);

        //Creating a scene object
        Scene scene = new Scene(root, 600, 300);

        //Setting camera
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setTranslateX(30);
        camera.setTranslateY(0);
        camera.setTranslateZ(-100);

        camera.setRotationAxis(new Point3D(1,0,0));
        scene.setCamera(camera);

        //Setting title to the Stage
        stage.setTitle("Drawing a Box");

        //Adding scene to the stage
        stage.setScene(scene);

        //Displaying the contents of the stage
        stage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

1 Answers1

3

Try to change the farClip value. By default farClip value is 100 in FX perspective camera.

    camera.setFarClip(2000.0);

With above piece code addition, I could see the Box. If I move the camera further away(in Z direction), I could see Image also,

    camera.setTranslateZ(-1000);

Reference: http://www.dummies.com/programming/java/javafx-add-a-perspective-camera/

Arunprasad Rajkumar
  • 1,374
  • 1
  • 15
  • 31