[committed] MaterialState doesn't disable

Every render state except material state will revert to defaults when it is disabled. I've fixed LWJGLMaterialState and JOGLMaterialState to comply with being disabled so its behavior is consistent with the rest of the system.



LWJGLMaterialState.java

Index: C:/Projects New/jME2/src/com/jme/scene/state/lwjgl/LWJGLMaterialState.java
===================================================================
--- C:/Projects New/jME2/src/com/jme/scene/state/lwjgl/LWJGLMaterialState.java   (revision 4041)
+++ C:/Projects New/jME2/src/com/jme/scene/state/lwjgl/LWJGLMaterialState.java   (working copy)
@@ -73,25 +73,46 @@
                 .getStateRecord(RS_MATERIAL);
         context.currentStates[RS_MATERIAL] = this;
 
-        int face = getGLMaterialFace(getMaterialFace());
-
-        // setup colormaterial, if changed.
-        applyColorMaterial(getColorMaterial(), face, record);
-       
-        // apply colors, if needed and not what is currently set.
-        applyColor(GL11.GL_AMBIENT, getAmbient(), face, record);
-        applyColor(GL11.GL_DIFFUSE, getDiffuse(), face, record);
-        applyColor(GL11.GL_EMISSION, getEmissive(), face, record);
-        applyColor(GL11.GL_SPECULAR, getSpecular(), face, record);
-
-        // set our shine
-        if (!record.isValid() || face != record.face || record.shininess != shininess) {
-            GL11.glMaterialf(face, GL11.GL_SHININESS, shininess);
-            record.shininess = shininess;
+        if(isEnabled()) {
+           int face = getGLMaterialFace(getMaterialFace());
+   
+           // setup colormaterial, if changed.
+           applyColorMaterial(getColorMaterial(), face, record);
+          
+           // apply colors, if needed and not what is currently set.
+           applyColor(GL11.GL_AMBIENT, getAmbient(), face, record);
+           applyColor(GL11.GL_DIFFUSE, getDiffuse(), face, record);
+           applyColor(GL11.GL_EMISSION, getEmissive(), face, record);
+           applyColor(GL11.GL_SPECULAR, getSpecular(), face, record);
+   
+           // set our shine
+           if (!record.isValid() || face != record.face || record.shininess != shininess) {
+               GL11.glMaterialf(face, GL11.GL_SHININESS, shininess);
+               record.shininess = shininess;
+           }
+          
+           record.face = face;
         }
+        else {
+           // apply defaults
+           int face = getGLMaterialFace(defaultMaterialFace);
+           
+           applyColorMaterial(defaultColorMaterial, face, record);
+          
+           applyColor(GL11.GL_AMBIENT, defaultAmbient, face, record);
+           applyColor(GL11.GL_DIFFUSE, defaultDiffuse, face, record);
+           applyColor(GL11.GL_EMISSION, defaultEmissive, face, record);
+           applyColor(GL11.GL_SPECULAR, defaultSpecular, face, record);
+   
+           // set our shine
+           if (!record.isValid() || face != record.face || record.shininess != defaultShininess) {
+               GL11.glMaterialf(face, GL11.GL_SHININESS, defaultShininess);
+               record.shininess = defaultShininess;
+           }
+          
+           record.face = face;
+        }
         
-        record.face = face;
-       
         if (!record.isValid())
             record.validate();
     }



JOGLMaterialState.java

Index: C:/Projects New/jME2/src/com/jme/scene/state/jogl/JOGLMaterialState.java
===================================================================
--- C:/Projects New/jME2/src/com/jme/scene/state/jogl/JOGLMaterialState.java   (revision 4041)
+++ C:/Projects New/jME2/src/com/jme/scene/state/jogl/JOGLMaterialState.java   (working copy)
@@ -35,6 +35,8 @@
 import javax.media.opengl.GL;
 import javax.media.opengl.glu.GLU;
 
+import org.lwjgl.opengl.GL11;
+
 import com.jme.renderer.ColorRGBA;
 import com.jme.renderer.RenderContext;
 import com.jme.scene.state.MaterialState;
