Added adapter interface for PerspectiveCamera frustum and transformations in View3D.

- Tested interface while initialising HostController

#story[1196]
main
Connor Taylor-Brown 9 years ago
parent e04f199fc7
commit bb7eb7cb44

@ -52,13 +52,18 @@ public class HostController extends Controller {
private Event game;
private View3D fancyStuff;
private View3D view3D;
@Override
public void initialize(URL location, ResourceBundle resources) {
fancyStuff = new View3D();
playerContainer.add(fancyStuff, 0,0);
fancyStuff.addShape(new Box(100,100,100));
view3D = new View3D();
playerContainer.add(view3D, 0,0);
Box box = new Box(100, 100, 100);
view3D.addShape(box);
view3D.setPivot(box);
view3D.setDistance(500);
view3D.setPitch(20);
}
/**

@ -6,38 +6,129 @@ import javafx.scene.SubScene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Shape3D;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import java.util.ArrayList;
import java.util.List;
/**
* Created by connortaylorbrown on 30/08/17.
* Control for rendering 3D objects visible through a PerspectiveCamera. Implements Adapter Pattern to
* interface with camera, and allows clients to add shapes to the scene. All scenes contain sea plane and
* sky box, whose textures are set with special methods.
*/
public class View3D extends Pane {
SubScene scene;
Group world;
List<Shape3D> shapes;
private Group world;
private List<Shape3D> shapes;
private double nearClip;
private double farClip;
/**
* Position camera pivots around
*/
private Translate pivot;
/**
* Distance of camera from pivot point
*/
private Translate distance;
/**
* Angle along ground between z-axis and camera
*/
private Rotate yaw;
/**
* Angle between ground plane and camera direction
*/
private Rotate pitch;
/**
* Default constructor for View3D. Sets up Scene and PerspectiveCamera.
*/
public View3D() {
shapes = new ArrayList<>();
world = new Group();
scene = new SubScene(world, 300, 300);
SubScene scene = new SubScene(world, 300, 300);
scene.widthProperty().bind(this.widthProperty());
scene.heightProperty().bind(this.heightProperty());
scene.setFill(Color.BLACK);
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(1000.0);
camera.setTranslateZ(-500);
scene.setCamera(camera);
scene.setCamera(buildCamera());
this.getChildren().add(scene);
}
/**
* Sets up camera view frustum and binds transformations
* @return perspective camera
*/
private PerspectiveCamera buildCamera() {
PerspectiveCamera camera = new PerspectiveCamera(true);
// Set up view frustum
nearClip = 0.1;
farClip = 1000.0;
camera.setNearClip(nearClip);
camera.setFarClip(farClip);
// Set up transformations
pivot = new Translate();
distance = new Translate();
yaw = new Rotate(0, Rotate.Y_AXIS);
pitch = new Rotate(0, Rotate.X_AXIS);
camera.getTransforms().addAll(pivot, yaw, pitch, distance);
return camera;
}
/**
* Adds new Shape3D object to scene
* @param shape to add
*/
public void addShape(Shape3D shape) {
shapes.add(shape);
world.getChildren().add(shape);
}
public void setNearClip(double nearClip) {
this.nearClip = nearClip;
}
public void setFarClip(double farClip) {
this.farClip = farClip;
}
/**
* Set object to centre on camera
* @param pivot centred object
*/
public void setPivot(Shape3D pivot) {
this.pivot.setX(pivot.getTranslateX());
this.pivot.setY(pivot.getTranslateY());
this.pivot.setZ(pivot.getTranslateZ());
}
/**
* Set distance of camera from pivot
* @param distance in units
*/
public void setDistance(double distance) {
this.distance.setZ(-distance);
}
/**
* Set angle of camera from z-axis along ground
* @param yaw in degrees
*/
public void setYaw(double yaw) {
this.yaw.setAngle(yaw);
}
/**
* Set elevation of camera
* @param pitch in degrees
*/
public void setPitch(double pitch) {
this.pitch.setAngle(-pitch);
}
}

Loading…
Cancel
Save