protected void runTestAGL(GLProfile glprofile) throws InterruptedException { GLData gldata = new GLData(); gldata.doubleBuffer = true; // need SWT.NO_BACKGROUND to prevent SWT from clearing the window // at the wrong times (we use glClear for this instead) final GLCanvas glcanvas = new GLCanvas(composite, SWT.NO_BACKGROUND, gldata); Assert.assertNotNull(glcanvas); glcanvas.setCurrent(); final GLContext glcontext = GLDrawableFactory.getFactory(glprofile).createExternalGLContext(); Assert.assertNotNull(glcontext); // fix the viewport when the user resizes the window glcanvas.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle rectangle = glcanvas.getClientArea(); glcanvas.setCurrent(); glcontext.makeCurrent(); GL2ES1 gl = glcontext.getGL().getGL2ES1(); OneTriangle.setup(gl, rectangle.width, rectangle.height); glcontext.release(); System.err.println("resize"); } }); // draw the triangle when the OS tells us that any part of the window needs drawing glcanvas.addPaintListener( new PaintListener() { public void paintControl(PaintEvent paintevent) { Rectangle rectangle = glcanvas.getClientArea(); glcanvas.setCurrent(); glcontext.makeCurrent(); GL2ES1 gl = glcontext.getGL().getGL2ES1(); OneTriangle.render(gl, rectangle.width, rectangle.height); glcanvas.swapBuffers(); glcontext.release(); System.err.println("paint"); } }); shell.setText(getClass().getName()); shell.setSize(640, 480); shell.open(); long lStartTime = System.currentTimeMillis(); long lEndTime = lStartTime + duration; try { while ((System.currentTimeMillis() < lEndTime) && !glcanvas.isDisposed()) { if (!display.readAndDispatch()) { // blocks on linux .. display.sleep(); Thread.sleep(10); } } } catch (Throwable throwable) { throwable.printStackTrace(); Assume.assumeNoException(throwable); } glcanvas.dispose(); }
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(); try { GLContext.useContext(canvas); } catch (LWJGLException e) { e.printStackTrace(); } 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(); try { GLContext.useContext(canvas); } catch (LWJGLException e) { e.printStackTrace(); } GL11.glViewport(0, 0, bounds.width, bounds.height); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); 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.setText("SWT/LWJGL Example"); shell.setSize(640, 480); shell.open(); display.asyncExec( new Runnable() { int rot = 0; public void run() { if (!canvas.isDisposed()) { canvas.setCurrent(); try { GLContext.useContext(canvas); } catch (LWJGLException 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); float frot = rot; GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); rot++; 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 * frot))), 15, 15); canvas.swapBuffers(); display.asyncExec(this); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public void createPartControl(final Composite parent) { Composite top = new Composite(parent, SWT.NONE); top.setLayout(new FillLayout()); GLData data = new GLData(); data.doubleBuffer = true; canvas = new GLCanvas(top, SWT.NONE, data); canvas.setCurrent(); final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext(); context.makeCurrent(); GL gl = context.getGL(); geocontext.initialize(context); gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); context.release(); canvas.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle bounds = canvas.getBounds(); canvas.setCurrent(); context.makeCurrent(); GL gl = context.getGL(); geocontext.setHeight(bounds.height); geocontext.setWidth(bounds.width); gl.glViewport(0, 0, bounds.width, bounds.height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); new GLU().gluOrtho2D(0, 1, 0, 1); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); context.release(); } }); canvas.addMouseMoveListener( new MouseMoveListener() { @Override public void mouseMove(MouseEvent e) { if (e.stateMask == SWT.SHIFT) { dxx = (int) (geocontext.getZoom() * (e.x - canvas.getBounds().width / 2) / 10); dyy = (int) (geocontext.getZoom() * (e.y - canvas.getBounds().height / 2) / 10); geocontext.setDirty(true); } else { Point p = new Point(); try { p.x = (int) (geocontext.getX() + e.x * geocontext.getWidth() / canvas.getBounds().width * geocontext.getZoom()); p.y = (int) (geocontext.getY() + e.y * geocontext.getHeight() / canvas.getBounds().height * geocontext.getZoom()); root.mouseMoved(p, geocontext); } catch (Exception ex) { } } } }); canvas.addMouseWheelListener( new MouseWheelListener() { @Override public void mouseScrolled(MouseEvent e) { float zoom = (float) (geocontext.getZoom() * Math.pow(2, -e.count / 20.0)); // check mouse position int posX = e.x; int posY = e.y; // calculate the image position of the current position of the mouse int x = (int) (geocontext.getX() + posX * geocontext.getWidth() / canvas.getBounds().width * geocontext.getZoom()); int y = (int) (geocontext.getY() + posY * geocontext.getHeight() / canvas.getBounds().height * geocontext.getZoom()); geocontext.setZoom(zoom); // translate the image origin to have the same mouse position in the geocontext geocontext.setX( (int) (x - posX * geocontext.getWidth() / canvas.getBounds().width * geocontext.getZoom())); geocontext.setY( (int) (y - posY * geocontext.getHeight() / canvas.getBounds().height * geocontext.getZoom())); geocontext.setDirty(true); } }); final Runnable core = new Runnable() { int count = 0; long[] times = new long[500]; public void run() { if (!canvas.isDisposed()) { if (count == times.length) { count = 0; double fps = 0; for (int i = 0; i < times.length - 1; i++) { fps += (times[i + 1] - times[i]) / 1000.; } fps /= times.length - 1; System.out.println(1 / fps); } else { times[count++] = System.currentTimeMillis(); } canvas.setCurrent(); context.makeCurrent(); GL gl = context.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); if (dxx != 0 || dyy != 0) { dxx /= 1.2; dyy /= 1.2; geocontext.setX(geocontext.getX() + dxx); geocontext.setY(geocontext.getY() + dyy); } geocontext.initialize(context); geocontext.setDirty(true); root.render(geocontext); canvas.swapBuffers(); context.release(); try { Thread.sleep(15); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } canvas.getDisplay().asyncExec(this); } } }; canvas.getDisplay().asyncExec(core); registerView(); renderLayers(); }
private static void addNewCanvas( final TabFolder tabFolder, final ExampleScene scene, final FrameHandler frameWork, final LogicalLayer logicalLayer) { i++; logger.info("Adding canvas"); // Add a new tab to hold our canvas final TabItem item = new TabItem(tabFolder, SWT.NONE); item.setText("Canvas #" + i); tabFolder.setSelection(item); final Composite canvasParent = new Composite(tabFolder, SWT.NONE); canvasParent.setLayout(new FillLayout()); item.setControl(canvasParent); final GLData data = new GLData(); data.depthSize = 8; data.doubleBuffer = true; final SashForm splitter = new SashForm(canvasParent, SWT.HORIZONTAL); final SashForm splitterLeft = new SashForm(splitter, SWT.VERTICAL); final Composite topLeft = new Composite(splitterLeft, SWT.NONE); topLeft.setLayout(new FillLayout()); final Composite bottomLeft = new Composite(splitterLeft, SWT.NONE); bottomLeft.setLayout(new FillLayout()); final SashForm splitterRight = new SashForm(splitter, SWT.VERTICAL); final Composite topRight = new Composite(splitterRight, SWT.NONE); topRight.setLayout(new FillLayout()); final Composite bottomRight = new Composite(splitterRight, SWT.NONE); bottomRight.setLayout(new FillLayout()); canvasParent.layout(); final SwtCanvas canvas1 = new SwtCanvas(topLeft, SWT.NONE, data); final LwjglCanvasRenderer lwjglCanvasRenderer1 = new LwjglCanvasRenderer(scene); addCallback(canvas1, lwjglCanvasRenderer1); canvas1.setCanvasRenderer(lwjglCanvasRenderer1); frameWork.addCanvas(canvas1); canvas1.addControlListener(newResizeHandler(canvas1, lwjglCanvasRenderer1)); canvas1.setFocus(); final SwtCanvas canvas2 = new SwtCanvas(bottomLeft, SWT.NONE, data); final LwjglCanvasRenderer lwjglCanvasRenderer2 = new LwjglCanvasRenderer(scene); addCallback(canvas2, lwjglCanvasRenderer2); canvas2.setCanvasRenderer(lwjglCanvasRenderer2); frameWork.addCanvas(canvas2); canvas2.addControlListener(newResizeHandler(canvas2, lwjglCanvasRenderer2)); final SwtCanvas canvas3 = new SwtCanvas(topRight, SWT.NONE, data); final LwjglCanvasRenderer lwjglCanvasRenderer3 = new LwjglCanvasRenderer(scene); addCallback(canvas3, lwjglCanvasRenderer3); canvas3.setCanvasRenderer(lwjglCanvasRenderer3); frameWork.addCanvas(canvas3); canvas3.addControlListener(newResizeHandler(canvas3, lwjglCanvasRenderer3)); final SwtCanvas canvas4 = new SwtCanvas(bottomRight, SWT.NONE, data); final LwjglCanvasRenderer lwjglCanvasRenderer4 = new LwjglCanvasRenderer(scene); addCallback(canvas4, lwjglCanvasRenderer4); canvas4.setCanvasRenderer(lwjglCanvasRenderer4); frameWork.addCanvas(canvas4); canvas4.addControlListener(newResizeHandler(canvas4, lwjglCanvasRenderer4)); final SwtKeyboardWrapper keyboardWrapper = new SwtKeyboardWrapper(canvas1); final SwtMouseWrapper mouseWrapper = new SwtMouseWrapper(canvas1); final SwtFocusWrapper focusWrapper = new SwtFocusWrapper(canvas1); final SwtMouseManager mouseManager = new SwtMouseManager(canvas1); final ControllerWrapper controllerWrapper = new DummyControllerWrapper(); final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper); logicalLayer.registerInput(canvas1, pl); logicalLayer.registerTrigger( new InputTrigger( new KeyPressedCondition(Key.H), new TriggerAction() { public void perform( final Canvas source, final TwoInputStates inputStates, final double tpf) { if (source != canvas1) { return; } if (_showCursor1.get(canvas1)) { mouseManager.setCursor(_cursor1); } else { mouseManager.setCursor(_cursor2); } _showCursor1.put(canvas1, !_showCursor1.get(canvas1)); } })); logicalLayer.registerTrigger( new InputTrigger( new KeyPressedCondition(Key.J), new TriggerAction() { public void perform( final Canvas source, final TwoInputStates inputStates, final double tpf) { if (source != canvas1) { return; } mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT); } })); logicalLayer.registerTrigger( new InputTrigger( new KeyPressedCondition(Key.G), new TriggerAction() { public void perform( final Canvas source, final TwoInputStates inputStates, final double tpf) { if (source != canvas1) { return; } mouseManager.setGrabbed( mouseManager.getGrabbed() == GrabbedState.NOT_GRABBED ? GrabbedState.GRABBED : GrabbedState.NOT_GRABBED); } })); final AWTImageLoader awtImageLoader = new AWTImageLoader(); try { _cursor1 = createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/wait_cursor.png"); _cursor2 = createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/movedata.gif"); } catch (final IOException ioe) { ioe.printStackTrace(); } _showCursor1.put(canvas1, true); }