From d39b780fa5062299a6e1f52f1a5000a67d59c2c0 Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Sun, 24 Jul 2016 12:40:45 +0200 Subject: [PATCH 1/6] Added RenderState.BlendMode.Custom BlendMode.Custom allows glBlendFuncSeparate() calls. --- .../com/jme3/renderer/android/AndroidGL.java | 4 + .../java/com/jme3/material/RenderState.java | 114 +++++++++++++++++- .../java/com/jme3/renderer/opengl/GL.java | 1 + .../com/jme3/renderer/opengl/GLDebugES.java | 6 + .../com/jme3/renderer/opengl/GLRenderer.java | 3 + .../java/com/jme3/renderer/ios/IosGL.java | 5 + .../com/jme3/renderer/ios/JmeIosGLES.java | 1 + .../java/com/jme3/renderer/jogl/JoglGL.java | 6 + .../java/com/jme3/renderer/lwjgl/LwjglGL.java | 4 + .../java/com/jme3/renderer/lwjgl/LwjglGL.java | 4 + .../export/material/J3MOutputCapsule.java | 6 +- 11 files changed, 149 insertions(+), 5 deletions(-) diff --git a/jme3-android/src/main/java/com/jme3/renderer/android/AndroidGL.java b/jme3-android/src/main/java/com/jme3/renderer/android/AndroidGL.java index 8aeb883e0..12e743810 100644 --- a/jme3-android/src/main/java/com/jme3/renderer/android/AndroidGL.java +++ b/jme3-android/src/main/java/com/jme3/renderer/android/AndroidGL.java @@ -103,6 +103,10 @@ public class AndroidGL implements GL, GLExt, GLFbo { public void glBlendFunc(int sfactor, int dfactor) { GLES20.glBlendFunc(sfactor, dfactor); } + + public void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha) { + GLES20.glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha); + } public void glBufferData(int target, FloatBuffer data, int usage) { GLES20.glBufferData(target, getLimitBytes(data), data, usage); diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 1fb33a079..9e3b1d257 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -32,6 +32,7 @@ package com.jme3.material; import com.jme3.export.*; +import com.jme3.renderer.opengl.GL; import com.jme3.scene.Mesh; import com.jme3.scene.Mesh.Mode; import java.io.IOException; @@ -277,7 +278,13 @@ public class RenderState implements Cloneable, Savable { * Result = (Source Color * (1 - Dest Color)) + (Dest Color * (1 - Source Color)) * -> (GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR) */ - Exclusion + Exclusion, + /** + * Allows for custom blending by using glBlendFuncSeparate. + *

+ * + */ + Custom } /** @@ -408,7 +415,11 @@ public class RenderState implements Cloneable, Savable { TestFunction frontStencilFunction = TestFunction.Always; TestFunction backStencilFunction = TestFunction.Always; int cachedHashCode = -1; - + int sfactorRGB=GL.GL_ONE; + int dfactorRGB=GL.GL_ZERO; + int sfactorAlpha=GL.GL_ONE; + int dfactorAlpha=GL.GL_ZERO; + public void write(JmeExporter ex) throws IOException { OutputCapsule oc = ex.getCapsule(this); oc.write(true, "pointSprite", false); @@ -434,6 +445,10 @@ public class RenderState implements Cloneable, Savable { oc.write(blendEquationAlpha, "blendEquationAlpha", BlendEquationAlpha.InheritColor); oc.write(depthFunc, "depthFunc", TestFunction.LessOrEqual); oc.write(lineWidth, "lineWidth", 1); + oc.write(sfactorRGB, "sfactorRGB", GL.GL_ONE); + oc.write(dfactorRGB, "dfactorRGB", GL.GL_ZERO); + oc.write(sfactorAlpha, "sfactorAlpha", GL.GL_ONE); + oc.write(dfactorAlpha, "dfactorAlpha", GL.GL_ZERO); // Only "additional render state" has them set to false by default oc.write(applyWireFrame, "applyWireFrame", true); @@ -474,6 +489,10 @@ public class RenderState implements Cloneable, Savable { blendEquationAlpha = ic.readEnum("blendEquationAlpha", BlendEquationAlpha.class, BlendEquationAlpha.InheritColor); depthFunc = ic.readEnum("depthFunc", TestFunction.class, TestFunction.LessOrEqual); lineWidth = ic.readFloat("lineWidth", 1); + sfactorRGB = ic.readInt("sfactorRGB", GL.GL_ONE); + dfactorAlpha = ic.readInt("dfactorRGB", GL.GL_ZERO); + sfactorRGB = ic.readInt("sfactorAlpha", GL.GL_ONE); + dfactorAlpha = ic.readInt("dfactorAlpha", GL.GL_ZERO); applyWireFrame = ic.readBoolean("applyWireFrame", true); @@ -605,6 +624,14 @@ public class RenderState implements Cloneable, Savable { if(lineWidth != rs.lineWidth){ return false; } + + if (blendMode.equals(BlendMode.Custom)) { + return sfactorRGB==rs.getCustomSfactorRGB() + && dfactorRGB==rs.getCustomDfactorRGB() + && sfactorAlpha==rs.getCustomSfactorAlpha() + && dfactorAlpha==rs.getCustomDfactorAlpha(); + + } return true; } @@ -744,6 +771,26 @@ public class RenderState implements Cloneable, Savable { cachedHashCode = -1; } + + /** + * Sets the custom blend factors for BlendMode.Custom as + * defined by glBlendFuncSeparate. + * + * @param sfactorRGB The source blend factor for RGB components. + * @param dfactorRGB The destination blend factor for RGB components. + * @param sfactorAlpha The source blend factor for the alpha component. + * @param dfactorAlpha The destination blend factor for the alpha component. + */ + public void setCustomBlendFactors(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha) + { + this.sfactorRGB = sfactorRGB; + this.dfactorRGB = dfactorRGB; + this.sfactorAlpha = sfactorAlpha; + this.dfactorAlpha = dfactorAlpha; + cachedHashCode = -1; + } + + /** * Enable depth testing. * @@ -1084,7 +1131,47 @@ public class RenderState implements Cloneable, Savable { public BlendMode getBlendMode() { return blendMode; } - + + /** + * Provides the source factor for the RGB components in + * BlendMode.Custom. + * + * @return the custom source factor for RGB components. + */ + public int getCustomSfactorRGB() { + return sfactorRGB; + } + + /** + * Provides the destination factor for the RGB components in + * BlendMode.Custom. + * + * @return the custom destination factor for RGB components. + */ + public int getCustomDfactorRGB() { + return dfactorRGB; + } + + /** + * Provides the source factor for the alpha component in + * BlendMode.Custom. + * + * @return the custom destination factor for alpha component. + */ + public int getCustomSfactorAlpha() { + return sfactorAlpha; + } + + /** + * Provides the destination factor for the alpha component in + * BlendMode.Custom. + * + * @return the custom destination factor for alpha component. + */ + public int getCustomDfactorAlpha() { + return dfactorAlpha; + } + /** * @return true * @deprecated Always returns true since point sprite is always enabled. @@ -1306,6 +1393,11 @@ public class RenderState implements Cloneable, Savable { hash = 79 * hash + (this.frontStencilFunction != null ? this.frontStencilFunction.hashCode() : 0); hash = 79 * hash + (this.backStencilFunction != null ? this.backStencilFunction.hashCode() : 0); hash = 79 * hash + Float.floatToIntBits(this.lineWidth); + + hash = 79 * hash + this.sfactorRGB; + hash = 79 * hash + this.dfactorRGB; + hash = 79 * hash + this.sfactorAlpha; + hash = 79 * hash + this.dfactorAlpha; cachedHashCode = hash; } return cachedHashCode; @@ -1380,6 +1472,13 @@ public class RenderState implements Cloneable, Savable { } if (additionalState.applyBlendMode) { state.blendMode = additionalState.blendMode; + if (additionalState.getBlendMode().equals(BlendMode.Custom)) { + state.setCustomBlendFactors( + additionalState.getCustomSfactorRGB(), + additionalState.getCustomDfactorRGB(), + additionalState.getCustomSfactorAlpha(), + additionalState.getCustomDfactorAlpha()); + } } else { state.blendMode = blendMode; } @@ -1464,6 +1563,11 @@ public class RenderState implements Cloneable, Savable { applyPolyOffset = true; applyDepthFunc = true; applyLineWidth = true; + + sfactorRGB = state.sfactorRGB; + dfactorRGB = state.dfactorRGB; + sfactorAlpha = state.sfactorAlpha; + dfactorAlpha = state.dfactorAlpha; } @Override @@ -1490,6 +1594,8 @@ public class RenderState implements Cloneable, Savable { + "\noffsetFactor=" + offsetFactor + "\noffsetUnits=" + offsetUnits + "\nlineWidth=" + lineWidth - + "\n]"; + + (blendMode.equals(BlendMode.Custom)? + "\ncustomBlendFactors=("+sfactorRGB+", "+dfactorRGB+", "+sfactorAlpha+", "+dfactorAlpha+")": + "\n]"); } } diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java index a4a738152..30996cd7d 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java @@ -197,6 +197,7 @@ public interface GL { public void glBindTexture(int target, int texture); public void glBlendEquationSeparate(int colorMode, int alphaMode); public void glBlendFunc(int sfactor, int dfactor); + public void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha); public void glBufferData(int target, long data_size, int usage); public void glBufferData(int target, FloatBuffer data, int usage); public void glBufferData(int target, ShortBuffer data, int usage); diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugES.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugES.java index ed6b336f8..6b15bb967 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugES.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugES.java @@ -44,6 +44,12 @@ public class GLDebugES extends GLDebug implements GL, GLFbo, GLExt { gl.glBlendFunc(sfactor, dfactor); checkError(); } + + public void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dFactorAlpha) + { + gl.glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dFactorAlpha); + checkError(); + } public void glBufferData(int target, FloatBuffer data, int usage) { gl.glBufferData(target, data, usage); diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 1a8270435..bcb4a4d5c 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -748,6 +748,9 @@ public final class GLRenderer implements Renderer { case Exclusion: gl.glBlendFunc(GL.GL_ONE_MINUS_DST_COLOR, GL.GL_ONE_MINUS_SRC_COLOR); break; + case Custom: + gl.glBlendFuncSeparate(state.getCustomSfactorRGB(), state.getCustomDfactorRGB(), state.getCustomSfactorAlpha(), state.getCustomDfactorAlpha()); + break; default: throw new UnsupportedOperationException("Unrecognized blend mode: " + state.getBlendMode()); diff --git a/jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java b/jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java index 958163596..d73c5158a 100644 --- a/jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java +++ b/jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java @@ -135,6 +135,11 @@ public class IosGL implements GL, GLExt, GLFbo { public void glBlendFunc(int sfactor, int dfactor) { JmeIosGLES.glBlendFunc(sfactor, dfactor); } + + public void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha) { + JmeIosGLES.glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha); + } + public void glBufferData(int target, FloatBuffer data, int usage) { JmeIosGLES.glBufferData(target, getLimitBytes(data), data, usage); diff --git a/jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java b/jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java index b8ec75a77..b5cde6721 100644 --- a/jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java +++ b/jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java @@ -144,6 +144,7 @@ public class JmeIosGLES { // public static native void glBindVertexArray // TODO: Investigate this public static native void glBlendEquationSeparate(int colorMode, int alphaMode); public static native void glBlendFunc(int sfactor, int dfactor); + public static native void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha); public static native void glBufferData(int target, int size, Buffer data, int usage); public static native void glBufferData2(int target, int size, byte[] data, int offset, int usage); public static native void glBufferSubData(int target, int offset, int size, Buffer data); diff --git a/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java b/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java index 8b222d751..bbd1b24f9 100644 --- a/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java +++ b/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java @@ -88,6 +88,11 @@ public class JoglGL implements GL, GL2, GL3, GL4 { GLContext.getCurrentGL().glBlendFunc(param1, param2); } + @Override + public void glBlendFuncSeparate(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha) { + GLContext.getCurrentGL().glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha); + } + @Override public void glBufferData(int param1, long param2, int param3) { GLContext.getCurrentGL().glBufferData(param1, param2, null, param3); @@ -610,4 +615,5 @@ public class JoglGL implements GL, GL2, GL3, GL4 { public void glFramebufferTextureLayer(int param1, int param2, int param3, int param4, int param5) { GLContext.getCurrentGL().getGL3().glFramebufferTextureLayer(param1, param2, param3, param4, param5); } + } diff --git a/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java b/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java index abc419c41..59e50e3f7 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java +++ b/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java @@ -58,6 +58,10 @@ public final class LwjglGL implements GL, GL2, GL3, GL4 { GL11.glBlendFunc(param1, param2); } + public void glBlendFuncSeparate(int param1, int param2, int param3, int param4) { + GL14.glBlendFuncSeparate(param1, param2, param3, param4); + } + public void glBufferData(int param1, long param2, int param3) { GL15.glBufferData(param1, param2, param3); } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java b/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java index 10358c009..f5f6a7b1a 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java @@ -86,6 +86,10 @@ public class LwjglGL implements GL, GL2, GL3, GL4 { public void glBlendFunc(int param1, int param2) { GL11.glBlendFunc(param1, param2); } + + public void glBlendFuncSeparate(int param1, int param2, int param3, int param4) { + GL14.glBlendFuncSeparate(param1, param2, param3, param4); + } public void glBufferData(int param1, long param2, int param3) { GL15.glBufferData(param1, param2, param3); diff --git a/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MOutputCapsule.java b/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MOutputCapsule.java index 7dd6a2a59..01ae2b915 100644 --- a/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MOutputCapsule.java +++ b/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MOutputCapsule.java @@ -367,7 +367,11 @@ public class J3MOutputCapsule implements OutputCapsule { @Override public void write(int value, String name, int defVal) throws IOException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (value == defVal) { + return; + } + + putParameter(name, Integer.toString(value)); } @Override From 1bbe34160c403b294f7f721e76005452a01ac442 Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Sun, 24 Jul 2016 17:16:29 +0200 Subject: [PATCH 2/6] Corrected the bug in RenderState.toString() --- jme3-core/src/main/java/com/jme3/material/RenderState.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 9e3b1d257..22195ad97 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -1594,8 +1594,7 @@ public class RenderState implements Cloneable, Savable { + "\noffsetFactor=" + offsetFactor + "\noffsetUnits=" + offsetUnits + "\nlineWidth=" + lineWidth - + (blendMode.equals(BlendMode.Custom)? - "\ncustomBlendFactors=("+sfactorRGB+", "+dfactorRGB+", "+sfactorAlpha+", "+dfactorAlpha+")": - "\n]"); + + (blendMode.equals(BlendMode.Custom)? "\ncustomBlendFactors=("+sfactorRGB+", "+dfactorRGB+", "+sfactorAlpha+", "+dfactorAlpha+")":"" + +"\n]"); } } From 6728ac111573fdedd3ca4c388daba780a9129a9c Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Sun, 24 Jul 2016 19:57:22 +0200 Subject: [PATCH 3/6] Corrected parenthesis bug. --- jme3-core/src/main/java/com/jme3/material/RenderState.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 22195ad97..93691b471 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -1594,7 +1594,7 @@ public class RenderState implements Cloneable, Savable { + "\noffsetFactor=" + offsetFactor + "\noffsetUnits=" + offsetUnits + "\nlineWidth=" + lineWidth - + (blendMode.equals(BlendMode.Custom)? "\ncustomBlendFactors=("+sfactorRGB+", "+dfactorRGB+", "+sfactorAlpha+", "+dfactorAlpha+")":"" - +"\n]"); + + (blendMode.equals(BlendMode.Custom)? "\ncustomBlendFactors=("+sfactorRGB+", "+dfactorRGB+", "+sfactorAlpha+", "+dfactorAlpha+")":"") + +"\n]"; } } From 187e9944fde20cc48ff8e4a51febc0daae8d0347 Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Sun, 13 Nov 2016 22:44:16 +0100 Subject: [PATCH 4/6] Added the BlendFunc enums in RenderState and updated the GLRenderer accordingly. --- .../java/com/jme3/material/RenderState.java | 117 ++++++++++++++---- .../java/com/jme3/renderer/opengl/GL.java | 1 + .../java/com/jme3/renderer/opengl/GL3.java | 2 + .../com/jme3/renderer/opengl/GLRenderer.java | 38 +++++- 4 files changed, 132 insertions(+), 26 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 93691b471..107b9fe40 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -32,9 +32,7 @@ package com.jme3.material; import com.jme3.export.*; -import com.jme3.renderer.opengl.GL; import com.jme3.scene.Mesh; -import com.jme3.scene.Mesh.Mode; import java.io.IOException; /** @@ -206,6 +204,77 @@ public class RenderState implements Cloneable, Savable { */ Max } + + + /** + * BlendFunc defines the blending functions for use with + * BlendMode.Custom. + * Source color components are referred to as (R_s0, G_s0, B_s0, A_s0). + * Destination color components are referred to as (R_d, G_d, B_d, A_d). + */ + public enum BlendFunc { + /** + * RGB Factor (0, 0, 0), Alpha Factor (0) + */ + Zero, + /** + * RGB Factor (1, 1, 1), Alpha Factor (1) + */ + One, + /** + * RGB Factor (R_s0, G_s0, B_s0), Alpha Factor (A_s0) + */ + Src_Color, + /** + * RGB Factor (1-R_s0, 1-G_s0, 1-B_s0), Alpha Factor (1-A_s0) + */ + One_Minus_Src_Color, + /** + * RGB Factor (R_d, G_d, B_d), Alpha Factor (A_d) + */ + Dst_Color, + /** + * RGB Factor (1-R_d, 1-G_d, 1-B_d), Alpha Factor (1-A_d) + */ + One_Minus_Dst_Color, + /** + * RGB Factor (A_s0, A_s0, A_s0), Alpha Factor (A_s0) + */ + Src_Alpha, + /** + * RGB Factor (1-A_s0, 1-A_s0, 1-A_s0), Alpha Factor (1-A_s0) + */ + One_Minus_Src_Alpha, + /** + * RGB Factor (A_d, A_d, A_d), Alpha Factor (A_d) + */ + Dst_Alpha, + /** + * RGB Factor (1-A_d, 1-A_d, 1-A_d), Alpha Factor (1-A_d) + */ + One_Minus_Dst_Alpha, + /** + * RGB Factor (R_c, G_c, B_c), Alpha Factor (A_c) + */ + //Constant_Color, + /** + * RGB Factor (1-R_c, 1-G_c, 1-B_c), Alpha Factor (1-A_c) + */ + //One_Minus_Constant_Color, + /** + * RGB Factor (A_c, A_c, A_c), Alpha Factor (A_c) + */ + //Constant_Alpha, + /** + * RGB Factor (1-A_c, 1-A_c, 1-A_c), Alpha Factor (A_c) + */ + //One_Minus_Constant_Alpha, + /** + * RGB Factor (i, i, i), Alpha Factor (1) + */ + Src_Alpha_Saturate + } + /** * BlendMode specifies the blending operation to use. @@ -415,10 +484,10 @@ public class RenderState implements Cloneable, Savable { TestFunction frontStencilFunction = TestFunction.Always; TestFunction backStencilFunction = TestFunction.Always; int cachedHashCode = -1; - int sfactorRGB=GL.GL_ONE; - int dfactorRGB=GL.GL_ZERO; - int sfactorAlpha=GL.GL_ONE; - int dfactorAlpha=GL.GL_ZERO; + BlendFunc sfactorRGB=BlendFunc.One; + BlendFunc dfactorRGB=BlendFunc.Zero; + BlendFunc sfactorAlpha=BlendFunc.One; + BlendFunc dfactorAlpha=BlendFunc.Zero; public void write(JmeExporter ex) throws IOException { OutputCapsule oc = ex.getCapsule(this); @@ -445,10 +514,10 @@ public class RenderState implements Cloneable, Savable { oc.write(blendEquationAlpha, "blendEquationAlpha", BlendEquationAlpha.InheritColor); oc.write(depthFunc, "depthFunc", TestFunction.LessOrEqual); oc.write(lineWidth, "lineWidth", 1); - oc.write(sfactorRGB, "sfactorRGB", GL.GL_ONE); - oc.write(dfactorRGB, "dfactorRGB", GL.GL_ZERO); - oc.write(sfactorAlpha, "sfactorAlpha", GL.GL_ONE); - oc.write(dfactorAlpha, "dfactorAlpha", GL.GL_ZERO); + oc.write(sfactorRGB, "sfactorRGB", sfactorRGB); + oc.write(dfactorRGB, "dfactorRGB", dfactorRGB); + oc.write(sfactorAlpha, "sfactorAlpha", sfactorAlpha); + oc.write(dfactorAlpha, "dfactorAlpha", dfactorAlpha); // Only "additional render state" has them set to false by default oc.write(applyWireFrame, "applyWireFrame", true); @@ -489,10 +558,10 @@ public class RenderState implements Cloneable, Savable { blendEquationAlpha = ic.readEnum("blendEquationAlpha", BlendEquationAlpha.class, BlendEquationAlpha.InheritColor); depthFunc = ic.readEnum("depthFunc", TestFunction.class, TestFunction.LessOrEqual); lineWidth = ic.readFloat("lineWidth", 1); - sfactorRGB = ic.readInt("sfactorRGB", GL.GL_ONE); - dfactorAlpha = ic.readInt("dfactorRGB", GL.GL_ZERO); - sfactorRGB = ic.readInt("sfactorAlpha", GL.GL_ONE); - dfactorAlpha = ic.readInt("dfactorAlpha", GL.GL_ZERO); + sfactorRGB = ic.readEnum("sfactorRGB", BlendFunc.class, BlendFunc.One); + dfactorAlpha = ic.readEnum("dfactorRGB", BlendFunc.class, BlendFunc.Zero); + sfactorRGB = ic.readEnum("sfactorAlpha", BlendFunc.class, BlendFunc.One); + dfactorAlpha = ic.readEnum("dfactorAlpha", BlendFunc.class, BlendFunc.Zero); applyWireFrame = ic.readBoolean("applyWireFrame", true); @@ -774,14 +843,14 @@ public class RenderState implements Cloneable, Savable { /** * Sets the custom blend factors for BlendMode.Custom as - * defined by glBlendFuncSeparate. + * defined by the appropriate BlendFunc. * * @param sfactorRGB The source blend factor for RGB components. * @param dfactorRGB The destination blend factor for RGB components. * @param sfactorAlpha The source blend factor for the alpha component. * @param dfactorAlpha The destination blend factor for the alpha component. */ - public void setCustomBlendFactors(int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha) + public void setCustomBlendFactors(BlendFunc sfactorRGB, BlendFunc dfactorRGB, BlendFunc sfactorAlpha, BlendFunc dfactorAlpha) { this.sfactorRGB = sfactorRGB; this.dfactorRGB = dfactorRGB; @@ -1138,7 +1207,7 @@ public class RenderState implements Cloneable, Savable { * * @return the custom source factor for RGB components. */ - public int getCustomSfactorRGB() { + public BlendFunc getCustomSfactorRGB() { return sfactorRGB; } @@ -1148,7 +1217,7 @@ public class RenderState implements Cloneable, Savable { * * @return the custom destination factor for RGB components. */ - public int getCustomDfactorRGB() { + public BlendFunc getCustomDfactorRGB() { return dfactorRGB; } @@ -1158,7 +1227,7 @@ public class RenderState implements Cloneable, Savable { * * @return the custom destination factor for alpha component. */ - public int getCustomSfactorAlpha() { + public BlendFunc getCustomSfactorAlpha() { return sfactorAlpha; } @@ -1168,7 +1237,7 @@ public class RenderState implements Cloneable, Savable { * * @return the custom destination factor for alpha component. */ - public int getCustomDfactorAlpha() { + public BlendFunc getCustomDfactorAlpha() { return dfactorAlpha; } @@ -1394,10 +1463,10 @@ public class RenderState implements Cloneable, Savable { hash = 79 * hash + (this.backStencilFunction != null ? this.backStencilFunction.hashCode() : 0); hash = 79 * hash + Float.floatToIntBits(this.lineWidth); - hash = 79 * hash + this.sfactorRGB; - hash = 79 * hash + this.dfactorRGB; - hash = 79 * hash + this.sfactorAlpha; - hash = 79 * hash + this.dfactorAlpha; + hash = 79 * hash + this.sfactorRGB.hashCode(); + hash = 79 * hash + this.dfactorRGB.hashCode(); + hash = 79 * hash + this.sfactorAlpha.hashCode(); + hash = 79 * hash + this.dfactorAlpha.hashCode(); cachedHashCode = hash; } return cachedHashCode; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java index 30996cd7d..d7894ce83 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java @@ -137,6 +137,7 @@ public interface GL { public static final int GL_SHADING_LANGUAGE_VERSION = 0x8B8C; public static final int GL_SHORT = 0x1402; public static final int GL_SRC_ALPHA = 0x302; + public static final int GL_SRC_ALPHA_SATURATE = 0x0308; public static final int GL_SRC_COLOR = 0x300; public static final int GL_STATIC_DRAW = 0x88E4; public static final int GL_STENCIL_BUFFER_BIT = 0x400; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java index 1fbad5c2d..63b24ccb1 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java @@ -41,7 +41,9 @@ import java.nio.IntBuffer; public interface GL3 extends GL2 { public static final int GL_DEPTH_STENCIL_ATTACHMENT = 0x821A; + public static final int GL_DST_ALPHA = 0x0304; public static final int GL_GEOMETRY_SHADER = 0x8DD9; + public static final int GL_ONE_MINUS_DST_ALPHA = 0x0305; public static final int GL_NUM_EXTENSIONS = 0x821D; public static final int GL_R8 = 0x8229; public static final int GL_R16F = 0x822D; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index bcb4a4d5c..8d38c5ad7 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -32,6 +32,7 @@ package com.jme3.renderer.opengl; import com.jme3.material.RenderState; +import com.jme3.material.RenderState.BlendFunc; import com.jme3.material.RenderState.StencilOperation; import com.jme3.material.RenderState.TestFunction; import com.jme3.math.*; @@ -749,8 +750,12 @@ public final class GLRenderer implements Renderer { gl.glBlendFunc(GL.GL_ONE_MINUS_DST_COLOR, GL.GL_ONE_MINUS_SRC_COLOR); break; case Custom: - gl.glBlendFuncSeparate(state.getCustomSfactorRGB(), state.getCustomDfactorRGB(), state.getCustomSfactorAlpha(), state.getCustomDfactorAlpha()); - break; + gl.glBlendFuncSeparate( + convertBlendFunc(state.getCustomSfactorRGB()), + convertBlendFunc(state.getCustomDfactorRGB()), + convertBlendFunc(state.getCustomSfactorAlpha()), + convertBlendFunc(state.getCustomDfactorAlpha())); + break; default: throw new UnsupportedOperationException("Unrecognized blend mode: " + state.getBlendMode()); @@ -853,6 +858,35 @@ public final class GLRenderer implements Renderer { } } + private int convertBlendFunc(BlendFunc blendFunc) { + switch (blendFunc) { + case Zero: + return GL.GL_ZERO; + case One: + return GL.GL_ONE; + case Src_Color: + return GL.GL_SRC_COLOR; + case One_Minus_Src_Color: + return GL.GL_ONE_MINUS_SRC_COLOR; + case Dst_Color: + return GL.GL_DST_COLOR; + case One_Minus_Dst_Color: + return GL.GL_ONE_MINUS_DST_COLOR; + case Src_Alpha: + return GL.GL_SRC_ALPHA; + case One_Minus_Src_Alpha: + return GL.GL_ONE_MINUS_SRC_ALPHA; + case Dst_Alpha: + return GL3.GL_DST_ALPHA; + case One_Minus_Dst_Alpha: + return GL3.GL_ONE_MINUS_DST_ALPHA; + case Src_Alpha_Saturate: + return GL.GL_SRC_ALPHA_SATURATE; + default: + throw new UnsupportedOperationException("Unrecognized blend function: " + blendFunc); + } + } + private int convertStencilOperation(StencilOperation stencilOp) { switch (stencilOp) { case Keep: From ca17bd592a6fdee1be8fbc413128ce43305d3cb1 Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Mon, 14 Nov 2016 10:10:19 +0100 Subject: [PATCH 5/6] Removed the switch/case statements in convertBlendFunc() by extending the BlendFunc enumeration with a convenience integer value. --- .../java/com/jme3/material/RenderState.java | 53 ++++++++++--------- .../com/jme3/renderer/opengl/GLRenderer.java | 37 ++----------- 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 107b9fe40..56b608e33 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -216,63 +216,64 @@ public class RenderState implements Cloneable, Savable { /** * RGB Factor (0, 0, 0), Alpha Factor (0) */ - Zero, + Zero(0), /** * RGB Factor (1, 1, 1), Alpha Factor (1) */ - One, + One(1), /** * RGB Factor (R_s0, G_s0, B_s0), Alpha Factor (A_s0) */ - Src_Color, + Src_Color(2), /** * RGB Factor (1-R_s0, 1-G_s0, 1-B_s0), Alpha Factor (1-A_s0) */ - One_Minus_Src_Color, + One_Minus_Src_Color(3), /** * RGB Factor (R_d, G_d, B_d), Alpha Factor (A_d) */ - Dst_Color, + Dst_Color(4), /** * RGB Factor (1-R_d, 1-G_d, 1-B_d), Alpha Factor (1-A_d) */ - One_Minus_Dst_Color, + One_Minus_Dst_Color(5), /** * RGB Factor (A_s0, A_s0, A_s0), Alpha Factor (A_s0) */ - Src_Alpha, + Src_Alpha(6), /** * RGB Factor (1-A_s0, 1-A_s0, 1-A_s0), Alpha Factor (1-A_s0) */ - One_Minus_Src_Alpha, + One_Minus_Src_Alpha(7), /** * RGB Factor (A_d, A_d, A_d), Alpha Factor (A_d) */ - Dst_Alpha, + Dst_Alpha(8), /** * RGB Factor (1-A_d, 1-A_d, 1-A_d), Alpha Factor (1-A_d) */ - One_Minus_Dst_Alpha, - /** - * RGB Factor (R_c, G_c, B_c), Alpha Factor (A_c) - */ - //Constant_Color, - /** - * RGB Factor (1-R_c, 1-G_c, 1-B_c), Alpha Factor (1-A_c) - */ - //One_Minus_Constant_Color, - /** - * RGB Factor (A_c, A_c, A_c), Alpha Factor (A_c) - */ - //Constant_Alpha, + One_Minus_Dst_Alpha(9), /** - * RGB Factor (1-A_c, 1-A_c, 1-A_c), Alpha Factor (A_c) + * RGB Factor (i, i, i), Alpha Factor (1) */ - //One_Minus_Constant_Alpha, + Src_Alpha_Saturate(10); + + // Convenience value for easy use by the renderer. + private final int value; + + // Convenience constructor. + private BlendFunc(int value) { + this.value=value; + } + /** - * RGB Factor (i, i, i), Alpha Factor (1) + * Provides a predefined integer value for easy use by the renderer. + * + * @return The predefined integer value. */ - Src_Alpha_Saturate + public int getIntValue() { + return value; + } } diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 8d38c5ad7..72bff6586 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -751,10 +751,10 @@ public final class GLRenderer implements Renderer { break; case Custom: gl.glBlendFuncSeparate( - convertBlendFunc(state.getCustomSfactorRGB()), - convertBlendFunc(state.getCustomDfactorRGB()), - convertBlendFunc(state.getCustomSfactorAlpha()), - convertBlendFunc(state.getCustomDfactorAlpha())); + state.getCustomSfactorRGB().getIntValue(), + state.getCustomDfactorRGB().getIntValue(), + state.getCustomSfactorAlpha().getIntValue(), + state.getCustomDfactorAlpha().getIntValue()); break; default: throw new UnsupportedOperationException("Unrecognized blend mode: " @@ -858,35 +858,6 @@ public final class GLRenderer implements Renderer { } } - private int convertBlendFunc(BlendFunc blendFunc) { - switch (blendFunc) { - case Zero: - return GL.GL_ZERO; - case One: - return GL.GL_ONE; - case Src_Color: - return GL.GL_SRC_COLOR; - case One_Minus_Src_Color: - return GL.GL_ONE_MINUS_SRC_COLOR; - case Dst_Color: - return GL.GL_DST_COLOR; - case One_Minus_Dst_Color: - return GL.GL_ONE_MINUS_DST_COLOR; - case Src_Alpha: - return GL.GL_SRC_ALPHA; - case One_Minus_Src_Alpha: - return GL.GL_ONE_MINUS_SRC_ALPHA; - case Dst_Alpha: - return GL3.GL_DST_ALPHA; - case One_Minus_Dst_Alpha: - return GL3.GL_ONE_MINUS_DST_ALPHA; - case Src_Alpha_Saturate: - return GL.GL_SRC_ALPHA_SATURATE; - default: - throw new UnsupportedOperationException("Unrecognized blend function: " + blendFunc); - } - } - private int convertStencilOperation(StencilOperation stencilOp) { switch (stencilOp) { case Keep: From 67881b1e011a897f2eabe27839b88c7c4e679b0e Mon Sep 17 00:00:00 2001 From: Michael Braunstingl Date: Mon, 14 Nov 2016 23:06:01 +0100 Subject: [PATCH 6/6] Two changes: 1. GL_DST_ALPHA and GL_ONE_MINUS_DST_ALPHA were moved from GL3 to GL. 2. I reverted to the previous switch/case statements in GLRenderer.convertBlendFunc(BlendFunc). --- .../java/com/jme3/material/RenderState.java | 41 +++++-------------- .../java/com/jme3/renderer/opengl/GL.java | 2 + .../java/com/jme3/renderer/opengl/GL3.java | 2 - .../com/jme3/renderer/opengl/GLRenderer.java | 37 +++++++++++++++-- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/RenderState.java b/jme3-core/src/main/java/com/jme3/material/RenderState.java index 56b608e33..f6236efd0 100644 --- a/jme3-core/src/main/java/com/jme3/material/RenderState.java +++ b/jme3-core/src/main/java/com/jme3/material/RenderState.java @@ -205,7 +205,6 @@ public class RenderState implements Cloneable, Savable { Max } - /** * BlendFunc defines the blending functions for use with * BlendMode.Custom. @@ -216,67 +215,49 @@ public class RenderState implements Cloneable, Savable { /** * RGB Factor (0, 0, 0), Alpha Factor (0) */ - Zero(0), + Zero, /** * RGB Factor (1, 1, 1), Alpha Factor (1) */ - One(1), + One, /** * RGB Factor (R_s0, G_s0, B_s0), Alpha Factor (A_s0) */ - Src_Color(2), + Src_Color, /** * RGB Factor (1-R_s0, 1-G_s0, 1-B_s0), Alpha Factor (1-A_s0) */ - One_Minus_Src_Color(3), + One_Minus_Src_Color, /** * RGB Factor (R_d, G_d, B_d), Alpha Factor (A_d) */ - Dst_Color(4), + Dst_Color, /** * RGB Factor (1-R_d, 1-G_d, 1-B_d), Alpha Factor (1-A_d) */ - One_Minus_Dst_Color(5), + One_Minus_Dst_Color, /** * RGB Factor (A_s0, A_s0, A_s0), Alpha Factor (A_s0) */ - Src_Alpha(6), + Src_Alpha, /** * RGB Factor (1-A_s0, 1-A_s0, 1-A_s0), Alpha Factor (1-A_s0) */ - One_Minus_Src_Alpha(7), + One_Minus_Src_Alpha, /** * RGB Factor (A_d, A_d, A_d), Alpha Factor (A_d) */ - Dst_Alpha(8), + Dst_Alpha, /** * RGB Factor (1-A_d, 1-A_d, 1-A_d), Alpha Factor (1-A_d) */ - One_Minus_Dst_Alpha(9), + One_Minus_Dst_Alpha, /** * RGB Factor (i, i, i), Alpha Factor (1) */ - Src_Alpha_Saturate(10); - - // Convenience value for easy use by the renderer. - private final int value; - - // Convenience constructor. - private BlendFunc(int value) { - this.value=value; - } - - /** - * Provides a predefined integer value for easy use by the renderer. - * - * @return The predefined integer value. - */ - public int getIntValue() { - return value; - } + Src_Alpha_Saturate; } - /** * BlendMode specifies the blending operation to use. * diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java index d7894ce83..136442f77 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java @@ -62,6 +62,7 @@ public interface GL { public static final int GL_DEPTH_COMPONENT16 = 0x81A5; public static final int GL_DEPTH_TEST = 0xB71; public static final int GL_DOUBLE = 0x140A; + public static final int GL_DST_ALPHA = 0x0304; public static final int GL_DST_COLOR = 0x306; public static final int GL_DYNAMIC_DRAW = 0x88E8; public static final int GL_ELEMENT_ARRAY_BUFFER = 0x8893; @@ -118,6 +119,7 @@ public interface GL { public static final int GL_NONE = 0x0; public static final int GL_NOTEQUAL = 0x205; public static final int GL_ONE = 0x1; + public static final int GL_ONE_MINUS_DST_ALPHA = 0x0305; public static final int GL_ONE_MINUS_DST_COLOR = 0x307; public static final int GL_ONE_MINUS_SRC_ALPHA = 0x303; public static final int GL_ONE_MINUS_SRC_COLOR = 0x301; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java index 63b24ccb1..1fbad5c2d 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java @@ -41,9 +41,7 @@ import java.nio.IntBuffer; public interface GL3 extends GL2 { public static final int GL_DEPTH_STENCIL_ATTACHMENT = 0x821A; - public static final int GL_DST_ALPHA = 0x0304; public static final int GL_GEOMETRY_SHADER = 0x8DD9; - public static final int GL_ONE_MINUS_DST_ALPHA = 0x0305; public static final int GL_NUM_EXTENSIONS = 0x821D; public static final int GL_R8 = 0x8229; public static final int GL_R16F = 0x822D; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 72bff6586..72a97d426 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -751,10 +751,10 @@ public final class GLRenderer implements Renderer { break; case Custom: gl.glBlendFuncSeparate( - state.getCustomSfactorRGB().getIntValue(), - state.getCustomDfactorRGB().getIntValue(), - state.getCustomSfactorAlpha().getIntValue(), - state.getCustomDfactorAlpha().getIntValue()); + convertBlendFunc(state.getCustomSfactorRGB()), + convertBlendFunc(state.getCustomDfactorRGB()), + convertBlendFunc(state.getCustomSfactorAlpha()), + convertBlendFunc(state.getCustomDfactorAlpha())); break; default: throw new UnsupportedOperationException("Unrecognized blend mode: " @@ -857,6 +857,35 @@ public final class GLRenderer implements Renderer { throw new UnsupportedOperationException("Unrecognized alpha blend operation: " + blendEquationAlpha); } } + + private int convertBlendFunc(BlendFunc blendFunc) { + switch (blendFunc) { + case Zero: + return GL.GL_ZERO; + case One: + return GL.GL_ONE; + case Src_Color: + return GL.GL_SRC_COLOR; + case One_Minus_Src_Color: + return GL.GL_ONE_MINUS_SRC_COLOR; + case Dst_Color: + return GL.GL_DST_COLOR; + case One_Minus_Dst_Color: + return GL.GL_ONE_MINUS_DST_COLOR; + case Src_Alpha: + return GL.GL_SRC_ALPHA; + case One_Minus_Src_Alpha: + return GL.GL_ONE_MINUS_SRC_ALPHA; + case Dst_Alpha: + return GL.GL_DST_ALPHA; + case One_Minus_Dst_Alpha: + return GL.GL_ONE_MINUS_DST_ALPHA; + case Src_Alpha_Saturate: + return GL.GL_SRC_ALPHA_SATURATE; + default: + throw new UnsupportedOperationException("Unrecognized blend function operation: " + blendFunc); + } + } private int convertStencilOperation(StencilOperation stencilOp) { switch (stencilOp) {