Graphics: fix light problems 52/14252/3
Calixte DENIZET [Mon, 31 Mar 2014 13:06:44 +0000 (15:06 +0200)]
Change-Id: Ib98374c702ab72d83dea628071c00ec1d7475d03

scilab/modules/renderer/src/java/org/scilab/modules/renderer/JoGLView/util/LightingUtils.java
scilab/modules/scirenderer/src/org/scilab/forge/scirenderer/implementation/jogl/lightning/JoGLLight.java
scilab/modules/scirenderer/src/org/scilab/forge/scirenderer/implementation/jogl/lightning/JoGLLightManager.java

index 39e7548..5447444 100644 (file)
@@ -73,7 +73,7 @@ public class LightingUtils {
             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++);
+                    setLight(manager, (org.scilab.modules.graphic_objects.lighting.Light)object, index++, axes);
                     hasLight = true;
                 }
             }
@@ -82,7 +82,7 @@ public class LightingUtils {
             }
         }
         for (int i = index; i < manager.getLightNumber(); ++i) {
-            manager.getLight(i).setEnable(false);
+            //manager.getLight(i).setEnable(false);
         }
         manager.setLightningEnable(hasLight);
     }
@@ -93,9 +93,13 @@ public class LightingUtils {
      * @param light the light.
      * @param index the light index.
      */
-    public static void setLight(LightManager manager, org.scilab.modules.graphic_objects.lighting.Light light, int index) {
-
+    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];
 
         Double[] color = light.getAmbientColor();
         sciLight.setAmbientColor(new Color(color[0].floatValue(), color[1].floatValue(), color[2].floatValue()));
@@ -104,15 +108,10 @@ public class LightingUtils {
         color = light.getSpecularColor();
         sciLight.setSpecularColor(new Color(color[0].floatValue(), color[1].floatValue(), color[2].floatValue()));
 
-        switch (light.getLightTypeAsInteger()) {
-            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));
         }
 
         sciLight.setEnable(true);
index b32837a..2a99c41 100644 (file)
@@ -29,7 +29,7 @@ public class JoGLLight implements Light {
     private Color diffuseColor = new Color(0, 0, 0);
     private Color specularColor = new Color(0, 0, 0);
     private Vector3d position = new Vector3d(0, 0, 0);
-    private Vector3d spotDirection = new Vector3d(0, 0, -1);
+    private Vector3d spotDirection = new Vector3d(0, 0, 1);
     private Vector3d direction = new Vector3d(0, 0, 0);
     private float spotAngle = 180;
     private boolean isDirectional = false;
@@ -56,7 +56,12 @@ public class JoGLLight implements Light {
         gl.glLightfv(GL2.GL_LIGHT0 + index, GL2.GL_DIFFUSE, diffuseColor.getRGBComponents(null), 0);
         gl.glLightfv(GL2.GL_LIGHT0 + index, GL2.GL_SPECULAR, specularColor.getRGBComponents(null), 0);
         if (isDirectional) {
-            gl.glLightfv(GL2.GL_LIGHT0 + index, GL2.GL_POSITION, direction.getDataAsFloatArray(4), 0);
+            float[] pos = position.getDataAsFloatArray(4);
+            pos[3] = 0.0f;
+            pos[0] = -pos[0];
+            pos[1] = -pos[1];
+            pos[2] = -pos[2];
+            gl.glLightfv(GL2.GL_LIGHT0 + index, GL2.GL_POSITION, pos, 0);
         } else {
             float[] pos = position.getDataAsFloatArray(4);
             pos[3] = 1.0f;
@@ -143,6 +148,9 @@ public class JoGLLight implements Light {
             this.direction = direction;
             float[] dir = direction.getDataAsFloatArray(4);
             dir[3] = 0.0f;
+            dir[0] = -dir[0];
+            dir[1] = -dir[1];
+            dir[2] = -dir[2];
             gl.glLightfv(GL2.GL_LIGHT0 + index, GL2.GL_POSITION, dir, 0);
         }
     }
index 641852a..4621f50 100644 (file)
@@ -84,7 +84,9 @@ public class JoGLLightManager implements LightManager {
             GLShortCuts.setEnable(drawingTools.getGl().getGL2(), GL2.GL_COLOR_MATERIAL, material.isColorMaterialEnable());
             float[] black = new float[] {0.0f, 0.0f, 0.0f, 1.0f};
             drawingTools.getGl().glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, black, 0);
-            drawingTools.getGl().glLightModeli(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, GL2.GL_TRUE);
+            drawingTools.getGl().glLightModeli(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, GL2.GL_FALSE);
+            drawingTools.getGl().glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE, GL2.GL_TRUE);
+
             if (material.isColorMaterialEnable()) {
                 drawingTools.getGl().glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
             } else {
@@ -104,7 +106,7 @@ public class JoGLLightManager implements LightManager {
         GL2 gl = drawingTools.getGl().getGL2();
         GLShortCuts.setEnable(gl, GL2.GL_LIGHTING, isLightningEnable);
         for (JoGLLight light : lights) {
-            if (light != null) {
+            if (light != null && light.isEnable()) {
                 light.reload(gl);
             }
         }