Skip to content

Commit

Permalink
connect jGL with SWT GLCanvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Läubrich committed Jun 22, 2021
1 parent dc897c3 commit 9cc9b2d
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
34 changes: 34 additions & 0 deletions jzy3d-emul-gl-swt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jzy3d</groupId>
<artifactId>jzy3d-all</artifactId>
<version>2.0.1-SNAPSHOT</version>
</parent>
<artifactId>jzy3d-emul-gl-swt</artifactId>
<name>Jzy3d EmulGL Implementation for SWT with jGL</name>
<properties>
<swt-version>3.116.100</swt-version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
<version>${swt-version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.swt</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jzy3d</groupId>
<artifactId>jGL</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

110 changes: 110 additions & 0 deletions jzy3d-emul-gl-swt/src/main/java/org/jzy3d/swt/JGLCanvas.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
42 changes: 42 additions & 0 deletions jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/SWTTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
67 changes: 67 additions & 0 deletions jzy3d-emul-gl-swt/src/test/java/org/jzy3d/swt/bezcurve.java
Original file line number Diff line number Diff line change
@@ -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();
}


}

0 comments on commit 9cc9b2d

Please sign in to comment.