TempVars.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.util;
  33. import com.jme3.collision.bih.BIHNode.BIHStackData;
  34. import com.jme3.math.Eigen3f;
  35. import com.jme3.math.Matrix4f;
  36. import com.jme3.math.Matrix3f;
  37. import com.jme3.math.Plane;
  38. import com.jme3.math.Quaternion;
  39. import com.jme3.math.Triangle;
  40. import com.jme3.math.Vector2f;
  41. import com.jme3.math.Vector3f;
  42. import com.jme3.scene.Spatial;
  43. import java.nio.FloatBuffer;
  44. import java.nio.IntBuffer;
  45. import java.util.ArrayList;
  46. /**
  47. * Temporary variables assigned to each thread. Engine classes may access
  48. * these temp variables with TempVars.get(), all retrieved TempVars
  49. * instances must be returned via TempVars.release().
  50. * This returns an available instance of the TempVar class ensuring this
  51. * particular instance is never used elsewhere in the mean time.
  52. */
  53. public class TempVars {
  54. /**
  55. * Allow X instances of TempVars in a single thread.
  56. */
  57. private static final int STACK_SIZE = 5;
  58. /**
  59. * <code>TempVarsStack</code> contains a stack of TempVars.
  60. * Every time TempVars.get() is called, a new entry is added to the stack,
  61. * and the index incremented.
  62. * When TempVars.release() is called, the entry is checked against
  63. * the current instance and then the index is decremented.
  64. */
  65. private static class TempVarsStack {
  66. int index = 0;
  67. TempVars[] tempVars = new TempVars[STACK_SIZE];
  68. }
  69. /**
  70. * ThreadLocal to store a TempVarsStack for each thread.
  71. * This ensures each thread has a single TempVarsStack that is
  72. * used only in method calls in that thread.
  73. */
  74. private static final ThreadLocal<TempVarsStack> varsLocal = new ThreadLocal<TempVarsStack>() {
  75. @Override
  76. public TempVarsStack initialValue() {
  77. return new TempVarsStack();
  78. }
  79. };
  80. /**
  81. * This instance of TempVars has been retrieved but not released yet.
  82. */
  83. private boolean isUsed = false;
  84. private TempVars() {
  85. }
  86. /**
  87. * Acquire an instance of the TempVar class.
  88. * You have to release the instance after use by calling the
  89. * release() method.
  90. * If more than STACK_SIZE (currently 5) instances are requested
  91. * in a single thread then an ArrayIndexOutOfBoundsException will be thrown.
  92. *
  93. * @return A TempVar instance
  94. */
  95. public static TempVars get() {
  96. TempVarsStack stack = varsLocal.get();
  97. TempVars instance = stack.tempVars[stack.index];
  98. if (instance == null){
  99. // Create new
  100. instance = new TempVars();
  101. // Put it in there
  102. stack.tempVars[stack.index] = instance;
  103. }
  104. stack.index++;
  105. instance.isUsed = true;
  106. return instance;
  107. }
  108. /**
  109. * Releases this instance of TempVars.
  110. * Once released, the contents of the TempVars are undefined.
  111. * The TempVars must be released in the opposite order that they are retrieved,
  112. * e.g. Acquiring vars1, then acquiring vars2, vars2 MUST be released
  113. * first otherwise an exception will be thrown.
  114. */
  115. public void release() {
  116. if (!isUsed){
  117. throw new IllegalStateException("This instance of TempVars was already released!");
  118. }
  119. isUsed = false;
  120. TempVarsStack stack = varsLocal.get();
  121. // Return it to the stack
  122. stack.index--;
  123. // Check if it is actually there
  124. if (stack.tempVars[stack.index] != this){
  125. throw new IllegalStateException("An instance of TempVars has not been released in a called method!");
  126. }
  127. }
  128. /**
  129. * For interfacing with OpenGL in Renderer.
  130. */
  131. public final IntBuffer intBuffer1 = BufferUtils.createIntBuffer(1);
  132. public final IntBuffer intBuffer16 = BufferUtils.createIntBuffer(16);
  133. public final FloatBuffer floatBuffer16 = BufferUtils.createFloatBuffer(16);
  134. /**
  135. * Skinning buffers
  136. */
  137. public final float[] skinPositions = new float[512 * 3];
  138. public final float[] skinNormals = new float[512 * 3];
  139. /**
  140. * Fetching triangle from mesh
  141. */
  142. public final Triangle triangle = new Triangle();
  143. /**
  144. * General vectors.
  145. */
  146. public final Vector3f vect1 = new Vector3f();
  147. public final Vector3f vect2 = new Vector3f();
  148. public final Vector3f vect3 = new Vector3f();
  149. public final Vector3f vect4 = new Vector3f();
  150. public final Vector3f vect5 = new Vector3f();
  151. public final Vector3f vect6 = new Vector3f();
  152. public final Vector3f vect7 = new Vector3f();
  153. //seems the maximum number of vector used is 7 in com.jme3.bounding.java
  154. public final Vector3f vect8 = new Vector3f();
  155. public final Vector3f vect9 = new Vector3f();
  156. public final Vector3f vect10 = new Vector3f();
  157. public final Vector3f[] tri = {new Vector3f(),
  158. new Vector3f(),
  159. new Vector3f()};
  160. /**
  161. * 2D vector
  162. */
  163. public final Vector2f vect2d = new Vector2f();
  164. public final Vector2f vect2d2 = new Vector2f();
  165. /**
  166. * General matrices.
  167. */
  168. public final Matrix3f tempMat3 = new Matrix3f();
  169. public final Matrix4f tempMat4 = new Matrix4f();
  170. /**
  171. * General quaternions.
  172. */
  173. public final Quaternion quat1 = new Quaternion();
  174. public final Quaternion quat2 = new Quaternion();
  175. /**
  176. * Eigen
  177. */
  178. public final Eigen3f eigen = new Eigen3f();
  179. /**
  180. * Plane
  181. */
  182. public final Plane plane = new Plane();
  183. /**
  184. * BoundingBox ray collision
  185. */
  186. public final float[] fWdU = new float[3];
  187. public final float[] fAWdU = new float[3];
  188. public final float[] fDdU = new float[3];
  189. public final float[] fADdU = new float[3];
  190. public final float[] fAWxDdU = new float[3];
  191. /**
  192. * Maximum tree depth .. 32 levels??
  193. */
  194. public final Spatial[] spatialStack = new Spatial[32];
  195. public final float[] matrixWrite = new float[16];
  196. /**
  197. * BIHTree
  198. */
  199. public final float[] bihSwapTmp = new float[9];
  200. public final ArrayList<BIHStackData> bihStack = new ArrayList<BIHStackData>();
  201. }