Browse Source

Added the BlendFunc enums in RenderState and updated the GLRenderer
accordingly.

Michael Braunstingl 8 years ago
parent
commit
187e9944fd

+ 93 - 24
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
     }
+    
+    
+    /**
+     * <code>BlendFunc</code> defines the blending functions for use with 
+     * <code>BlendMode.Custom</code>.
+     * 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
+    }
+    
 
     /**
      * <code>BlendMode</code> 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 <code>BlendMode.Custom</code> as 
-     * defined by glBlendFuncSeparate.
+     * defined by the appropriate <code>BlendFunc</code>.
      * 
      * @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;

+ 1 - 0
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;

+ 2 - 0
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;

+ 36 - 2
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: