DrawableFigureGL.java

Go to the documentation of this file.
00001 /*
00002  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
00003  * Copyright (C) 2007 - INRIA - Jean-Baptiste Silvy
00004  * Copyright (C) 2008 - INRIA - Vincent Couvert
00005  * desc : Class containing the driver dependant routines to draw a 
00006  * figure object with JoGL
00007  * 
00008  * This file must be used under the terms of the CeCILL.
00009  * This source file is licensed as described in the file COPYING, which
00010  * you should have received as part of this distribution.  The terms
00011  * are also available at    
00012  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
00013  *
00014  */
00015 
00016 package org.scilab.modules.renderer.figureDrawing;
00017 
00018 import java.lang.reflect.InvocationTargetException;
00019 
00020 import javax.media.opengl.GL;
00021 import javax.media.opengl.GLAutoDrawable;
00022 import javax.swing.SwingUtilities;
00023 
00024 import org.scilab.modules.renderer.ObjectGL;
00025 import org.scilab.modules.renderer.FigureMapper;
00026 import org.scilab.modules.renderer.arcDrawing.ArcRendererFactory;
00027 import org.scilab.modules.renderer.arcDrawing.NurbsArcRendererFactory;
00028 import org.scilab.modules.renderer.polylineDrawing.JOGLShadeFacetDrawer;
00029 import org.scilab.modules.renderer.polylineDrawing.ShadeFacetDrawer;
00030 import org.scilab.modules.renderer.utils.CoordinateTransformation;
00031 import org.scilab.modules.renderer.utils.TexturedColorMap;
00032 import org.scilab.modules.renderer.utils.glTools.ClipPlane3DManager;
00033 import org.scilab.modules.renderer.utils.selection.RubberBox;
00034 import org.scilab.modules.renderer.utils.textRendering.JOGLTextRendererFactory;
00035 import org.scilab.modules.renderer.utils.textRendering.TextRendererFactory;
00036 import org.scilab.modules.renderer.utils.textRendering.TextRendererManager;
00037 import org.scilab.modules.renderer.ObjectGLCleaner;
00038 
00039 
00040 
00041 
00046 public class DrawableFigureGL extends ObjectGL {
00047         
00049         private static final int[] LOGICAL_OPS = {GL.GL_CLEAR,
00050                                                                                           GL.GL_AND,
00051                                                                                           GL.GL_AND_REVERSE,
00052                                                                                           GL.GL_COPY,
00053                                                                                           GL.GL_AND_INVERTED,
00054                                                                                           GL.GL_NOOP,
00055                                                                                           GL.GL_XOR,
00056                                                                                           GL.GL_OR,
00057                                                                                           GL.GL_NOR,
00058                                                                                           GL.GL_EQUIV,
00059                                                                                           GL.GL_INVERT,
00060                                                                                           GL.GL_OR_REVERSE,
00061                                                                                           GL.GL_COPY_INVERTED,
00062                                                                                           GL.GL_OR_INVERTED,
00063                                                                                           GL.GL_NAND,
00064                                                                                           GL.GL_SET};
00065         
00066         
00068         private RendererProperties guiProperties;
00070         private int figureId;
00072         private ObjectGLCleaner destroyedObjects;
00073         
00075         private GLAutoDrawable renderingTarget;
00076 
00078         private ArcRendererFactory arcRendererFactory;
00079         
00081         private int backGroundColorIndex;
00082         
00084         private RubberBox rubberBox;
00085         
00087         private ShadeFacetDrawer shadeFacetDrawer;
00088         
00089         private TextRendererManager textRendererCreator;
00090         
00091         private ClipPlane3DManager clipPlaneManager;
00092         
00094         private boolean renderRequested;
00095         
00097         private CoordinateTransformation transform;
00098         
00100         private int nbSubwins;
00101         
00105         public DrawableFigureGL() {
00106                 super();
00107         guiProperties = null;
00108         setColorMap(TexturedColorMap.create());
00109         figureId = -1; // figure ids should be greater than 0.
00110         destroyedObjects = new ObjectGLCleaner();
00111         renderingTarget = null;
00112         textRendererCreator = new TextRendererManager();
00113         setDefaultTextRenderer();
00114         setDefaultArcRendererFactory();
00115         setDefaultShadeFacetDrawer();
00116         backGroundColorIndex = 0;
00117         rubberBox = null;
00118         renderRequested = false;
00119         transform = new CoordinateTransformation();
00120         clipPlaneManager = new ClipPlane3DManager();
00121         nbSubwins = 0;
00122     }
00123         
00127         public void setDefaultTextRenderer() {
00128                 this.setTextRendererFactory(new JOGLTextRendererFactory());
00129         }
00130         
00134         public void setDefaultArcRendererFactory() {
00135                 arcRendererFactory = new NurbsArcRendererFactory();
00136         }
00137         
00141         public void setDefaultShadeFacetDrawer() {
00142                 shadeFacetDrawer = new JOGLShadeFacetDrawer();
00143         }
00144         
00148         public CoordinateTransformation getCoordinateTransformation() {
00149                 return transform;
00150         }
00151         
00155         public ClipPlane3DManager getClipPlaneManager() {
00156                 return clipPlaneManager;
00157         }
00158         
00163         public void setFigureIndex(int figureId) {
00164                 // a chack will be performed to see if the figure is alraedy here
00165                 FigureMapper.addMapping(figureId, this);
00166                 this.figureId = figureId;
00167         }
00168         
00172         public int getFigureId() {
00173                 return figureId;
00174         }
00175         
00180         public void setRendererProperties(RendererProperties prop) {
00181                 guiProperties = prop;
00182         }
00183         
00189         public void initializeDrawing(int parentFigureIndex) {
00190                 // destroy all the objects stored in the destroy list
00191                 destroyedObjects.destroyAll(parentFigureIndex);
00192                 
00193                 super.initializeDrawing(parentFigureIndex);
00194                 
00195                 GL gl = getGL();
00196                 // don't clear the color buffer as usual, we may need to keep it
00197                 // depending on the pixel mode
00198                 gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
00199                 gl.glClearDepth(1.0f);
00200         }
00201         
00205         public void endDrawing() {
00206                 super.endDrawing();
00207         }
00208   
00213         public String getInfoMessage() {
00214                 return guiProperties.getInfoMessage();
00215         }
00216         
00221         public void setInfoMessage(String infoMessage) {
00222                 guiProperties.setInfoMessage(infoMessage);
00223         }
00224 
00229         public String getTitle() {
00230                 return guiProperties.getTitle();
00231         }
00232         
00237         public void setTitle(String title) {
00238                 guiProperties.setTitle(title);
00239         }
00240 
00245         public void drawCanvas() {
00246                 setRenderingRequested(true);
00247                 guiProperties.forceDisplay();
00248         }
00249         
00253         public void setNbSubwins(int nbSubwins) {
00254                 this.nbSubwins = nbSubwins;
00255         }
00256         
00260         public int getNbSubwins() {
00261                 return nbSubwins;
00262         }
00263         
00267         public synchronized boolean getRenderingRequested() {
00268                 return renderRequested;
00269         }
00270         
00276         public synchronized void setRenderingRequested(boolean requested) {
00277                 this.renderRequested = requested;
00278         }
00279 
00283         public void closeRenderingCanvas() {
00284                 guiProperties = null;
00285         }
00286   
00292         public void setColorMapData(double[] rgbMat) {
00293                 getColorMap().setData(rgbMat);
00294         }
00295         
00301         public double[] getColorMapData() {
00302                 return getColorMap().getData();
00303         }
00304   
00309   public int getColorMapSize() {
00310     return getColorMap().getSize();
00311   }
00312         
00318         public void getColorMapData(double[] data) {
00319                 getColorMap().getData(data);
00320         }
00321         
00325         public ObjectGLCleaner getObjectCleaner() {
00326                 return destroyedObjects;
00327         }
00328         
00333         public RendererProperties getRendererProperties() { return guiProperties; }
00334         
00335         
00340         public void setBackgroundColor(int colorIndex) {
00341                 backGroundColorIndex = colorIndex;
00342                 double[] color = getBackgroundColor();
00343                 getRendererProperties().setBackgroundColor(color[0], color[1], color[2]);
00344         }
00345         
00349         public double[] getBackgroundColor() {
00350                 return getColorMap().getColor(backGroundColorIndex);
00351         }
00352         
00356         public void drawBackground() {
00357                 GL gl = getGL();
00358                 double[] color = getBackgroundColor();
00359                 // not really needed actually
00360                 gl.glClearColor((float) color[0], (float) color[1], (float) color[2], 1.0f); // alpha is set to 1
00361                 gl.glClear(GL.GL_COLOR_BUFFER_BIT);
00362         }
00363         
00364         
00369         public void setLogicalOp(int logicOpIndex) {
00370                 // convert to OpenGL value
00371                 GL gl = getGL();
00372                 gl.glEnable(GL.GL_COLOR_LOGIC_OP); // to use pixel drawing mode
00373                 gl.glLogicOp(LOGICAL_OPS[logicOpIndex]);
00374         }
00375         
00380         @Override
00381         public void destroy(int parentFigureIndex) {
00382                 getRendererProperties().disableCanvas();
00383                 setRenderingRequested(false);
00384                 // destroy all the objects stored in the destroy list
00385                 destroyedObjects = null;
00386                 FigureMapper.removeMapping(figureId);
00387         
00388                 // blocking call. So graphic synchrnonization must be desactivated here.
00389                 try {
00390                         SwingUtilities.invokeAndWait(new Runnable() {
00391                                 public void run() {
00392                                         getRendererProperties().closeCanvas();
00393                                 }
00394                         });
00395                 } catch (InterruptedException e) {
00396                         e.printStackTrace();
00397                 } catch (InvocationTargetException e) {
00398                         e.printStackTrace();
00399                 }
00400                 
00401         }
00402         
00403         
00407         public int getCanvasWidth() {
00408                 return guiProperties.getCanvasWidth();
00409         }
00410         
00414         public int getCanvasHeight() {
00415                 return guiProperties.getCanvasHeight();
00416         }
00417         
00424         public int setCanvasSize(int width, int height) {
00425                 return guiProperties.setCanvasSize(width, height);
00426         }
00427         
00431         public int getWindowWidth() {
00432                 return guiProperties.getWindowWidth();
00433         }
00434         
00438         public int getWindowHeight() {
00439                 return guiProperties.getWindowHeight();
00440         }
00441         
00447         public void setWindowSize(int width, int height) {
00448                 guiProperties.setWindowSize(width, height);
00449         }
00450         
00455         public int getWindowPosX() {
00456                 return guiProperties.getWindowPosX();
00457         }
00458         
00463         public int getWindowPosY() {
00464                 return guiProperties.getWindowPosY();
00465         }
00466         
00472         public void setWindowPosition(int posX, int posY) {
00473                 guiProperties.setWindowPosition(posX, posY);
00474         }
00475         
00480         @Override
00481         public GL getGL() {
00482                 return guiProperties.getGLPipeline();
00483         }
00484         
00485         
00491     public void setAutoResizeMode(boolean onOrOff) {
00492         getRendererProperties().setAutoResizeMode(onOrOff);
00493     }
00494     
00498     public boolean getAutoResizeMode() {
00499         return getRendererProperties().getAutoResizeMode();
00500     }
00501     
00506    public int[] getViewport() {
00507            return getRendererProperties().getViewport();
00508    }
00509    
00519         public void setViewport(int posX, int posY, int width, int height) {
00520                 getRendererProperties().setViewport(posX, posY, width, height);
00521         }
00522         
00527         public void setRenderingTarget(GLAutoDrawable target) {
00528                 this.renderingTarget = target;
00529         }
00530         
00534         public GLAutoDrawable getRenderingTarget() {
00535                 return renderingTarget;
00536         }
00537         
00542         public void setTextRendererFactory(TextRendererFactory textRendererFactory) {
00543                 textRendererCreator.setTextRendererFactory(textRendererFactory);
00544         }
00545         
00550         public void setArcRendererFactory(ArcRendererFactory arcRendererFactory) {
00551                 this.arcRendererFactory = arcRendererFactory;
00552         }
00553         
00558         public ArcRendererFactory getArcRendererFactory() {
00559                 return arcRendererFactory;
00560                 
00561         }
00562         
00567         public ShadeFacetDrawer getShadeFacetDrawer() {
00568                 return shadeFacetDrawer;
00569         }
00570 
00576         public void setShadeFacetDrawer(ShadeFacetDrawer shadeFacetDrawer) {
00577                 this.shadeFacetDrawer = shadeFacetDrawer;
00578         }
00579         
00584         public synchronized void setRubberBox(RubberBox rb) {
00585                 this.rubberBox = rb;
00586         }
00587         
00591         public synchronized void removeRubberBox() {
00592                 this.rubberBox = null;
00593         }
00594         
00599         public synchronized RubberBox getRubberBox() {
00600                 return this.rubberBox;
00601         }
00602         
00606         public synchronized boolean isRubberBoxModeOn() {
00607                 return this.rubberBox != null;
00608         }
00609         
00620         public int[] rubberBox(boolean isClick, boolean isZoom, int[] initialRect) {
00621                 
00622                 // giws is unable to pass null pointers
00623                 // we found a 0 sized array instead
00624                 int[] realIntialRect;
00625                 if (initialRect == null || initialRect.length == 0) {
00626                         realIntialRect = null;
00627                 } else {
00628                         realIntialRect = initialRect;
00629                 }
00630                 
00631                 int[] res = {0, 0, 0, 0, 0};
00632                 
00633                 // get [x1,y1,x2,y2] in res and button
00634                 int button = getRendererProperties().rubberBox(isClick, isZoom, realIntialRect, res);
00635                 res[res.length - 1] = button;
00636                 return res;
00637         }
00638         
00645         public int[] getRotationDisplacement() {
00646                 int[] res = new int[2 + 1];
00647                 
00648                 // get [dx, dy] displacement
00649                 boolean stop = getRendererProperties().getRotationDisplacement(res);
00650                 
00651                 if (stop) {
00652                         res[2] = 1;
00653                 } else {
00654                         res[2] = 0;
00655                 }
00656                 
00657                 return res;
00658         }
00659         
00663         public void stopRotationRecording() {
00664                 getRendererProperties().stopRotationRecording();
00665         }
00666         
00670         public TextRendererManager getTextRendererCreator() {
00671                 return textRendererCreator;
00672         }
00673         
00677         public void showWindow() {
00678                 getRendererProperties().showWindow();
00679         }
00680         
00681 }

Generated on Tue Sep 9 17:48:30 2008 for Scilab [trunk] by  doxygen 1.5.5