Experimented with parallel camera in 3d gameview. Works ok for rendering boats in isometric view.

#test
This commit is contained in:
cir27
2017-09-08 14:05:52 +12:00
parent cadf995bf7
commit c2c3c9eb53
5 changed files with 72 additions and 67 deletions
+18 -19
View File
@@ -13,9 +13,7 @@ import javafx.animation.Timeline;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.Group; import javafx.scene.*;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
@@ -89,7 +87,7 @@ public class GameView extends Pane {
private List<Node> mapTokens; private List<Node> mapTokens;
private ImageView mapImage = new ImageView(); private ImageView mapImage = new ImageView();
private PerspectiveCamera camera; private Camera camera;
//FRAME RATE //FRAME RATE
@@ -143,24 +141,25 @@ public class GameView extends Pane {
public GameView () { public GameView () {
gameObjects = this.getChildren(); gameObjects = this.getChildren();
// AmbientLight ambientLight = new AmbientLight(new Color(1,1,1,0.4)); // AmbientLight ambientLight = new AmbientLight(new Color(1,1,1,0.4));
//// ambientLight.setOpacity(0.5); // ambientLight.setOpacity(0.5);
// gameObjects.add(ambientLight); // gameObjects.add(ambientLight);
// create image view for map, bind panel size to image // create image view for map, bind panel size to image
// camera = new PerspectiveCamera(true); camera = new ParallelCamera();
// camera.setTranslateZ(-500); camera.setTranslateZ(-500);
// camera.setTranslateY(500); camera.setFarClip(Double.MAX_VALUE);
// camera.setTranslateX(800); camera.setNearClip(0.1);
// camera.setFieldOfView(100); PointLight pl = new PointLight();
// camera.setFarClip(50); pl.setLightOn(true);
// camera.setNearClip(-600); pl.layoutYProperty().bind(camera.layoutYProperty());
//// gameObjects.add(camera); pl.layoutXProperty().bind(camera.layoutXProperty());
// this.sceneProperty().addListener((obs, oldValue, scene) -> { // gameObjects.add(camera);
// if (scene != null) { this.sceneProperty().addListener((obs, oldValue, scene) -> {
// scene.setCamera(camera); if (scene != null) {
// } scene.setCamera(camera);
// }); }
});
initializeTimer(); initializeTimer();
gameObjects.addAll(mapImage, raceBorder, markers, tokens); gameObjects.addAll(mapImage, raceBorder, markers, tokens, pl);
} }
private void initializeTimer() { private void initializeTimer() {
@@ -1,6 +1,5 @@
package seng302.visualiser.fxObjects.assets_2D; package seng302.visualiser.fxObjects.assets_2D;
import com.interactivemesh.jfx.importer.stl.StlMeshImporter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javafx.application.Platform; import javafx.application.Platform;
@@ -13,13 +12,15 @@ import javafx.scene.PointLight;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial; import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Line; import javafx.scene.shape.Line;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.Polygon; import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline; import javafx.scene.shape.Polyline;
import javafx.scene.shape.Shape3D; import javafx.scene.shape.Shape3D;
import javafx.scene.transform.Rotate; import javafx.scene.transform.Rotate;
import javafx.scene.transform.Scale; import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate; import javafx.scene.transform.Translate;
import seng302.visualiser.fxObjects.assets_3D.BoatMeshType;
import seng302.visualiser.fxObjects.assets_3D.BoatModel;
import seng302.visualiser.fxObjects.assets_3D.ModelFactory;
/** /**
* BoatGroup is a javafx group that by default contains a graphical objects for representing a 2 * BoatGroup is a javafx group that by default contains a graphical objects for representing a 2
@@ -59,7 +60,7 @@ public class BoatObject extends Group {
//Graphical objects //Graphical objects
private Polyline trail = new Polyline(); private Polyline trail = new Polyline();
// private Polygon boatPoly; // private Polygon boatPoly;
private Shape3D boatPoly; private BoatModel boatPoly;
private Polygon sail; private Polygon sail;
private Wake wake; private Wake wake;
private Line leftLayLine; private Line leftLayLine;
@@ -101,29 +102,29 @@ public class BoatObject extends Group {
*/ */
private void initChildren(double... points) { private void initChildren(double... points) {
boatPoly = makeBoatPolygon(); boatPoly = makeBoatPolygon();
boatPoly.getTransforms().addAll( boatPoly.getAssets().getTransforms().addAll(
new Rotate(-40, new Point3D(1,0,0)), new Rotate(-40, new Point3D(1,0,0)),
rotation, rotation,
new Rotate(180, new Point3D(0, 0, 1)) new Rotate(-90, new Point3D(0,0,1))
); );
boatPoly.getTransforms().add(new Scale(10, 10, 10)); boatPoly.getAssets().getTransforms().add(new Scale(5, 5, 5));
// boatPoly.setDrawMode(DrawMode.FILL); // boatPoly.setDrawMode(DrawMode.FILL);
// boatPoly.setFill(colour); // boatPoly.setFill(colour);
// boatPoly.setFill(this.colour); // boatPoly.setFill(this.colour);
// boatPoly.setMaterial(new PhongMaterial(this.colour)); // boatPoly.setMaterial(new PhongMaterial(this.colour));
boatPoly.setOnMouseEntered(event -> { boatPoly.getAssets().setOnMouseEntered(event -> {
// boatPoly.setFill(Color.FLORALWHITE); // boatPoly.setFill(Color.FLORALWHITE);
// boatPoly.setStroke(Color.RED); // boatPoly.setStroke(Color.RED);
// boatPoly.setMaterial(new PhongMaterial(Color.FLORALWHITE)); // boatPoly.setMaterial(new PhongMaterial(Color.FLORALWHITE));
}); });
boatPoly.setOnMouseExited(event -> { boatPoly.getAssets().setOnMouseExited(event -> {
// boatPoly.setMaterial(new PhongMaterial(this.colour)); // boatPoly.setMaterial(new PhongMaterial(this.colour));
// boatPoly.setFill(colour); // boatPoly.setFill(colour);
// boatPoly.setFill(this.colour); // boatPoly.setFill(this.colour);
// boatPoly.setStroke(Color.BLACK); // boatPoly.setStroke(Color.BLACK);
}); });
boatPoly.setOnMouseClicked(event -> setIsSelected(!isSelected)); boatPoly.getAssets().setOnMouseClicked(event -> setIsSelected(!isSelected));
boatPoly.setCache(true); boatPoly.getAssets().setCache(true);
// boatPoly.setCacheHint(CacheHint.SPEED); // boatPoly.setCacheHint(CacheHint.SPEED);
// annotationBox = new AnnotationBox(); // annotationBox = new AnnotationBox();
@@ -162,20 +163,22 @@ public class BoatObject extends Group {
// super.getChildren().add(pointLight); // super.getChildren().add(pointLight);
AmbientLight light = new AmbientLight(new Color(0.5,0.5,0.5,1)); AmbientLight light = new AmbientLight(new Color(0.5,0.5,0.5,1));
super.getChildren().add(light); super.getChildren().add(light);
super.getChildren().addAll(boatPoly);//, sail); super.getChildren().addAll(boatPoly.getAssets());//, sail);
} }
public void setFill (Color value) { public void setFill (Color value) {
this.colour = value; this.colour = value;
PhongMaterial pm = new PhongMaterial(this.colour); PhongMaterial pm = new PhongMaterial(this.colour);
pm.setSpecularPower(0.5); for (int i=0;i<2;i++) {
boatPoly.setMaterial(pm); Shape3D s = (Shape3D) boatPoly.getAssets().getChildren().get(i);
s.setMaterial(pm);
}
trail.setStroke(colour); trail.setStroke(colour);
} }
public Shape3D makeBoatPolygon () { public BoatModel makeBoatPolygon () {
StlMeshImporter importer = new StlMeshImporter(); // StlMeshImporter importer = new StlMeshImporter();
importer.read(getClass().getResource("/meshes/hollow_simple_boat.stl").toString()); // importer.read(getClass().getResource("/meshes/hollow_simple_boat.stl").toString());
// importer.read(getClass().getResource("/cube.stl").toString()); // importer.read(getClass().getResource("/cube.stl").toString());
// importer.read(getClass().getResource("/meshes/simple_yacht.stl").toString()); // importer.read(getClass().getResource("/meshes/simple_yacht.stl").toString());
// ObjModelImporter importer = new ObjModelImporter(); // ObjModelImporter importer = new ObjModelImporter();
@@ -194,7 +197,7 @@ public class BoatObject extends Group {
// FloatBuffer texCoords = ObjData.getTexCoords(obj); // FloatBuffer texCoords = ObjData.getTexCoords(obj);
// MeshView // MeshView
// FloatBuffer normals = ObjData.getNormals(obj); // FloatBuffer normals = ObjData.getNormals(obj);
return new MeshView(importer.getImport()); return ModelFactory.boatGameView(BoatMeshType.DINGHY, colour);
// } catch (Exception e) { // } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// return null; // return null;
@@ -210,12 +213,12 @@ public class BoatObject extends Group {
* @param sailIn Boolean to toggle sail state. * @param sailIn Boolean to toggle sail state.
*/ */
public void moveTo(double x, double y, double rotation, double velocity, Boolean sailIn, double windDir) { public void moveTo(double x, double y, double rotation, double velocity, Boolean sailIn, double windDir) {
Double dx = Math.abs(boatPoly.getLayoutX() - x); Double dx = Math.abs(boatPoly.getAssets().getLayoutX() - x);
Double dy = Math.abs(boatPoly.getLayoutY() - y); Double dy = Math.abs(boatPoly.getAssets().getLayoutY() - y);
Platform.runLater(() -> { Platform.runLater(() -> {
rotateTo(rotation, sailIn, windDir); rotateTo(rotation, sailIn, windDir);
boatPoly.setLayoutX(x); boatPoly.getAssets().setLayoutX(x);
boatPoly.setLayoutY(y); boatPoly.getAssets().setLayoutY(y);
if (sailIn) { if (sailIn) {
// sail.getPoints().clear(); // sail.getPoints().clear();
// sail.getPoints().addAll(0.0, 0.0, 4.0, 1.5, 8.0, 3.0, 12.0, 3.5, 16.0, 3.0, 20.0, 1.5, 24.0, 0.0); // sail.getPoints().addAll(0.0, 0.0, 4.0, 1.5, 8.0, 3.0, 12.0, 3.5, 16.0, 3.0, 20.0, 1.5, 24.0, 0.0);
@@ -261,33 +264,30 @@ public class BoatObject extends Group {
private void rotateTo(double heading, boolean sailsIn, double windDir) { private void rotateTo(double heading, boolean sailsIn, double windDir) {
rotation.setAngle(heading); rotation.setAngle(heading);
if (sailsIn) { if (sailsIn) {
boatPoly.showSail();
Double sailWindOffset = 30.0; Double sailWindOffset = 30.0;
Double upwindAngleLimit = 15.0; Double upwindAngleLimit = 15.0;
Double downwindAngleLimit = 10.0; //Upwind from normalised horizontal Double downwindAngleLimit = 10.0; //Upwind from normalised horizontal
Double normalizedHeading = normalizeHeading(heading, windDir); Double normalizedHeading = normalizeHeading(heading, windDir);
if (normalizedHeading < 180) { if (normalizedHeading < 180) {
sail.getTransforms().setAll(new Rotate(windDir + 90 + sailWindOffset));
sail.getPoints().clear();
sail.getPoints().addAll(0.0, 0.0, 4.0, -1.5, 8.0, -3.0, 12.0, -3.5, 16.0, -3.0, 20.0, -1.5, 24.0, 0.0);
if (normalizedHeading > 90 + sailWindOffset){
sail.getTransforms().setAll(new Rotate(heading + downwindAngleLimit));
}
if (normalizedHeading < sailWindOffset + upwindAngleLimit){ if (normalizedHeading < sailWindOffset + upwindAngleLimit){
sail.getTransforms().setAll(new Rotate(heading + 90 - upwindAngleLimit)); boatPoly.rotateSail(heading + 90 - upwindAngleLimit);
} else if (normalizedHeading > 90 + sailWindOffset){
boatPoly.rotateSail(heading + downwindAngleLimit);
} else {
boatPoly.rotateSail(windDir + 90 + sailWindOffset);
} }
} else { } else {
sail.getTransforms().setAll(new Rotate(windDir + 90 - sailWindOffset));
sail.getPoints().clear();
sail.getPoints().addAll(0.0, 0.0, 4.0, 1.5, 8.0, 3.0, 12.0, 3.5, 16.0, 3.0, 20.0, 1.5, 24.0, 0.0);
if (normalizedHeading < 270 - sailWindOffset){
sail.getTransforms().setAll(new Rotate(heading + 180 - downwindAngleLimit));
}
if (normalizedHeading > 360 - (sailWindOffset + upwindAngleLimit)){ if (normalizedHeading > 360 - (sailWindOffset + upwindAngleLimit)){
sail.getTransforms().setAll(new Rotate(heading + 90 + upwindAngleLimit)); boatPoly.rotateSail(heading + 90 + upwindAngleLimit);
} else if (normalizedHeading < 270 - sailWindOffset){
boatPoly.rotateSail(heading + 180 - downwindAngleLimit);
} else {
boatPoly.rotateSail(windDir + 90 - sailWindOffset);
} }
} }
} else { } else {
sail.getTransforms().setAll(new Rotate(windDir)); boatPoly.hideSail();
} }
} }
@@ -423,12 +423,12 @@ public class BoatObject extends Group {
} }
public Double getBoatLayoutX() { public Double getBoatLayoutX() {
return boatPoly.getLayoutX(); return boatPoly.getAssets().getLayoutX();
} }
public Double getBoatLayoutY() { public Double getBoatLayoutY() {
return boatPoly.getLayoutY(); return boatPoly.getAssets().getLayoutY();
} }
/** /**
@@ -7,13 +7,16 @@ import javafx.scene.Group;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint; import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle; import javafx.scene.shape.Circle;
import javafx.scene.transform.Scale;
import seng302.visualiser.fxObjects.assets_3D.ModelFactory;
import seng302.visualiser.fxObjects.assets_3D.ModelType;
/** /**
* Visual object for a mark. Contains a coloured circle and any specified arrows. * Visual object for a mark. Contains a coloured circle and any specified arrows.
*/ */
public class Marker extends Group { public class Marker extends Group {
private Circle mark = new Circle(); private Group mark = ModelFactory.importModel(ModelType.PLAIN_MARKER).getAssets();
private Paint colour = Color.BLACK; private Paint colour = Color.BLACK;
private List<Group> enterArrows = new ArrayList<>(); private List<Group> enterArrows = new ArrayList<>();
private List<Group> exitArrows = new ArrayList<>(); private List<Group> exitArrows = new ArrayList<>();
@@ -24,10 +27,13 @@ public class Marker extends Group {
* Creates a new Marker containing only a circle. The default colour is black. * Creates a new Marker containing only a circle. The default colour is black.
*/ */
public Marker() { public Marker() {
mark.setRadius(5); // mark.setRadius(5);
mark.setCenterX(0); // mark.setCenterX(0);
mark.setCenterY(0); // mark.setCenterY(0);
Platform.runLater(() -> this.getChildren().addAll(mark, new Group())); //Empty group placeholder or arrows. Platform.runLater(() -> {
mark.getTransforms().add(new Scale(5,5,5));
this.getChildren().addAll(mark, new Group());
}); //Empty group placeholder or arrows.
} }
/** /**
@@ -37,7 +43,7 @@ public class Marker extends Group {
public Marker(Paint colour) { public Marker(Paint colour) {
this(); this();
this.colour = colour; this.colour = colour;
mark.setFill(colour); // mark.setFill(colour);
} }
/** /**
@@ -7,7 +7,7 @@ package seng302.visualiser.fxObjects.assets_3D;
*/ */
public enum BoatMeshType { public enum BoatMeshType {
DINGHY ("dinghy_hull.stl", "dinghy_mast.stl", 0, "dinghy_sail.stl", -1.36653); DINGHY ("dinghy_hull.stl", "dinghy_mast.stl", -1.36653, "dinghy_sail.stl", -1.36653);
final String hullFile, mastFile, sailFile; final String hullFile, mastFile, sailFile;
final double mastOffset, sailOffset; final double mastOffset, sailOffset;
@@ -33,14 +33,14 @@ public class BoatModel extends Model {
* Rotates the sail of this model by the given amount. * Rotates the sail of this model by the given amount.
* @param degrees The rotation of the sail in degrees * @param degrees The rotation of the sail in degrees
*/ */
public void RotateSail(double degrees) { public void rotateSail(double degrees) {
MeshView mast = getMeshViewChild(MAST_INDEX); MeshView mast = getMeshViewChild(MAST_INDEX);
MeshView sail = getMeshViewChild(SAIL_INDEX); MeshView sail = getMeshViewChild(SAIL_INDEX);
mast.getTransforms().setAll( mast.getTransforms().setAll(
new Rotate(degrees, meshType.mastOffset, 0,0, new Point3D(0, 0, 1)) new Rotate(degrees, -meshType.mastOffset, 0,0, new Point3D(0, 0, 1))
); );
sail.getTransforms().setAll( sail.getTransforms().setAll(
new Rotate(degrees, meshType.sailOffset, 0,0, new Point3D(0, 0, 1)) new Rotate(degrees, -meshType.sailOffset, 0,0, new Point3D(0, 0, 1))
); );
} }