1 16 17 package com.buchuki.ensmer; 18 19 import com.sun.j3d.utils.picking.PickTool; 20 import java.util.*; 21 import javax.media.j3d.*; 22 import javax.vecmath.*; 23 24 30 public class SceneGraphUtils { 31 32 35 private SceneGraphUtils() { } 36 37 45 public static void enableEnsmerCapabilities(Node node) { 46 47 if (node instanceof Shape3D) { 48 PickTool.setCapabilities(node, PickTool.INTERSECT_TEST); 49 ((Shape3D) node).setCapability(Shape3D.ALLOW_APPEARANCE_READ); 50 Appearance app = ((Shape3D) node).getAppearance(); 51 if (app != null) { 52 app.setCapability(Appearance.ALLOW_MATERIAL_READ); 53 Material matt = app.getMaterial(); 54 if (matt != null) { 55 matt.setCapability(Material.ALLOW_COMPONENT_READ); 56 matt.setCapability(Material.ALLOW_COMPONENT_WRITE); 57 } 58 } 59 } 60 else if (node instanceof Group) { 61 Enumeration en = ((Group) node).getAllChildren(); 62 while (en.hasMoreElements()) { 63 enableEnsmerCapabilities((Node) en.nextElement()); 64 } 65 } 66 } 67 68 79 public static int greaterBinaryPower(int value, int max) { 80 int pow = 2; 81 int newWidth = -1; 82 for (int i = 1; i < max; ++i, pow*=2) { 83 if (value <= pow) { 84 newWidth = pow; 85 break; 86 } 87 } 88 return newWidth == -1 ? pow : newWidth; 89 } 90 91 92 99 @SuppressWarnings ({"unchecked"}) 100 public static Collection getAppearanceNodes(Node node) { 101 ArrayList<Appearance> ret = new ArrayList<Appearance>(); 102 if (node instanceof Shape3D) { 103 Appearance app = ((Shape3D) node).getAppearance(); 104 if (app != null) { 105 ret.add(app); 106 } 107 } 108 else if (node instanceof Group) { 109 Enumeration en = ((Group) node).getAllChildren(); 110 while (en.hasMoreElements()) { 111 ret.addAll(getAppearanceNodes((Node) en.nextElement())); 112 } 113 } 114 return ret; 115 } 116 117 123 public static Matrix4f positionInFrontOfUser(Area area) { 124 return positionInFrontOfUser(area, 2); 125 } 126 127 134 public static Matrix4f positionInFrontOfUser(Area area, float distance) { 135 Vector3f addition = new Vector3f(0, 0, -1 * distance); 136 return transformToUser(area, addition); 137 } 138 139 148 public static Matrix4f randomPositionInFrontOfUser(Area area) { 149 return randomPositionInFrontOfUser(area, 5f); 150 } 151 152 162 public static Matrix4f randomPositionInFrontOfUser(Area area, float maxDistance) { 163 Vector3f addition = new Vector3f((float) (3f * Math.random() - 1.5f), 164 (float) (3f * Math.random() - 1.5f), 165 (float) (3f * Math.random() - (maxDistance + 3))); 166 return transformToUser(area, addition); 167 } 168 169 178 private static Matrix4f transformToUser(Area area, Vector3f vector) { 179 Matrix4f userPos = area.getUserPosition(); 180 Matrix3f userRot = new Matrix3f(); 181 userPos.getRotationScale(userRot); 182 Vector3f userVec = new Vector3f(); 183 userPos.get(userVec); 184 userPos.transform(vector); 185 userVec.add(vector); 186 Transform3D trans = new Transform3D(); 187 trans.setTranslation(userVec); 188 trans.setRotation(userRot); 189 Matrix4f loc = new Matrix4f(); 190 trans.get(loc); 191 return loc; 192 } 193 } | Popular Tags |