|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2021 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.shader; |
| 33 | + |
| 34 | +import com.jme3.math.*; |
| 35 | +import org.hamcrest.MatcherAssert; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import java.nio.FloatBuffer; |
| 39 | +import java.nio.IntBuffer; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +import static org.junit.Assert.*; |
| 45 | +import static org.junit.Assert.assertEquals; |
| 46 | + |
| 47 | +public class UniformTest { |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testSetValue_IntArray() { |
| 51 | + Uniform uniform = new Uniform(); |
| 52 | + |
| 53 | + // Set value for the first time |
| 54 | + int[] intArray1 = new int[] {1, 2, 4, 8}; |
| 55 | + uniform.setValue(VarType.IntArray, intArray1); |
| 56 | + |
| 57 | + assertTrue(uniform.getValue() instanceof IntBuffer); |
| 58 | + verifyIntBufferContent((IntBuffer) uniform.getValue(), intArray1); |
| 59 | + |
| 60 | + // Overriding the previous value |
| 61 | + int[] intArray2 = new int[] {3, 5, 7, 11, 13}; |
| 62 | + uniform.setValue(VarType.IntArray, intArray2); |
| 63 | + |
| 64 | + assertTrue(uniform.getValue() instanceof IntBuffer); |
| 65 | + verifyIntBufferContent((IntBuffer) uniform.getValue(), intArray2); |
| 66 | + } |
| 67 | + |
| 68 | + private void verifyIntBufferContent(IntBuffer intBuffer, int[] intArray) { |
| 69 | + assertEquals(0, intBuffer.position()); |
| 70 | + assertEquals(intArray.length, intBuffer.capacity()); |
| 71 | + assertEquals(intArray.length, intBuffer.limit()); |
| 72 | + |
| 73 | + for (int i = 0; i < intArray.length; i++) { |
| 74 | + assertEquals(intArray[i], intBuffer.get(i)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testSetValue_FloatArray() { |
| 81 | + Uniform uniform = new Uniform(); |
| 82 | + |
| 83 | + // Set value for the first time |
| 84 | + float[] floatArray1 = new float[] {1.1f, 2.2f, 4.4f, 8.8f}; |
| 85 | + uniform.setValue(VarType.FloatArray, floatArray1); |
| 86 | + |
| 87 | + verifyFloatBufferContent(uniform.getMultiData(), floatArray1); |
| 88 | + |
| 89 | + // Overriding the previous value |
| 90 | + float[] floatArray2 = new float[] {3.3f, 5.5f, 7.7f, 11.11f, 13.13f}; |
| 91 | + uniform.setValue(VarType.FloatArray, floatArray2); |
| 92 | + |
| 93 | + verifyFloatBufferContent(uniform.getMultiData(), floatArray2); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testSetValue_Vector2Array() { |
| 99 | + Uniform uniform = new Uniform(); |
| 100 | + |
| 101 | + // Set value for the first time |
| 102 | + float[] expectedData1 = new float[] { |
| 103 | + 1.1f, 2.2f, |
| 104 | + 3.3f, 4.4f |
| 105 | + }; |
| 106 | + Vector2f[] vector2Array1 = new Vector2f[] { |
| 107 | + new Vector2f(expectedData1[0], expectedData1[1]), |
| 108 | + new Vector2f(expectedData1[2], expectedData1[3]) |
| 109 | + }; |
| 110 | + uniform.setValue(VarType.Vector2Array, vector2Array1); |
| 111 | + |
| 112 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData1); |
| 113 | + |
| 114 | + // Overriding the previous value |
| 115 | + float[] expectedData2 = new float[] { |
| 116 | + 1.2f, 2.3f, |
| 117 | + 3.4f, 4.5f, |
| 118 | + 5.6f, 6.7f |
| 119 | + }; |
| 120 | + Vector2f[] vector2Array2 = new Vector2f[] { |
| 121 | + new Vector2f(expectedData2[0], expectedData2[1]), |
| 122 | + new Vector2f(expectedData2[2], expectedData2[3]), |
| 123 | + new Vector2f(expectedData2[4], expectedData2[5]) |
| 124 | + }; |
| 125 | + uniform.setValue(VarType.Vector2Array, vector2Array2); |
| 126 | + |
| 127 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData2); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testSetValue_Vector3Array() { |
| 133 | + Uniform uniform = new Uniform(); |
| 134 | + |
| 135 | + // Set value for the first time |
| 136 | + float[] expectedData1 = new float[] { |
| 137 | + 1.1f, 2.2f, 3.3f, |
| 138 | + 4.4f, 5.5f, 6.6f |
| 139 | + }; |
| 140 | + Vector3f[] vector3Array1 = new Vector3f[] { |
| 141 | + new Vector3f(expectedData1[0], expectedData1[1], expectedData1[2]), |
| 142 | + new Vector3f(expectedData1[3], expectedData1[4], expectedData1[5]) |
| 143 | + }; |
| 144 | + uniform.setValue(VarType.Vector3Array, vector3Array1); |
| 145 | + |
| 146 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData1); |
| 147 | + |
| 148 | + // Overriding the previous value |
| 149 | + float[] expectedData2 = new float[] { |
| 150 | + 1.2f, 2.3f, 3.4f, |
| 151 | + 4.5f, 5.6f, 6.7f, |
| 152 | + 7.8f, 8.9f, 9.1f |
| 153 | + }; |
| 154 | + Vector3f[] vector3Array2 = new Vector3f[] { |
| 155 | + new Vector3f(expectedData2[0], expectedData2[1], expectedData2[2]), |
| 156 | + new Vector3f(expectedData2[3], expectedData2[4], expectedData2[5]), |
| 157 | + new Vector3f(expectedData2[6], expectedData2[7], expectedData2[8]) |
| 158 | + }; |
| 159 | + uniform.setValue(VarType.Vector3Array, vector3Array2); |
| 160 | + |
| 161 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData2); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testSetValue_Vector4Array() { |
| 167 | + Uniform uniform = new Uniform(); |
| 168 | + |
| 169 | + // Set value for the first time |
| 170 | + float[] expectedData1 = new float[] { |
| 171 | + 1.1f, 2.2f, 3.3f, 4.4f, |
| 172 | + 5.5f, 6.6f, 7.7f, 8.8f |
| 173 | + }; |
| 174 | + Vector4f[] vector4Array1 = new Vector4f[] { |
| 175 | + new Vector4f(expectedData1[0], expectedData1[1], expectedData1[2], expectedData1[3]), |
| 176 | + new Vector4f(expectedData1[4], expectedData1[5], expectedData1[6], expectedData1[7]) |
| 177 | + }; |
| 178 | + uniform.setValue(VarType.Vector4Array, vector4Array1); |
| 179 | + |
| 180 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData1); |
| 181 | + |
| 182 | + // Overriding the previous value |
| 183 | + float[] expectedData2 = new float[] { |
| 184 | + 1.2f, 2.3f, 3.4f, 4.5f, |
| 185 | + 5.6f, 6.7f, 7.8f, 8.9f, |
| 186 | + 9.10f, 10.11f, 11.12f, 12.13f |
| 187 | + }; |
| 188 | + Vector4f[] vector4Array2 = new Vector4f[] { |
| 189 | + new Vector4f(expectedData2[0], expectedData2[1], expectedData2[2], expectedData2[3]), |
| 190 | + new Vector4f(expectedData2[4], expectedData2[5], expectedData2[6], expectedData2[7]), |
| 191 | + new Vector4f(expectedData2[8], expectedData2[9], expectedData2[10], expectedData2[11]) |
| 192 | + }; |
| 193 | + uniform.setValue(VarType.Vector4Array, vector4Array2); |
| 194 | + |
| 195 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData2); |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testSetValue_Matrix3Array() { |
| 201 | + Uniform uniform = new Uniform(); |
| 202 | + |
| 203 | + // Set value for the first time |
| 204 | + float[] expectedData1 = new float[] { |
| 205 | + 1.1f, 2.2f, 3.3f, |
| 206 | + 4.4f, 5.5f, 6.6f, |
| 207 | + 7.7f, 8.8f, 9.9f, |
| 208 | + |
| 209 | + 10.10f, 11.11f, 12.12f, |
| 210 | + 13.13f, 14.14f, 15.15f, |
| 211 | + 16.16f, 17.17f, 18.18f |
| 212 | + }; |
| 213 | + Matrix3f[] matrix3Array1 = new Matrix3f[] { |
| 214 | + new Matrix3f( |
| 215 | + expectedData1[0], expectedData1[3], expectedData1[6], |
| 216 | + expectedData1[1], expectedData1[4], expectedData1[7], |
| 217 | + expectedData1[2], expectedData1[5], expectedData1[8] |
| 218 | + ), |
| 219 | + new Matrix3f( |
| 220 | + expectedData1[9], expectedData1[12], expectedData1[15], |
| 221 | + expectedData1[10], expectedData1[13], expectedData1[16], |
| 222 | + expectedData1[11], expectedData1[14], expectedData1[17] |
| 223 | + ) |
| 224 | + }; |
| 225 | + uniform.setValue(VarType.Matrix3Array, matrix3Array1); |
| 226 | + |
| 227 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData1); |
| 228 | + |
| 229 | + // Overriding the previous value |
| 230 | + float[] expectedData2 = new float[] { |
| 231 | + 1.2f, 2.3f, 3.4f, |
| 232 | + 4.5f, 5.6f, 6.7f, |
| 233 | + 7.8f, 8.9f, 9.1f, |
| 234 | + |
| 235 | + 10.11f, 11.12f, 12.13f, |
| 236 | + 13.14f, 14.15f, 15.16f, |
| 237 | + 16.17f, 17.18f, 18.19f, |
| 238 | + |
| 239 | + 19.20f, 20.21f, 21.22f, |
| 240 | + 22.23f, 23.24f, 24.25f, |
| 241 | + 25.26f, 26.27f, 27.28f |
| 242 | + }; |
| 243 | + Matrix3f[] matrix3Array2 = new Matrix3f[] { |
| 244 | + new Matrix3f( |
| 245 | + expectedData2[0], expectedData2[3], expectedData2[6], |
| 246 | + expectedData2[1], expectedData2[4], expectedData2[7], |
| 247 | + expectedData2[2], expectedData2[5], expectedData2[8] |
| 248 | + ), |
| 249 | + new Matrix3f( |
| 250 | + expectedData2[9], expectedData2[12], expectedData2[15], |
| 251 | + expectedData2[10], expectedData2[13], expectedData2[16], |
| 252 | + expectedData2[11], expectedData2[14], expectedData2[17] |
| 253 | + ), |
| 254 | + new Matrix3f( |
| 255 | + expectedData2[18], expectedData2[21], expectedData2[24], |
| 256 | + expectedData2[19], expectedData2[22], expectedData2[25], |
| 257 | + expectedData2[20], expectedData2[23], expectedData2[26] |
| 258 | + ) |
| 259 | + }; |
| 260 | + uniform.setValue(VarType.Matrix3Array, matrix3Array2); |
| 261 | + |
| 262 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData2); |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testSetValue_Matrix4Array() { |
| 268 | + Uniform uniform = new Uniform(); |
| 269 | + |
| 270 | + // Set value for the first time |
| 271 | + float[] expectedData1 = new float[] { |
| 272 | + 1.1f, 2.2f, 3.3f, 4.4f, |
| 273 | + 5.5f, 6.6f, 7.7f, 8.8f, |
| 274 | + 9.9f, 10.10f, 11.11f, 12.12f, |
| 275 | + 13.13f, 14.14f, 15.15f, 16.16f, |
| 276 | + |
| 277 | + 17.17f, 18.18f, 19.19f, 20.20f, |
| 278 | + 21.21f, 22.22f, 23.23f, 24.24f, |
| 279 | + 25.25f, 26.26f, 27.27f, 28.28f, |
| 280 | + 29.29f, 30.30f, 31.31f, 32.32f |
| 281 | + }; |
| 282 | + Matrix4f[] matrix4Array1 = new Matrix4f[] { |
| 283 | + new Matrix4f(Arrays.copyOfRange(expectedData1, 0, 16)), |
| 284 | + new Matrix4f(Arrays.copyOfRange(expectedData1, 16, 32)) |
| 285 | + }; |
| 286 | + uniform.setValue(VarType.Matrix4Array, matrix4Array1); |
| 287 | + |
| 288 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData1); |
| 289 | + |
| 290 | + // Overriding the previous value |
| 291 | + float[] expectedData2 = new float[] { |
| 292 | + 1.2f, 2.3f, 3.4f, 4.5f, |
| 293 | + 5.6f, 6.7f, 7.8f, 8.9f, |
| 294 | + 9.1f, 10.11f, 11.12f, 12.13f, |
| 295 | + 13.14f, 14.15f, 15.16f, 16.17f, |
| 296 | + |
| 297 | + 17.18f, 18.19f, 19.20f, 20.21f, |
| 298 | + 21.22f, 22.23f, 23.24f, 24.25f, |
| 299 | + 25.26f, 26.27f, 27.28f, 28.29f, |
| 300 | + 29.30f, 30.31f, 31.32f, 32.33f, |
| 301 | + |
| 302 | + 33.34f, 34.35f, 35.36f, 36.37f, |
| 303 | + 37.38f, 38.39f, 39.40f, 40.41f, |
| 304 | + 41.42f, 42.43f, 43.44f, 44.45f, |
| 305 | + 45.46f, 46.47f, 47.48f, 48.49f |
| 306 | + }; |
| 307 | + Matrix4f[] matrix4Array2 = new Matrix4f[] { |
| 308 | + new Matrix4f(Arrays.copyOfRange(expectedData2, 0, 16)), |
| 309 | + new Matrix4f(Arrays.copyOfRange(expectedData2, 16, 32)), |
| 310 | + new Matrix4f(Arrays.copyOfRange(expectedData2, 32, 48)) |
| 311 | + }; |
| 312 | + uniform.setValue(VarType.Matrix4Array, matrix4Array2); |
| 313 | + |
| 314 | + verifyFloatBufferContent(uniform.getMultiData(), expectedData2); |
| 315 | + } |
| 316 | + |
| 317 | + private void verifyFloatBufferContent(FloatBuffer floatBuffer, float[] floatArray) { |
| 318 | + assertEquals(0, floatBuffer.position()); |
| 319 | + assertEquals(floatArray.length, floatBuffer.capacity()); |
| 320 | + assertEquals(floatArray.length, floatBuffer.limit()); |
| 321 | + |
| 322 | + for (int i = 0; i < floatArray.length; i++) { |
| 323 | + assertEquals(floatArray[i], floatBuffer.get(i), 0f); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | +} |
0 commit comments