Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Pazderski2019-06-03 21:23:08 +0000
committerEric Williams2019-06-10 15:36:37 +0000
commit55635f432dd8824cb107c035a00fa4435bbf93df (patch)
treeed87840d2762d53923d0e5dec4a3d2e52369d8fa /examples
parent0b22592e3f6c547b0bec5a5b491bd41a583e8e60 (diff)
downloadeclipse.platform.swt-55635f432dd8824cb107c035a00fa4435bbf93df.tar.gz
eclipse.platform.swt-55635f432dd8824cb107c035a00fa4435bbf93df.tar.xz
eclipse.platform.swt-55635f432dd8824cb107c035a00fa4435bbf93df.zip
Bug 547937 - [Snippets] Update OpenGL Snippets for current library
versions The OpenGL Snippets are not usable with latest versions of lwjgl or jogamp due to changes or feature removal in those libraries. Change-Id: Ifec7e059a1558b669866116eee37973fabd290de Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java24
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java35
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet341.java50
3 files changed, 52 insertions, 57 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java
index b87ed40334..3897e1c200 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -18,7 +18,7 @@ package org.eclipse.swt.snippets;
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
- *
+ *
* @since 3.2
*/
import org.eclipse.swt.*;
@@ -26,10 +26,7 @@ import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.opengl.*;
import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.widgets.Display;
-import org.lwjgl.*;
import org.lwjgl.opengl.*;
-import org.lwjgl.util.glu.*;
public class Snippet195 {
static void drawTorus(float r, float R, int nsides, int rings) {
@@ -70,21 +67,20 @@ public class Snippet195 {
final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
canvas.addListener(SWT.Resize, event -> {
Rectangle bounds = canvas.getBounds();
float fAspect = (float) bounds.width / (float) bounds.height;
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
GL11.glViewport(0, 0, bounds.width, bounds.height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
- GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
+ float near = 0.5f;
+ float bottom = -near * (float) Math.tan(45.f / 2);
+ float left = fAspect * bottom;
+ GL11.glFrustum(left, -left, bottom, -bottom, near, 400.f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
});
@@ -106,9 +102,7 @@ public class Snippet195 {
public void run() {
if (!canvas.isDisposed()) {
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(.3f, .5f, .8f, 1.0f);
GL11.glLoadIdentity();
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java
index 6c8df0b87f..837e623c29 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -18,23 +18,20 @@ package org.eclipse.swt.snippets;
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
- *
+ *
* @since 3.2
*/
import org.eclipse.swt.*;
+import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
+import org.eclipse.swt.opengl.*;
import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.graphics.*;
-import org.eclipse.swt.opengl.GLCanvas;
-import org.eclipse.swt.opengl.GLData;
-import javax.media.opengl.GL;
-import javax.media.opengl.GLContext;
-import javax.media.opengl.GLDrawableFactory;
-import javax.media.opengl.glu.GLU;
+import com.jogamp.opengl.*;
+import com.jogamp.opengl.glu.*;
public class Snippet209 {
- static void drawTorus(GL gl, float r, float R, int nsides, int rings) {
+ static void drawTorus(GL2 gl, float r, float R, int nsides, int rings) {
float ringDelta = 2.0f * (float) Math.PI / rings;
float sideDelta = 2.0f * (float) Math.PI / nsides;
float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
@@ -42,7 +39,7 @@ public class Snippet209 {
float theta1 = theta + ringDelta;
float cosTheta1 = (float) Math.cos(theta1);
float sinTheta1 = (float) Math.sin(theta1);
- gl.glBegin(GL.GL_QUAD_STRIP);
+ gl.glBegin(GL2.GL_QUAD_STRIP);
float phi = 0.0f;
for (int j = nsides; j >= 0; j--) {
phi += sideDelta;
@@ -72,7 +69,7 @@ public class Snippet209 {
final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);
canvas.setCurrent();
- final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();
+ final GLContext context = GLDrawableFactory.getFactory(GLProfile.getGL2GL3()).createExternalGLContext();;
canvas.addListener(SWT.Resize, new Listener() {
@Override
@@ -81,23 +78,23 @@ public class Snippet209 {
float fAspect = (float) bounds.width / (float) bounds.height;
canvas.setCurrent();
context.makeCurrent();
- GL gl = context.getGL ();
+ GL2 gl = (GL2) context.getGL ();
gl.glViewport(0, 0, bounds.width, bounds.height);
- gl.glMatrixMode(GL.GL_PROJECTION);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
GLU glu = new GLU();
glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
- gl.glMatrixMode(GL.GL_MODELVIEW);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
context.release();
}
});
context.makeCurrent();
- GL gl = context.getGL ();
+ GL2 gl = (GL2) context.getGL ();
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glColor3f(1.0f, 0.0f, 0.0f);
- gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
+ gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glClearDepth(1.0);
gl.glLineWidth(2);
gl.glEnable(GL.GL_DEPTH_TEST);
@@ -114,7 +111,7 @@ public class Snippet209 {
if (!canvas.isDisposed()) {
canvas.setCurrent();
context.makeCurrent();
- GL gl = context.getGL ();
+ GL2 gl = (GL2) context.getGL ();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(.3f, .5f, .8f, 1.0f);
gl.glLoadIdentity();
@@ -123,7 +120,7 @@ public class Snippet209 {
gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
rot++;
- gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
+ gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
gl.glColor3f(0.9f, 0.9f, 0.9f);
drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
canvas.swapBuffers();
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet341.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet341.java
index 83754a3965..990c8ccb6c 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet341.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet341.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,13 +15,13 @@ package org.eclipse.swt.snippets;
/*
* SWT OpenGL snippet: capture a LWJGL drawing to an SWT Image
- *
+ *
* This example is essentially Snippet195 with the capture() method
* added. It should be easily adaptable to other OpenGL packages.
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
- *
+ *
* @since 3.2
*/
import java.nio.*;
@@ -31,10 +31,7 @@ import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.opengl.*;
import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.widgets.Display;
-import org.lwjgl.*;
import org.lwjgl.opengl.*;
-import org.lwjgl.util.glu.*;
public class Snippet341 {
static void drawTorus(float r, float R, int nsides, int rings) {
@@ -67,29 +64,26 @@ public class Snippet341 {
public static void main(String [] args) {
final Display display = new Display();
Shell shell = new Shell(display);
- shell.setText("Snippet 341");
- shell.setLayout(new GridLayout());
+ shell.setLayout(new FormLayout());
GLData data = new GLData ();
data.doubleBuffer = true;
final GLCanvas canvas = new GLCanvas(shell, SWT.NONE, data);
- canvas.setLayoutData(new GridData(640,480));
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
canvas.addListener(SWT.Resize, event -> {
Rectangle bounds = canvas.getBounds();
float fAspect = (float) bounds.width / (float) bounds.height;
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
GL11.glViewport(0, 0, bounds.width, bounds.height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
- GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
+ float near = 0.5f;
+ float bottom = -near * (float) Math.tan(45.f / 2);
+ float left = fAspect * bottom;
+ GL11.glFrustum(left, -left, bottom, -bottom, near, 400.f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
});
@@ -104,6 +98,19 @@ public class Snippet341 {
Button button = new Button(shell, SWT.PUSH);
button.setText("Capture");
button.addListener(SWT.Selection, event -> capture(canvas));
+
+ FormData formData = new FormData(640, 480);
+ formData.top = new FormAttachment(0, 0);
+ formData.left = new FormAttachment(0, 0);
+ formData.bottom = new FormAttachment(button, 0);
+ formData.right = new FormAttachment(100, 0);
+ canvas.setLayoutData(formData);
+ formData = new FormData();
+ formData.left = new FormAttachment(0, 0);
+ formData.bottom = new FormAttachment(100, 0);
+ formData.right = new FormAttachment(100, 0);
+ button.setLayoutData(formData);
+
shell.pack();
shell.open();
@@ -113,9 +120,7 @@ public class Snippet341 {
public void run() {
if (!canvas.isDisposed()) {
canvas.setCurrent();
- try {
- GLContext.useContext(canvas);
- } catch(LWJGLException e) { e.printStackTrace(); }
+ GL.createCapabilities();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(.3f, .5f, .8f, 1.0f);
GL11.glLoadIdentity();
@@ -139,7 +144,7 @@ public class Snippet341 {
}
display.dispose();
}
-
+
static void capture(GLCanvas glCanvas) {
final int PAD = 4;
Display display = glCanvas.getDisplay();
@@ -150,11 +155,11 @@ public class Snippet341 {
GL11.glReadPixels(0, 0, bounds.width, bounds.height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
byte[] bytes = new byte[size];
buffer.get(bytes);
- /*
+ /*
* In OpenGL (0,0) is at the bottom-left corner, and y values ascend in the upward
* direction. This is opposite to SWT which defines (0,0) to be at the top-left
* corner with y values ascendingin the downwards direction. Re-order the OpenGL-
- * provided bytes to SWT's expected order so that the Image will not appear inverted.
+ * provided bytes to SWT's expected order so that the Image will not appear inverted.
*/
byte[] temp = new byte[bytes.length];
for (int i = 0; i < bytes.length; i += bounds.width * PAD) {
@@ -165,7 +170,6 @@ public class Snippet341 {
final Image image = new Image(display, data);
Shell shell = new Shell(display);
- shell.setText("Capture");
shell.setLayout(new GridLayout());
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.setLayoutData(new GridData(bounds.width, bounds.height));

Back to the top