/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * 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 java.nio.FloatBuffer; import javax.vecmath.Color4f; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; import org.simantics.proconf.g3d.base.VecmathJmeTools; import com.jme.bounding.BoundingBox; import com.jme.renderer.ColorRGBA; 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 TransformInlineGizmo extends AbstractGizmo { TriMesh geom = new TriMesh(); ColorRGBA colors[] = new ColorRGBA[2]; public static String PICK_NAME = "translate"; public static String X_NAME = "ax"; public static final int X = 0; public static final int SELECTED = 1; public static final int UNSELECTED = 0; private boolean selected = false; /* (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; return -1; } private void updateColor(int sel) { FloatBuffer buff = geom.getColorBuffer(0); for (int i = 0; i < geom.getBatch(0).getVertexCount(); i++) { BufferUtils.setInBuffer(colors[sel], buff, i); } } /* (non-Javadoc) * @see fi.vtt.proconf.shapeeditor.common.Gizmo#changeSelected(java.lang.String) */ public void setSelected(String name) { if (name == null) { setChanged(true); updateColor(UNSELECTED); selected = false; return; } int index = getIndexForName(name); if (index == -1) { selected = false; return; } selected = true; updateColor(SELECTED); setChanged(true); } @Override public void setScale(Vector3f scale) { /** * A bug in JME (?) * Negative scaling breaks picking (ray hits the bounding box, but ray doesn't intersect any triangles) */ scale.x = Math.abs(scale.x); scale.y = Math.abs(scale.y); scale.z = Math.abs(scale.z); super.setScale(scale); } /* public TransformInlineGizmo(){ */ public TransformInlineGizmo(Renderer renderer) { super(); float size = 2.f; float sizeD2 = 1.f; float offset = 0.07f; Color4f colorx = new Color4f(0.5f,0.f,0.f,0.5f); Color4f scolorx = new Color4f(1.f,0.f,0.f,0.7f); float coordinates[] = new float[18*3]; float cols[] = new float[18*4]; int[] indices = new int[]{0,2,1, 0,3,2, 0,4,3, 0,1,4, 1,2,6, 1,6,5, 2,3,7, 2,7,6, 3,4,8, 3,8,7, 4,1,5, 4,5,8, 5,6,7, 5,7,8, 9,11,10, 9,12,11, 9,13,12, 9,10,13, 10,11,15, 10,15,14, 11,12,16, 11,16,15, 12,13,17, 12,17,16, 13,10,14, 13,14,17, 14,15,16, 14,16,17 }; for (int i = 0; i < 18; i++) setColor(cols,i, colorx); setCoordinate(coordinates,0, new Point3f(size, 0.f, 0.f)); setCoordinate(coordinates,1, new Point3f(size - offset*2, offset, offset)); setCoordinate(coordinates,2, new Point3f(size - offset*2, offset, -offset)); setCoordinate(coordinates,3, new Point3f(size - offset*2, -offset, -offset)); setCoordinate(coordinates,4, new Point3f(size - offset*2, -offset, offset)); setCoordinate(coordinates,5, new Point3f(sizeD2, offset, offset)); setCoordinate(coordinates,6, new Point3f(sizeD2, offset, -offset)); setCoordinate(coordinates,7, new Point3f(sizeD2, -offset, -offset)); setCoordinate(coordinates,8, new Point3f(sizeD2, -offset, offset)); setCoordinate(coordinates,9, new Point3f(-size, 0.f, 0.f)); setCoordinate(coordinates,10, new Point3f(-size + offset*2, offset, offset)); setCoordinate(coordinates,11, new Point3f(-size + offset*2, offset, -offset)); setCoordinate(coordinates,12, new Point3f(-size + offset*2, -offset, -offset)); setCoordinate(coordinates,13, new Point3f(-size + offset*2, -offset, offset)); setCoordinate(coordinates,14, new Point3f(-sizeD2, offset, offset)); setCoordinate(coordinates,15, new Point3f(-sizeD2, offset, -offset)); setCoordinate(coordinates,16, new Point3f(-sizeD2, -offset, -offset)); setCoordinate(coordinates,17, new Point3f(-sizeD2, -offset, offset)); TriMesh linex = new TriMesh(PICK_NAME+X_NAME,BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices)); getGizmoNode().attachChild(linex); getGizmoNode().setModelBound(new BoundingBox()); getGizmoNode().updateModelBound(); geom = linex; geom.getBatch(0).setCastsShadows(false); colors[0] = VecmathJmeTools.get(colorx); colors[1] = VecmathJmeTools.get(scolorx); 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); getGizmoNode().setRenderState(zs); } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }