Ejemplo n.º 1
0
  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();
  }
Ejemplo n.º 2
0
 @Override
 public void refresh() {
   geocontext.setDirty(true);
   root.render(geocontext);
 }