package org.eclipse.swt.snippets; import org.eclipse.opengl.GL; import org.eclipse.opengl.GLU; import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.opengl.GLCanvas; import org.eclipse.swt.opengl.GLData; public class EXP_SWT { static long startTime = System.currentTimeMillis() + 5000; static long fps = 0; static void drawTorus(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(GL.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; } } public static void main(String [] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new FillLayout()); GLData data = new GLData (); data.doubleBuffer = true; final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); canvas.setCurrent(); canvas.addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle bounds = canvas.getBounds(); float fAspect = (float) bounds.width / (float) bounds.height; canvas.setCurrent(); GL.glViewport(0, 0, bounds.width, bounds.height); GL.glMatrixMode(GL.GL_PROJECTION); GL.glLoadIdentity(); GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); GL.glMatrixMode(GL.GL_MODELVIEW); GL.glLoadIdentity(); } }); 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.glClearDepth(1.0); GL.glLineWidth(2); GL.glEnable(GL.GL_DEPTH_TEST); shell.setText("SWT/JOGL Example"); shell.setSize(640, 480); shell.open(); display.asyncExec(new Runnable() { int rot = 0; public void run() { if (!canvas.isDisposed()) { canvas.setCurrent(); GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); GL.glClearColor(.3f, .5f, .8f, 1.0f); GL.glLoadIdentity(); GL.glTranslatef(0.0f, 0.0f, -10.0f); float frot = rot; 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.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15); canvas.swapBuffers(); if (startTime > System.currentTimeMillis()) { fps++; } else { long timeUsed = 5000 + (startTime - System .currentTimeMillis()); startTime = System.currentTimeMillis() + 5000; System.out.println(fps + " frames in " + (float) (timeUsed / 1000f) + " seconds = " + (fps / (timeUsed / 1000f))); fps = 0; } display.asyncExec(this); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }