Skip to content

Commit cd7c60c

Browse files
theMinkaNehon
authored andcommitted
Fixes handling of blend equations and factors in GLRenderer (jMonkeyEngine#848)
* Stronger separation of BlendMode.Custom from the other blend modes * Custom blend equations and factors now only gets used on BlendMode.Custom * Updated some JavaDocs in RenderState class
1 parent 2f683c1 commit cd7c60c

3 files changed

Lines changed: 219 additions & 162 deletions

File tree

jme3-core/src/main/java/com/jme3/material/RenderState.java

Lines changed: 81 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,17 @@ public enum BlendMode {
331331
*/
332332
Exclusion,
333333
/**
334-
* Allows for custom blending by using glBlendFuncSeparate.
334+
* Uses the blend equations and blend factors defined by the render state.
335335
* <p>
336-
*
336+
* These attributes can be set by using the following methods:
337+
* <ul>
338+
* <li>{@link RenderState#setBlendEquation(BlendEquation)}<br/>
339+
* <li>{@link RenderState#setBlendEquationAlpha(BlendEquationAlpha)}<br/>
340+
* <li>{@link RenderState#setCustomBlendFactors(BlendFunc, BlendFunc, BlendFunc, BlendFunc)}<br/>
341+
* </ul>
342+
* <p>
343+
* Result.RGB = BlendEquation( sfactorRGB * Source.RGB , dfactorRGB * Destination.RGB )<br/>
344+
* Result.A = BlendEquationAlpha( sfactorAlpha * Source.A , dfactorAlpha * Destination.A )
337345
*/
338346
Custom
339347
}
@@ -425,8 +433,6 @@ public enum StencilOperation {
425433
ADDITIONAL.applyDepthWrite = false;
426434
ADDITIONAL.applyDepthTest = false;
427435
ADDITIONAL.applyColorWrite = false;
428-
ADDITIONAL.applyBlendEquation = false;
429-
ADDITIONAL.applyBlendEquationAlpha = false;
430436
ADDITIONAL.applyBlendMode = false;
431437
ADDITIONAL.applyPolyOffset = false;
432438
}
@@ -441,9 +447,7 @@ public enum StencilOperation {
441447
boolean colorWrite = true;
442448
boolean applyColorWrite = true;
443449
BlendEquation blendEquation = BlendEquation.Add;
444-
boolean applyBlendEquation = true;
445450
BlendEquationAlpha blendEquationAlpha = BlendEquationAlpha.InheritColor;
446-
boolean applyBlendEquationAlpha = true;
447451
BlendMode blendMode = BlendMode.Off;
448452
boolean applyBlendMode = true;
449453
float offsetFactor = 0;
@@ -466,10 +470,10 @@ public enum StencilOperation {
466470
TestFunction frontStencilFunction = TestFunction.Always;
467471
TestFunction backStencilFunction = TestFunction.Always;
468472
int cachedHashCode = -1;
469-
BlendFunc sfactorRGB=BlendFunc.One;
470-
BlendFunc dfactorRGB=BlendFunc.Zero;
471-
BlendFunc sfactorAlpha=BlendFunc.One;
472-
BlendFunc dfactorAlpha=BlendFunc.Zero;
473+
BlendFunc sfactorRGB = BlendFunc.One;
474+
BlendFunc dfactorRGB = BlendFunc.One;
475+
BlendFunc sfactorAlpha = BlendFunc.One;
476+
BlendFunc dfactorAlpha = BlendFunc.One;
473477

474478
public void write(JmeExporter ex) throws IOException {
475479
OutputCapsule oc = ex.getCapsule(this);
@@ -507,8 +511,6 @@ public void write(JmeExporter ex) throws IOException {
507511
oc.write(applyDepthWrite, "applyDepthWrite", true);
508512
oc.write(applyDepthTest, "applyDepthTest", true);
509513
oc.write(applyColorWrite, "applyColorWrite", true);
510-
oc.write(applyBlendEquation, "applyBlendEquation", true);
511-
oc.write(applyBlendEquationAlpha, "applyBlendEquationAlpha", true);
512514
oc.write(applyBlendMode, "applyBlendMode", true);
513515
oc.write(applyPolyOffset, "applyPolyOffset", true);
514516
oc.write(applyDepthFunc, "applyDepthFunc", true);
@@ -541,24 +543,21 @@ public void read(JmeImporter im) throws IOException {
541543
depthFunc = ic.readEnum("depthFunc", TestFunction.class, TestFunction.LessOrEqual);
542544
lineWidth = ic.readFloat("lineWidth", 1);
543545
sfactorRGB = ic.readEnum("sfactorRGB", BlendFunc.class, BlendFunc.One);
544-
dfactorAlpha = ic.readEnum("dfactorRGB", BlendFunc.class, BlendFunc.Zero);
546+
dfactorAlpha = ic.readEnum("dfactorRGB", BlendFunc.class, BlendFunc.One);
545547
sfactorRGB = ic.readEnum("sfactorAlpha", BlendFunc.class, BlendFunc.One);
546-
dfactorAlpha = ic.readEnum("dfactorAlpha", BlendFunc.class, BlendFunc.Zero);
548+
dfactorAlpha = ic.readEnum("dfactorAlpha", BlendFunc.class, BlendFunc.One);
547549

548550

549551
applyWireFrame = ic.readBoolean("applyWireFrame", true);
550552
applyCullMode = ic.readBoolean("applyCullMode", true);
551553
applyDepthWrite = ic.readBoolean("applyDepthWrite", true);
552554
applyDepthTest = ic.readBoolean("applyDepthTest", true);
553555
applyColorWrite = ic.readBoolean("applyColorWrite", true);
554-
applyBlendEquation = ic.readBoolean("applyBlendEquation", true);
555-
applyBlendEquationAlpha = ic.readBoolean("applyBlendEquationAlpha", true);
556556
applyBlendMode = ic.readBoolean("applyBlendMode", true);
557557
applyPolyOffset = ic.readBoolean("applyPolyOffset", true);
558558
applyDepthFunc = ic.readBoolean("applyDepthFunc", true);
559559
applyLineWidth = ic.readBoolean("applyLineWidth", true);
560560

561-
562561
}
563562

564563
/**
@@ -615,19 +614,32 @@ public boolean equals(Object o) {
615614
return false;
616615
}
617616

618-
if (blendEquation != rs.blendEquation) {
617+
if (blendMode != rs.blendMode) {
619618
return false;
620619
}
621620

622-
if (blendEquationAlpha != rs.blendEquationAlpha) {
623-
return false;
624-
}
621+
if (blendMode == BlendMode.Custom) {
622+
if (blendEquation != rs.blendEquation) {
623+
return false;
624+
}
625+
if (blendEquationAlpha != rs.blendEquationAlpha) {
626+
return false;
627+
}
625628

626-
if (blendMode != rs.blendMode) {
627-
return false;
629+
if (sfactorRGB != rs.sfactorRGB) {
630+
return false;
631+
}
632+
if (dfactorRGB != rs.dfactorRGB) {
633+
return false;
634+
}
635+
if (sfactorAlpha != rs.sfactorAlpha) {
636+
return false;
637+
}
638+
if (dfactorAlpha != rs.dfactorAlpha) {
639+
return false;
640+
}
628641
}
629642

630-
631643
if (offsetEnabled != rs.offsetEnabled) {
632644
return false;
633645
}
@@ -675,14 +687,6 @@ public boolean equals(Object o) {
675687
if(lineWidth != rs.lineWidth){
676688
return false;
677689
}
678-
679-
if (blendMode.equals(BlendMode.Custom)) {
680-
return sfactorRGB==rs.getCustomSfactorRGB()
681-
&& dfactorRGB==rs.getCustomDfactorRGB()
682-
&& sfactorAlpha==rs.getCustomSfactorAlpha()
683-
&& dfactorAlpha==rs.getCustomDfactorAlpha();
684-
685-
}
686690

687691
return true;
688692
}
@@ -768,80 +772,68 @@ public void setBlendMode(BlendMode blendMode) {
768772
}
769773

770774
/**
771-
* Set the blending equation.
775+
* Set the blending equation for the color component (RGB).
772776
* <p>
773-
* When blending is enabled, (<code>blendMode</code> is not
774-
* {@link BlendMode#Off}) the input pixel will be blended with the pixel
775-
* already in the color buffer. The blending equation is determined by the
776-
* {@link BlendEquation}. For example, the mode {@link BlendMode#Additive}
777-
* and {@link BlendEquation#Add} will add the input pixel's color to the
778-
* color already in the color buffer:
777+
* The blending equation determines, how the RGB values of the input pixel
778+
* will be blended with the RGB values of the pixel already in the color buffer.<br/>
779+
* For example, {@link BlendEquation#Add} will add the input pixel's color
780+
* to the color already in the color buffer:
779781
* <br/>
780782
* <code>Result = Source Color + Destination Color</code>
781-
* <br/>
782-
* However, the mode {@link BlendMode#Additive}
783-
* and {@link BlendEquation#Subtract} will subtract the input pixel's color to the
784-
* color already in the color buffer:
785-
* <br/>
786-
* <code>Result = Source Color - Destination Color</code>
783+
* <p>
784+
* <b>Note:</b> This gets only used in {@link BlendMode#Custom} mode.
785+
* All other blend modes will ignore this setting.
787786
*
788-
* @param blendEquation The blend equation to use.
787+
* @param blendEquation The {@link BlendEquation} to use.
789788
*/
790789
public void setBlendEquation(BlendEquation blendEquation) {
791-
applyBlendEquation = true;
792790
this.blendEquation = blendEquation;
793791
cachedHashCode = -1;
794792
}
795-
793+
796794
/**
797795
* Set the blending equation for the alpha component.
798796
* <p>
799-
* When blending is enabled, (<code>blendMode</code> is not
800-
* {@link BlendMode#Off}) the input pixel will be blended with the pixel
801-
* already in the color buffer. The blending equation is determined by the
802-
* {@link BlendEquation} and can be overrode for the alpha component using
803-
* the {@link BlendEquationAlpha} . For example, the mode
804-
* {@link BlendMode#Additive} and {@link BlendEquationAlpha#Add} will add
805-
* the input pixel's alpha to the alpha component already in the color
806-
* buffer:
797+
* The alpha blending equation determines, how the alpha values of the input pixel
798+
* will be blended with the alpha values of the pixel already in the color buffer.<br/>
799+
* For example, {@link BlendEquationAlpha#Add} will add the input pixel's color
800+
* to the color already in the color buffer:
807801
* <br/>
808-
* <code>Result = Source Alpha + Destination Alpha</code>
809-
* <br/>
810-
* However, the mode {@link BlendMode#Additive} and
811-
* {@link BlendEquationAlpha#Subtract} will subtract the input pixel's alpha
812-
* to the alpha component already in the color buffer:
813-
* <br/>
814-
* <code>Result = Source Alpha - Destination Alpha</code>
802+
* <code>Result = Source Color + Destination Color</code>
803+
* <p>
804+
* <b>Note:</b> This gets only used in {@link BlendMode#Custom} mode.
805+
* All other blend modes will ignore this setting.
815806
*
816-
* @param blendEquationAlpha The blend equation to use for the alpha
817-
* component.
807+
* @param blendEquationAlpha The {@link BlendEquationAlpha} to use.
818808
*/
819809
public void setBlendEquationAlpha(BlendEquationAlpha blendEquationAlpha) {
820-
applyBlendEquationAlpha = true;
821810
this.blendEquationAlpha = blendEquationAlpha;
822811
cachedHashCode = -1;
823812
}
824813

825-
826814
/**
827-
* Sets the custom blend factors for <code>BlendMode.Custom</code> as
828-
* defined by the appropriate <code>BlendFunc</code>.
829-
*
815+
* Sets the blend factors used for the source and destination color.
816+
* <p>
817+
* These factors will be multiplied with the color values of the input pixel
818+
* and the pixel already in the color buffer, before both colors gets combined by the {@link BlendEquation}.
819+
* <p>
820+
* <b>Note:</b> This gets only used in {@link BlendMode#Custom} mode.
821+
* All other blend modes will ignore this setting.
822+
*
830823
* @param sfactorRGB The source blend factor for RGB components.
831824
* @param dfactorRGB The destination blend factor for RGB components.
832825
* @param sfactorAlpha The source blend factor for the alpha component.
833826
* @param dfactorAlpha The destination blend factor for the alpha component.
834827
*/
835-
public void setCustomBlendFactors(BlendFunc sfactorRGB, BlendFunc dfactorRGB, BlendFunc sfactorAlpha, BlendFunc dfactorAlpha)
836-
{
828+
public void setCustomBlendFactors(BlendFunc sfactorRGB, BlendFunc dfactorRGB, BlendFunc sfactorAlpha, BlendFunc dfactorAlpha) {
837829
this.sfactorRGB = sfactorRGB;
838830
this.dfactorRGB = dfactorRGB;
839831
this.sfactorAlpha = sfactorAlpha;
840832
this.dfactorAlpha = dfactorAlpha;
841833
cachedHashCode = -1;
842834
}
843-
844-
835+
836+
845837
/**
846838
* Enable depth testing.
847839
*
@@ -1374,14 +1366,6 @@ public boolean isApplyBlendMode() {
13741366
return applyBlendMode;
13751367
}
13761368

1377-
public boolean isApplyBlendEquation() {
1378-
return applyBlendEquation;
1379-
}
1380-
1381-
public boolean isApplyBlendEquationAlpha() {
1382-
return applyBlendEquationAlpha;
1383-
}
1384-
13851369
public boolean isApplyColorWrite() {
13861370
return applyColorWrite;
13871371
}
@@ -1511,27 +1495,26 @@ public RenderState copyMergedTo(RenderState additionalState, RenderState state)
15111495
} else {
15121496
state.colorWrite = colorWrite;
15131497
}
1514-
if (additionalState.applyBlendEquation) {
1515-
state.blendEquation = additionalState.blendEquation;
1516-
} else {
1517-
state.blendEquation = blendEquation;
1518-
}
1519-
if (additionalState.applyBlendEquationAlpha) {
1520-
state.blendEquationAlpha = additionalState.blendEquationAlpha;
1521-
} else {
1522-
state.blendEquationAlpha = blendEquationAlpha;
1523-
}
15241498
if (additionalState.applyBlendMode) {
15251499
state.blendMode = additionalState.blendMode;
1526-
if (additionalState.getBlendMode().equals(BlendMode.Custom)) {
1527-
state.setCustomBlendFactors(
1528-
additionalState.getCustomSfactorRGB(),
1529-
additionalState.getCustomDfactorRGB(),
1530-
additionalState.getCustomSfactorAlpha(),
1531-
additionalState.getCustomDfactorAlpha());
1500+
if (additionalState.blendMode == BlendMode.Custom) {
1501+
state.blendEquation = additionalState.blendEquation;
1502+
state.blendEquationAlpha = additionalState.blendEquationAlpha;
1503+
state.sfactorRGB = additionalState.sfactorRGB;
1504+
state.dfactorRGB = additionalState.dfactorRGB;
1505+
state.sfactorAlpha = additionalState.sfactorAlpha;
1506+
state.dfactorAlpha = additionalState.dfactorAlpha;
15321507
}
15331508
} else {
15341509
state.blendMode = blendMode;
1510+
if (blendMode == BlendMode.Custom) {
1511+
state.blendEquation = blendEquation;
1512+
state.blendEquationAlpha = blendEquationAlpha;
1513+
state.sfactorRGB = sfactorRGB;
1514+
state.dfactorRGB = dfactorRGB;
1515+
state.sfactorAlpha = sfactorAlpha;
1516+
state.dfactorAlpha = dfactorAlpha;
1517+
}
15351518
}
15361519

15371520
if (additionalState.applyPolyOffset) {
@@ -1608,8 +1591,6 @@ public void set(RenderState state) {
16081591
applyDepthWrite = true;
16091592
applyDepthTest = true;
16101593
applyColorWrite = true;
1611-
applyBlendEquation = true;
1612-
applyBlendEquationAlpha = true;
16131594
applyBlendMode = true;
16141595
applyPolyOffset = true;
16151596
applyDepthFunc = true;
@@ -1636,8 +1617,6 @@ public String toString() {
16361617
+ "\ncolorWrite=" + colorWrite
16371618
+ "\napplyColorWrite=" + applyColorWrite
16381619
+ "\nblendEquation=" + blendEquation
1639-
+ "\napplyBlendEquation=" + applyBlendEquation
1640-
+ "\napplyBlendEquationAlpha=" + applyBlendEquationAlpha
16411620
+ "\nblendMode=" + blendMode
16421621
+ "\napplyBlendMode=" + applyBlendMode
16431622
+ "\noffsetEnabled=" + offsetEnabled

jme3-core/src/main/java/com/jme3/renderer/RenderContext.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package com.jme3.renderer;
3333

3434
import com.jme3.material.RenderState;
35+
import com.jme3.material.RenderState.BlendFunc;
3536
import com.jme3.math.ColorRGBA;
3637
import com.jme3.scene.Mesh;
3738
import com.jme3.scene.VertexBuffer;
@@ -110,6 +111,30 @@ public class RenderContext {
110111
*/
111112
public RenderState.BlendEquationAlpha blendEquationAlpha = RenderState.BlendEquationAlpha.InheritColor;
112113

114+
/**
115+
* @see RenderState#setCustomBlendFactors(com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc,
116+
* com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc)
117+
*/
118+
public RenderState.BlendFunc sfactorRGB = RenderState.BlendFunc.One;
119+
120+
/**
121+
* @see RenderState#setCustomBlendFactors(com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc,
122+
* com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc)
123+
*/
124+
public RenderState.BlendFunc dfactorRGB = RenderState.BlendFunc.One;
125+
126+
/**
127+
* @see RenderState#setCustomBlendFactors(com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc,
128+
* com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc)
129+
*/
130+
public RenderState.BlendFunc sfactorAlpha = RenderState.BlendFunc.One;
131+
132+
/**
133+
* @see RenderState#setCustomBlendFactors(com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc,
134+
* com.jme3.material.RenderState.BlendFunc, com.jme3.material.RenderState.BlendFunc)
135+
*/
136+
public RenderState.BlendFunc dfactorAlpha = RenderState.BlendFunc.One;
137+
113138
/**
114139
* @see RenderState#setWireframe(boolean)
115140
*/
@@ -266,6 +291,10 @@ public void reset(){
266291
blendMode = RenderState.BlendMode.Off;
267292
blendEquation = RenderState.BlendEquation.Add;
268293
blendEquationAlpha = RenderState.BlendEquationAlpha.InheritColor;
294+
sfactorRGB = BlendFunc.One;
295+
dfactorRGB = BlendFunc.One;
296+
sfactorAlpha = BlendFunc.One;
297+
dfactorAlpha = BlendFunc.One;
269298
wireframe = false;
270299
boundShaderProgram = 0;
271300
boundShader = null;

0 commit comments

Comments
 (0)