diff --git a/jzy3d-emul-gl-swt/pom.xml b/jzy3d-emul-gl-swt/pom.xml new file mode 100644 index 000000000..41b5b1c9c --- /dev/null +++ b/jzy3d-emul-gl-swt/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + org.jzy3d + jzy3d-all + 2.0.1-SNAPSHOT + + jzy3d-emul-gl-swt + Jzy3d EmulGL Implementation for SWT with jGL + + 3.116.100 + + + + org.eclipse.platform + org.eclipse.swt.gtk.linux.x86_64 + ${swt-version} + + + org.eclipse.platform + org.eclipse.swt + + + + + org.jzy3d + jGL + ${project.version} + + + + diff --git a/jzy3d-emul-gl-swt/src/main/java/org/jzy3d/swt/JGLCanvas.java b/jzy3d-emul-gl-swt/src/main/java/org/jzy3d/swt/JGLCanvas.java new file mode 100644 index 000000000..00ed5d9bb --- /dev/null +++ b/jzy3d-emul-gl-swt/src/main/java/org/jzy3d/swt/JGLCanvas.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * (c) 2021 Läubisoft GmbH, Christoph Läubrich + * + * This library is free software; you can redistribute it and/or modify it under the terms of the + * GNU Lesser General Public License as published by the Free Software Foundation; either version + * 2.1 of the License, or any later version. the GNU Lesser General Public License should be + * included with this distribution in the file LICENSE. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package org.jzy3d.swt; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.ControlListener; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.PaletteData; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.opengl.GLCanvas; +import org.eclipse.swt.opengl.GLData; +import org.eclipse.swt.widgets.Composite; + +import jgl.GL; + +public class JGLCanvas extends GLCanvas { + + protected GL gl = new SWTGL(); + + public JGLCanvas(Composite parent, int style, GLData data) { + + super(parent, style, data); + gl.setAutoAdaptToHiDPI(false); + doResize(0, 0); + addControlListener(new ControlListener() { + + @Override + public void controlResized(ControlEvent e) { + + Point size = getSize(); + doResize(size.x, size.y); + } + + + @Override + public void controlMoved(ControlEvent e) { + + } + }); + addPaintListener(e -> { + doPaint(e.gc); + }); + } + + protected void doResize(int w, int h) { + + gl.glViewport(0, 0, w, h); + } + + protected void doPaint(GC gc) { + + gc.setBackground(getBackground()); + Point size = getSize(); + gc.fillRectangle(0, 0, size.x, size.y); + gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_RED)); + gc.drawLine(0, 0, size.x, size.y); + SWTGL swtgl = (SWTGL)gl; + if(swtgl.imageData != null) { + Image image = new Image(gc.getDevice(), swtgl.imageData); + gc.drawImage(image, 0, 0); + image.dispose(); + } + System.out.println("paint done"); + } + + public GL getGL() { + + return gl; + } + + @Override + public void setCurrent() { + + super.setCurrent(); + } + + private static final class SWTGL extends GL { + + private ImageData imageData; + + @Override + public void glFlush() { + + int[] buffer = Context.ColorBuffer.Buffer; + int width = Context.Viewport.Width; + int height = Context.Viewport.Height; + PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); + imageData = new ImageData(width, height, 24, palette); + imageData.setPixels(0, 0, buffer.length, buffer, 0); + byte[] alphaBytes = new byte[buffer.length]; + for(int i = 0; i < alphaBytes.length; i++) { + alphaBytes[i] = (byte)((buffer[i] >> 24) & 0xFF); + } + imageData.setAlphas(0, 0, alphaBytes.length, alphaBytes, 0); + } + } +} diff --git a/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/SWTTest.java b/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/SWTTest.java new file mode 100644 index 000000000..2f0850cf3 --- /dev/null +++ b/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/SWTTest.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2021 Läubisoft GmbH and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.jzy3d.swt; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.opengl.GLData; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +public class SWTTest { + + public static void main(String[] args) { + + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("jGL Canvas Test"); + shell.setLayout(new FillLayout()); + shell.open(); + GLData data = new GLData(); + data.doubleBuffer = true; + JGLCanvas canvas = new bezcurve(shell, SWT.NONE, data); + canvas.setCurrent(); + while(!shell.isDisposed()) { + if(!display.readAndDispatch()) { + display.sleep(); + } + } + display.dispose(); + } +} diff --git a/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/bezcurve.java b/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/bezcurve.java new file mode 100644 index 000000000..334614952 --- /dev/null +++ b/jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/bezcurve.java @@ -0,0 +1,67 @@ +package org.jzy3d.swt; +/* + * bezcurve.java This program uses evaluators to draw a Bezier curve. + */ + +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.opengl.GLData; +import org.eclipse.swt.widgets.Composite; + +import jgl.GL; + +public class bezcurve extends JGLCanvas { + + public bezcurve(Composite parent, int style, GLData data) { + + super(parent, style, data); + myinit(); + } + + private static final float ctrlpoints[][] = {{-4.0f, -4.0f, 0.0f}, {-2.0f, 4.0f, 0.0f}, {2.0f, -4.0f, 0.0f}, {4.0f, 4.0f, 0.0f}}; + + private void myinit() { + + gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + gl.glShadeModel(GL.GL_FLAT); + gl.glMap1f(GL.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, 4, ctrlpoints); + gl.glEnable(GL.GL_MAP1_VERTEX_3); + } + + @Override + protected void doPaint(GC gc) { + + int i; + gl.glClear(GL.GL_COLOR_BUFFER_BIT); + gl.glColor3f(1.0f, 1.0f, 1.0f); + gl.glBegin(GL.GL_LINE_STRIP); + for(i = 0; i <= 30; i++) + gl.glEvalCoord1f(i / 30.0f); + gl.glEnd(); + /* The following code displays the control points as dots. */ + gl.glPointSize(5.0f); + gl.glColor3f(1.0f, 1.0f, 0.0f); + gl.glBegin(GL.GL_POINTS); + for(i = 0; i < 4; i++) + gl.glVertex3fv(ctrlpoints[i]); + gl.glEnd(); + gl.glFlush(); + super.doPaint(gc); + } + + @Override + protected void doResize(int w, int h) { + + gl.glViewport(0, 0, w, h); + gl.glMatrixMode(GL.GL_PROJECTION); + gl.glLoadIdentity(); + if(w <= h) { + gl.glOrtho(-5.0f, 5.0f, -5.0f * h / w, 5.0f * h / w, -5.0f, 5.0f); + } else { + gl.glOrtho(-5.0f * w / h, 5.0f * w / h, -5.0f, 5.0f, -5.0f, 5.0f); + } + gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glLoadIdentity(); + } + + +}