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,
        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();

    final Runnable run =
        new Runnable() {
          int rot = 0;

          @Override
          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);
            }
          }
        };
    canvas.addListener(SWT.Paint, event -> run.run());
    display.asyncExec(run);

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
Esempio n. 2
0
 /**
  * Returns a GLData object describing the created context.
  *
  * @return GLData description of the OpenGL context attributes
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public GLData getGLData() {
   checkWidget();
   int /*long*/ window = OS.GTK_WIDGET_WINDOW(handle);
   int /*long*/ xDisplay = OS.gdk_x11_drawable_get_xdisplay(window);
   GLData data = new GLData();
   int[] value = new int[1];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_DOUBLEBUFFER, value);
   data.doubleBuffer = value[0] != 0;
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_STEREO, value);
   data.stereo = value[0] != 0;
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_RED_SIZE, value);
   data.redSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_GREEN_SIZE, value);
   data.greenSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_BLUE_SIZE, value);
   data.blueSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_ALPHA_SIZE, value);
   data.alphaSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_DEPTH_SIZE, value);
   data.depthSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_STENCIL_SIZE, value);
   data.stencilSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_ACCUM_RED_SIZE, value);
   data.accumRedSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_ACCUM_GREEN_SIZE, value);
   data.accumGreenSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_ACCUM_BLUE_SIZE, value);
   data.accumBlueSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_ACCUM_ALPHA_SIZE, value);
   data.accumAlphaSize = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_SAMPLE_BUFFERS, value);
   data.sampleBuffers = value[0];
   GLX.glXGetConfig(xDisplay, vinfo, GLX.GLX_SAMPLES, value);
   data.samples = value[0];
   return data;
 }