|
|
|
@ -1,35 +1,27 @@ |
|
|
|
|
/* |
|
|
|
|
* Copyright (c) 2009-2010 jMonkeyEngine |
|
|
|
|
* All rights reserved. |
|
|
|
|
* |
|
|
|
|
* Copyright (c) 2009-2010 jMonkeyEngine All rights reserved. <p/> |
|
|
|
|
* 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. |
|
|
|
|
* modification, are permitted provided that the following conditions are met: |
|
|
|
|
* |
|
|
|
|
* * 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 |
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, |
|
|
|
|
* this list of conditions and the following disclaimer. <p/> * 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. <p/> * 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. <p/> 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. |
|
|
|
|
* 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.shader; |
|
|
|
|
|
|
|
|
|
import com.jme3.export.JmeExporter; |
|
|
|
@ -42,6 +34,7 @@ import com.jme3.math.Matrix4f; |
|
|
|
|
import com.jme3.math.Quaternion; |
|
|
|
|
import com.jme3.math.Vector2f; |
|
|
|
|
import com.jme3.math.Vector3f; |
|
|
|
|
import com.jme3.math.Vector4f; |
|
|
|
|
import com.jme3.util.BufferUtils; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.nio.FloatBuffer; |
|
|
|
@ -51,23 +44,19 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
private static final Integer ZERO_INT = Integer.valueOf(0); |
|
|
|
|
private static final Float ZERO_FLT = Float.valueOf(0); |
|
|
|
|
private static final FloatBuffer ZERO_BUF = BufferUtils.createFloatBuffer(4 * 4); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Currently set value of the uniform. |
|
|
|
|
*/ |
|
|
|
|
protected Object value = null; |
|
|
|
|
protected FloatBuffer multiData = null; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Type of uniform |
|
|
|
|
*/ |
|
|
|
|
protected VarType varType; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Binding to a renderer value, or null if user-defined uniform |
|
|
|
|
*/ |
|
|
|
|
protected UniformBinding binding; |
|
|
|
|
|
|
|
|
|
protected boolean setByCurrentMaterial = false; |
|
|
|
|
// protected Object lastChanger = null;
|
|
|
|
|
|
|
|
|
@ -213,7 +202,6 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
// public Object getLastChanger(){
|
|
|
|
|
// return lastChanger;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public void clearValue() { |
|
|
|
|
updateNeeded = true; |
|
|
|
|
|
|
|
|
@ -231,8 +219,9 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (varType == null) |
|
|
|
|
if (varType == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (varType) { |
|
|
|
|
case Int: |
|
|
|
@ -245,16 +234,18 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
this.value = ZERO_FLT; |
|
|
|
|
break; |
|
|
|
|
case Vector2: |
|
|
|
|
this.value = Vector2f.ZERO; |
|
|
|
|
((Vector2f) this.value).set(Vector2f.ZERO); |
|
|
|
|
break; |
|
|
|
|
case Vector3: |
|
|
|
|
this.value = Vector3f.ZERO; |
|
|
|
|
((Vector3f) this.value).set(Vector3f.ZERO); |
|
|
|
|
break; |
|
|
|
|
case Vector4: |
|
|
|
|
if (this.value instanceof ColorRGBA) { |
|
|
|
|
this.value = ColorRGBA.BlackNoAlpha; |
|
|
|
|
((ColorRGBA) this.value).set(ColorRGBA.BlackNoAlpha); |
|
|
|
|
} else if (this.value instanceof Quaternion) { |
|
|
|
|
((Quaternion) this.value).set(Quaternion.ZERO); |
|
|
|
|
} else { |
|
|
|
|
this.value = Quaternion.ZERO; |
|
|
|
|
((Vector4f) this.value).set(Vector4f.ZERO); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
@ -264,30 +255,35 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setValue(VarType type, Object value) { |
|
|
|
|
if (location == -1) |
|
|
|
|
if (location == -1) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (varType != null && varType != type) |
|
|
|
|
if (varType != null && varType != type) { |
|
|
|
|
throw new IllegalArgumentException("Expected a " + varType.name() + " value!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (value == null) |
|
|
|
|
if (value == null) { |
|
|
|
|
throw new NullPointerException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setByCurrentMaterial = true; |
|
|
|
|
|
|
|
|
|
switch (type) { |
|
|
|
|
case Matrix3: |
|
|
|
|
Matrix3f m3 = (Matrix3f) value; |
|
|
|
|
if (multiData == null) |
|
|
|
|
if (multiData == null) { |
|
|
|
|
multiData = BufferUtils.createFloatBuffer(9); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m3.fillFloatBuffer(multiData, true); |
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
|
case Matrix4: |
|
|
|
|
Matrix4f m4 = (Matrix4f) value; |
|
|
|
|
if (multiData == null) |
|
|
|
|
if (multiData == null) { |
|
|
|
|
multiData = BufferUtils.createFloatBuffer(16); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m4.fillFloatBuffer(multiData, true); |
|
|
|
|
multiData.clear(); |
|
|
|
@ -311,8 +307,9 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
multiData = BufferUtils.ensureLargeEnough(multiData, v2a.length * 2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < v2a.length; i++) |
|
|
|
|
for (int i = 0; i < v2a.length; i++) { |
|
|
|
|
BufferUtils.setInBuffer(v2a[i], multiData, i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
@ -324,8 +321,9 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
multiData = BufferUtils.ensureLargeEnough(multiData, v3a.length * 3); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < v3a.length; i++) |
|
|
|
|
for (int i = 0; i < v3a.length; i++) { |
|
|
|
|
BufferUtils.setInBuffer(v3a[i], multiData, i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
@ -337,36 +335,39 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
multiData = BufferUtils.ensureLargeEnough(multiData, v4a.length * 4); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < v4a.length; i++) |
|
|
|
|
for (int i = 0; i < v4a.length; i++) { |
|
|
|
|
BufferUtils.setInBuffer(v4a[i], multiData, i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
|
case Matrix3Array: |
|
|
|
|
Matrix3f[] m3a = (Matrix3f[]) value; |
|
|
|
|
|
|
|
|
|
if (multiData == null) |
|
|
|
|
if (multiData == null) { |
|
|
|
|
multiData = BufferUtils.createFloatBuffer(m3a.length * 9); |
|
|
|
|
else{ |
|
|
|
|
} else { |
|
|
|
|
multiData = BufferUtils.ensureLargeEnough(multiData, m3a.length * 9); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < m3a.length; i++) |
|
|
|
|
for (int i = 0; i < m3a.length; i++) { |
|
|
|
|
m3a[i].fillFloatBuffer(multiData, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
|
case Matrix4Array: |
|
|
|
|
Matrix4f[] m4a = (Matrix4f[]) value; |
|
|
|
|
|
|
|
|
|
if (multiData == null) |
|
|
|
|
if (multiData == null) { |
|
|
|
|
multiData = BufferUtils.createFloatBuffer(m4a.length * 16); |
|
|
|
|
else{ |
|
|
|
|
} else { |
|
|
|
|
multiData = BufferUtils.ensureLargeEnough(multiData, m4a.length * 16); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < m4a.length; i++) |
|
|
|
|
for (int i = 0; i < m4a.length; i++) { |
|
|
|
|
m4a[i].fillFloatBuffer(multiData, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiData.clear(); |
|
|
|
|
break; |
|
|
|
@ -374,8 +375,9 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
case Int: |
|
|
|
|
case Float: |
|
|
|
|
case Boolean: |
|
|
|
|
if (this.value != null && this.value.equals(value)) |
|
|
|
|
if (this.value != null && this.value.equals(value)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.value = value; |
|
|
|
|
break; |
|
|
|
@ -384,16 +386,18 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (multiData != null) |
|
|
|
|
if (multiData != null) { |
|
|
|
|
this.value = multiData; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
varType = type; |
|
|
|
|
updateNeeded = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setVector4Length(int length) { |
|
|
|
|
if (location == -1) |
|
|
|
|
if (location == -1) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
FloatBuffer fb = (FloatBuffer) value; |
|
|
|
|
if (fb == null || fb.capacity() < length) { |
|
|
|
@ -406,11 +410,13 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setVector4InArray(float x, float y, float z, float w, int index) { |
|
|
|
|
if (location == -1) |
|
|
|
|
if (location == -1) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (varType != null && varType != VarType.Vector4Array) |
|
|
|
|
if (varType != null && varType != VarType.Vector4Array) { |
|
|
|
|
throw new IllegalArgumentException("Expected a " + varType.name() + " value!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
FloatBuffer fb = (FloatBuffer) value; |
|
|
|
|
fb.position(index * 4); |
|
|
|
@ -433,5 +439,4 @@ public class Uniform extends ShaderVariable { |
|
|
|
|
location = -2; |
|
|
|
|
updateNeeded = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|