@@ -77,25 +79,46 @@
                 .getStateRecord(RS_MATERIAL);
         context.currentStates[RS_MATERIAL] = this;
 
-        int face = getGLMaterialFace(getMaterialFace());
-
-        // setup colormaterial, if changed.
-        applyColorMaterial(getColorMaterial(), face, record);
-
-        // apply colors, if needed and not what is currently set.
-        applyColor(GL.GL_AMBIENT, getAmbient(), face, record);
-        applyColor(GL.GL_DIFFUSE, getDiffuse(), face, record);
-        applyColor(GL.GL_EMISSION, getEmissive(), face, record);
-        applyColor(GL.GL_SPECULAR, getSpecular(), face, record);
-
-        // set our shine
-        if (!record.isValid() || face != record.face || record.shininess != shininess) {
-            gl.glMaterialf(face, GL.GL_SHININESS, shininess);
-            record.shininess = shininess;
+        if(isEnabled()) {
+           int face = getGLMaterialFace(getMaterialFace());
+   
+           // setup colormaterial, if changed.
+           applyColorMaterial(getColorMaterial(), face, record);
+   
+           // apply colors, if needed and not what is currently set.
+           applyColor(GL.GL_AMBIENT, getAmbient(), face, record);
+           applyColor(GL.GL_DIFFUSE, getDiffuse(), face, record);
+           applyColor(GL.GL_EMISSION, getEmissive(), face, record);
+           applyColor(GL.GL_SPECULAR, getSpecular(), face, record);
+   
+           // set our shine
+           if (!record.isValid() || face != record.face || record.shininess != shininess) {
+               gl.glMaterialf(face, GL.GL_SHININESS, shininess);
+               record.shininess = shininess;
+           }
+   
+           record.face = face;
         }
+        else {
+           // apply defaults
+           int face = getGLMaterialFace(defaultMaterialFace);
+           
+           applyColorMaterial(defaultColorMaterial, face, record);
+          
+           applyColor(GL11.GL_AMBIENT, defaultAmbient, face, record);
+           applyColor(GL11.GL_DIFFUSE, defaultDiffuse, face, record);
+           applyColor(GL11.GL_EMISSION, defaultEmissive, face, record);
+           applyColor(GL11.GL_SPECULAR, defaultSpecular, face, record);
+   
+           // set our shine
+           if (!record.isValid() || face != record.face || record.shininess != defaultShininess) {
+               GL11.glMaterialf(face, GL11.GL_SHININESS, defaultShininess);
+               record.shininess = defaultShininess;
+           }
+          
+           record.face = face;
+        }
 
-        record.face = face;
-
         if (!record.isValid())
             record.validate();
     }


Nice catch :).

committed.

Sorry but your fix is wrong for JOGL. Don’t mix LWJGL with JOGL. The fix of the regression you introduced is here:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=9723.msg75735#msg75735

Please commit it as soon as possible. I should have read all suggested fixes here, it is my fault.

Indeed the changes to the JOGL part are a bit… weird.



Nymon, Can you revert your change or check in gouessej patch in the thread he linked? (please check history to see if eg Coredump did not beat you to it).

llama said:

Indeed the changes to the JOGL part are a bit.. weird.

Nymon, Can you revert your change or check in gouessej patch in the thread he linked? (please check history to see if eg Coredump did not beat you to it).

It is easier not to revert Nymon's fix and apply my fix on it, it is simple, I only remove an import and use JOGL constants instead of LWJGL GL11 constants.

Well reverting is basically pressing 1 button so  :D  both are pretty easy.

llama said:

Well reverting is basically pressing 1 button so  :D  both are pretty easy.

I know but his fix is algorithmically correct, it is only a problem of implementation.

That was just negligence on my part, sorry. I fixed it with your patch, gouessej.

nymon said:

That was just negligence on my part, sorry. I fixed it with your patch, gouessej.

Thank you very much. I have a problem with my write access, I couldn't submit it yet.