Graphics: fix light problems
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / util / LightingUtils.java
index d619618..5447444 100644 (file)
@@ -6,7 +6,7 @@
  * This source file is licensed as described in the file COPYING, which
  * you should have received as part of this distribution.  The terms
  * are also available at
- * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
  */
 package org.scilab.modules.renderer.JoGLView.util;
 
@@ -16,6 +16,9 @@ import org.scilab.forge.scirenderer.tranformations.Vector3d;
 import org.scilab.forge.scirenderer.lightning.Light;
 import org.scilab.forge.scirenderer.lightning.LightManager;
 
+import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
+import org.scilab.modules.graphic_objects.graphicController.GraphicController;
+
 /**
  * Utility functions to setup lighting.
  * @author Pedro SOUZA
@@ -36,22 +39,68 @@ public class LightingUtils {
         color = m.getSpecularColor();
         mtl.setSpecularColor(new Color(color[0].floatValue(), color[1].floatValue(), color[2].floatValue()));
         mtl.setColorMaterialEnable(m.getColorMaterialMode().booleanValue());
-        mtl.setShinines(m.getShininess().floatValue());
+        mtl.setShininess(m.getShininess().floatValue());
 
         return mtl;
     }
 
     /**
+     * Enables/disables lighting.
+     * @param manager the light manager.
+     * @param status the status.
+     */
+    public static void setLightingEnable(LightManager manager, Boolean status) {
+        if (manager == null) {
+            return;
+        }
+        manager.setLightningEnable(status.booleanValue());
+        Light light = manager.getLight(0);
+        light.setEnable(status.booleanValue());
+    }
+
+
+    public static void setupLights(LightManager manager, org.scilab.modules.graphic_objects.axes.Axes axes) {
+
+        if (manager == null) {
+            return;
+        }
+
+        boolean hasLight = false;
+        int index = 0;
+
+        for (Integer child : axes.getChildren()) {
+            GraphicObject object = GraphicController.getController().getObjectFromId(child);
+            if (object instanceof org.scilab.modules.graphic_objects.lighting.Light) {
+                //setup only visible lights
+                if (((org.scilab.modules.graphic_objects.lighting.Light)object).getVisible()) {
+                    setLight(manager, (org.scilab.modules.graphic_objects.lighting.Light)object, index++, axes);
+                    hasLight = true;
+                }
+            }
+            if (index >= manager.getLightNumber()) {
+                break;
+            }
+        }
+        for (int i = index; i < manager.getLightNumber(); ++i) {
+            //manager.getLight(i).setEnable(false);
+        }
+        manager.setLightningEnable(hasLight);
+    }
+
+    /**
      * Setup the give light.
      * @param manager the light manager.
      * @param light the light.
+     * @param index the light index.
      */
-    public static void setLight(LightManager manager, org.scilab.modules.graphic_objects.lighting.Light light) {
-
-        if (manager == null) return;
-        setLightingEnable(manager, light.isEnable());
+    public static void setLight(LightManager manager, org.scilab.modules.graphic_objects.lighting.Light light, int index, org.scilab.modules.graphic_objects.axes.Axes axes) {
+        Light sciLight = manager.getLight(index);
+        double[][] factors = axes.getScaleTranslateFactors();
+        Double[] coords = light.getLightTypeAsInteger() == 0 ? light.getDirection() : light.getPosition();
+        coords[0] = coords[0] * factors[0][0] + factors[1][0];
+        coords[1] = coords[1] * factors[0][1] + factors[1][1];
+        coords[2] = coords[2] * factors[0][2] + factors[1][2];
 
-        Light sciLight = manager.getLight(0);
         Double[] color = light.getAmbientColor();
         sciLight.setAmbientColor(new Color(color[0].floatValue(), color[1].floatValue(), color[2].floatValue()));
         color = light.getDiffuseColor();
@@ -59,27 +108,12 @@ public class LightingUtils {
         color = light.getSpecularColor();
         sciLight.setSpecularColor(new Color(color[0].floatValue(), color[1].floatValue(), color[2].floatValue()));
 
-        switch (light.getTypeAsInteger()) {
-            case 0: //directional
-                sciLight.setDirection(new Vector3d(light.getDirection()));
-                break;
-            case 1: //point
-                sciLight.setPosition(new Vector3d(light.getPosition()));
-                break;
-            default:
-                break;
+        if (light.getLightTypeAsInteger() == 0) {
+            sciLight.setDirection(new Vector3d(coords));
+        } else {
+            sciLight.setPosition(new Vector3d(coords));
         }
-    }
 
-    /**
-     * Enables/disables lighting.
-     * @param manager the light manager.
-     * @param status the status.
-     */
-    public static void setLightingEnable(LightManager manager, Boolean status) {
-        if (manager == null) return;
-        manager.setLightningEnable(status.booleanValue());
-        Light light = manager.getLight(0);
-        light.setEnable(status.booleanValue());
+        sciLight.setEnable(true);
     }
 }