-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpdl3DJavaFX_molecule.h
291 lines (219 loc) · 8.11 KB
/
dpdl3DJavaFX_molecule.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File: graphics/dpdl3DJavaFX_molecule.h
#
# Example: Small Dpdl app that uses JavaFX library to render and animate chemical molecules in 3D.
# This code makes use of a custom java class implementation 'DpdlTestForm.java' located in the folder './java'
# The example is has bee ported from the Oracle javafx graphics tutorials at https://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d.htm#CJAHFAF
#
# Author: A.Costa
# e-mail: [email protected]
#
#
import('thread')
double CAMERA_INITIAL_DISTANCE = -450.0d
double CAMERA_INITIAL_X_ANGLE = 70.0d
double CAMERA_INITIAL_Y_ANGLE = 320.0d
double CAMERA_NEAR_CLIP = 0.1d
double CAMERA_FAR_CLIP = 10000.0d
double AXIS_LENGTH = 250.0d
double HYDROGEN_ANGLE = 104.5d
double CONTROL_MULTIPLIER = 0.1d
double SHIFT_MULTIPLIER = 10.0d
double MOUSE_SPEED = 0.1d
double ROTATION_SPEED = 2.0d
double TRACK_SPEED = 0.3d
double mousePosX
double mousePosY
double mouseOldX
double mouseOldY
double mouseDeltaX
double mouseDeltaY
func dpdl_javafx_start(object stage)
stage.setTitle("Dpdl Sample Application (Molecule 3D)")
stage.setScene(scene)
stage.show()
scene.setCamera(camera)
end
func handleKeyEvent(object key)
object key_code = key.getCode()
int code = key_code.getCode()
if(code == 90)
cam2Trans.setX(0.0d)
cam2Trans.setY(0.0d)
camera.setTranslateZ(CAMERA_INITIAL_DISTANCE)
camRotY.setAngle(CAMERA_INITIAL_Y_ANGLE)
camRotX.setAngle(CAMERA_INITIAL_X_ANGLE)
elseif(code == 88)
axisGroup.setVisible(!axisGroup.isVisible())
elseif(code == 86)
moleculeGroup.setVisible(!moleculeGroup.isVisible())
else
println("unkown key command: " + code + " press 'Z' 'V' or 'X' ")
fi
end
func handleMouseEvent(object me)
mouseOldX = mousePosX
mouseOldY = mousePosY
mousePosX = me.getSceneX()
mousePosY = me.getSceneY()
mouseDeltaX = (mousePosX - mouseOldX)
mouseDeltaY = (mousePosY - mouseOldY)
double modifier = 1.0d
if (me.isControlDown())
modifier = CONTROL_MULTIPLIER
fi
if (me.isShiftDown())
modifier = SHIFT_MULTIPLIER
fi
if (me.isPrimaryButtonDown())
camRotY.setAngle(camRotY.getAngle() - (mouseDeltaX * MOUSE_SPEED * modifier * ROTATION_SPEED))
camRotX.setAngle(camRotX.getAngle() + (mouseDeltaY * MOUSE_SPEED * modifier * ROTATION_SPEED))
elseif (me.isSecondaryButtonDown())
double z = camera.getTranslateZ()
double newZ = z + (mouseDeltaX * MOUSE_SPEED * modifier)
camera.setTranslateZ(newZ)
elseif (me.isMiddleButtonDown())
cam2Trans.setX(cam2Trans.getX() + (mouseDeltaX * MOUSE_SPEED * modifier * TRACK_SPEED))
cam2Trans.setY(cam2Trans.getY() + (mouseDeltaY * MOUSE_SPEED * modifier * TRACK_SPEED))
else
println("unknown mouse command")
fi
end
func buildCamera()
println("constructing camera...")
object root_child = root.getChildren()
root_child.add(cameraXform)
object cameraxform_child = cameraXform.getChildren()
cameraxform_child.add(cameraXform2)
object cameraxform2_child = cameraXform2.getChildren()
cameraxform2_child.add(cameraXform3)
object cameraxform3_child = cameraXform3.getChildren()
cameraxform3_child.add(camera)
cameraXform3.setRotateZ(180.0d)
camera.setNearClip(CAMERA_NEAR_CLIP)
camera.setFarClip(CAMERA_FAR_CLIP)
camera.setTranslateZ(CAMERA_INITIAL_DISTANCE)
cameraXform.setAngleRotRY(CAMERA_INITIAL_Y_ANGLE)
cameraXform.setAngleRotRX(CAMERA_INITIAL_X_ANGLE)
end
func buildAxes()
println("constructing axes...")
object color = getClass("paint.Color")
object redMaterial = loadObj("PhongMaterial")
redMaterial.setDiffuseColor(color.DARKRED)
redMaterial.setSpecularColor(color.RED)
object greenMaterial = loadObj("PhongMaterial")
greenMaterial.setDiffuseColor(color.DARKGREEN)
greenMaterial.setSpecularColor(color.GREEN)
object blueMaterial = loadObj("PhongMaterial")
blueMaterial.setDiffuseColor(color.DARKBLUE)
blueMaterial.setSpecularColor(color.BLUE)
object xAxis = loadObj("shape.Box", AXIS_LENGTH, 1.0d, 1.0d)
object yAxis = loadObj("shape.Box", 1.0d, AXIS_LENGTH, 1.0d)
object zAxis = loadObj("shape.Box", 1.0d, 1.0d, AXIS_LENGTH)
xAxis.setMaterial(redMaterial)
yAxis.setMaterial(greenMaterial)
zAxis.setMaterial(blueMaterial)
axisGroup.addAllTMP3(xAxis, yAxis, zAxis)
bool bv = true
axisGroup.setVisible(bv)
world.addAllTMP1(axisGroup)
end
func buildMolecule()
println("building molecules...")
object color = getClass("paint.Color")
object redMaterial = loadObj("PhongMaterial")
redMaterial.setDiffuseColor(color.DARKRED)
redMaterial.setSpecularColor(color.RED)
object whiteMaterial = loadObj("PhongMaterial")
whiteMaterial.setDiffuseColor(color.WHITE)
whiteMaterial.setSpecularColor(color.LIGHTBLUE)
object greyMaterial = loadObj("PhongMaterial")
greyMaterial.setDiffuseColor(color.DARKGREY)
greyMaterial.setSpecularColor(color.GREY)
object moleculeXform = loadObj("DpdlTestForm")
object oxygenXform = loadObj("DpdlTestForm")
object hydrogen1SideXform = loadObj("DpdlTestForm")
object hydrogen1Xform = loadObj("DpdlTestForm")
object hydrogen2SideXform = loadObj("DpdlTestForm")
object hydrogen2Xform = loadObj("DpdlTestForm")
object oxygenSphere = loadObj("Sphere", 40.0d)
oxygenSphere.setMaterial(redMaterial)
object hydrogen1Sphere = loadObj("Sphere", 30.0d)
hydrogen1Sphere.setMaterial(whiteMaterial)
hydrogen1Sphere.setTranslateX(0.0d)
object hydrogen2Sphere = loadObj("Sphere", 30.0d)
hydrogen2Sphere.setMaterial(whiteMaterial)
hydrogen2Sphere.setTranslateZ(0.0d)
object rotate = getClass("Rotate")
object bond1Cylinder = loadObj("Cylinder", 5.0d, 100.0d)
bond1Cylinder.setMaterial(greyMaterial)
bond1Cylinder.setTranslateX(50.0d)
bond1Cylinder.setRotationAxis(rotate.Z_AXIS)
bond1Cylinder.setRotate(90.0d)
object bond2Cylinder = loadObj("Cylinder", 5.0d, 100.0d)
bond2Cylinder.setMaterial(greyMaterial)
bond2Cylinder.setTranslateX(50.0d)
bond2Cylinder.setRotationAxis(rotate.Z_AXIS)
bond2Cylinder.setRotate(90.0d)
object moleculeXform_child = moleculeXform.getChildren()
moleculeXform_child.add(oxygenXform)
moleculeXform_child.add(hydrogen1SideXform)
moleculeXform_child.add(hydrogen2SideXform)
object oxygenXform_child = oxygenXform.getChildren()
oxygenXform_child.add(oxygenSphere)
object hydrogen1SideXform_child = hydrogen1SideXform.getChildren()
object hydrogen2SideXform_child = hydrogen2SideXform.getChildren()
hydrogen1SideXform_child.add(hydrogen1Xform)
hydrogen2SideXform_child.add(hydrogen2Xform)
object hydrogen1Xform_child = hydrogen1Xform.getChildren()
hydrogen1Xform_child.add(hydrogen1Sphere)
object hydrogen2Xform_child = hydrogen2Xform.getChildren()
hydrogen2Xform_child.add(hydrogen2Sphere)
hydrogen1SideXform_child = hydrogen1SideXform.getChildren()
hydrogen1SideXform_child.add(bond1Cylinder)
hydrogen2SideXform_child = hydrogen2SideXform.getChildren()
hydrogen2SideXform_child.add(bond2Cylinder)
hydrogen1Xform.setTx(100.0d)
hydrogen2Xform.setTx(100.0d)
hydrogen2SideXform.setRotateY(HYDROGEN_ANGLE)
moleculeGroup.addAllTMP1(moleculeXform)
world.addAllTMP1(moleculeGroup)
end
# main
println("starting...")
object runtime = getClass("Platform")
runtime.startup(thread.getRunnable())
object root = loadObj("scene.Group")
object axisGroup = loadObj("DpdlTestForm")
object moleculeGroup = loadObj("DpdlTestForm")
object world = loadObj("DpdlTestForm")
bool b = true
object camera = loadObj("PerspectiveCamera", b)
object cameraXform = loadObj("DpdlTestForm")
object cameraXform2 = loadObj("DpdlTestForm")
object cameraXform3 = loadObj("DpdlTestForm")
object depthtest = getClass("DepthTest")
object color = getClass("paint.Color")
object root_child = root.getChildren()
root_child.add(world)
root.setDepthTest(depthtest.ENABLE)
println("constructing scene...")
buildCamera()
buildAxes()
buildMolecule()
println("loading scene...")
raise(root, "root Group scene is null")
bool bs = true
object scene = loadObj("Scene", root, 1024.0d, 768.0d, bs)
scene.setFill(color.GREY)
println("starting App....")
object app = loadObj("DpdlJavaFXApp")
app.initApp()
# mouse & key event handling
object camRotY = cameraXform.ry
object camRotX = cameraXform.rx
object cam2Trans = cameraXform2.t
object mouse_event_handler = loadObj("MouseEventHandler")
scene.setOnMouseDragged(mouse_event_handler)
object key_event_handler = loadObj("KeyEventHandler")
scene.setOnKeyPressed(key_event_handler)