Adds possibility to color marks in scatter plots
[scilab.git] / scilab / modules / scirenderer / src / org / scilab / forge / scirenderer / implementation / jogl / texture / JoGLTextureManager.java
index 71876e1..1470650 100644 (file)
@@ -69,7 +69,7 @@ public class JoGLTextureManager implements TextureManager {
     /**
      * Draw the given texture.
      * @param drawingTools used drawing tools.
-     * @param texture the texture too drawn.
+     * @param texture the texture to draw.
      * @throws org.scilab.forge.scirenderer.SciRendererException if the texture is invalid.
      */
     public void draw(JoGLDrawingTools drawingTools, Texture texture) throws SciRendererException {
@@ -82,35 +82,75 @@ public class JoGLTextureManager implements TextureManager {
         }
     }
 
-    public void draw(JoGLDrawingTools drawingTools, Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle) throws SciRendererException {
+    public void draw(JoGLDrawingTools drawingTools, Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle, ElementsBuffer colors) throws SciRendererException {
         if ((texture instanceof JoGLTexture) && (allTextures.contains((JoGLTexture) texture))) {
             if (positions != null) {
                 FloatBuffer data = positions.getData();
+                FloatBuffer dataColors = null;
+
                 if (data != null) {
+                    data.rewind();
+                    // Initializing dataColors Buffer
+                    if (colors != null) {
+                        dataColors = colors.getData();
+                        if (dataColors != null){
+                            dataColors.rewind();
+                            // There should be as many colors as there are positions
+                            if (dataColors.limit() != data.limit()) {
+                                throw new SciRendererException("Vertices do not have the same number of positions and colors");
+                            }
+                        }
+                    }
                     float[] position = {0, 0, 0, 1};
+                    float[] color = {0, 0, 0, 0};
+                    int mark = 0;
+
                     final JoGLTexture jt = (JoGLTexture) texture;
                     if (jt.preDraw(drawingTools)) {
                         stride = stride < 1 ? 1 : stride;
                         offset = offset < 0 ? 0 : offset;
                         if (stride == 1) {
-                            data.position(4 * offset);
+                            // skip offset positions in the data Buffer
+                            // a position is 4 elements
+                            mark = 4 * offset;
+                            data.position(mark);
+                            if (dataColors != null) {
+                                dataColors.position(mark);
+                            }
                             while (data.remaining() >= 4) {
                                 data.get(position);
-                                jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle);
+                                if (dataColors == null){
+                                    jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle, null);
+                                }
+                                else {
+                                    dataColors.get(color);
+                                    jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle, color);
+                                }
                             }
                         } else {
-                            int mark = 4 * offset;
+                            mark = 4 * offset;
                             if (mark < data.capacity()) {
                                 data.position(mark);
                                 while (data.remaining() >= 4) {
                                     data.get(position);
+                                    if (dataColors == null){
+                                        jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle, null);
+                                    }
+                                    else {
+                                        dataColors.position(mark);
+                                        dataColors.get(color);
+                                        jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle, color);
+                                    }
+                                    // reposition the mark on data and dataColors
                                     mark += stride * 4;
                                     if (mark < data.capacity()) {
                                         data.position(mark);
+                                        if (dataColors != null) {
+                                            dataColors.position(mark);
+                                        }
                                     } else {
                                         break;
                                     }
-                                    jt.draw(drawingTools, anchor, new Vector3d(position), rotationAngle);
                                 }
                             }
                         }
@@ -125,7 +165,7 @@ public class JoGLTextureManager implements TextureManager {
         if ((texture instanceof JoGLTexture) && (allTextures.contains((JoGLTexture) texture))) {
             final JoGLTexture jt = (JoGLTexture) texture;
             jt.preDraw(drawingTools);
-            jt.draw(drawingTools, anchor, position, rotationAngle);
+            jt.draw(drawingTools, anchor, position, rotationAngle, null);
             jt.postDraw(drawingTools);
         }
     }
@@ -361,7 +401,8 @@ public class JoGLTextureManager implements TextureManager {
 
             gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
 
-            gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
+            gl.glColor4f(1f, 1f, 1f, 1f);
+            gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
 
             for (int k = 0; k < wCuts * hCuts; k++) {
                 textures[k].enable(gl);
@@ -379,7 +420,7 @@ public class JoGLTextureManager implements TextureManager {
             return true;
         }
 
-        public void draw(JoGLDrawingTools drawingTools, AnchorPosition anchor, Vector3d position, double rotationAngle) throws SciRendererException {
+        public void draw(JoGLDrawingTools drawingTools, AnchorPosition anchor, Vector3d position, double rotationAngle, float[] color) throws SciRendererException {
             TransformationManager transformationManager = drawingTools.getTransformationManager();
             Transformation canvasProjection = transformationManager.getCanvasProjection();
             boolean sceneCoordinate = drawingTools.getTransformationManager().isUsingSceneCoordinate();
@@ -406,7 +447,7 @@ public class JoGLTextureManager implements TextureManager {
                 gl.glTranslated(Math.round(getAnchorDeltaX(anchor)), Math.round(getAnchorDeltaY(anchor)), 0);
             }
 
-            draw(drawingTools);
+            draw(drawingTools, color);
 
             gl.glMatrixMode(GL2.GL_MODELVIEW);
             gl.glPopMatrix();
@@ -431,12 +472,16 @@ public class JoGLTextureManager implements TextureManager {
             gl.glPopMatrix();
         }
 
+        public void draw(JoGLDrawingTools drawingTools) throws SciRendererException {
+            draw(drawingTools, null);
+        }
+
         /**
          * Draw the texture in XY plane.
          * @param drawingTools the drawing tools.
          * @throws SciRendererException if the texture is invalid.
          */
-        public void draw(JoGLDrawingTools drawingTools) throws SciRendererException {
+        public void draw(JoGLDrawingTools drawingTools, float[] color) throws SciRendererException {
             final int maxSize = drawingTools.getGLCapacity().getMaximumTextureSize();
             final Dimension textureSize = getDataProvider().getTextureSize();
             final GL2 gl = drawingTools.getGl().getGL2();
@@ -451,6 +496,10 @@ public class JoGLTextureManager implements TextureManager {
                     }
                     textures[0].bind(gl);
 
+                    if (color != null) {
+                        gl.glColor4fv(color, 0);
+                    }
+
                     gl.glBegin(GL2.GL_QUADS);
                     gl.glTexCoord2f(coords.left(), coords.bottom());
                     if (isRowMajorOrder()) {
@@ -487,6 +536,10 @@ public class JoGLTextureManager implements TextureManager {
                             }
                             textures[k].bind(gl);
 
+                            if (color != null) {
+                                gl.glColor4fv(color, 0);
+                            }
+
                             gl.glBegin(GL2.GL_QUADS);
                             gl.glTexCoord2f(coords.left(), coords.top());
                             gl.glVertex2f(x, textureSize.height - y);