--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.gizmo;\r
+\r
+import javax.vecmath.AxisAngle4f;\r
+import javax.vecmath.Color4f;\r
+import javax.vecmath.Quat4f;\r
+import javax.vecmath.Tuple3d;\r
+import javax.vecmath.Tuple3f;\r
+import javax.vecmath.Vector3d;\r
+import javax.vecmath.Vector3f;\r
+\r
+import org.simantics.proconf.g3d.Activator;\r
+import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
+import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
+import org.simantics.proconf.g3d.preferences.PreferenceConstants;\r
+\r
+import com.jme.renderer.Renderer;\r
+import com.jme.scene.Node;\r
+\r
+\r
+public abstract class AbstractGizmo implements Gizmo{\r
+ \r
+ private Node position;\r
+ private Node rotate;\r
+ private Node scale;\r
+ \r
+ private boolean changed = false;\r
+ \r
+ private double userScale = 1.0;\r
+ \r
+ public AbstractGizmo() {\r
+ createGroups();\r
+ }\r
+ \r
+ public void setChanged(boolean b) {\r
+ changed = b;\r
+ }\r
+ \r
+ public boolean isChanged() {\r
+ return changed;\r
+ }\r
+ \r
+ public double getUserScale() {\r
+ return Activator.getDefault().getPreferenceStore().getDouble(PreferenceConstants.GIZMO_SCALE);\r
+ }\r
+ \r
+ \r
+ public void setScale(float scale) {\r
+ this.scale.setLocalScale(scale);\r
+ }\r
+ \r
+ public void setScale(Vector3f scale) {\r
+ this.scale.setLocalScale(VecmathJmeTools.get(scale));\r
+ }\r
+ \r
+ public Vector3d getPosition() {\r
+ return VecmathJmeTools.getD(position.getWorldTranslation());\r
+ }\r
+ \r
+ public Vector3f getPositionFloat() {\r
+ return VecmathJmeTools.get(position.getWorldTranslation());\r
+ }\r
+ \r
+ public void setPosition(Tuple3d position) {\r
+ this.position.setLocalTranslation(VecmathJmeTools.get(position));\r
+ }\r
+ \r
+ public void setPosition(Vector3f position) {\r
+ this.position.setLocalTranslation(VecmathJmeTools.get(position));\r
+ }\r
+ \r
+ public void setRotation(Quat4f q) {\r
+ rotate.setLocalRotation(VecmathJmeTools.get(q));\r
+ }\r
+ \r
+ public void setRotation(AxisAngle4f q) {\r
+ rotate.setLocalRotation(VecmathJmeTools.get(q));\r
+ }\r
+ \r
+ public Node getNode() {\r
+ userScale = getUserScale();\r
+ return position;\r
+ }\r
+ \r
+ protected Node getGizmoNode() {\r
+ return scale;\r
+ }\r
+ \r
+ private void createGroups() {\r
+\r
+ position = new Node();\r
+ rotate = new Node();\r
+ scale = new Node();\r
+ position.attachChild(rotate);\r
+ rotate.attachChild(scale);\r
+ position.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);\r
+ }\r
+\r
+ public void update(Tuple3d position, Tuple3d cameraPosition, JmeRenderingComponent component) {\r
+ setPosition(position);\r
+ com.jme.math.Vector3f p = VecmathJmeTools.get(position);\r
+ p.subtractLocal(VecmathJmeTools.get(cameraPosition));\r
+ rotate.getLocalRotation().inverse().multLocal(p);\r
+ if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {\r
+\r
+ double distance = p.length();\r
+ // (bug caused in Xith->JME translation ?)\r
+ p.negateLocal();\r
+ double fov = component.getFieldOfView();\r
+ float s = (float) (Math.sin(fov) * distance * 0.1); // scaling factor was 0.2 with Xith\r
+ s *= (float)userScale;\r
+ Vector3f scale = new Vector3f(1.f, 1.f, 1.f);\r
+ \r
+ if (p.x > 0.f)\r
+ scale.x = -1.f;\r
+ if (p.y > 0.f)\r
+ scale.y = -1.f;\r
+ if (p.z > 0.f)\r
+ scale.z = -1.f;\r
+ scale.scale(s);\r
+ setScale(scale);\r
+ } else {\r
+ Vector3f scale = new Vector3f(1.f, 1.f, 1.f);\r
+ float s = component.getScreenScale() / 5.f;\r
+ s *= (float)userScale;\r
+ if (p.x > 0.f)\r
+ scale.x = -1.f;\r
+ if (p.y > 0.f)\r
+ scale.y = -1.f;\r
+ if (p.z > 0.f)\r
+ scale.z = -1.f;\r
+ scale.scale(s);\r
+ setScale(scale);\r
+\r
+ }\r
+ }\r
+\r
+ public void update(Tuple3d cameraPosition, JmeRenderingComponent component) {\r
+\r
+ com.jme.math.Vector3f p = VecmathJmeTools.get(getPosition());\r
+ p.subtractLocal(VecmathJmeTools.get(cameraPosition));\r
+ rotate.getLocalRotation().inverse().multLocal(p);\r
+ if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {\r
+\r
+ double distance = p.length();\r
+ double fov = component.getFieldOfView();\r
+ float s = (float)(Math.sin(fov) * distance * 0.1); // scaling factor was 0.2 with Xith\r
+ s *= (float)userScale;\r
+ Vector3f scale = new Vector3f(1.f,1.f,1.f);\r
+ if (p.x > 0.f)\r
+ scale.x = -1.f;\r
+ if (p.y > 0.f)\r
+ scale.y = -1.f;\r
+ if (p.z > 0.f)\r
+ scale.z = -1.f;\r
+ scale.scale(s);\r
+ setScale(scale);\r
+ } else {\r
+ \r
+ Vector3f scale = new Vector3f(1.f,1.f,1.f);\r
+ float s = component.getScreenScale()/5.f;\r
+ s *= (float)userScale;\r
+ if (p.x > 0.f)\r
+ scale.x = -1.f;\r
+ if (p.y > 0.f)\r
+ scale.y = -1.f;\r
+ if (p.z > 0.f)\r
+ scale.z = -1.f;\r
+ scale.scale(s);\r
+ setScale(scale);\r
+ \r
+ }\r
+ }\r
+ \r
+ protected void setCoordinate(float array[], int index, Tuple3f c) {\r
+ index *= 3;\r
+ array[index++] = c.x;\r
+ array[index++] = c.y;\r
+ array[index] = c.z;\r
+ }\r
+ \r
+ protected void setColor(float array[], int index, Color4f c) {\r
+ index *= 4;\r
+ array[index++] = c.x;\r
+ array[index++] = c.y;\r
+ array[index++] = c.z;\r
+ array[index] = c.w;\r
+ }\r
+}\r