diff options
author | Shashank | 2017-05-29 12:40:26 +0530 |
---|---|---|
committer | Shashank | 2017-05-29 12:40:26 +0530 |
commit | 0345245e860375a32c9a437c4a9d9cae807134e9 (patch) | |
tree | ad51ecbfa7bcd3cc5f09834f1bb8c08feaa526a4 /modules/scirenderer/examples | |
download | scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.gz scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.bz2 scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.zip |
CMSCOPE changed
Diffstat (limited to 'modules/scirenderer/examples')
14 files changed, 3055 insertions, 0 deletions
diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ChickenPoxCube.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ChickenPoxCube.java new file mode 100755 index 000000000..21c84739e --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ChickenPoxCube.java @@ -0,0 +1,234 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.picking.PickingTask; +import org.scilab.forge.scirenderer.picking.PickingTools; +import org.scilab.forge.scirenderer.renderer.Renderer; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; +import org.scilab.forge.scirenderer.utils.shapes.geometry.CubeFactory; + +import java.awt.Dimension; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.nio.FloatBuffer; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class ChickenPoxCube extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Rotating Cube Example"; + + /** + * The last used transformation. + */ + private Transformation lastTransformation; + + /** + * The clicked point. + */ + private final ElementsBuffer spritePosition; + + /** + * Private constructor. + * This is the main class. + */ + private ChickenPoxCube() { + setTitle(TITLE); + + /** + * Add a {@link MouseUnprojectAdapter} + */ + MouseListener mua = new MouseUnprojectAdapter(getCanvas()); + getPanel().addMouseListener(mua); + + getCanvas().setMainDrawer(new SimpleCubeDrawer(getCanvas())); + spritePosition = getCanvas().getBuffersManager().createElementsBuffer(); + + animate(true); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new ChickenPoxCube(); + } + + /** + * A simple cube drawer. + */ + private final class SimpleCubeDrawer implements Drawer { + + /** + * The sprite size. + */ + private static final int SPRITE_RADIUS = 15; + + /** + * The cube geometry. + */ + private final Geometry cube; + + /** + * A rendering proxy. + */ + private final Renderer renderer; + private final Texture sprite; + + /** + * Default constructor. + * @param canvas the canvas where the cube is drawn. + */ + public SimpleCubeDrawer(Canvas canvas) { + cube = CubeFactory.createCube(canvas, 3, true); + renderer = canvas.getRendererManager().createRenderer(); + + final Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(0, 0, 0)); + appearance.setLineWidth(4); + + sprite = createSprite(canvas); + + renderer.setDrawer(new Drawer() { + @Override + public void draw(DrawingTools dt) { + try { + dt.draw(cube, appearance); + } catch (SciRendererException ignored) { + } + } + + @Override + public boolean is2DView() { + return false; + } + }); + } + + /** + * Sprite creator. + * @param canvas the canvas where the sprite will be drawn. + * @return a sprite to represent clicked point. + */ + private Texture createSprite(Canvas canvas) { + Texture newSprite = canvas.getTextureManager().createTexture(); + newSprite.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.fillDisc(0, 0, SPRITE_RADIUS, new Color(1, 0, 0)); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(SPRITE_RADIUS * 2, SPRITE_RADIUS * 2); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.CENTER; + } + }); + + return newSprite; + } + + @Override + public void draw(DrawingTools dt) { + try { + double t = System.currentTimeMillis() / 50.; + dt.clear(new Color()); + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + Transformation perspective = TransformationFactory.getPerspectiveTransformation(1, 10, 45); + Transformation displacement = TransformationFactory.getTranslateTransformation(0, 0, -5); + dt.getTransformationManager().getProjectionStack().push(projection.rightTimes(perspective.rightTimes(displacement))); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(t, 1, 2, 3)); + lastTransformation = dt.getTransformationManager().getTransformation(); + dt.draw(renderer); + dt.draw(sprite, AnchorPosition.CENTER, spritePosition); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + } + + /** + * This class is a simple example of picking usage. + * @author Pierre Lando + */ + private final class MouseUnprojectAdapter extends MouseAdapter { + + private final Canvas canvas; + + /** + * Default constructor. + * @param canvas the listened canvas. + */ + public MouseUnprojectAdapter(Canvas canvas) { + this.canvas = canvas; + } + + @Override + public void mousePressed(final MouseEvent e) { + canvas.getPickingManager().addPickingTask(new PickingTask() { + @Override + public void perform(PickingTools pickingTools) { + Vector3d l = pickingTools.getUnderlyingPoint(e.getPoint()); + if ((lastTransformation != null) && (l.getZ() != 1)) { + Vector3d lastClickedPoint = lastTransformation.unproject(l); + FloatBuffer data = spritePosition.getData(); + FloatBuffer newData; + if (data == null) { + newData = FloatBuffer.allocate(4); + newData.rewind(); + } else { + data.rewind(); + newData = FloatBuffer.allocate(data.limit() + 4); + float[] dataArray = new float[data.limit()]; + data.get(dataArray); + newData.put(dataArray); + } + newData.put(lastClickedPoint.getDataAsFloatArray(4)); + spritePosition.setData(newData, 4); + } + } + }); + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ClippedTetrahedron.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ClippedTetrahedron.java new file mode 100755 index 000000000..df4769ab8 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ClippedTetrahedron.java @@ -0,0 +1,152 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.clipping.ClippingPlane; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; +import org.scilab.forge.scirenderer.tranformations.Vector4d; +import org.scilab.forge.scirenderer.utils.shapes.geometry.TetrahedronFactory; + +import javax.swing.BoundedRangeModel; +import javax.swing.DefaultBoundedRangeModel; +import javax.swing.JSlider; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import java.awt.BorderLayout; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class ClippedTetrahedron extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Clipping Example"; + + /** + * Private constructor. + * This is the main class. + */ + private ClippedTetrahedron() { + setTitle(TITLE); + + BoundedRangeModel brm = new DefaultBoundedRangeModel(0, 0, -100, 100); + add(new JSlider(brm), BorderLayout.SOUTH); + + /** + * Add a mouse rotation adapter. + */ + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(Math.toRadians(90), new Vector3d(1, 0, 0)), + getCanvas() + ); + getPanel().addMouseListener(mra); + + /** + * Set main drawer. + */ + getCanvas().setMainDrawer(new ClippingDrawer(getCanvas(), mra, brm)); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new ClippedTetrahedron(); + } + + /** + * The clipped tetrahedron drawer. + * @author Pierre Lando + */ + private final class ClippingDrawer implements Drawer { + private final MouseRotationAdapter mra; + private final Geometry tetrahedron; + private final BoundedRangeModel brm; + + /** + * Clipping example drawer constructor. + * @param canvas the canvas. + * @param mra the mouse rotation adapter used for interaction. + * @param brm the {@link javax.swing.BoundedRangeModel} used for the clipping plane coordinate. + */ + public ClippingDrawer(final Canvas canvas, MouseRotationAdapter mra, BoundedRangeModel brm) { + this.mra = mra; + this.tetrahedron = TetrahedronFactory.createTetrahedron(canvas); + this.brm = brm; + + brm.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + canvas.redraw(); + } + }); + } + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color(1f, 1f, 1f)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getScaleTransformation(.5, .5, .5)); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mra.getRotation())); + ClippingPlane cp = dt.getClippingManager().getClippingPlane(0); + cp.setEnable(true); + cp.setEquation(new Vector4d(-1, 0, 0, -getValue())); + + Appearance appearance = new Appearance(); + appearance.setFillColor(new Color(1f, 0f, 0f)); + appearance.setLineColor(new Color(.2f, .2f, .2f)); + appearance.setLineWidth(3); + dt.draw(tetrahedron, appearance); + + cp.setEquation(new Vector4d(1, 0, 0, getValue())); + appearance.setFillColor(new Color(0f, 1f, 0f)); + dt.draw(tetrahedron, appearance); + + cp.setEnable(false); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + + /** + * Current {@link javax.swing.BoundedRangeModel} value getter. + * @return the current {@link javax.swing.BoundedRangeModel}. + */ + private double getValue() { + return -(double) brm.getValue() / 100.0; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JLatexMath.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JLatexMath.java new file mode 100755 index 000000000..24d6222ec --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JLatexMath.java @@ -0,0 +1,243 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.jlatexmath.TeXConstants; +import org.scilab.forge.jlatexmath.TeXFormula; +import org.scilab.forge.jlatexmath.TeXIcon; +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import javax.swing.JComponent; +import javax.swing.JTextArea; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.BadLocationException; +import javax.swing.text.PlainDocument; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.util.EventListener; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class JLatexMath extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "JLatexMath Drawing Example"; + + /** + * Private constructor. + * This is the main class. + */ + private JLatexMath() { + setTitle(TITLE); + LatexCompositor latexCompositor = new LatexCompositor(); + + + add(latexCompositor, BorderLayout.SOUTH); + LatexDrawer latexDrawer = new LatexDrawer(getCanvas()); + latexCompositor.addListener(latexDrawer); + getCanvas().setMainDrawer(latexDrawer); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new JLatexMath(); + } + + /** + * @author Pierre Lando + */ + private final class LatexCompositor extends JComponent { + + private static final String GAUSS = "\\int_{-\\infty}^{+\\infty}\\!e^{-x^2}\\, \\mathrm{d}x = \\sqrt \\pi"; + private final PlainDocument document; + private final JTextArea textArea; + + /** + * Default constructor. + */ + public LatexCompositor() { + setLayout(new BorderLayout()); + document = new PlainDocument(); + try { + document.insertString(0, GAUSS, null); + } catch (BadLocationException ignored) { + } + + document.addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + fireDataChanged(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + fireDataChanged(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + fireDataChanged(); + } + }); + + textArea = new JTextArea(document); + add(textArea, BorderLayout.CENTER); + } + + /** + * Compute the {@see TeXIcon} corresponding to the current formula. + * @return the {@see TeXIcon} corresponding to the current formula. + */ + public TeXIcon getTextIcon() { + try { + TeXFormula formula = new TeXFormula(document.getText(0, document.getLength())); + return formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 32); + } catch (org.scilab.forge.jlatexmath.ParseException e) { + return null; + } catch (BadLocationException e) { + return null; + } + } + + /** + * Add a listener on the latex composition. + * @param latexDrawer the new listener. + */ + public void addListener(LatexDrawer latexDrawer) { + listenerList.add(LatexCompositorListener.class, latexDrawer); + latexDrawer.dataChanged(this); + } + + /** + * Notify listeners for a data change. + */ + private void fireDataChanged() { + for (LatexCompositorListener listener : listenerList.getListeners(LatexCompositorListener.class)) { + listener.dataChanged(this); + } + } + } + + /** + * @author Pierre Lando + */ + private interface LatexCompositorListener extends EventListener { + + /** + * Inform listeners from data changes. + * @param compositor compositor where data have changed. + */ + void dataChanged(LatexCompositor compositor); + } + + /** + * @author Pierre Lando + */ + private final class LatexDrawer implements Drawer, LatexCompositorListener { + + + private final Canvas canvas; + private Texture sprite; + + /** + * Default constructor. + * @param canvas the {@link org.scilab.forge.scirenderer.Canvas} where the drawn will be performed. + */ + public LatexDrawer(Canvas canvas) { + this.canvas = canvas; + sprite = null; + } + + @Override + public void draw(DrawingTools drawingTools) { + drawingTools.clear(new Color()); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(drawingTools.getCanvas().getDimension(), 1f); + drawingTools.getTransformationManager().getProjectionStack().push(projection); + drawingTools.getTransformationManager().getModelViewStack().push(TransformationFactory.getScaleTransformation(.1, .1, .1)); + if (sprite != null) { + drawingTools.draw(sprite, AnchorPosition.CENTER, new Vector3d(0, 0, 0)); + } + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + + @Override + public void dataChanged(final LatexCompositor compositor) { + compositor.textArea.setBackground(Color.RED); + final TeXIcon ti = compositor.getTextIcon(); + canvas.getTextureManager().dispose(sprite); + sprite = null; + if (ti != null) { + ti.setForeground(java.awt.Color.WHITE); + + sprite = canvas.getTextureManager().createTexture(); + sprite.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.clear(new Color(0, 0, 0)); + drawingTools.draw(ti, -(ti.getIconWidth() / 2), -(ti.getIconHeight() / 2)); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(ti.getIconWidth(), ti.getIconHeight()); + } + + @Override + public TextureDrawer.OriginPosition getOriginPosition() { + return TextureDrawer.OriginPosition.CENTER; + } + }); + } + + /** Wait image to set text area background to green */ + new Thread(new Runnable() { + @Override + public void run() { + canvas.waitImage(); + compositor.textArea.setBackground(Color.GREEN); + } + }).start(); + + canvas.redraw(); + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JScrollPaneExample.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JScrollPaneExample.java new file mode 100755 index 000000000..89dc17701 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/JScrollPaneExample.java @@ -0,0 +1,94 @@ +package org.scilab.forge.scirenderer.examples; + +import com.jogamp.opengl.util.Animator; +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.implementation.jogl.JoGLCanvasFactory; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.utils.shapes.geometry.CubeFactory; + +import javax.media.opengl.awt.GLCanvas; +import javax.media.opengl.awt.GLJPanel; +import javax.swing.*; +import java.awt.*; + +/** + * @Author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public class JScrollPaneExample extends JFrame { + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new JScrollPaneExample(); + } + + /** The canvas. */ + private final Canvas canvas; + + /** The gl panel. */ + private final GLCanvas glPanel; + + private JScrollPaneExample() { + glPanel = new GLCanvas(); + canvas = JoGLCanvasFactory.createCanvas(glPanel); + glPanel.setPreferredSize(new Dimension(1024, 768)); + + Animator animator = new Animator(); + animator.add(glPanel); + animator.start(); + + final Geometry cube = CubeFactory.createCube(canvas, 5, true); + final Appearance appearance = new Appearance(); + appearance.setLineWidth(3); + appearance.setLineColor(new Color(.7f, .6f, .5f)); + + canvas.setMainDrawer(new Drawer() { + @Override + public void draw(DrawingTools drawingTools) { + try { + double t = System.currentTimeMillis() / 50.; + drawingTools.clear(new Color()); + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(drawingTools.getCanvas().getDimension(), 1f); + Transformation perspective = TransformationFactory.getPerspectiveTransformation(1, 10, 45); + Transformation displacement = TransformationFactory.getTranslateTransformation(0, 0, -5); + drawingTools.getTransformationManager().getProjectionStack().push(projection.rightTimes(perspective.rightTimes(displacement))); + drawingTools.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(t, 1, 2, 3)); + + drawingTools.draw(cube, appearance); + } catch (SciRendererException ignored) { + } + } + + @Override + public boolean is2DView() { + return false; + } + }); + + JPanel panel = new JPanel(new BorderLayout()); + JScrollPane scrollPane = new JScrollPane(glPanel); + JTextArea textArea = new JTextArea(); + textArea.setText("Test\nTest\nTest\nTest\n"); + + panel.add(scrollPane, BorderLayout.CENTER); + panel.add(textArea, BorderLayout.NORTH); + + + add(panel); + setLocationRelativeTo(null); + setTitle("GLCanvas in JScrollPane example"); + setSize(800, 600); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setVisible(true); + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LightenedCube.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LightenedCube.java new file mode 100755 index 000000000..488db4e2a --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LightenedCube.java @@ -0,0 +1,166 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.lightning.Light; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; +import org.scilab.forge.scirenderer.utils.shapes.geometry.CubeFactory; + +import java.awt.Dimension; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class LightenedCube extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Lightening Example"; + + /** + * Private constructor. + * This is the main class. + */ + private LightenedCube() { + setTitle(TITLE); + + /** + * A MouseRotationAdapter to add some interactivity. + */ + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(), + getCanvas() + ); + getPanel().addMouseListener(mra); + + getCanvas().setMainDrawer(new LightenedCubeDrawer(mra, getCanvas())); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new LightenedCube(); + } + + /** + * The lightened cube drawer. + */ + private final class LightenedCubeDrawer implements Drawer { + + /** + * A sprite to represent light position. + */ + private final Texture sprite; + + /** + * The cube geometry. + */ + private final Geometry cube; + + /** + * The mouse rotation adapter used for interactivity. + */ + private final MouseRotationAdapter mra; + + /** + * Default constructor. + * @param mouseRotationAdapter the mouse rotation adapter used for interactivity. + * @param canvas the canvas where the draw will be performed. + */ + public LightenedCubeDrawer(MouseRotationAdapter mouseRotationAdapter, Canvas canvas) { + sprite = createSprite(canvas); + cube = CubeFactory.createCube(canvas, 10); + mra = mouseRotationAdapter; + } + + /** + * Sprite constructor. + * @param canvas the canvas where the sprite is created. + * @return the sprite representing the light. + */ + private Texture createSprite(Canvas canvas) { + Texture newSprite = canvas.getTextureManager().createTexture(); + newSprite.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.fillDisc(0, 0, 10, new Color(1, 0, 0)); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(16, 16); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.CENTER; + } + }); + return newSprite; + } + + + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color()); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + dt.getTransformationManager().getModelViewStack().push(TransformationFactory.getScaleTransformation(.2, .2, .2)); + + dt.getLightManager().setLightningEnable(true); + Light light = dt.getLightManager().getLight(0); + light.setEnable(true); + light.setAmbientColor(new Color(.5f, .5f, .5f)); + light.setDiffuseColor(new Color(.5f, 0, 0)); + light.setSpecularColor(new Color(.4f, .4f, 0)); + + Vector3d lightPosition = new Vector3d(0, 2, -2); + light.setPosition(lightPosition); + dt.draw(sprite, AnchorPosition.CENTER, lightPosition); + + + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mra.getRotation())); + dt.draw(cube); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LineRendering.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LineRendering.java new file mode 100755 index 000000000..793de9c8f --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/LineRendering.java @@ -0,0 +1,192 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.DefaultGeometry; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.TextEntity; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.DegenerateMatrixException; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; +import org.scilab.forge.scirenderer.utils.shapes.geometry.TetrahedronFactory; + +import java.awt.Dimension; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class LineRendering extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Line Rendering Example"; + + private final LineDrawer drawer; + + /** + * Private constructor. + * This is the main class. + */ + private LineRendering() { + setTitle(TITLE); + + /** + * Add a mouse rotation adapter. + */ + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(Math.toRadians(90), new Vector3d(1, 0, 0)), + getCanvas() + ); + getPanel().addMouseListener(mra); + + drawer = new LineDrawer(getCanvas(), mra); + getCanvas().setMainDrawer(drawer); + + /** + * Add a key listener. + */ + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_F) { + drawer.switchFace(); + getCanvas().redraw(); + } + } + }); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new LineRendering(); + } + + /** + * @author Pierre Lando + */ + private final class LineDrawer implements Drawer { + private static final String MESSAGE_TEXT = "Press 'F' to switch culling mode"; + private final DefaultGeometry tetrahedron; + private final MouseRotationAdapter mra; + private final Texture message; + + /** + * Default constructor. + * @param canvas the canvas where the drawing will be performed. + * @param mouseRotationAdapter the {@link org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter} used for interactivity. + */ + public LineDrawer(Canvas canvas, MouseRotationAdapter mouseRotationAdapter) { + tetrahedron = TetrahedronFactory.createTetrahedron(canvas); + tetrahedron.setFaceCullingMode(Geometry.FaceCullingMode.CCW); + message = createMessage(canvas); + mra = mouseRotationAdapter; + } + + /** + * Switch culled face. + */ + public void switchFace() { + switch (tetrahedron.getFaceCullingMode()) { + case CCW: + tetrahedron.setFaceCullingMode(Geometry.FaceCullingMode.CW); + break; + default: + tetrahedron.setFaceCullingMode(Geometry.FaceCullingMode.CCW); + } + } + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color(1f, 1f, 1f)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + } catch (DegenerateMatrixException ignored) { + // Should not occur. + } + + try { + dt.draw(message, AnchorPosition.UPPER_LEFT, new Vector3d(-.95, .95, 0)); + } catch (SciRendererException ignored) { + } + + try { + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getScaleTransformation(.5, .5, .5)); + + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mra.getRotation())); + + Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(.2f, .2f, .2f)); + appearance.setLineWidth(3); + dt.draw(tetrahedron, appearance); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + + /** + * Create a help message. + * @param canvas the canvas where the message sprite is created. + * @return a sprite that draws the message. + */ + private Texture createMessage(final Canvas canvas) { + final TextEntity text = new TextEntity(MESSAGE_TEXT); + Texture texture = canvas.getTextureManager().createTexture(); + texture.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.draw(text, 0, 0); + } + + @Override + public Dimension getTextureSize() { + return text.getSize(); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.UPPER_LEFT; + } + }); + + return texture; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/MilkDrop.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/MilkDrop.java new file mode 100755 index 000000000..78dc35745 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/MilkDrop.java @@ -0,0 +1,295 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.buffers.IndicesBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.ruler.DefaultRulerModel; +import org.scilab.forge.scirenderer.ruler.RulerDrawer; +import org.scilab.forge.scirenderer.ruler.RulerModel; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.DefaultGeometry; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +/** + * Simple example of SciRenderer usage. + * + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class MilkDrop extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "MilkDrop example"; + + /** + * Private constructor. + * This is the main class. + */ + private MilkDrop() { + setTitle(TITLE); + + /** + * Add a mouse rotation adapter for interactivity. + */ + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(Math.toRadians(90), new Vector3d(1, 0, 0)), + getCanvas() + ); + getPanel().addMouseListener(mra); + + getCanvas().setMainDrawer(new MilkDropDrawer(getCanvas(), mra)); + } + + /** + * Main function. + * @param arguments main arguments. + */ + public static void main(String[] arguments) { + new MilkDrop(); + } + + /** + * @author Pierre Lando + */ + private final class MilkDropDrawer implements Drawer { + private final RulerDrawer rulerDrawer; + private final Geometry milkDrop; + private final MouseRotationAdapter mouseRotationAdapter; + private final Appearance appearance; + + private DefaultRulerModel xRulerModel; + private DefaultRulerModel yRulerModel; + private DefaultRulerModel zRulerModel; + + /** + * Default constructor. + * @param canvas the canvas where the draw will be done. + * @param mra the {@link org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter} used for interactivity. + */ + public MilkDropDrawer(Canvas canvas, MouseRotationAdapter mra) { + this.mouseRotationAdapter = mra; + + appearance = new Appearance(); + appearance.setLineWidth(1); + appearance.setLineColor(new Color(0, 0, 0)); + rulerDrawer = new RulerDrawer(canvas.getTextureManager()); + milkDrop = new MilkDropGeometry(canvas); + } + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color(1f, 1f, 1f)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getScaleTransformation(.03, .03, .03)); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mouseRotationAdapter.getRotation())); + dt.draw(milkDrop, appearance); + } catch (SciRendererException ignored) { + // Should not occur. + } + + rulerDrawer.draw(dt, getXRulerModel()); + rulerDrawer.draw(dt, getYRulerModel()); + rulerDrawer.draw(dt, getZRulerModel()); + } + + @Override + public boolean is2DView() { + return false; + } + + /** + * X ruler model getter. + * @return the X ruler model. + */ + public RulerModel getXRulerModel() { + if (xRulerModel == null) { + xRulerModel = new DefaultRulerModel(); + xRulerModel.setFirstPoint(new Vector3d(-20, 20, 0)); + xRulerModel.setSecondPoint(new Vector3d(20, 20, 0)); + xRulerModel.setValues(-20, 20); + xRulerModel.setLineVisible(true); + xRulerModel.setTicksDirection(new Vector3d(0, 1, 0)); + } + return xRulerModel; + } + + /** + * Y ruler model getter. + * @return the Y ruler model. + */ + public RulerModel getYRulerModel() { + if (yRulerModel == null) { + yRulerModel = new DefaultRulerModel(); + yRulerModel.setFirstPoint(new Vector3d(20, -20, 0)); + yRulerModel.setSecondPoint(new Vector3d(20, 20, 0)); + yRulerModel.setValues(-20, 20); + yRulerModel.setLineVisible(true); + yRulerModel.setTicksDirection(new Vector3d(1, 0, 0)); + } + return yRulerModel; + } + + /** + * Z ruler model getter. + * @return the Z ruler model. + */ + public RulerModel getZRulerModel() { + if (zRulerModel == null) { + zRulerModel = new DefaultRulerModel(); + zRulerModel.setFirstPoint(new Vector3d(20, 20, 20)); + zRulerModel.setSecondPoint(new Vector3d(20, 20, -20)); + zRulerModel.setValues(-20, 20); + zRulerModel.setLineVisible(true); + zRulerModel.setTicksDirection(new Vector3d(1, 1, 0)); + } + return zRulerModel; + } + } + + /** + * A geometry implementation representing a MilkDrop. + * @author Pierre Lando + */ + private final class MilkDropGeometry extends DefaultGeometry { + private static final int HALF_SIDE = 20; + private static final int SIDE = 2 * HALF_SIDE + 1; + private static final int ELEMENTS_SIZE = 4; + + + /** + * @param canvas the canvas where the buffers are created. + */ + public MilkDropGeometry(Canvas canvas) { + FloatBuffer vertices = FloatBuffer.allocate(SIDE * SIDE * ELEMENTS_SIZE); + FloatBuffer colors = FloatBuffer.allocate(SIDE * SIDE * ELEMENTS_SIZE); + IntBuffer wireIndices = IntBuffer.allocate((SIDE - 1) * (SIDE - 1) * 8); + IntBuffer indices = IntBuffer.allocate((SIDE - 1) * (SIDE - 1) * 6); + + fillVerticesAndColors(vertices, colors); + fillIndices(indices, wireIndices); + + ElementsBuffer vertexBuffer = canvas.getBuffersManager().createElementsBuffer(); + ElementsBuffer colorBuffer = canvas.getBuffersManager().createElementsBuffer(); + IndicesBuffer indicesBuffer = canvas.getBuffersManager().createIndicesBuffer(); + IndicesBuffer wireIndicesBuffer = canvas.getBuffersManager().createIndicesBuffer(); + + vertexBuffer.setData(vertices, ELEMENTS_SIZE); + colorBuffer.setData(colors, ELEMENTS_SIZE); + indicesBuffer.setData(indices); + wireIndicesBuffer.setData(wireIndices); + + setFillDrawingMode(FillDrawingMode.TRIANGLES); + setLineDrawingMode(LineDrawingMode.SEGMENTS); + setPolygonOffsetMode(true); + setVertices(vertexBuffer); + setColors(colorBuffer); + setIndices(indicesBuffer); + setWireIndices(wireIndicesBuffer); + } + + /** + * Fill indices buffers. + * @param indices the triangles indices to fill. + * @param wireIndices the segments indices to fill.s + */ + private void fillIndices(IntBuffer indices, IntBuffer wireIndices) { + for (int x = 0; x < SIDE - 1; x++) { + for (int y = 0; y < SIDE - 1; y++) { + /** + * (x, y) -- (x+1, y) + * | | + * | | + * (x, y+1)--(x+1, y+1) + * + * (x,y) => (x*SIDE + y); + * + */ + + if (((x >= 0) && (y < 0)) || ((x < 0) && (y >= 0))) { + indices.put((x * SIDE) + y); + indices.put(((x + 1) * SIDE) + y); + indices.put(((x + 1) * SIDE) + (y + 1)); + + indices.put((x * SIDE) + y); + indices.put(((x + 1) * SIDE) + (y + 1)); + indices.put((x * SIDE) + (y + 1)); + } else { + indices.put((x * SIDE) + y); + indices.put(((x + 1) * SIDE) + y); + indices.put((x * SIDE) + (y + 1)); + + indices.put(((x + 1) * SIDE) + y); + indices.put(((x + 1) * SIDE) + (y + 1)); + indices.put((x * SIDE) + (y + 1)); + } + + wireIndices.put((x * SIDE) + y); + wireIndices.put((x * SIDE) + (y + 1)); + wireIndices.put((x * SIDE) + (y + 1)); + wireIndices.put(((x + 1) * SIDE) + (y + 1)); + wireIndices.put(((x + 1) * SIDE) + (y + 1)); + wireIndices.put(((x + 1) * SIDE) + y); + wireIndices.put(((x + 1) * SIDE) + y); + wireIndices.put((x * SIDE) + y); + } + } + } + + /** + * Fill the given buffer with vertices data and color data. + * @param vertices the vertices buffer. + * @param colors the colors buffer. + */ + private void fillVerticesAndColors(FloatBuffer vertices, FloatBuffer colors) { + vertices.rewind(); + colors.rewind(); + for (int x = -HALF_SIDE; x <= HALF_SIDE; x++) { + for (int y = -HALF_SIDE; y <= HALF_SIDE; y++) { + double d = Math.sqrt((x * x) + (y * y)); + double z = HALF_SIDE * Math.cos(d) * Math.exp(-d / 6) / 2; + + vertices.put(x); + vertices.put(y); + vertices.put((float) z); + vertices.put(1); + + colors.put((float) z); + colors.put((float) (1 - z)); + colors.put(0); + colors.put(1); + } + } + vertices.rewind(); + colors.rewind(); + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/RotatableSprite.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/RotatableSprite.java new file mode 100755 index 000000000..5aa2ff215 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/RotatableSprite.java @@ -0,0 +1,392 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * Copyright (C) 2011 - DIGITEO - Manuel Juliachs + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.TextEntity; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.texture.TextureManager; +import org.scilab.forge.scirenderer.tranformations.DegenerateMatrixException; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.awt.Dimension; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +/** + * A basic example featuring rotatable 2D sprites. + * + * @author Pierre Lando + * @author Manuel Juliachs + */ +@SuppressWarnings(value = { "serial" }) +public final class RotatableSprite extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Rotatable Sprites Example"; + + /** + * Private constructor. + * This is the main class. + */ + private RotatableSprite() { + setTitle(TITLE); + + final RotatableSpriteDrawer drawer = new RotatableSpriteDrawer(getCanvas()); + getCanvas().setMainDrawer(drawer); + + /* Key listener */ + getPanel().addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_T) { + drawer.toggleAnchorDisplay(); + } else if (e.getKeyCode() == KeyEvent.VK_D) { + drawer.switchRotationDirection(); + } else if (e.getKeyCode() == KeyEvent.VK_M) { + drawer.toggleMessageDisplay(); + } + } + }); + + animate(true); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new RotatableSprite(); + } + + /** + * A rotatable sprite drawer. + * Draws one rotating sprite for each existing sprite anchor position value. + * + * @author Manuel Juliachs + */ + public static class RotatableSpriteDrawer implements Drawer { + + /** The sprites' text strings. */ + private static final String[] TEXT_STRINGS = { + "Center anchor", "Left anchor", "Lower-left anchor", + "Down anchor", "Lower-right anchor", "Right anchor", + "Upper-right anchor", "Up anchor", "Upper-left anchor" + }; + + /** The anchor positions. */ + private static final AnchorPosition[] ANCHOR_POSITIONS = { + AnchorPosition.CENTER, AnchorPosition.LEFT, AnchorPosition.LOWER_LEFT, + AnchorPosition.DOWN, AnchorPosition.LOWER_RIGHT, AnchorPosition.RIGHT, + AnchorPosition.UPPER_RIGHT, AnchorPosition.UP, AnchorPosition.UPPER_LEFT + }; + + /** The step added to the rotation angle at each draw call. */ + private static final float ROTATION_ANGLE_STEP = 2.5f; + + /** Arbitrary displacement used to position the sprites. */ + private static final float DELTA_POSITION = 0.8f; + + /** The number of sprites to draw, equal to the number of available sprite anchor position values. */ + private static final int NUM_SPRITES = 9; + + /** The margin used for the text sprites. */ + private static final int MARGIN = 4; + + /** The sprites' coordinates. */ + private final float[] positions; + + /** The buffer used to position anchor sprites. */ + private final ElementsBuffer position; + + /** The text sprites. */ + private final Texture[] textSprites; + + /** The sprite used to draw anchors. */ + private final Texture anchorSprite; + + /** The message sprite. */ + private final Texture messageSprite; + + /** The sprites' text entities. */ + private final TextEntity[] textEntities; + + /** The rotation angle applied to all the sprites. */ + private float rotationAngle; + + /** The line width of the sprites' box. */ + private final int lineWidth = 2; + + /** The line's half width. */ + private final int halfLineWidth = lineWidth / 2; + + /** Specifies whether anchor sprites are displayed or not. */ + private boolean anchorsDisplayed; + + /** Specifies the sprites' rotation direction. */ + private boolean clockwiseRotation; + + /** Specifies whether the message is displayed or not. */ + private boolean messageDisplayed; + + /** + * Constructor. + * @param canvas the canvas to use. + */ + public RotatableSpriteDrawer(Canvas canvas) { + /* The texture manager. */ + TextureManager textureManager = canvas.getTextureManager(); + + anchorsDisplayed = true; + clockwiseRotation = false; + messageDisplayed = true; + rotationAngle = 0; + + textEntities = new TextEntity[NUM_SPRITES]; + + for (int i = 0; i < NUM_SPRITES; i++) { + TextEntity textEntity = new TextEntity(TEXT_STRINGS[i]); + textEntity.setTextColor(new Color(0.0f, 0.0f, 0.0f)); + textEntity.setTextAntiAliased(false); + textEntities[i] = textEntity; + } + + textSprites = new Texture[NUM_SPRITES]; + + for (int i = 0; i < NUM_SPRITES; i++) { + textSprites[i] = textureManager.createTexture(); + textSprites[i].setDrawer(getTextDrawer(i)); + } + + /* The sprite used to visualize anchor points */ + anchorSprite = textureManager.createTexture(); + anchorSprite.setDrawer(getSpriteDrawer()); + + position = canvas.getBuffersManager().createElementsBuffer(); + + positions = new float[] { + 0, 0, 0, + -DELTA_POSITION, 0, 0, + -DELTA_POSITION, -DELTA_POSITION, 0, + 0, -DELTA_POSITION, 0, + DELTA_POSITION, -DELTA_POSITION, 0, + DELTA_POSITION, 0, 0, + DELTA_POSITION, DELTA_POSITION, 0, + 0, DELTA_POSITION, 0, + -DELTA_POSITION, DELTA_POSITION, 0 + }; + + position.setData(positions, 3); + + /* The message sprite */ + messageSprite = createMessage(canvas); + } + + @Override + public void draw(DrawingTools drawingTools) { + try { + drawingTools.clear(new Color(1.0f, 1.0f, 1.0f)); + + Transformation projection = null; + try { + projection = TransformationFactory.getPreferredAspectRatioTransformation(drawingTools.getCanvas().getDimension(), 1f); + } catch (DegenerateMatrixException ignored) { + // Should not occur. + } + + drawingTools.getTransformationManager().getProjectionStack().push(projection); + + for (int i = 0; i < NUM_SPRITES; i++) { + drawingTools.draw( + textSprites[i], + ANCHOR_POSITIONS[i], + new Vector3d(positions[3 * i], positions[3 * i + 1], positions[3 * i + 2]), + rotationAngle + ); + } + + if (anchorsDisplayed) { + drawingTools.draw(anchorSprite, AnchorPosition.CENTER, position); + } + + if (messageDisplayed) { + drawingTools.draw(messageSprite, AnchorPosition.UPPER_LEFT, new Vector3d(-1.0, 1.0, 0.0)); + } + + + if (clockwiseRotation) { + rotationAngle -= ROTATION_ANGLE_STEP; + } else { + rotationAngle += ROTATION_ANGLE_STEP; + } + + rotationAngle %= 360.0f; + } catch (SciRendererException ignored) { + } + } + + @Override + public boolean is2DView() { + return false; + } + + /** + * Returns the anchor sprite drawer. + * @return the anchor sprite drawer. + */ + public TextureDrawer getSpriteDrawer() { + + return new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.fillDisc(0, 0, 10, new Color(1, 0, 0)); + + + Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(0, 0, 0)); + drawingTools.drawCircle(0, 0, 10, appearance); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(10, 10); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.CENTER; + } + }; + } + + /** + * Returns the sprite drawer associated to a particular text sprite. + * @param textSpriteIndex the sprite index. + * @return the text sprite drawer. + */ + public TextureDrawer getTextDrawer(final int textSpriteIndex) { + + if ((textSpriteIndex > (NUM_SPRITES - 1)) || (textSpriteIndex < 0)) { + return null; + } + + return new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + TextEntity textEntity = textEntities[textSpriteIndex]; + + drawingTools.draw(textEntity, 0 + MARGIN + halfLineWidth, 0 + MARGIN + halfLineWidth); + + Dimension dimension = getTextureSize(); + int boxWidth = dimension.width; + int boxHeight = dimension.height; + + Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(0, 0, 0)); + appearance.setLineWidth((float) lineWidth); + + drawingTools.drawPolyline(new int[] {halfLineWidth, halfLineWidth, boxWidth - halfLineWidth, halfLineWidth, + boxWidth - halfLineWidth, boxHeight - halfLineWidth, halfLineWidth, boxHeight - halfLineWidth, + halfLineWidth, halfLineWidth + }, + appearance); + } + + @Override + public Dimension getTextureSize() { + Dimension dimension = textEntities[textSpriteIndex].getSize(); + + int boxWidth = (int) dimension.getWidth() + 2 * MARGIN + lineWidth; + int boxHeight = (int) dimension.getHeight() + 2 * MARGIN + lineWidth; + + return new Dimension(boxWidth, boxHeight); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.UPPER_LEFT; + } + + }; + } + + /** + * Toggles display of the anchors. + */ + public void toggleAnchorDisplay() { + anchorsDisplayed = !anchorsDisplayed; + } + + /** + * Switches the rotation direction. + */ + public void switchRotationDirection() { + clockwiseRotation = !clockwiseRotation; + } + + /** + * Toggles display of the help message. + */ + public void toggleMessageDisplay() { + messageDisplayed = !messageDisplayed; + } + + /** + * Creates a help message. + * @param canvas the canvas where the message sprite is created. + * @return a sprite that draws the message. + */ + private Texture createMessage(Canvas canvas) { + final TextEntity text = new TextEntity("Press 'T' to toggle anchor drawing, 'D' to switch the rotation direction, 'M' to toggle this message."); + Texture message = canvas.getTextureManager().createTexture(); + message.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + drawingTools.draw(text, 0, 0); + } + + @Override + public Dimension getTextureSize() { + return text.getSize(); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.UPPER_LEFT; + } + }); + + return message; + } + + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ScilabLikePlot2D.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ScilabLikePlot2D.java new file mode 100755 index 000000000..fcc84ac9b --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/ScilabLikePlot2D.java @@ -0,0 +1,259 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.ruler.DefaultRulerModel; +import org.scilab.forge.scirenderer.ruler.RulerDrawer; +import org.scilab.forge.scirenderer.ruler.RulerModel; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.DefaultGeometry; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.awt.Dimension; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class ScilabLikePlot2D extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Scilab like 'plot2D' Example"; + + /** + * Private constructor. + * This is the main class. + */ + private ScilabLikePlot2D() { + setTitle(TITLE); + + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(Math.toRadians(0), new Vector3d(1, 0, 0)), + getCanvas() + ); + getPanel().addMouseListener(mra); + + getCanvas().setMainDrawer(new Plot2DDrawer(getCanvas(), mra)); + } + + /** + * Main function. + * @param arguments main arguments. + */ + public static void main(String[] arguments) { + new ScilabLikePlot2D(); + } + + /** + * A plot2d like drawer + * + * @author Pierre Lando + */ + public static class Plot2DDrawer implements Drawer { + + // Some geometry + private final DefaultGeometry plot1; + private final DefaultGeometry plot2; + + // Some sprite and position buffer for it. + private final ElementsBuffer plot3Data; + private final Texture sprite; + + // Some rulers. + private final RulerDrawer rulerDrawer; + private DefaultRulerModel xRulerModel; + private DefaultRulerModel yRulerModel; + + private final MouseRotationAdapter mra; + + + /** + * Default constructor. + * @param canvas parent {@link org.scilab.forge.scirenderer.Canvas} + * @param mra a {@link org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter} to add some interactivity. + */ + public Plot2DDrawer(Canvas canvas, MouseRotationAdapter mra) { + + this.mra = mra; + /** + * Create the ruler drawer. + */ + rulerDrawer = new RulerDrawer(canvas.getTextureManager()); + + // Create a geometry + ElementsBuffer sinData1 = canvas.getBuffersManager().createElementsBuffer(); + sinData1.setData(getSinData(3, 100, 10), 4); + plot1 = new DefaultGeometry(); + plot1.setFillDrawingMode(Geometry.FillDrawingMode.NONE); + plot1.setLineDrawingMode(Geometry.LineDrawingMode.SEGMENTS_STRIP); + plot1.setVertices(sinData1); + + // Create a second geometry + ElementsBuffer sinData2 = canvas.getBuffersManager().createElementsBuffer(); + sinData2.setData(getSinData(2, 100, 10), 4); + plot2 = new DefaultGeometry(); + plot2.setFillDrawingMode(Geometry.FillDrawingMode.NONE); + plot2.setLineDrawingMode(Geometry.LineDrawingMode.SEGMENTS_STRIP); + plot2.setVertices(sinData2); + + // Create a position buffer to draw some sprite. + plot3Data = canvas.getBuffersManager().createElementsBuffer(); + plot3Data.setData(getSinData(1, 60, 10), 4); + + // Create a sprite. + sprite = canvas.getTextureManager().createTexture(); + sprite.setDrawer(getSpriteDrawer()); + } + + @Override + public void draw(DrawingTools drawingTools) { + drawingTools.clear(new Color(1f, 1f, 1f)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(drawingTools.getCanvas().getDimension(), 1f); + drawingTools.getTransformationManager().getProjectionStack().push(projection); + drawingTools.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getScaleTransformation(.1, .1, .1)); + drawingTools.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mra.getRotation())); + Appearance appearance = new Appearance(); + + // Draw first geometry. + appearance.setLineColor(new Color(0, 1, 0)); + appearance.setLineWidth(4); + drawingTools.draw(plot1, appearance); + + // Draw second geometry. + appearance.setLineColor(new Color(0, 0, 0)); + appearance.setLineWidth(1); + drawingTools.draw(plot2, appearance); + + // Draw sprites. + drawingTools.draw(sprite, AnchorPosition.CENTER, plot3Data); + + // Draw rulers. + rulerDrawer.draw(drawingTools, getXRulerModel()); + rulerDrawer.draw(drawingTools, getYRulerModel()); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + + @Override + public boolean is2DView() { + return true; + } + + /** + * Return a float array filled with some "y = sin(theta * x)" data. + * @param theta the data period. + * @param density the data density (number of point). + * @param bound the data bounds (result have x in [-bound, bound[). + * @return a float array filled with some "y = sin(theta * x)" data. + */ + public float[] getSinData(float theta, int density, float bound) { + float[] sinData = new float[density * 4]; + for (int i = 0; i < density; i++) { + float x = bound * 2 * (i - density / 2f) / density; // x : [-bound, bound[ + float y = (float) Math.sin(x * theta); + + sinData[i * 4] = x; + sinData[i * 4 + 1] = y; + sinData[i * 4 + 2] = 0; + sinData[i * 4 + 3] = 1; + } + return sinData; + } + + /** + * Return a sprite drawer. + * @return a sprite drawer. + */ + public TextureDrawer getSpriteDrawer() { + return new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(1, 0, 0)); + drawingTools.drawPolyline(new int[] { + -5, 0, + +5, 0 + }, appearance); + drawingTools.drawPolyline(new int[] { + 0, -5, + 0, +5 + }, appearance); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(16, 16); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.CENTER; + } + }; + } + + /** + * Return the X ruler model. + * @return the X ruler model. + */ + public RulerModel getXRulerModel() { + if (xRulerModel == null) { + xRulerModel = new DefaultRulerModel(); + xRulerModel.setFirstPoint(new Vector3d(-10, -1, 0)); + xRulerModel.setSecondPoint(new Vector3d(10, -1, 0)); + xRulerModel.setValues(10, -10); + xRulerModel.setLineVisible(true); + xRulerModel.setTicksDirection(new Vector3d(0, -1, 0)); + } + return xRulerModel; + } + + /** + * Return the Y ruler model. + * @return the Y ruler model. + */ + public RulerModel getYRulerModel() { + if (yRulerModel == null) { + yRulerModel = new DefaultRulerModel(); + yRulerModel.setFirstPoint(new Vector3d(-10, -1, 0)); + yRulerModel.setSecondPoint(new Vector3d(-10, 1, 0)); + yRulerModel.setValues(1, -1); + yRulerModel.setLineVisible(true); + yRulerModel.setTicksDirection(new Vector3d(-1, 0, 0)); + } + return yRulerModel; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/SimpleRuler.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/SimpleRuler.java new file mode 100755 index 000000000..2caad0999 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/SimpleRuler.java @@ -0,0 +1,184 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.ruler.DefaultRulerModel; +import org.scilab.forge.scirenderer.ruler.RulerDrawer; +import org.scilab.forge.scirenderer.ruler.RulerModel; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.tranformations.DegenerateMatrixException; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import javax.swing.BoundedRangeModel; +import javax.swing.DefaultBoundedRangeModel; +import javax.swing.JCheckBox; +import javax.swing.JFormattedTextField; +import javax.swing.JPanel; +import javax.swing.JSlider; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.DecimalFormat; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class SimpleRuler extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Ruler Example"; + + /** + * Private constructor. + * This is the main class. + */ + private SimpleRuler() { + setTitle(TITLE); + + final BoundedRangeModel zoomLevel = new DefaultBoundedRangeModel(13, 0, 1, 1000); + + + JPanel buttonsPanel = new JPanel(new FlowLayout()); + + final JFormattedTextField minValue = new JFormattedTextField(new DecimalFormat("0.0000")); + minValue.setValue(1); + + final JFormattedTextField maxValue = new JFormattedTextField(new DecimalFormat("0.0000")); + maxValue.setValue(4); + + final JCheckBox logBox = new JCheckBox("Log"); + logBox.setSelected(true); + + buttonsPanel.add(minValue); + buttonsPanel.add(maxValue); + buttonsPanel.add(logBox); + buttonsPanel.add(new JSlider(zoomLevel)); + + add(buttonsPanel, BorderLayout.SOUTH); + + // SciRenderer canvas. + final DefaultRulerModel rulerModel = new DefaultRulerModel(); + rulerModel.setFirstPoint(new Vector3d(-1, 0, 0)); + rulerModel.setSecondPoint(new Vector3d(1, 0, 0)); + rulerModel.setFirstValue(Double.parseDouble(minValue.getValue().toString())); + rulerModel.setSecondValue(Double.parseDouble(maxValue.getValue().toString())); + rulerModel.setTicksDirection(new Vector3d(0, -1, 0)); + rulerModel.setLogarithmic(true); + + Drawer drawer = new MainRulerDrawer(getCanvas(), zoomLevel, rulerModel); + getCanvas().setMainDrawer(drawer); + + zoomLevel.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + getCanvas().redraw(); + } + }); + + logBox.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + if (logBox.isSelected()) { + double a = Math.abs(rulerModel.getFirstValue()); + double b = Math.abs(rulerModel.getSecondValue()); + if (b == 0) { + b = 10; + } + if (a == 0) { + a = 1; + } + rulerModel.setFirstValue(Math.min(a, b)); + rulerModel.setSecondValue(Math.max(a, b)); + rulerModel.setLogarithmic(true); + } else { + rulerModel.setLogarithmic(false); + } + getCanvas().redraw(); + } + }); + + ActionListener valueChangedListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + rulerModel.setFirstValue(Double.parseDouble(minValue.getValue().toString())); + rulerModel.setSecondValue(Double.parseDouble(maxValue.getValue().toString())); + getCanvas().redraw(); + } + }; + + maxValue.addActionListener(valueChangedListener); + minValue.addActionListener(valueChangedListener); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new SimpleRuler(); + } + + /** + * The ruler drawer. + */ + public static class MainRulerDrawer implements Drawer { + + private final RulerDrawer rulerDrawer; + private final RulerModel rulerModel; + private final BoundedRangeModel zoomLevel; + + /** + * Default constructor. + * @param canvas the canvas where the draw will be performed. + * @param zoomLevel the current zoom level model. + * @param rulerModel the current ruler model. + */ + public MainRulerDrawer(Canvas canvas, BoundedRangeModel zoomLevel, RulerModel rulerModel) { + this.zoomLevel = zoomLevel; + this.rulerModel = rulerModel; + this.rulerDrawer = new RulerDrawer(canvas.getTextureManager()); + } + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color(1f, 1f, 1f)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getScaleTransformation(zoomLevel.getValue() / 100.0)); + + } catch (DegenerateMatrixException ignored) { + // Should not occur. + } + + rulerDrawer.draw(dt, rulerModel); + } + + @Override + public boolean is2DView() { + return false; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/Sprites.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/Sprites.java new file mode 100755 index 000000000..adc9dc1c5 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/Sprites.java @@ -0,0 +1,173 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.examples.utils.MouseRotationAdapter; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.texture.AnchorPosition; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.texture.TextureDrawer; +import org.scilab.forge.scirenderer.texture.TextureDrawingTools; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.awt.Dimension; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class Sprites extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Sprites Example"; + + /** + * Private constructor. + * This is the main class. + */ + private Sprites() { + setTitle(TITLE); + + /** + * Add a mouse rotation adapter. + */ + final MouseRotationAdapter mra = new MouseRotationAdapter( + new Rotation(Math.toRadians(90), new Vector3d(1, 0, 0)), + getCanvas() + ); + getPanel().addMouseListener(mra); + + getCanvas().setMainDrawer(new SpritesDrawer(getCanvas(), mra)); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new Sprites(); + } + + /** + * Sprites drawer. + */ + public static class SpritesDrawer implements Drawer { + private static final int SPRITE_RADIUS = 5; + private final int cubeSize = 8; + private final MouseRotationAdapter mouseRotationAdapter; + private final ElementsBuffer position; + private final Texture sprite; + + /** + * Constructor. + * @param canvas the canvas where the sprites are drawn. + * @param mra the mouse listener used for the interaction. + */ + public SpritesDrawer(Canvas canvas, MouseRotationAdapter mra) { + mouseRotationAdapter = mra; + sprite = createSprite(canvas); + + position = canvas.getBuffersManager().createElementsBuffer(); + position.setData(createData(), 4); + } + + @Override + public void draw(DrawingTools drawingTools) { + drawingTools.clear(new Color(1, 1, 1)); + + try { + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(drawingTools.getCanvas().getDimension(), 1f); + drawingTools.getTransformationManager().getProjectionStack().push(projection); + drawingTools.getTransformationManager().getModelViewStack().push(TransformationFactory.getScaleTransformation(.6, .6, .6)); + drawingTools.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(mouseRotationAdapter.getRotation())); + drawingTools.draw(sprite, AnchorPosition.CENTER, position); + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + + /** + * Create sprite position data. + * @return an array filled with the position where the sprite will be drawn. + */ + private float[] createData() { + float[] data = new float[4 * cubeSize * cubeSize * cubeSize]; + int index = 0; + for (float i = 0; i < cubeSize; i++) { + for (float j = 0; j < cubeSize; j++) { + for (float k = 0; k < cubeSize; k++) { + data[index++] = (i / (cubeSize - 1)) * 2 - 1; + data[index++] = (j / (cubeSize - 1)) * 2 - 1; + data[index++] = (k / (cubeSize - 1)) * 2 - 1; + data[index++] = 1; + } + } + } + return data; + } + + /** + * Sprite constructor. + * @param canvas the canvas where the sprite will be drawn. + * @return the example sprite. + */ + private Texture createSprite(Canvas canvas) { + Texture newSprite = canvas.getTextureManager().createTexture(); + newSprite.setDrawer(new TextureDrawer() { + + @Override + public void draw(TextureDrawingTools drawingTools) { + Appearance appearance = new Appearance(); + appearance.setLineColor(new Color(0, 0, 0)); + drawingTools.drawPolyline(new int[] { + -SPRITE_RADIUS, 0, + +SPRITE_RADIUS, 0, + }, appearance); + + drawingTools.drawPolyline(new int[] { + 0, +SPRITE_RADIUS, + 0, -SPRITE_RADIUS + }, appearance); + } + + @Override + public Dimension getTextureSize() { + return new Dimension(SPRITE_RADIUS * 2 + 1, SPRITE_RADIUS * 2 + 1); + } + + @Override + public OriginPosition getOriginPosition() { + return OriginPosition.CENTER; + } + }); + + return newSprite; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/TextureExample.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/TextureExample.java new file mode 100755 index 000000000..d687b759e --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/TextureExample.java @@ -0,0 +1,463 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ +package org.scilab.forge.scirenderer.examples; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.Drawer; +import org.scilab.forge.scirenderer.DrawingTools; +import org.scilab.forge.scirenderer.SciRendererException; +import org.scilab.forge.scirenderer.buffers.ElementsBuffer; +import org.scilab.forge.scirenderer.buffers.IndicesBuffer; +import org.scilab.forge.scirenderer.examples.utils.ExampleFrame; +import org.scilab.forge.scirenderer.shapes.appearance.Appearance; +import org.scilab.forge.scirenderer.shapes.appearance.Color; +import org.scilab.forge.scirenderer.shapes.geometry.DefaultGeometry; +import org.scilab.forge.scirenderer.shapes.geometry.Geometry; +import org.scilab.forge.scirenderer.texture.AbstractTextureDataProvider; +import org.scilab.forge.scirenderer.texture.Texture; +import org.scilab.forge.scirenderer.tranformations.Transformation; +import org.scilab.forge.scirenderer.tranformations.TransformationFactory; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.awt.Dimension; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.nio.ByteBuffer; +import java.util.Date; + +/** + * @author Pierre Lando + */ +@SuppressWarnings(value = { "serial" }) +public final class TextureExample extends ExampleFrame { + + /** + * Frame title. + */ + private static final String TITLE = "Texture Example"; + + /** + * Private constructor. + * This is the main class. + */ + private TextureExample() { + + setTitle(TITLE); + + final ClickCounter clickCounter = new ClickCounter(); + getPanel().addMouseListener(clickCounter); + getCanvas().setMainDrawer(new TextureDrawer(getCanvas(), clickCounter)); + + animate(true); + } + + /** + * Main function. + * @param arguments launch arguments. Ignored. + */ + public static void main(String[] arguments) { + new TextureExample(); + } + + /** + * Texture drawer. + */ + private final class TextureDrawer implements Drawer { + private final ClickCounter clickCounter; + + private final Appearance appearance1; + private final Appearance appearance2; + + private final Geometry geometry1; + private final Geometry geometry2; + + /** + * Constructor. + * @param canvas the canvas where the textures will be drawn. + * @param clickCounter the click counter used to determine witch texture is drawn. + */ + public TextureDrawer(Canvas canvas, ClickCounter clickCounter) { + this.clickCounter = clickCounter; + + geometry1 = new SimpleGeometry(canvas); + geometry2 = new LessSimpleGeometry(canvas); + + Texture texture1 = canvas.getTextureManager().createTexture(); + Texture texture2 = canvas.getTextureManager().createTexture(); + + appearance1 = new Appearance(); + texture1.setDataProvider(new Simple2DTextureDataProvider()); + appearance1.setTexture(texture1); + + appearance2 = new Appearance(); + texture2.setDataProvider(new Simple1DTextureDataProvider()); + appearance2.setTexture(texture2); + } + + @Override + public void draw(DrawingTools dt) { + dt.clear(new Color()); + + try { + double t = System.currentTimeMillis() / 30.; + + Transformation projection = TransformationFactory.getPreferredAspectRatioTransformation(dt.getCanvas().getDimension(), 1f); + dt.getTransformationManager().getProjectionStack().push(projection); + dt.getTransformationManager().getModelViewStack().push(TransformationFactory.getScaleTransformation(Math.sqrt(2) / 2)); + dt.getTransformationManager().getModelViewStack().pushRightMultiply(TransformationFactory.getRotationTransformation(t, 1, 2, 3)); + + if (clickCounter.getCount() % 2 == 0) { + dt.draw(geometry1, appearance1); + } else { + dt.draw(geometry2, appearance2); + } + } catch (SciRendererException ignored) { + // Should not occur. + } + } + + @Override + public boolean is2DView() { + return false; + } + } + + /** + * This mouse adapter count the number of click. + */ + private final class ClickCounter extends MouseAdapter { + private int count; + + /** + * Default constructor. + */ + public ClickCounter() { + count = 0; + } + + @Override + public void mousePressed(MouseEvent e) { + count++; + } + + /** + * Number of click getter. + * @return the number of click until the application start. + */ + public int getCount() { + return count; + } + } + + /** + * @author Pierre Lando + */ + private final class SimpleGeometry extends DefaultGeometry implements Geometry { + + /** + * Default constructor. + * @param canvas the canvas where the geometry will be created. + */ + public SimpleGeometry(Canvas canvas) { + ElementsBuffer vertices = canvas.getBuffersManager().createElementsBuffer(); + vertices.setData(new float[] { + -1, -1, 0, 1, + -1, +1, 0, 1, + +1, +1, 0, 1, + +1, -1, 0, 1, + }, 4); + + IndicesBuffer indices = canvas.getBuffersManager().createIndicesBuffer(); + indices.setData(new int[] { + 0, 1, 2, 0, 2, 3 + }); + + ElementsBuffer textureCoordinates = canvas.getBuffersManager().createElementsBuffer(); + textureCoordinates.setData(new float[] { + 0, 0, + 0, 1, + 1, 1, + 1, 0 + }, 2); + + setFillDrawingMode(FillDrawingMode.TRIANGLES); + setVertices(vertices); + setIndices(indices); + setTextureCoordinates(textureCoordinates); + } + } + + /** + * @author Pierre Lando + */ + private final class LessSimpleGeometry extends DefaultGeometry implements Geometry { + + private static final int SIZE = 65; + private static final double HALF_SIZE = SIZE / 2.; + + /** + * Default constructor. + * @param canvas the {@link org.scilab.forge.scirenderer.Canvas} where the drawn will be performed. + */ + public LessSimpleGeometry(Canvas canvas) { + int k; + ElementsBuffer vertices = canvas.getBuffersManager().createElementsBuffer(); + float[] verticesData = new float [SIZE * SIZE * 4]; + k = 0; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + double x = 2 * (i / (SIZE - 1f) - .5); + double y = 2 * (j / (SIZE - 1f) - .5); + double d = Math.max(Math.abs(x), Math.abs(y)); + Vector3d v = new Vector3d(x, y, 0).getNormalized().times(d); + verticesData[k++] = (float) v.getX(); + verticesData[k++] = (float) v.getY(); + verticesData[k++] = 0; + verticesData[k++] = 1; + } + } + + vertices.setData(verticesData, 4); + + ElementsBuffer textureCoordinates = canvas.getBuffersManager().createElementsBuffer(); + float[] textureCoordinatesData = new float[SIZE * SIZE]; + k = 0; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + double v = Math.sqrt(Math.pow(i - HALF_SIZE, 2) + Math.pow(j - HALF_SIZE, 2)); + v = (Math.sin(4 * Math.PI * v / SIZE) + 1) / 2; + textureCoordinatesData[k++] = (float) v; + } + } + textureCoordinates.setData(textureCoordinatesData, 1); + + IndicesBuffer indices = canvas.getBuffersManager().createIndicesBuffer(); + int[] indicesData = new int [6 * (SIZE - 1) * (SIZE - 1)]; + k = 0; + int u = 0; + for (int i = 0; i < SIZE - 1; i++) { + for (int j = 0; j < SIZE - 1; j++) { + indicesData[k++] = u; + indicesData[k++] = u + 1; + indicesData[k++] = u + SIZE + 1; + indicesData[k++] = u; + indicesData[k++] = u + SIZE + 1; + indicesData[k++] = u + SIZE; + u++; + } + u++; + } + indices.setData(indicesData); + + setFillDrawingMode(FillDrawingMode.TRIANGLES); + setVertices(vertices); + setIndices(indices); + setTextureCoordinates(textureCoordinates); + } + } + + /** + * @author Pierre Lando + */ + private final class Simple1DTextureDataProvider extends AbstractTextureDataProvider { + private final int size = 16; + private ByteBuffer buffer; + + /** + * Default constructor. + */ + public Simple1DTextureDataProvider() { + imageType = ImageType.RGBA_BYTE; + this.buffer = ByteBuffer.allocate(4 * size); // 4 for RGBA. + buffer.rewind(); + new SimpleThread().start(); + } + + /** + * Fill the data buffer. + */ + private void fillBuffer() { + double phi = Math.PI * 2 / 1024; + long time = new Date().getTime(); + long base = (int) ((time / 4) % 1024); + ByteBuffer tempBuffer = ByteBuffer.allocate(4 * size); + tempBuffer.rewind(); + for (int i = 0; i < size; i++) { + tempBuffer.put(toByte((Math.sin(base * phi) + 1) / 2)); + tempBuffer.put(toByte((Math.sin(2 * base * phi + i) + 1) / 2)); + tempBuffer.put(toByte((Math.sin(base * phi + i) + 1) / 2)); + tempBuffer.put(toByte(255)); + } + tempBuffer.rewind(); + this.buffer = tempBuffer; + } + + @Override + public Dimension getTextureSize() { + return new Dimension(size, 1); + } + + @Override + public ByteBuffer getData() { + buffer.rewind(); + return buffer; + } + + @Override + public ByteBuffer getSubData(int x, int y, final int width, final int height) { + ByteBuffer tempBuffer = ByteBuffer.allocate(4 * width * height); + this.buffer.position(x + y * size); + byte[] data = new byte[4]; + for (int i = x; i < x + width; i++) { + for (int j = y; j < y + height; j++) { + this.buffer.get(data); + tempBuffer.put(data); + } + } + tempBuffer.rewind(); + this.buffer.rewind(); + return tempBuffer; + } + + @Override + public boolean isValid() { + return true; + } + + /** + * This thread update the texture regularly. + */ + private class SimpleThread extends Thread { + + /** + * Default constructor. + */ + public SimpleThread() { + } + + @Override + public void run() { + for (;;) { + fillBuffer(); + fireUpdate(); + try { + synchronized (this) { + wait(32); + } + } catch (InterruptedException e) { + break; + } + } + } + } + } + + /** + * @author Pierre Lando + */ + private final class Simple2DTextureDataProvider extends AbstractTextureDataProvider { + private final int size = 16; + private ByteBuffer buffer; + + /** + * Default constructor. + */ + public Simple2DTextureDataProvider() { + imageType = ImageType.RGBA_BYTE; + this.buffer = ByteBuffer.allocate(4 * size * size); // 4 for RGBA. + buffer.rewind(); + new SimpleThread().start(); + } + + /** + * Fill the data buffer. + */ + private void fillBuffer() { + double phi = Math.PI * 2 / 1024; + long time = new Date().getTime(); + long base = (int) ((time / 4) % 1024); + ByteBuffer tempBuffer = ByteBuffer.allocate(4 * size * size); + tempBuffer.rewind(); + int k = 0; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + tempBuffer.put(toByte((Math.sin((5 * base + 16 * i) * phi) + 1) / 2)); + tempBuffer.put(toByte((Math.sin((2 * base + 8 * j) * phi) + 1) / 2)); + tempBuffer.put(toByte((Math.sin((7 * base + 4 * k) * phi) + 1) / 2)); + tempBuffer.put(toByte(255)); + k++; + } + } + tempBuffer.rewind(); + this.buffer = tempBuffer; + } + + @Override + public Dimension getTextureSize() { + return new Dimension(size, size); + } + + @Override + public ByteBuffer getData() { + buffer.rewind(); + return buffer; + } + + @Override + public ByteBuffer getSubData(int x, int y, final int width, final int height) { + ByteBuffer tempBuffer = ByteBuffer.allocate(4 * width * height); + this.buffer.position(x + y * size); + byte[] data = new byte[4]; + for (int i = x; i < x + width; i++) { + for (int j = y; j < y + height; j++) { + this.buffer.get(data); + tempBuffer.put(data); + } + } + tempBuffer.rewind(); + this.buffer.rewind(); + return tempBuffer; + } + + @Override + public boolean isValid() { + return true; + } + + /** + * This thread update the texture regularly. + */ + private class SimpleThread extends Thread { + + /** + * Default constructor. + */ + public SimpleThread() { + } + + @Override + public void run() { + for (;;) { + fillBuffer(); + fireUpdate(); + try { + synchronized (this) { + wait(32); + } + } catch (InterruptedException e) { + break; + } + } + } + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/ExampleFrame.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/ExampleFrame.java new file mode 100755 index 000000000..5bd6d008d --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/ExampleFrame.java @@ -0,0 +1,98 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples.utils; + +import com.jogamp.opengl.util.Animator; +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.implementation.jogl.JoGLCanvasFactory; + +import javax.media.opengl.awt.GLJPanel; +import javax.swing.JFrame; +import javax.swing.JPanel; +import java.awt.BorderLayout; +import java.awt.Dimension; + +/** + * @author Pierre Lando + */ +public abstract class ExampleFrame extends JFrame { + + /** + * The default frame size. + */ + private static final Dimension DEFAULT_SIZE = new Dimension(800, 800); + + /** + * The used animator. + */ + private Animator animator; + + /** + * The canvas. + */ + private final Canvas canvas; + + /** + * The gl panel. + */ + private final GLJPanel glPanel; + + /** + * Default constructor. + * Initialise the GUI. + */ + protected ExampleFrame() { + glPanel = new GLJPanel(); + canvas = JoGLCanvasFactory.createCanvas(glPanel); + + setSize(DEFAULT_SIZE); + setLayout(new BorderLayout()); + add(glPanel, BorderLayout.CENTER); + + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + /** + * Canvas getter. + * @return the {@link Canvas}. + */ + protected final Canvas getCanvas() { + return canvas; + } + + /** + * OpenGL panel getter. + * @return the OpenGL panel. + */ + protected final JPanel getPanel() { + return glPanel; + } + + /** + * Animated scene setter. + * @param animated true if the scene is auto-animated. + */ + protected final void animate(boolean animated) { + if (animated && (animator == null)) { + animator = new Animator(); + animator.add(glPanel); + animator.start(); + } + if (!animated && (animator != null)) { + animator.remove(glPanel); + animator.stop(); + animator = null; + } + } +} diff --git a/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/MouseRotationAdapter.java b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/MouseRotationAdapter.java new file mode 100755 index 000000000..7758093a9 --- /dev/null +++ b/modules/scirenderer/examples/org/scilab/forge/scirenderer/examples/utils/MouseRotationAdapter.java @@ -0,0 +1,110 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + */ + +package org.scilab.forge.scirenderer.examples.utils; + +import org.scilab.forge.scirenderer.Canvas; +import org.scilab.forge.scirenderer.tranformations.Rotation; +import org.scilab.forge.scirenderer.tranformations.Vector3d; + +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +/** + * This mouse adapter generate a {@link org.scilab.forge.scirenderer.tranformations.Rotation}, depending on mouse drags. + * + * @author Pierre Lando + */ +public class MouseRotationAdapter implements MouseListener, MouseMotionListener { + private static final float SPEED_RATIO = 128f; + + private Rotation currentRotation = new Rotation(Math.toRadians(0), new Vector3d(1, 0, 0)); + private Rotation startRotation; + private Point startPoint; + + /** + * The canvas will be notified for update. + */ + private Canvas canvas; + + /** + * Default constructor. + * @param startRotation the starting rotation. + */ + public MouseRotationAdapter(Rotation startRotation) { + currentRotation = startRotation; + this.canvas = null; + } + + /** + * A constructor. + * The given canvas will be notified for when the current rotation change. + * @param rotation the starting rotation. + * @param canvas the canvas to be notified on rotation changes. + */ + public MouseRotationAdapter(Rotation rotation, Canvas canvas) { + currentRotation = rotation; + this.canvas = canvas; + } + + @Override + public void mousePressed(MouseEvent e) { + startRotation = currentRotation; + startPoint = e.getPoint(); + e.getComponent().addMouseMotionListener(this); + } + + @Override + public void mouseReleased(MouseEvent e) { + e.getComponent().removeMouseMotionListener(this); + } + + @Override + public void mouseDragged(MouseEvent e) { + int deltaX = e.getX() - startPoint.x; + int deltaY = e.getY() - startPoint.y; + + Rotation delta = new Rotation(deltaY / SPEED_RATIO, new Vector3d(1, 0, 0)); + delta.multiply(new Rotation(deltaX / SPEED_RATIO, new Vector3d(0, 1, 0))); + + currentRotation = startRotation.times(delta); + + if (canvas != null) { + canvas.redraw(); + } + } + + /** + * Return the current rotation. + * @return the current rotation. + */ + public Rotation getRotation() { + return currentRotation; + } + + @Override + public void mouseClicked(MouseEvent e) { + } + + @Override + public void mouseEntered(MouseEvent e) { + } + + @Override + public void mouseExited(MouseEvent e) { + } + + @Override + public void mouseMoved(MouseEvent e) { + } +} |