1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.gizmo;
\r
13 import javax.vecmath.AxisAngle4f;
\r
14 import javax.vecmath.Color4f;
\r
15 import javax.vecmath.Quat4f;
\r
16 import javax.vecmath.Tuple3d;
\r
17 import javax.vecmath.Tuple3f;
\r
18 import javax.vecmath.Vector3d;
\r
19 import javax.vecmath.Vector3f;
\r
21 import org.simantics.proconf.g3d.Activator;
\r
22 import org.simantics.proconf.g3d.base.JmeRenderingComponent;
\r
23 import org.simantics.proconf.g3d.base.VecmathJmeTools;
\r
24 import org.simantics.proconf.g3d.preferences.PreferenceConstants;
\r
26 import com.jme.renderer.Renderer;
\r
27 import com.jme.scene.Node;
\r
30 public abstract class AbstractGizmo implements Gizmo{
\r
32 private Node position;
\r
33 private Node rotate;
\r
36 private boolean changed = false;
\r
38 private double userScale = 1.0;
\r
40 public AbstractGizmo() {
\r
44 public void setChanged(boolean b) {
\r
48 public boolean isChanged() {
\r
52 public double getUserScale() {
\r
53 return Activator.getDefault().getPreferenceStore().getDouble(PreferenceConstants.GIZMO_SCALE);
\r
57 public void setScale(float scale) {
\r
58 this.scale.setLocalScale(scale);
\r
61 public void setScale(Vector3f scale) {
\r
62 this.scale.setLocalScale(VecmathJmeTools.get(scale));
\r
65 public Vector3d getPosition() {
\r
66 return VecmathJmeTools.getD(position.getWorldTranslation());
\r
69 public Vector3f getPositionFloat() {
\r
70 return VecmathJmeTools.get(position.getWorldTranslation());
\r
73 public void setPosition(Tuple3d position) {
\r
74 this.position.setLocalTranslation(VecmathJmeTools.get(position));
\r
77 public void setPosition(Vector3f position) {
\r
78 this.position.setLocalTranslation(VecmathJmeTools.get(position));
\r
81 public void setRotation(Quat4f q) {
\r
82 rotate.setLocalRotation(VecmathJmeTools.get(q));
\r
85 public void setRotation(AxisAngle4f q) {
\r
86 rotate.setLocalRotation(VecmathJmeTools.get(q));
\r
89 public Node getNode() {
\r
90 userScale = getUserScale();
\r
94 protected Node getGizmoNode() {
\r
98 private void createGroups() {
\r
100 position = new Node();
\r
101 rotate = new Node();
\r
102 scale = new Node();
\r
103 position.attachChild(rotate);
\r
104 rotate.attachChild(scale);
\r
105 position.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
\r
108 public void update(Tuple3d position, Tuple3d cameraPosition, JmeRenderingComponent component) {
\r
109 setPosition(position);
\r
110 com.jme.math.Vector3f p = VecmathJmeTools.get(position);
\r
111 p.subtractLocal(VecmathJmeTools.get(cameraPosition));
\r
112 rotate.getLocalRotation().inverse().multLocal(p);
\r
113 if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {
\r
115 double distance = p.length();
\r
116 // (bug caused in Xith->JME translation ?)
\r
118 double fov = component.getFieldOfView();
\r
119 float s = (float) (Math.sin(fov) * distance * 0.1); // scaling factor was 0.2 with Xith
\r
120 s *= (float)userScale;
\r
121 Vector3f scale = new Vector3f(1.f, 1.f, 1.f);
\r
132 Vector3f scale = new Vector3f(1.f, 1.f, 1.f);
\r
133 float s = component.getScreenScale() / 5.f;
\r
134 s *= (float)userScale;
\r
147 public void update(Tuple3d cameraPosition, JmeRenderingComponent component) {
\r
149 com.jme.math.Vector3f p = VecmathJmeTools.get(getPosition());
\r
150 p.subtractLocal(VecmathJmeTools.get(cameraPosition));
\r
151 rotate.getLocalRotation().inverse().multLocal(p);
\r
152 if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {
\r
154 double distance = p.length();
\r
155 double fov = component.getFieldOfView();
\r
156 float s = (float)(Math.sin(fov) * distance * 0.1); // scaling factor was 0.2 with Xith
\r
157 s *= (float)userScale;
\r
158 Vector3f scale = new Vector3f(1.f,1.f,1.f);
\r
169 Vector3f scale = new Vector3f(1.f,1.f,1.f);
\r
170 float s = component.getScreenScale()/5.f;
\r
171 s *= (float)userScale;
\r
184 protected void setCoordinate(float array[], int index, Tuple3f c) {
\r
186 array[index++] = c.x;
\r
187 array[index++] = c.y;
\r
188 array[index] = c.z;
\r
191 protected void setColor(float array[], int index, Color4f c) {
\r
193 array[index++] = c.x;
\r
194 array[index++] = c.y;
\r
195 array[index++] = c.z;
\r
196 array[index] = c.w;
\r