From 01bf1ca84386bb3c6b67cdd232b9022a9bbfcbc0 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Sun, 23 Dec 2012 13:55:17 +0000 Subject: [PATCH] Fixed 2 issues with shadows : - Looks like Poly offset behave defferently between ATI and NVIDIA i added a bit of offset to avoid shadow acne for NVIDIA - Added a flag needCompareModeUpdate in the Texture class to avoid setting it on every frame in the renderer. I changed Lwjgl and Jogl renderer accordingly. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10043 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../Common/MatDefs/Light/Lighting.j3md | 2 +- .../Common/MatDefs/Shadow/PreShadow.j3md | 2 +- engine/src/core/com/jme3/texture/Texture.java | 12 ++++++++++ .../com/jme3/renderer/jogl/JoglRenderer.java | 20 +++++++++++------ .../jme3/renderer/lwjgl/LwjglRenderer.java | 22 ++++++++++++------- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/engine/src/core-data/Common/MatDefs/Light/Lighting.j3md b/engine/src/core-data/Common/MatDefs/Light/Lighting.j3md index 349c0ded4..54bfdee76 100644 --- a/engine/src/core-data/Common/MatDefs/Light/Lighting.j3md +++ b/engine/src/core-data/Common/MatDefs/Light/Lighting.j3md @@ -192,7 +192,7 @@ MaterialDef Phong Lighting { FaceCull Off DepthTest On DepthWrite On - PolyOffset 5 0 + PolyOffset 5 3 ColorWrite Off } diff --git a/engine/src/core-data/Common/MatDefs/Shadow/PreShadow.j3md b/engine/src/core-data/Common/MatDefs/Shadow/PreShadow.j3md index 070949f42..d270e71a9 100644 --- a/engine/src/core-data/Common/MatDefs/Shadow/PreShadow.j3md +++ b/engine/src/core-data/Common/MatDefs/Shadow/PreShadow.j3md @@ -12,7 +12,7 @@ MaterialDef Pre Shadow { FaceCull Off DepthTest On DepthWrite On - PolyOffset 5 0 + PolyOffset 5 3 ColorWrite Off } } diff --git a/engine/src/core/com/jme3/texture/Texture.java b/engine/src/core/com/jme3/texture/Texture.java index 1b81504cd..925f49df7 100644 --- a/engine/src/core/com/jme3/texture/Texture.java +++ b/engine/src/core/com/jme3/texture/Texture.java @@ -298,6 +298,7 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable private MinFilter minificationFilter = MinFilter.BilinearNoMipMaps; private MagFilter magnificationFilter = MagFilter.Bilinear; private ShadowCompareMode shadowCompareMode = ShadowCompareMode.Off; + private boolean needCompareModeUpdate = false; private int anisotropicFilter; /** @@ -385,6 +386,7 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable "compareMode can not be null."); } this.shadowCompareMode = compareMode; + needCompareModeUpdate = true; } /** @@ -619,4 +621,14 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable magnificationFilter = capsule.readEnum("magnificationFilter", MagFilter.class, MagFilter.Bilinear); } + + public boolean isNeedCompareModeUpdate() { + return needCompareModeUpdate; + } + + public void compareModeUpdated() { + this.needCompareModeUpdate = false; + } + + } \ No newline at end of file diff --git a/engine/src/jogl/com/jme3/renderer/jogl/JoglRenderer.java b/engine/src/jogl/com/jme3/renderer/jogl/JoglRenderer.java index 8b197dcc4..7965cbd50 100644 --- a/engine/src/jogl/com/jme3/renderer/jogl/JoglRenderer.java +++ b/engine/src/jogl/com/jme3/renderer/jogl/JoglRenderer.java @@ -1813,15 +1813,21 @@ public class JoglRenderer implements Renderer { throw new UnsupportedOperationException("Unknown texture type: " + tex.getType()); } - // R to Texture compare mode - if (tex.getShadowCompareMode() != Texture.ShadowCompareMode.Off) { - gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_R_TO_TEXTURE); - gl.glTexParameteri(target, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY); - if (tex.getShadowCompareMode() == Texture.ShadowCompareMode.GreaterOrEqual) { - gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_GEQUAL); + if (tex.isNeedCompareModeUpdate()) { + // R to Texture compare mode + if (tex.getShadowCompareMode() != Texture.ShadowCompareMode.Off) { + gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_R_TO_TEXTURE); + gl.glTexParameteri(target, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY); + if (tex.getShadowCompareMode() == Texture.ShadowCompareMode.GreaterOrEqual) { + gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_GEQUAL); + } else { + gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL); + } } else { - gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL); + //restoring default value + gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_NONE); } + tex.compareModeUpdated(); } } diff --git a/engine/src/lwjgl/com/jme3/renderer/lwjgl/LwjglRenderer.java b/engine/src/lwjgl/com/jme3/renderer/lwjgl/LwjglRenderer.java index d26e39bfc..7072fbcd8 100644 --- a/engine/src/lwjgl/com/jme3/renderer/lwjgl/LwjglRenderer.java +++ b/engine/src/lwjgl/com/jme3/renderer/lwjgl/LwjglRenderer.java @@ -1764,15 +1764,21 @@ public class LwjglRenderer implements Renderer { throw new UnsupportedOperationException("Unknown texture type: " + tex.getType()); } - // R to Texture compare mode - if (tex.getShadowCompareMode() != Texture.ShadowCompareMode.Off) { - glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); - glTexParameteri(target, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); - if (tex.getShadowCompareMode() == Texture.ShadowCompareMode.GreaterOrEqual) { - glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL); - } else { - glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + if(tex.isNeedCompareModeUpdate()){ + // R to Texture compare mode + if (tex.getShadowCompareMode() != Texture.ShadowCompareMode.Off) { + glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); + glTexParameteri(target, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); + if (tex.getShadowCompareMode() == Texture.ShadowCompareMode.GreaterOrEqual) { + glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL); + } else { + glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + } + }else{ + //restoring default value + glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_NONE); } + tex.compareModeUpdated(); } }