1 /* 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3 * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando 4 * 5 * Copyright (C) 2012 - 2016 - Scilab Enterprises 6 * 7 * This file is hereby licensed under the terms of the GNU GPL v2.0, 8 * pursuant to article 5.3.4 of the CeCILL v.2.1. 9 * This file was originally licensed under the terms of the CeCILL v2.1, 10 * and continues to be available under such terms. 11 * For more information, see the COPYING file which you should have received 12 * along with this program. 13 */ 14 15 package org.scilab.forge.scirenderer.implementation.jogl; 16 17 import org.scilab.forge.scirenderer.DrawingTools; 18 import org.scilab.forge.scirenderer.SciRendererException; 19 import org.scilab.forge.scirenderer.buffers.ElementsBuffer; 20 import org.scilab.forge.scirenderer.clipping.ClippingManager; 21 import org.scilab.forge.scirenderer.implementation.jogl.clipping.JoGLClippingManager; 22 import org.scilab.forge.scirenderer.implementation.jogl.drawer.JoGLShapeDrawer; 23 import org.scilab.forge.scirenderer.implementation.jogl.lightning.JoGLLightManager; 24 import org.scilab.forge.scirenderer.implementation.jogl.JoGLPixelDrawingMode; 25 import org.scilab.forge.scirenderer.lightning.LightManager; 26 import org.scilab.forge.scirenderer.renderer.Renderer; 27 import org.scilab.forge.scirenderer.shapes.appearance.Appearance; 28 import org.scilab.forge.scirenderer.shapes.appearance.Color; 29 import org.scilab.forge.scirenderer.shapes.geometry.Geometry; 30 import org.scilab.forge.scirenderer.texture.AnchorPosition; 31 import org.scilab.forge.scirenderer.texture.Texture; 32 import org.scilab.forge.scirenderer.tranformations.TransformationManager; 33 import org.scilab.forge.scirenderer.tranformations.TransformationManagerImpl; 34 import org.scilab.forge.scirenderer.tranformations.TransformationManagerListener; 35 import org.scilab.forge.scirenderer.tranformations.Vector3d; 36 37 import com.jogamp.opengl.GL2; 38 39 /** 40 * 41 * JoGl implementation of the DrawingTools. 42 * 43 * @author Pierre Lando 44 */ 45 public class JoGLDrawingTools implements DrawingTools { 46 47 private final JoGLCapacity capacity = new JoGLCapacity(); 48 private final TransformationManager transformationManager; 49 private final JoGLLightManager lightManager; 50 private final JoGLClippingManager clippingManager; 51 private final JoGLCanvas canvas; 52 private GL2 gl; 53 private int PixelDrawingMode; 54 55 /** 56 * Default constructor. 57 * @param canvas the canvas where this drawing tools live. 58 */ JoGLDrawingTools(JoGLCanvas canvas)59 JoGLDrawingTools(JoGLCanvas canvas) { 60 this.transformationManager = new TransformationManagerImpl(canvas); 61 this.lightManager = new JoGLLightManager(this); 62 this.clippingManager = new JoGLClippingManager(this); 63 64 this.canvas = canvas; 65 66 this.PixelDrawingMode = GL2.GL_COPY; 67 68 transformationManager.addListener(new TransformationManagerListener() { 69 @Override 70 public void transformationChanged(TransformationManager transformationManager) { 71 gl.glMatrixMode(GL2.GL_MODELVIEW); 72 if (transformationManager.isUsingSceneCoordinate()) { 73 gl.glLoadMatrixd(transformationManager.getTransformation().getMatrix(), 0); 74 } else { 75 gl.glLoadMatrixd(transformationManager.getWindowTransformation().getMatrix(), 0); 76 } 77 } 78 }); 79 } 80 81 /** 82 * Synchronise to the given OpenGl context. 83 * @param gl the OpenGL context. 84 */ glSynchronize(GL2 gl)85 void glSynchronize(GL2 gl) { 86 this.gl = gl; 87 transformationManager.reset(); 88 capacity.glReload(gl); 89 lightManager.reload(); 90 clippingManager.reload(); 91 } 92 93 @Override getCanvas()94 public JoGLCanvas getCanvas() { 95 return canvas; 96 } 97 98 /** 99 * Return the OpenGl context. 100 * @return the OpenGl context. 101 */ getGl()102 public GL2 getGl() { 103 return gl; 104 } 105 106 /** 107 * Return the OpenGl capacity of this canvas. 108 * @return the OpenGl capacity of this canvas. 109 */ getGLCapacity()110 public JoGLCapacity getGLCapacity() { 111 return capacity; 112 } 113 114 @Override getTransformationManager()115 public TransformationManager getTransformationManager() { 116 return transformationManager; 117 } 118 119 @Override getLightManager()120 public LightManager getLightManager() { 121 return lightManager; 122 } 123 124 @Override getClippingManager()125 public ClippingManager getClippingManager() { 126 return clippingManager; 127 } 128 setPixelDrawingMode(JoGLPixelDrawingMode mode)129 public void setPixelDrawingMode(JoGLPixelDrawingMode mode) { 130 switch (mode) { 131 case CLEAR: 132 PixelDrawingMode = GL2.GL_CLEAR; 133 break; 134 case AND: 135 PixelDrawingMode = GL2.GL_AND; 136 break; 137 case AND_REVERSE: 138 PixelDrawingMode = GL2.GL_AND_REVERSE; 139 break; 140 case COPY: 141 PixelDrawingMode = GL2.GL_COPY; 142 break; 143 case AND_INVERTED: 144 PixelDrawingMode = GL2.GL_AND_INVERTED; 145 break; 146 case NOOP: 147 PixelDrawingMode = GL2.GL_NOOP; 148 break; 149 case XOR: 150 PixelDrawingMode = GL2.GL_XOR; 151 break; 152 case OR: 153 PixelDrawingMode = GL2.GL_OR; 154 break; 155 case NOR: 156 PixelDrawingMode = GL2.GL_NOR; 157 break; 158 case EQUIV: 159 PixelDrawingMode = GL2.GL_EQUIV; 160 break; 161 case INVERT: 162 PixelDrawingMode = GL2.GL_INVERT; 163 break; 164 case OR_REVERSE: 165 PixelDrawingMode = GL2.GL_OR_REVERSE; 166 break; 167 case COPY_INVERTED: 168 PixelDrawingMode = GL2.GL_COPY_INVERTED; 169 break; 170 case OR_INVERTED: 171 PixelDrawingMode = GL2.GL_OR_INVERTED; 172 break; 173 case NAND: 174 PixelDrawingMode = GL2.GL_NAND; 175 break; 176 case SET: 177 PixelDrawingMode = GL2.GL_SET; 178 break; 179 default: 180 PixelDrawingMode = GL2.GL_COPY; 181 break; 182 } 183 } 184 getGLPixelDrawingMode()185 public int getGLPixelDrawingMode() { 186 return PixelDrawingMode; 187 } 188 189 @Override clear(Color color)190 public void clear(Color color) { 191 gl.glClearColor(color.getRedAsFloat(), color.getGreenAsFloat(), color.getBlueAsFloat(), 1.f); 192 gl.glClear(GL2.GL_COLOR_BUFFER_BIT); 193 } 194 195 @Override clear(java.awt.Color color)196 public void clear(java.awt.Color color) { 197 float[] colorData = color.getRGBColorComponents(null); 198 gl.glClearColor(colorData[0], colorData[1], colorData[2], 1f); 199 gl.glClear(GL2.GL_COLOR_BUFFER_BIT); 200 } 201 202 @Override clearDepthBuffer()203 public void clearDepthBuffer() { 204 gl.glClear(GL2.GL_DEPTH_BUFFER_BIT); 205 } 206 207 @Override draw(Renderer renderer)208 public void draw(Renderer renderer) { 209 canvas.getRendererManager().draw(this, renderer); 210 } 211 212 @Override draw(Geometry geometry)213 public void draw(Geometry geometry) throws SciRendererException { 214 JoGLShapeDrawer.getDrawer().draw(this, geometry, Appearance.getDefault()); 215 } 216 217 @Override draw(Geometry geometry, Appearance appearance)218 public void draw(Geometry geometry, Appearance appearance) throws SciRendererException { 219 JoGLShapeDrawer.getDrawer().draw(this, geometry, appearance); 220 } 221 222 @Override draw(Texture texture)223 public void draw(Texture texture) throws SciRendererException { 224 canvas.getTextureManager().draw(this, texture); 225 } 226 227 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions)228 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions) throws SciRendererException { 229 canvas.getTextureManager().draw(this, texture, anchor, positions, 0, 1, 0, null, null); 230 } 231 232 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, double rotationAngle)233 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, double rotationAngle) throws SciRendererException { 234 canvas.getTextureManager().draw(this, texture, anchor, positions, 0, 1, rotationAngle, null, null); 235 } 236 237 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, Color auxColor, ElementsBuffer colors)238 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, Color auxColor, ElementsBuffer colors) throws SciRendererException { 239 canvas.getTextureManager().draw(this, texture, anchor, positions, 0, 1, 0, auxColor, colors); 240 } 241 242 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, double rotationAngle, Color auxColor, ElementsBuffer colors)243 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, double rotationAngle, Color auxColor, ElementsBuffer colors) throws SciRendererException { 244 canvas.getTextureManager().draw(this, texture, anchor, positions, 0, 1, rotationAngle, auxColor, colors); 245 } 246 247 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle)248 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle) throws SciRendererException { 249 canvas.getTextureManager().draw(this, texture, anchor, positions, offset, stride, rotationAngle, null, null); 250 } 251 252 @Override draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle, Color auxColor, ElementsBuffer colors)253 public void draw(Texture texture, AnchorPosition anchor, ElementsBuffer positions, int offset, int stride, double rotationAngle, Color auxColor, ElementsBuffer colors) throws SciRendererException { 254 canvas.getTextureManager().draw(this, texture, anchor, positions, offset, stride, rotationAngle, auxColor, colors); 255 } 256 257 @Override draw(Texture texture, AnchorPosition anchor, Vector3d position)258 public void draw(Texture texture, AnchorPosition anchor, Vector3d position) throws SciRendererException { 259 canvas.getTextureManager().draw(this, texture, anchor, position, 0); 260 } 261 262 @Override draw(Texture texture, AnchorPosition anchor, Vector3d position, double rotationAngle)263 public void draw(Texture texture, AnchorPosition anchor, Vector3d position, double rotationAngle) throws SciRendererException { 264 canvas.getTextureManager().draw(this, texture, anchor, position, rotationAngle); 265 } 266 267 /** 268 * Bind the given texture to the OpenGl context. 269 * @param texture the given texture. 270 * @throws SciRendererException is thrown if the texture is invalid. 271 */ bind(Texture texture)272 public void bind(Texture texture) throws SciRendererException { 273 canvas.getTextureManager().bind(this, texture); 274 } 275 } 276