X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fgizmo%2FTransformGizmo.java;fp=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fgizmo%2FTransformGizmo.java;h=8224d7e078be44daf3ad287e6ff17a45ea6ea889;hb=10f144a2bb2d7bec98b812b83acecb333fd098ea;hp=0000000000000000000000000000000000000000;hpb=3055b543aa5afc0cca4bb3b341704e7c5103fa6a;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/proconf/g3d/gizmo/TransformGizmo.java b/org.simantics.g3d/src/org/simantics/proconf/g3d/gizmo/TransformGizmo.java new file mode 100644 index 00000000..8224d7e0 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/proconf/g3d/gizmo/TransformGizmo.java @@ -0,0 +1,316 @@ +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.proconf.g3d.gizmo; + +import javax.vecmath.Color4f; +import javax.vecmath.Point3f; + +import com.jme.bounding.BoundingBox; +import com.jme.renderer.Renderer; +import com.jme.scene.TriMesh; +import com.jme.scene.state.AlphaState; +import com.jme.scene.state.MaterialState; +import com.jme.scene.state.ZBufferState; +import com.jme.util.geom.BufferUtils; + + +public class TransformGizmo extends MultiSelectionGizmo { + + + public static String PICK_NAME = "translate"; + public static String X_NAME = "ax"; + public static String Y_NAME = "ay"; + public static String Z_NAME = "az"; + public static String XZ_NAME = "xz"; + public static String XY_NAME = "xy"; + public static String YZ_NAME = "yz"; + public static String XYZ_NAME = "aa"; + + public static final int X = 0; + public static final int Y = 1; + public static final int Z = 2; + public static final int XY = 3; + public static final int XZ = 4; + public static final int YZ = 5; + public static final int XYZ = 6; + + + + /* (non-Javadoc) + * @see fi.vtt.proconf.shapeeditor.common.Gizmo#getPickPrefix() + */ + public String getPickPrefix() { + return PICK_NAME; + } + + public int getIndexForName(String name) { + if (!name.startsWith(PICK_NAME)) { + return -1; + } + name = name.substring(PICK_NAME.length()); + if (name.startsWith(X_NAME)) + return X; + if (name.startsWith(Y_NAME)) + return Y; + if (name.startsWith(Z_NAME)) + return Z; + if (name.startsWith(XY_NAME)) + return XY; + if (name.startsWith(XZ_NAME)) + return XZ; + if (name.startsWith(YZ_NAME)) + return YZ; + if (name.startsWith(XYZ_NAME)) + return XYZ; + return -1; + } + + @Override + public int getCount() { + return 7; + } + + + /* + public TransformGizmo() { + */ + public TransformGizmo(Renderer renderer) { + super(); + float size = 2.f; + float sizeD2 = 1.f; + float offset = 0.2f; + + Color4f colorx = new Color4f(0.5f,0.f,0.f,0.5f); + Color4f colory = new Color4f(0.f,0.5f,0.f,0.5f); + Color4f colorz = new Color4f(0.f,0.f,0.5f,0.5f); + Color4f scolorx = new Color4f(1.f,0.f,0.f,0.7f); + Color4f scolory = new Color4f(0.f,1.f,0.f,0.7f); + Color4f scolorz = new Color4f(0.f,0.f,1.f,0.7f); + + Color4f colorxy = new Color4f(); + colorxy.x = colorx.x + colory.x; + colorxy.y = colorx.y + colory.y; + colorxy.z = colorx.z + colory.z; + colorxy.w = 0.5f; + + Color4f colorxz = new Color4f(); + colorxz.x = colorx.x + colorz.x; + colorxz.y = colorx.y + colorz.y; + colorxz.z = colorx.z + colorz.z; + colorxz.w = 0.5f; + + Color4f coloryz = new Color4f(); + coloryz.x = colory.x + colorz.x; + coloryz.y = colory.y + colorz.y; + coloryz.z = colory.z + colorz.z; + coloryz.w = 0.5f; + + Color4f colorxyz = new Color4f(); + colorxyz.x = colorx.x + colory.x + colorz.x; + colorxyz.y = colorx.y + colory.y + colorz.y; + colorxyz.z = colorx.z +colory.z + colorz.z; + colorxyz.w = 0.5f; + + Color4f scolorxy = new Color4f(); + scolorxy.x = scolory.x + scolorx.x; + scolorxy.y = scolory.y + scolorx.y; + scolorxy.z = scolory.z + scolorx.z; + scolorxy.w = 0.5f; + + Color4f scolorxz = new Color4f(); + scolorxz.x = scolorx.x + scolorz.x; + scolorxz.y = scolorx.y + scolorz.y; + scolorxz.z = scolorx.z + scolorz.z; + scolorxz.w = 0.5f; + + Color4f scoloryz = new Color4f(); + scoloryz.x = scolory.x + scolorz.x; + scoloryz.y = scolory.y + scolorz.y; + scoloryz.z = scolory.z + scolorz.z; + scoloryz.w = 0.5f; + + Color4f scolorxyz = new Color4f(); + scolorxyz.x = scolorx.x + scolory.x + scolorz.x; + scolorxyz.y = scolorx.y + scolory.y + scolorz.y; + scolorxyz.z = scolorx.z + scolory.z + scolorz.z; + scolorxyz.w = 0.5f; + + + float coordinates[] = new float[6*3]; + float cols[] = new float[6*4]; + int[] indices = new int[]{0,1,3, + 1,2,3, + 0,3,5, + 3,4,5}; + for (int i = 0; i < 6; i++) + setColor(cols,i, colorx); + + setCoordinate(coordinates,0, new Point3f(size, 0.f, 0.f)); + setCoordinate(coordinates,1, new Point3f(size - offset, offset, 0.f)); + setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f)); + setCoordinate(coordinates,3, new Point3f(sizeD2, 0.f, 0.f)); + setCoordinate(coordinates,4, new Point3f(sizeD2 - offset, 0.f, offset)); + setCoordinate(coordinates,5, new Point3f(size - offset, 0.f, offset)); + + TriMesh linex = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + for (int i = 0; i < 6; i++) + setColor(cols,i, colory); + + setCoordinate(coordinates,0, new Point3f(0.f, size, 0.f)); + setCoordinate(coordinates,1, new Point3f(offset, size - offset, 0.f)); + setCoordinate(coordinates,2, new Point3f(offset, sizeD2 - offset, 0.f)); + setCoordinate(coordinates,3, new Point3f(0.f, sizeD2, 0.f)); + setCoordinate(coordinates,4, new Point3f(0.f, sizeD2 - offset, offset)); + setCoordinate(coordinates,5, new Point3f(0.f, size - offset, offset)); + + TriMesh liney = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + for (int i = 0; i < 6; i++) + setColor(cols,i, colorz); + + setCoordinate(coordinates,0, new Point3f(0.f, 0.f,size)); + setCoordinate(coordinates,1, new Point3f(offset, 0.f, size - offset)); + setCoordinate(coordinates,2, new Point3f(offset, 0.f, sizeD2 - offset)); + setCoordinate(coordinates,3, new Point3f(0.f, 0.f, sizeD2)); + setCoordinate(coordinates,4, new Point3f(0.f, offset, sizeD2 - offset)); + setCoordinate(coordinates,5, new Point3f(0.f, offset, size - offset)); + + TriMesh linez = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + // TODO : picking did not work properly without chancing indices; this must be investigated + indices = new int[]{2,1,3,0,1,3};//{0,1,3,1,2,3}; + coordinates = new float[4*3]; + cols = new float[4*4]; + + for (int i = 0; i < 4; i++) + setColor(cols,i, colorxz); + setCoordinate(coordinates,0, new Point3f(offset, 0.f, size-offset)); + setCoordinate(coordinates,1, new Point3f(offset, 0.f, sizeD2 - offset)); + setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, 0.f, offset)); + setCoordinate(coordinates,3, new Point3f(size-offset, 0.f, offset)); + + TriMesh trianglexz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + for (int i = 0; i < 4; i++) + setColor(cols,i, colorxy); + setCoordinate(coordinates,0, new Point3f(offset, size-offset, 0.f)); + setCoordinate(coordinates,1, new Point3f(offset, sizeD2 - offset, 0.f)); + setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f)); + setCoordinate(coordinates,3, new Point3f(size-offset, offset, 0.f)); + + TriMesh trianglexy = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + for (int i = 0; i < 4; i++) + setColor(cols,i, coloryz); + setCoordinate(coordinates,0, new Point3f( 0.f,offset, size-offset)); + setCoordinate(coordinates,1, new Point3f( 0.f,offset, sizeD2 - offset)); + setCoordinate(coordinates,2, new Point3f( 0.f,sizeD2 - offset, offset)); + setCoordinate(coordinates,3, new Point3f( 0.f,size-offset, offset)); + + TriMesh triangleyz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + indices = new int[]{0,1,2, + 0,2,3, + 0,3,4, + 0,4,5, + 0,5,6, + 0,6,7, + 0,7,8, + 0,8,9, + 0,9,1}; + coordinates = new float[10*3]; + cols = new float[10*4]; + + for (int i = 0; i < 10; i++) + setColor(cols,i, colorxyz); + setCoordinate(coordinates,0, new Point3f(0.f, 0.f, 0.f)); + setCoordinate(coordinates,1, new Point3f(sizeD2, 0.f, 0.f)); + setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f)); + setCoordinate(coordinates,3, new Point3f(offset, sizeD2 - offset, 0.f)); + setCoordinate(coordinates,4, new Point3f(0.f, sizeD2, 0.f)); + setCoordinate(coordinates,5, new Point3f(0.f, sizeD2 - offset, offset)); + setCoordinate(coordinates,6, new Point3f(0.f, offset, sizeD2-offset)); + setCoordinate(coordinates,7, new Point3f(0.f, 0.f, sizeD2)); + setCoordinate(coordinates,8, new Point3f(offset, 0.f, sizeD2-offset)); + setCoordinate(coordinates,9, new Point3f(sizeD2-offset, 0.f, offset)); + + TriMesh trianglexyz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); + + linex.setName(PICK_NAME+X_NAME); + liney.setName(PICK_NAME+Y_NAME); + linez.setName(PICK_NAME+Z_NAME); + trianglexy.setName(PICK_NAME+XY_NAME); + trianglexz.setName(PICK_NAME+XZ_NAME); + triangleyz.setName(PICK_NAME+YZ_NAME); + trianglexyz.setName(PICK_NAME+XYZ_NAME); + + linex.getBatch(0).setCastsShadows(false); + liney.getBatch(0).setCastsShadows(false); + linez.getBatch(0).setCastsShadows(false); + trianglexy.getBatch(0).setCastsShadows(false); + trianglexz.getBatch(0).setCastsShadows(false); + triangleyz.getBatch(0).setCastsShadows(false); + trianglexyz.getBatch(0).setCastsShadows(false); + + getGizmoNode().attachChild(linex); + getGizmoNode().attachChild(liney); + getGizmoNode().attachChild(linez); + getGizmoNode().attachChild(trianglexy); + getGizmoNode().attachChild(trianglexz); + getGizmoNode().attachChild(triangleyz); + getGizmoNode().attachChild(trianglexyz); + getGizmoNode().setModelBound(new BoundingBox()); + getGizmoNode().updateModelBound(); + + setGeometry(0,linex); + setGeometry(1,liney); + setGeometry(2,linez); + setGeometry(3,trianglexy); + setGeometry(4,trianglexz); + setGeometry(5,triangleyz); + setGeometry(6,trianglexyz); + + + setColor(0,0,colorx); + setColor(0,1,scolorx); + setColor(1,0,colory); + setColor(1,1,scolory); + setColor(2,0,colorz); + setColor(2,1,scolorz); + setColor(3,0,colorxy); + setColor(3,1,scolorxy); + setColor(4,0,colorxz); + setColor(4,1,scolorxz); + setColor(5,0,coloryz); + setColor(5,1,scoloryz); + setColor(6,0,colorxyz); + setColor(6,1,scolorxyz); + + + AlphaState as = renderer.createAlphaState(); + as.setBlendEnabled(true); + as.setSrcFunction(AlphaState.DB_SRC_ALPHA); + as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA); + as.setEnabled(true); + getGizmoNode().setRenderState(as); + MaterialState ms = renderer.createMaterialState(); + ms.setColorMaterial(MaterialState.CM_AMBIENT_AND_DIFFUSE); + ms.setMaterialFace(MaterialState.MF_FRONT_AND_BACK); + getGizmoNode().setRenderState(ms); + ZBufferState zs = renderer.createZBufferState(); + zs.setFunction(ZBufferState.CF_ALWAYS); + zs.setEnabled(true); + zs.setWritable(false); + getGizmoNode().setRenderState(zs); + } + +} \ No newline at end of file