import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.glu.GLU; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.newt.swt.NewtCanvasSWT; import com.jogamp.opengl.util.Animator; public class NewtTest { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); final NewtCanvasSWT canvas1 = init(shell, false); final NewtCanvasSWT canvas2 = init(shell, true); Button button1 = new Button(shell, SWT.PUSH); button1.setText("Set size to 0,0"); button1.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { canvas1.setSize(0, 0); canvas2.setSize(0, 0); } }); Button button2 = new Button(shell, SWT.PUSH); button2.setText("Set size to 50,50"); button2.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { canvas1.setSize(50, 50); canvas2.setSize(50, 50); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } public static NewtCanvasSWT init(Composite parent, final boolean applyFix) { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); final GLWindow glWindow = GLWindow.create(caps); NewtCanvasSWT canvas = new NewtCanvasSWT(parent, SWT.NONE, glWindow) { @Override public void setSize(int width, int height) { //do not allow a size of 0,0, because NEWT window becomes invisible (https://jogamp.org/bugzilla/show_bug.cgi?id=822) if (applyFix) { super.setSize(Math.max(1, width), Math.max(1, height)); } else { super.setSize(width, height); } } }; glWindow.addGLEventListener(new MyGlEventListener()); Animator animator = new Animator(glWindow); animator.setUpdateFPSFrames(1, null); animator.start(); return canvas; } /** * GLEventListener which draws a spinning torus. From * https://github.com/sgothel * /jogl-demos/blob/master/src/demos/swt/Snippet209.java. */ private static class MyGlEventListener implements GLEventListener { private int rot = 0; @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.setSwapInterval(1); gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); gl.glClearDepth(1.0); gl.glLineWidth(2); gl.glEnable(GL2.GL_DEPTH_TEST); } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { float fAspect = (float) width / (float) height; GL2 gl = drawable.getGL().getGL2(); gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); GLU glu = new GLU(); glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } @Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glClearColor(.3f, .5f, .8f, 1.0f); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -10.0f); gl.glRotatef(0.15f * rot, 2.0f * rot, 10.0f * rot, 1.0f); gl.glRotatef(0.3f * rot, 3.0f * rot, 1.0f * rot, 1.0f); rot++; gl.glPolygonMode(GL2.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 * rot))), 15, 15); } @Override public void dispose(GLAutoDrawable drawable) { } protected 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; for (int i = rings - 1; i >= 0; i--) { float theta1 = theta + ringDelta; float cosTheta1 = (float) Math.cos(theta1); float sinTheta1 = (float) Math.sin(theta1); gl.glBegin(GL2.GL_QUAD_STRIP); float phi = 0.0f; for (int j = nsides; j >= 0; j--) { phi += sideDelta; float cosPhi = (float) Math.cos(phi); float sinPhi = (float) Math.sin(phi); float dist = R + r * cosPhi; gl.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); gl.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); gl.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); gl.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi); } gl.glEnd(); theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } } } }