import java.util.HashSet; import java.util.Set; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.opengl.GL; import org.eclipse.swt.opengl.GLCanvas; import org.eclipse.swt.opengl.GLFormatData; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GLContext; import org.lwjgl.opengl.glu.GLU; public class GLTest { 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; float cosTheta = 1.0f; float 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) { Display display = new Display(); Shell shell = new Shell(display); FillLayout layout = new FillLayout(); layout.marginWidth = layout.marginHeight = 20; shell.setLayout(layout); GLFormatData data = new GLFormatData (); GLCanvas canvas = new GLCanvas(shell, SWT.NONE, data); GLCanvas canvas2 = new GLCanvas(shell, SWT.NONE, data); int xrot = 0, yrot = 0; int width = 640, height = 480; float fAspect = (float) width / (float) height; canvas.setCurrent(); try { org.lwjgl.opengl.GLContext.useContext(canvas); } catch(Exception e) { e.printStackTrace(); } GL11.glViewport(0, 0, width, height); GL11.glMatrixMode(GL11.GL_PROJECTION); // select the projection matrix GL11.glLoadIdentity(); // reset the projection matrix GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // select the modelview matrix GL11.glLoadIdentity(); GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); GL11.glClearDepth(1.0); GL11.glLineWidth(2); GL11.glEnable(GL11.GL_DEPTH_TEST); canvas2.setCurrent(); try { org.lwjgl.opengl.GLContext.useContext(canvas2); } catch(Exception e) { e.printStackTrace(); } GL11.glViewport(0, 0, width, height); GL11.glMatrixMode(GL11.GL_PROJECTION); // select the projection matrix GL11.glLoadIdentity(); // reset the projection matrix GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // select the modelview matrix GL11.glLoadIdentity(); GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); GL11.glClearDepth(1.0); GL11.glLineWidth(2); GL11.glEnable(GL11.GL_DEPTH_TEST); shell.setSize(640, 480); shell.open(); while(!shell.isDisposed()) { width = 40; height = 40; int depth = 40; canvas.setCurrent(); try { org.lwjgl.opengl.GLContext.useContext(canvas); } catch(Exception e) { e.printStackTrace(); } GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClearColor(.3f, .5f, .8f, 1.0f); GL11.glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -10.0f); GL11.glRotatef(0.15f * xrot, 2.0f * ((float) xrot), 10.0f * ((float) yrot), 1.0f); GL11.glRotatef(0.3f * yrot, 3.0f * ((float) xrot), 1.0f * ((float) yrot), 1.0f); xrot++; yrot++; GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); GL11.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(1, 1.9f + ((float) Math.sin((0.004f * xrot))), 15, 15); canvas.swapBuffers(); canvas2.setCurrent(); try { org.lwjgl.opengl.GLContext.useContext(canvas2); } catch(Exception e) { e.printStackTrace(); } GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClearColor(.8f, .5f, .3f, 1.0f); GL11.glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -10.0f); GL11.glRotatef(0.15f * xrot, 2.0f * ((float) xrot), 10.0f * ((float) yrot), 1.0f); GL11.glRotatef(0.3f * yrot, 3.0f * ((float) xrot), 1.0f * ((float) yrot), 1.0f); xrot++; yrot++; GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); GL11.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(1, 1.9f + ((float) Math.sin((0.004f * xrot))), 15, 15); canvas2.swapBuffers(); while (display.readAndDispatch()); // display.sleep(); } display.dispose(); } }