-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMain.java
More file actions
116 lines (90 loc) · 3.75 KB
/
Main.java
File metadata and controls
116 lines (90 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sample;
import com.javafx.experiments.importers.Importer3D;
import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
boolean supported = Platform.isSupported(ConditionalFeature.SCENE3D);
// if (supported) createScene3D(primaryStage);
if (supported) loadScene3D(primaryStage);
}
private void loadScene3D(Stage stage) throws IOException {
PointLight light1 = new PointLight();
light1.setTranslateZ(-500);
Node model = Importer3D.load(getClass().getResource("obj-model/cyborg.obj").toExternalForm());
model.setScaleX(150.0);
model.setScaleY(150.0);
model.setScaleZ(150.0);
Group root = new Group(model, light1);
Scene scene = new Scene(root, 1280, 768, true, SceneAntialiasing.BALANCED);
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(scene.getWidth() / -2.0);
camera.setTranslateY(scene.getHeight() / -2.0);
RotateTransition rt = new RotateTransition(Duration.seconds(10), model);
rt.setCycleCount(Animation.INDEFINITE);
rt.setFromAngle(0);
rt.setToAngle(360);
rt.setAxis(new Point3D(0, 1, 0));
rt.play();
scene.setFill(Color.CORNFLOWERBLUE);
scene.setCamera(camera);
stage.setTitle("JavaFX Graficos 3D - Cargar Modelo");
stage.setScene(scene);
stage.show();
}
private void createScene3D(Stage stage) {
Color color_luz = Color.WHITESMOKE;
Image image_diffuse = new Image(getClass().getResource("muro_diffuse.jpg").toExternalForm());
Image image_normal = new Image(getClass().getResource("muro_normal.jpg").toExternalForm());
// materiales para el box shape
PhongMaterial mat = new PhongMaterial();
mat.setSpecularColor(color_luz);
mat.setSpecularPower(64);
mat.setDiffuseMap(image_diffuse);
mat.setBumpMap(image_normal);
// crear un cubo 3D, anchura, altura y profundidad
Box box = new Box(300, 300, 300);
box.setRotate(45);
box.setRotationAxis(new Point3D(1, 1, 0));
box.setMaterial(mat);
// crear una luz puntual
PointLight light = new PointLight(color_luz);
light.setTranslateX(-350);
light.setTranslateY(+180);
light.setTranslateZ(-500);
Group root = new Group(box, light);
// crear la escena, true para activar el buffer de profindidad
Scene scene = new Scene(root, 1280, 768, true, SceneAntialiasing.BALANCED);
// crear una camara en perspectiva
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(scene.getWidth() / -2.0);
camera.setTranslateY(scene.getHeight() / -2.0);
// crear una animacion de rotacion sobre los ejes (X,Y,Z)
RotateTransition rt = new RotateTransition(Duration.seconds(10), box);
rt.setCycleCount(Animation.INDEFINITE);
rt.setFromAngle(0);
rt.setToAngle(360);
rt.setAxis(new Point3D(1, 1, 1));
rt.play();
scene.setCamera(camera);
stage.setTitle("JavaFX Graficos 3D");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}