2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
15 package org.scilab.forge.scirenderer.implementation.jogl;
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.implementantion.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;
37 import javax.media.opengl.GL2;
41 * JoGl implementation of the DrawingTools.
43 * @author Pierre Lando
45 public class JoGLDrawingTools implements DrawingTools {
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;
53 private int PixelDrawingMode;
56 * Default constructor.
57 * @param canvas the canvas where this drawing tools live.
59 JoGLDrawingTools(JoGLCanvas canvas) {
60 this.transformationManager = new TransformationManagerImpl(canvas);
61 this.lightManager = new JoGLLightManager(this);
62 this.clippingManager = new JoGLClippingManager(this);
66 this.PixelDrawingMode = GL2.GL_COPY;
68 transformationManager.addListener(new TransformationManagerListener() {
70 public void transformationChanged(TransformationManager transformationManager) {
71 gl.glMatrixMode(GL2.GL_MODELVIEW);
72 if (transformationManager.isUsingSceneCoordinate()) {
73 gl.glLoadMatrixd(transformationManager.getTransformation().getMatrix(), 0);
75 gl.glLoadMatrixd(transformationManager.getWindowTransformation().getMatrix(), 0);
82 * Synchronise to the given OpenGl context.
83 * @param gl the OpenGL context.
85 void glSynchronize(GL2 gl) {
87 transformationManager.reset();
88 capacity.glReload(gl);
89 lightManager.reload();
90 clippingManager.reload();
94 public JoGLCanvas getCanvas() {
99 * Return the OpenGl context.
100 * @return the OpenGl context.
107 * Return the OpenGl capacity of this canvas.
108 * @return the OpenGl capacity of this canvas.
110 public JoGLCapacity getGLCapacity() {
115 public TransformationManager getTransformationManager() {
116 return transformationManager;
120 public LightManager getLightManager() {
125 public ClippingManager getClippingManager() {
126 return clippingManager;
129 public void setPixelDrawingMode(JoGLPixelDrawingMode mode) {
132 PixelDrawingMode = GL2.GL_CLEAR;
135 PixelDrawingMode = GL2.GL_AND;
138 PixelDrawingMode = GL2.GL_AND_REVERSE;
141 PixelDrawingMode = GL2.GL_COPY;
144 PixelDrawingMode = GL2.GL_AND_INVERTED;
147 PixelDrawingMode = GL2.GL_NOOP;
150 PixelDrawingMode = GL2.GL_XOR;
153 PixelDrawingMode = GL2.GL_OR;
156 PixelDrawingMode = GL2.GL_NOR;
159 PixelDrawingMode = GL2.GL_EQUIV;
162 PixelDrawingMode = GL2.GL_INVERT;
165 PixelDrawingMode = GL2.GL_OR_REVERSE;
168 PixelDrawingMode = GL2.GL_COPY_INVERTED;
171 PixelDrawingMode = GL2.GL_OR_INVERTED;
174 PixelDrawingMode = GL2.GL_NAND;
177 PixelDrawingMode = GL2.GL_SET;
180 PixelDrawingMode = GL2.GL_COPY;
185 public int getGLPixelDrawingMode() {
186 return PixelDrawingMode;
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);
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);
203 public void clearDepthBuffer() {
204 gl.glClear(GL2.GL_DEPTH_BUFFER_BIT);
208 public void draw(Renderer renderer) {
209 canvas.getRendererManager().draw(this, renderer);
213 public void draw(Geometry geometry) throws SciRendererException {
214 JoGLShapeDrawer.getDrawer().draw(this, geometry, Appearance.getDefault());
218 public void draw(Geometry geometry, Appearance appearance) throws SciRendererException {
219 JoGLShapeDrawer.getDrawer().draw(this, geometry, appearance);
223 public void draw(Texture texture) throws SciRendererException {
224 canvas.getTextureManager().draw(this, texture);
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);
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);
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);
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);
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);
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);
258 public void draw(Texture texture, AnchorPosition anchor, Vector3d position) throws SciRendererException {
259 canvas.getTextureManager().draw(this, texture, anchor, position, 0);
263 public void draw(Texture texture, AnchorPosition anchor, Vector3d position, double rotationAngle) throws SciRendererException {
264 canvas.getTextureManager().draw(this, texture, anchor, position, rotationAngle);
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.
272 public void bind(Texture texture) throws SciRendererException {
273 canvas.getTextureManager().bind(this, texture);