From 4721c944a296835382fa8cbdfc0b4c2756d77d19 Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Wed, 15 Feb 2012 04:21:51 +0000 Subject: [PATCH] * Minor warning fix in BoundingBox, BIHTree * Fix BoundingSphere, calculation was bugged due to TempVars sharing * Add BoundingSphere vs. Triangle collision * Delete Natives class from core as it was moved to desktop previously * Parser classes now use whitespace instead of " " for splitting * Bone.getAttachmentsNode() no longer public due to misuse in user code git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9151 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/texture/plugins/HDRLoader.java | 2 +- engine/src/core/com/jme3/animation/Bone.java | 2 +- .../core/com/jme3/bounding/BoundingBox.java | 2 +- .../com/jme3/bounding/BoundingSphere.java | 35 +- .../core/com/jme3/collision/bih/BIHTree.java | 15 +- engine/src/core/com/jme3/system/Natives.java | 306 ------------------ .../scene/plugins/ogre/MaterialLoader.java | 6 +- .../jme3/scene/plugins/ogre/MeshLoader.java | 2 +- 8 files changed, 40 insertions(+), 330 deletions(-) delete mode 100644 engine/src/core/com/jme3/system/Natives.java diff --git a/engine/src/core-plugins/com/jme3/texture/plugins/HDRLoader.java b/engine/src/core-plugins/com/jme3/texture/plugins/HDRLoader.java index c66c5e503..4758ecf57 100644 --- a/engine/src/core-plugins/com/jme3/texture/plugins/HDRLoader.java +++ b/engine/src/core-plugins/com/jme3/texture/plugins/HDRLoader.java @@ -237,7 +237,7 @@ public class HDRLoader implements AssetLoader { continue; // comment or empty statement } else if (ln.startsWith("+") || ln.startsWith("-")){ // + or - mark image resolution and start of data - String[] resData = ln.split(" "); + String[] resData = ln.split("\\s"); if (resData.length != 4){ throw new IOException("Invalid resolution string in HDR file"); } diff --git a/engine/src/core/com/jme3/animation/Bone.java b/engine/src/core/com/jme3/animation/Bone.java index 135d7a5a8..e7e9e2b15 100644 --- a/engine/src/core/com/jme3/animation/Bone.java +++ b/engine/src/core/com/jme3/animation/Bone.java @@ -485,7 +485,7 @@ public final class Bone implements Savable { * Attach models and effects to this node to make * them follow this bone's motions. */ - public Node getAttachmentsNode() { + Node getAttachmentsNode() { if (attachNode == null) { attachNode = new Node(name + "_attachnode"); attachNode.setUserData("AttachedBone", this); diff --git a/engine/src/core/com/jme3/bounding/BoundingBox.java b/engine/src/core/com/jme3/bounding/BoundingBox.java index 9dde01533..bc1141118 100644 --- a/engine/src/core/com/jme3/bounding/BoundingBox.java +++ b/engine/src/core/com/jme3/bounding/BoundingBox.java @@ -180,7 +180,7 @@ public class BoundingBox extends BoundingVolume { vars.release(); } - public static final void checkMinMax(Vector3f min, Vector3f max, Vector3f point) { + public static void checkMinMax(Vector3f min, Vector3f max, Vector3f point) { if (point.x < min.x) { min.x = point.x; } diff --git a/engine/src/core/com/jme3/bounding/BoundingSphere.java b/engine/src/core/com/jme3/bounding/BoundingSphere.java index 5bb9a5d1e..12be035ff 100644 --- a/engine/src/core/com/jme3/bounding/BoundingSphere.java +++ b/engine/src/core/com/jme3/bounding/BoundingSphere.java @@ -207,12 +207,12 @@ public class BoundingSphere extends BoundingVolume { * in points. */ private void recurseMini(FloatBuffer points, int p, int b, int ap) { - TempVars vars = TempVars.get(); + //TempVars vars = TempVars.get(); - Vector3f tempA = vars.vect1; - Vector3f tempB = vars.vect2; - Vector3f tempC = vars.vect3; - Vector3f tempD = vars.vect4; + Vector3f tempA = new Vector3f(); //vars.vect1; + Vector3f tempB = new Vector3f(); //vars.vect2; + Vector3f tempC = new Vector3f(); //vars.vect3; + Vector3f tempD = new Vector3f(); //vars.vect4; switch (b) { case 0: @@ -240,7 +240,7 @@ public class BoundingSphere extends BoundingVolume { BufferUtils.populateFromBuffer(tempC, points, ap - 3); BufferUtils.populateFromBuffer(tempD, points, ap - 4); setSphere(tempA, tempB, tempC, tempD); - vars.release(); + //vars.release(); return; } for (int i = 0; i < p; i++) { @@ -253,10 +253,9 @@ public class BoundingSphere extends BoundingVolume { BufferUtils.setInBuffer(tempB, points, j - 1 + ap); } recurseMini(points, i, b + 1, ap + 1); - } } - vars.release(); + //vars.release(); } /** @@ -744,7 +743,7 @@ public class BoundingSphere extends BoundingVolume { * * @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray) */ - public int collideWithRay(Ray ray, CollisionResults results) { + private int collideWithRay(Ray ray, CollisionResults results) { TempVars vars = TempVars.get(); Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal( @@ -792,11 +791,27 @@ public class BoundingSphere extends BoundingVolume { return 1; } } - + public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); + } else if (other instanceof Triangle){ + Triangle t = (Triangle) other; + + float r2 = radius * radius; + float d1 = center.distanceSquared(t.get1()); + float d2 = center.distanceSquared(t.get2()); + float d3 = center.distanceSquared(t.get3()); + + if (d1 <= r2 || d2 <= r2 || d3 <= r2) { + CollisionResult r = new CollisionResult(); + r.setDistance(FastMath.sqrt(Math.min(Math.min(d1, d2), d3)) - radius); + results.addCollision(r); + return 1; + } + + return 0; } else { throw new UnsupportedCollisionException(); } diff --git a/engine/src/core/com/jme3/collision/bih/BIHTree.java b/engine/src/core/com/jme3/collision/bih/BIHTree.java index 76365e5c4..36609224e 100644 --- a/engine/src/core/com/jme3/collision/bih/BIHTree.java +++ b/engine/src/core/com/jme3/collision/bih/BIHTree.java @@ -67,15 +67,16 @@ public class BIHTree implements CollisionData { private int numTris; private float[] pointData; private int[] triIndices; + private transient CollisionResults boundResults = new CollisionResults(); private transient float[] bihSwapTmp; - private static final TriangleAxisComparator[] comparators = new TriangleAxisComparator[3]; - - static { - comparators[0] = new TriangleAxisComparator(0); - comparators[1] = new TriangleAxisComparator(1); - comparators[2] = new TriangleAxisComparator(2); - } + + private static final TriangleAxisComparator[] comparators = new TriangleAxisComparator[] + { + new TriangleAxisComparator(0), + new TriangleAxisComparator(1), + new TriangleAxisComparator(2) + }; private void initTriList(FloatBuffer vb, IndexBuffer ib) { pointData = new float[numTris * 3 * 3]; diff --git a/engine/src/core/com/jme3/system/Natives.java b/engine/src/core/com/jme3/system/Natives.java deleted file mode 100644 index 979c606be..000000000 --- a/engine/src/core/com/jme3/system/Natives.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright (c) 2009-2010 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package com.jme3.system; - -import java.io.*; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Helper class for extracting the natives (dll, so) from the jars. - * This class should only be used internally. - */ -public final class Natives { - - private static final Logger logger = Logger.getLogger(Natives.class.getName()); - private static final byte[] buf = new byte[1024]; - private static File extractionDirOverride = null; - private static File extractionDir = null; - - public static void setExtractionDir(String name) { - extractionDirOverride = new File(name).getAbsoluteFile(); - } - - public static File getExtractionDir() { - if (extractionDirOverride != null) { - return extractionDirOverride; - } - if (extractionDir == null) { - File workingFolder = new File("").getAbsoluteFile(); - if (!workingFolder.canWrite()) { - setStorageExtractionDir(); - } else { - try { - File file = new File(workingFolder.getAbsolutePath() + File.separator + ".jmetestwrite"); - file.createNewFile(); - file.delete(); - extractionDir = workingFolder; - } catch (Exception e) { - setStorageExtractionDir(); - } - } - } - return extractionDir; - } - - private static void setStorageExtractionDir() { - logger.log(Level.WARNING, "Working directory is not writable. Using home directory instead."); - extractionDir = new File(JmeSystem.getStorageFolder(), - "natives_" + Integer.toHexString(computeNativesHash())); - if (!extractionDir.exists()) { - extractionDir.mkdir(); - } - } - - private static int computeNativesHash() { - try { - String classpath = System.getProperty("java.class.path"); - URL url = Thread.currentThread().getContextClassLoader().getResource("com/jme3/system/Natives.class"); - - StringBuilder sb = new StringBuilder(url.toString()); - if (sb.indexOf("jar:") == 0) { - sb.delete(0, 4); - sb.delete(sb.indexOf("!"), sb.length()); - sb.delete(sb.lastIndexOf("/") + 1, sb.length()); - } - try { - url = new URL(sb.toString()); - } catch (MalformedURLException ex) { - throw new UnsupportedOperationException(ex); - } - - URLConnection conn = url.openConnection(); - int hash = classpath.hashCode() ^ (int) conn.getLastModified(); - return hash; - } catch (IOException ex) { - throw new UnsupportedOperationException(ex); - } - } - - public static void extractNativeLib(String sysName, String name) throws IOException { - extractNativeLib(sysName, name, false, true); - } - - public static void extractNativeLib(String sysName, String name, boolean load) throws IOException { - extractNativeLib(sysName, name, load, true); - } - - public static void extractNativeLib(String sysName, String name, boolean load, boolean warning) throws IOException { - String fullname = System.mapLibraryName(name); - - String path = "native/" + sysName + "/" + fullname; - URL url = Thread.currentThread().getContextClassLoader().getResource(path); - - if (url == null) { - if (!warning) { - logger.log(Level.WARNING, "Cannot locate native library: {0}/{1}", - new String[]{sysName, fullname}); - } - return; - } - - URLConnection conn = url.openConnection(); - InputStream in = conn.getInputStream(); - File targetFile = new File(getExtractionDir(), fullname); - OutputStream out = null; - try { - if (targetFile.exists()) { - // OK, compare last modified date of this file to - // file in jar - long targetLastModified = targetFile.lastModified(); - long sourceLastModified = conn.getLastModified(); - - // Allow ~1 second range for OSes that only support low precision - if (targetLastModified + 1000 > sourceLastModified) { - logger.log(Level.FINE, "Not copying library {0}. Latest already extracted.", fullname); - return; - } - } - - out = new FileOutputStream(targetFile); - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - in = null; - out.close(); - out = null; - - // NOTE: On OSes that support "Date Created" property, - // this will cause the last modified date to be lower than - // date created which makes no sense - targetFile.setLastModified(conn.getLastModified()); - } catch (FileNotFoundException ex) { - if (ex.getMessage().contains("used by another process")) { - return; - } - - throw ex; - } finally { - if (load) { - System.load(targetFile.getAbsolutePath()); - } - if(in != null){ - in.close(); - } - if(out != null){ - out.close(); - } - } - logger.log(Level.FINE, "Copied {0} to {1}", new Object[]{fullname, targetFile}); - } - - protected static boolean isUsingNativeBullet() { - try { - Class clazz = Class.forName("com.jme3.bullet.util.NativeMeshUtil"); - return clazz != null; - } catch (ClassNotFoundException ex) { - return false; - } - } - - public static void extractNativeLibs(Platform platform, AppSettings settings) throws IOException { - String renderer = settings.getRenderer(); - String audioRenderer = settings.getAudioRenderer(); - boolean needLWJGL = false; - boolean needOAL = false; - boolean needJInput = false; - boolean needNativeBullet = isUsingNativeBullet(); - - if (renderer != null) { - if (renderer.startsWith("LWJGL")) { - needLWJGL = true; - } - } - if (audioRenderer != null) { - if (audioRenderer.equals("LWJGL")) { - needLWJGL = true; - needOAL = true; - } - } - needJInput = settings.useJoysticks(); - - String libraryPath = getExtractionDir().toString(); - if (needLWJGL) { - logger.log(Level.INFO, "Extraction Directory: {0}", getExtractionDir().toString()); - - // LWJGL supports this feature where - // it can load libraries from this path. - System.setProperty("org.lwjgl.librarypath", libraryPath); - } - if (needJInput) { - // AND Luckily enough JInput supports the same feature. - System.setProperty("net.java.games.input.librarypath", libraryPath); - } - - switch (platform) { - case Windows64: - if (needLWJGL) { - extractNativeLib("windows", "lwjgl64"); - } - if (needOAL) { - extractNativeLib("windows", "OpenAL64"); - } - if (needJInput) { - extractNativeLib("windows", "jinput-dx8_64"); - extractNativeLib("windows", "jinput-raw_64"); - } - if (needNativeBullet) { - extractNativeLib("windows", "bulletjme64", true, false); - } - break; - case Windows32: - if (needLWJGL) { - extractNativeLib("windows", "lwjgl"); - } - if (needOAL) { - extractNativeLib("windows", "OpenAL32"); - } - if (needJInput) { - extractNativeLib("windows", "jinput-dx8"); - extractNativeLib("windows", "jinput-raw"); - } - if (needNativeBullet) { - extractNativeLib("windows", "bulletjme", true, false); - } - break; - case Linux64: - if (needLWJGL) { - extractNativeLib("linux", "lwjgl64"); - } - if (needJInput) { - extractNativeLib("linux", "jinput-linux64"); - } - if (needOAL) { - extractNativeLib("linux", "openal64"); - } - if (needNativeBullet) { - extractNativeLib("linux", "bulletjme64", true, false); - } - break; - case Linux32: - if (needLWJGL) { - extractNativeLib("linux", "lwjgl"); - } - if (needJInput) { - extractNativeLib("linux", "jinput-linux"); - } - if (needOAL) { - extractNativeLib("linux", "openal"); - } - if (needNativeBullet) { - extractNativeLib("linux", "bulletjme", true, false); - } - break; - case MacOSX_PPC32: - case MacOSX32: - case MacOSX_PPC64: - case MacOSX64: - if (needLWJGL) { - extractNativeLib("macosx", "lwjgl"); - } -// if (needOAL) -// extractNativeLib("macosx", "openal"); - if (needJInput) { - extractNativeLib("macosx", "jinput-osx"); - } - if (needNativeBullet) { - extractNativeLib("macosx", "bulletjme", true, false); - } - break; - } - } -} diff --git a/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java b/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java index bf9744b91..887ba5e6d 100644 --- a/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java +++ b/engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java @@ -73,7 +73,7 @@ public class MaterialLoader implements AssetLoader { private int texUnit = 0; private ColorRGBA readColor(String content){ - String[] split = content.split(" "); + String[] split = content.split("\\s"); ColorRGBA color = new ColorRGBA(); color.r = Float.parseFloat(split[0]); @@ -228,7 +228,7 @@ public class MaterialLoader implements AssetLoader { }else if (keyword.equals("emissive")){ emissive = readColor(split[1]); }else if (keyword.equals("specular")){ - String[] subsplit = split[1].split(" "); + String[] subsplit = split[1].split("\\s"); specular = new ColorRGBA(); specular.r = Float.parseFloat(subsplit[0]); specular.g = Float.parseFloat(subsplit[1]); @@ -304,7 +304,7 @@ public class MaterialLoader implements AssetLoader { if (statement.getLine().startsWith("technique")){ readTechnique(statement); }else if (statement.getLine().startsWith("receive_shadows")){ - String isOn = statement.getLine().split(" ")[1]; + String isOn = statement.getLine().split("\\s")[1]; if (isOn != null && isOn.equals("true")){ } } diff --git a/engine/src/ogre/com/jme3/scene/plugins/ogre/MeshLoader.java b/engine/src/ogre/com/jme3/scene/plugins/ogre/MeshLoader.java index c8297a33b..e7f4b10c0 100644 --- a/engine/src/ogre/com/jme3/scene/plugins/ogre/MeshLoader.java +++ b/engine/src/ogre/com/jme3/scene/plugins/ogre/MeshLoader.java @@ -493,7 +493,7 @@ public class MeshLoader extends DefaultHandler implements AssetLoader { private void pushColor(Attributes attribs) throws SAXException { FloatBuffer buf = (FloatBuffer) mesh.getBuffer(Type.Color).getData(); String value = parseString(attribs.getValue("value")); - String[] vals = value.split(" "); + String[] vals = value.split("\\s"); if (vals.length != 3 && vals.length != 4) { throw new SAXException("Color value must contain 3 or 4 components"); }