Пример #1
0
  WindowContext createWindow(final GLProfile glp, final boolean debugGL) {
    final GLCapabilities caps = new GLCapabilities(glp);
    //
    // Create native windowing resources .. X11/Win/OSX
    //
    final Display display = NewtFactory.createDisplay(null); // local display
    Assert.assertNotNull(display);

    final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
    Assert.assertNotNull(screen);

    final Window window = NewtFactory.createWindow(screen, caps);
    Assert.assertNotNull(window);
    window.setSize(128, 128);
    window.setVisible(true);

    final GLDrawableFactory factory = GLDrawableFactory.getFactory(glp);
    final GLDrawable drawable = factory.createGLDrawable(window);
    Assert.assertNotNull(drawable);

    drawable.setRealized(true);

    final GLContext context = drawable.createContext(null);
    Assert.assertNotNull(context);

    context.enableGLDebugMessage(debugGL);

    final int res = context.makeCurrent();
    Assert.assertTrue(GLContext.CONTEXT_CURRENT_NEW == res || GLContext.CONTEXT_CURRENT == res);

    return new WindowContext(window, context);
  }
 public static final GL2 getCurrentGL2() throws GLException {
   GLContext curContext = GLContext.getCurrent();
   if (curContext == null) {
     throw new GLException("No OpenGL context current on this thread");
   }
   return curContext.getGL().getGL2();
 }
Пример #3
0
Файл: Main.java Проект: io7m/r2
  /**
   * Main program.
   *
   * @param args Command line arguments
   * @throws Exception On errors
   */
  public static void main(final String[] args) throws Exception {
    if (args.length != 3 && args.length != 4) {
      Main.LOG.info("usage: root file.vert [file.geom] file.frag");
      System.exit(1);
    }

    final File root = new File(args[0]);

    final R2ShaderPreprocessorType p = R2ShaderPreprocessor.newPreprocessor(root);

    List<String> vs = null;
    Optional<List<String>> gs = null;
    List<String> fs = null;

    try {
      if (args.length == 4) {
        vs = p.preprocessFile(args[1]);
        gs = Optional.of(p.preprocessFile(args[2]));
        fs = p.preprocessFile(args[3]);
      } else {
        vs = p.preprocessFile(args[1]);
        gs = Optional.empty();
        fs = p.preprocessFile(args[2]);
      }
    } catch (final Exception e) {
      Main.LOG.error("Preprocessing failure: ", e);
      System.exit(1);
    }

    final GLProfile pro = GLProfile.get(GLProfile.GL3);
    final GLCapabilities caps = new GLCapabilities(pro);
    final GLDrawableFactory f = GLDrawableFactory.getFactory(pro);
    final GLOffscreenAutoDrawable drawable =
        f.createOffscreenAutoDrawable(null, caps, null, 32, 32);
    drawable.display();
    final GLContext ctx = drawable.getContext();
    ctx.makeCurrent();

    try {
      final JCGLImplementationJOGLType i = JCGLImplementationJOGL.getInstance();
      final JCGLContextType jc = i.newContextFrom(ctx, "offscreen");
      final JCGLInterfaceGL33Type gi = jc.contextGetGL33();
      final R2ShaderCheckerType ch = R2ShaderChecker.newChecker(gi.getShaders());

      ch.check(vs, gs, fs);
    } finally {
      ctx.release();
      drawable.destroy();
    }
  }
Пример #4
0
  @Override
  protected final GLDrawableImpl createOffscreenDrawableImpl(final NativeSurface target) {
    if (target == null) {
      throw new IllegalArgumentException("Null target");
    }
    final AbstractGraphicsConfiguration config = target.getGraphicsConfiguration();
    final GLCapabilitiesImmutable caps = (GLCapabilitiesImmutable) config.getChosenCapabilities();
    if (!caps.isPBuffer()) {
      return new X11PixmapGLXDrawable(this, target);
    }

    // PBuffer GLDrawable Creation
    GLDrawableImpl pbufferDrawable;
    final AbstractGraphicsDevice device = config.getScreen().getDevice();

    /**
     * Due to the ATI Bug https://bugzilla.mozilla.org/show_bug.cgi?id=486277, we need to have a
     * context current on the same Display to create a PBuffer. The dummy context shall also use the
     * same Display, since switching Display in this regard is another ATI bug.
     */
    final SharedResource sr = (SharedResource) sharedResourceRunner.getOrCreateShared(device);
    if (null != sr && sr.isGLXVendorATI() && null == GLContext.getCurrent()) {
      sr.getContext().makeCurrent();
      try {
        pbufferDrawable = new X11PbufferGLXDrawable(this, target);
      } finally {
        sr.getContext().release();
      }
    } else {
      pbufferDrawable = new X11PbufferGLXDrawable(this, target);
    }
    return pbufferDrawable;
  }
Пример #5
0
  public static void setBoundElementVBO(final RendererRecord rendRecord, final int id) {
    final GL gl = GLContext.getCurrentGL();

    if (!rendRecord.isElementVboValid() || rendRecord.getCurrentElementVboId() != id) {
      gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, id);
      rendRecord.setCurrentElementVboId(id);
      rendRecord.setElementVboValid(true);
    }
  }
Пример #6
0
  /**
   * //! Draw a pretty arrow on the z-axis using a cone and a cylinder (using GLUT)
   *
   * @param aArrowStart
   * @param aArrowTip
   * @param aWidth
   */
  public static void jDrawArrow(
      final JVector3d aArrowStart, final JVector3d aArrowTip, final double aWidth) {

    GL2 gl = GLContext.getCurrent().getGL().getGL2();

    gl.glPushMatrix();

    // We don't really care about the up vector, but it can't
    // be parallel to the arrow...
    JVector3d up = new JVector3d(0, 1, 0);
    // JVector3d arrow = aArrowTip-aArrowStart;
    JVector3d arrow = new JVector3d(0, 0, 0);
    arrow.normalize();
    double d = Math.abs(JMaths.jDot(up, arrow));
    if (d > .9) {
      up = new JVector3d(1, 0, 0);
    }

    JMatrixGL.jLookAt(gl, aArrowStart, aArrowTip, up);

    double distance = JMaths.jDistance(aArrowTip, aArrowStart);

    // This flips the z axis around
    gl.glRotatef(180, 1, 0, 0);

    // create a new OpenGL quadratic object
    GLUquadric quadObj;
    quadObj = glu.gluNewQuadric();

    // set rendering style
    glu.gluQuadricDrawStyle(quadObj, GLU.GLU_FILL);

    // set normal-rendering mode
    glu.gluQuadricNormals(quadObj, GLU.GLU_SMOOTH);

    // render a cylinder and a cone
    gl.glRotatef(180, 1, 0, 0);
    glu.gluDisk(quadObj, 0, aWidth, 10, 10);
    gl.glRotatef(180, 1, 0, 0);

    glu.gluCylinder(quadObj, aWidth, aWidth, distance * ARROW_CYLINDER_PORTION, 10, 10);
    gl.glTranslated(0, 0, ARROW_CYLINDER_PORTION * distance);

    gl.glRotatef(180, 1, 0, 0);
    glu.gluDisk(quadObj, 0, aWidth * 2.0, 10, 10);
    gl.glRotatef(180, 1, 0, 0);

    glu.gluCylinder(quadObj, aWidth * 2.0, 0.0, distance * ARRROW_CONE_PORTION, 10, 10);

    // delete our quadric object
    glu.gluDeleteQuadric(quadObj);

    gl.glPopMatrix();
  }
 private static final GLUgl2ProcAddressTable getGLUProcAddressTable() {
   if (gluProcAddressTable == null) {
     GLContext curContext = GLContext.getCurrent();
     if (curContext == null) {
       throw new GLException("No OpenGL context current on this thread");
     }
     GLDynamicLookupHelper glLookupHelper =
         ((GLContextImpl) curContext).getGLDynamicLookupHelper();
     glLookupHelper.loadGLULibrary();
     GLUgl2ProcAddressTable tmp = new GLUgl2ProcAddressTable(new GLProcAddressResolver());
     tmp.reset(glLookupHelper);
     gluProcAddressTable = tmp;
   }
   return gluProcAddressTable;
 }
Пример #8
0
  public static void applyScissors(final RendererRecord rendRecord) {
    final GL gl = GLContext.getCurrentGL();
    final Stack<ReadOnlyRectangle2> clips = rendRecord.getScissorClips();

    if (clips.size() > 0) {
      final Rectangle2 init = Rectangle2.fetchTempInstance();
      init.set(-1, -1, -1, -1);
      ReadOnlyRectangle2 r;
      boolean first = true;
      for (int i = clips.size(); --i >= 0; ) {
        r = clips.get(i);

        if (r == null) {
          break;
        }
        if (first) {
          init.set(r);
          first = false;
        } else {
          init.intersect(r, init);
        }
        if (init.getWidth() <= 0 || init.getHeight() <= 0) {
          init.setWidth(0);
          init.setHeight(0);
          break;
        }
      }

      if (init.getWidth() == -1) {
        setClippingEnabled(rendRecord, false);
      } else {
        setClippingEnabled(rendRecord, true);
        gl.glScissor(init.getX(), init.getY(), init.getWidth(), init.getHeight());
      }
      Rectangle2.releaseTempInstance(init);
    } else {
      // no clips, so disable
      setClippingEnabled(rendRecord, false);
    }
  }
Пример #9
0
  /**
   * Draw a line-based box with sides parallel to the x-y-z axes.
   *
   * @param aXMin
   * @param aXMax
   * @param aYMin
   * @param aYMax
   * @param aZMin
   * @param aZMax
   */
  public static void jDrawWireBox(
      final double aXMin,
      final double aXMax,
      final double aYMin,
      final double aYMax,
      final double aZMin,
      final double aZMax) {

    GL2 gl = GLContext.getCurrent().getGL().getGL2();
    // render lines for each edge of the box
    gl.glBegin(GL2.GL_LINES);
    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMax);

    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMax);

    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMax);
    gl.glEnd();
  }
Пример #10
0
  /**
   * //! Draw an x-y-z frame.
   *
   * @param aAxisLengthScale
   * @param aAxisThicknessScale
   * @param aModifyMaterialState
   */
  public static void jDrawFrame(
      final double aAxisLengthScale,
      final double aAxisThicknessScale,
      final boolean aModifyMaterialState) {
    // Triangle vertices:
    int nTriangles = 8;

    // Quad vertices:
    int nQuads = 16;

    GL2 gl = GLContext.getCurrent().getGL().getGL2();
    // set material properties
    float[] fnull = {0, 0, 0, 0};
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, fnull, 0);
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_EMISSION, fnull, 0);
    gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
    gl.glEnable(GL2.GL_COLOR_MATERIAL);
    gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);

    // enable vertex and normal arrays
    gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

    if (aModifyMaterialState) {
      gl.glEnable(GL2.GL_COLOR_MATERIAL);
      gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
      gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL);
    }

    for (int k = 0; k < 3; k++) {
      gl.glPushMatrix();

      // Rotate to the appropriate axis
      if (k == 0) {
        gl.glRotatef(-90.0f, 0, 1, 0);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
      } else if (k == 1) {
        gl.glRotatef(90.0f, 1, 0, 0);
        gl.glColor3f(0.0f, 1.0f, 0.0f);
      } else {
        gl.glRotatef(180.0f, 1, 0, 0);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
      }

      // scaling
      gl.glScaled(aAxisThicknessScale, aAxisThicknessScale, aAxisLengthScale);

      // render frame object

      gl.glVertexPointer(3, GL2.GL_FLOAT, 0, triVerticesBuffer);
      gl.glNormalPointer(GL2.GL_FLOAT, 0, triNormalsBuffer);

      gl.glDrawArrays(GL2.GL_TRIANGLES, 0, nTriangles * 3);

      gl.glVertexPointer(3, GL2.GL_FLOAT, 0, quadVerticesBuffer);
      gl.glNormalPointer(GL2.GL_FLOAT, 0, quadNormalsBuffer);

      gl.glDrawArrays(GL2.GL_QUADS, 0, nQuads * 4);

      gl.glPopMatrix();
    }

    // disable vertex and normal arrays
    gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
  }
Пример #11
0
 GL2 gl() {
   if (gl == null) {
     gl = GLContext.getCurrentGL().getGL2();
   }
   return gl;
 }
Пример #12
0
  public void asyncEachOneAnimator(final boolean destroyCleanOrder)
      throws InterruptedException, InvocationTargetException {
    final Frame f1 = new Frame();
    final Animator a1 = new Animator();
    final GearsES2 g1 = new GearsES2(0);
    final GLCanvas c1 = createGLCanvas(f1, 0, 0, g1);
    a1.add(c1);
    a1.start();
    // f1.setVisible(true); // we do this post f2 .. to test pending creation!

    final Frame f2 = new Frame();
    final Animator a2 = new Animator();
    final GearsES2 g2 = new GearsES2(0);
    g2.setSharedGears(g1);
    final GLCanvas c2 = createGLCanvas(f2, f1.getX() + width, f1.getY() + 0, g2);
    c2.setSharedAutoDrawable(c1);
    a2.add(c2);
    a2.start();
    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            f2.setVisible(true);
          }
        });

    Thread.sleep(200); // wait a while ..

    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            f1.setVisible(true); // test pending creation of f2
          }
        });

    final Frame f3 = new Frame();
    final Animator a3 = new Animator();
    final GearsES2 g3 = new GearsES2(0);
    g3.setSharedGears(g1);
    final GLCanvas c3 = createGLCanvas(f3, f1.getX() + 0, f1.getY() + height, g3);
    c3.setSharedAutoDrawable(c1);
    a3.add(c3);
    a3.start();
    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            f3.setVisible(true);
          }
        });

    Thread.sleep(
        1000 / 60
            * 10); // wait ~10 frames giving a chance to create (blocking until master share is
    // valid)

    Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, true));
    Assert.assertTrue(AWTRobotUtil.waitForVisible(c1, true));
    Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c1, true));
    Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));

    Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, true));
    Assert.assertTrue(AWTRobotUtil.waitForVisible(c2, true));
    Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c2, true));
    Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));

    Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, true));
    Assert.assertTrue(AWTRobotUtil.waitForVisible(c3, true));
    Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c3, true));
    Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));

    final GLContext ctx1 = c1.getContext();
    final GLContext ctx2 = c2.getContext();
    final GLContext ctx3 = c3.getContext();
    {
      final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
      final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
      final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
      MiscUtils.dumpSharedGLContext("XXX-C-3.1", ctx1);
      MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx2);
      MiscUtils.dumpSharedGLContext("XXX-C-3.3", ctx3);

      Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
      Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
      Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
      Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
      Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
      Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
    }

    Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
    Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
    Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());

    try {
      Thread.sleep(duration);
    } catch (final Exception e) {
      e.printStackTrace();
    }
    // Stopped animator allows native windowing system 'repaint' event
    // to trigger GLAD 'display'
    a1.stop();
    Assert.assertEquals(false, a1.isAnimating());
    a2.stop();
    Assert.assertEquals(false, a2.isAnimating());
    a3.stop();
    Assert.assertEquals(false, a3.isAnimating());

    if (destroyCleanOrder) {
      System.err.println("XXX Destroy in clean order NOW");
    } else {
      System.err.println(
          "XXX Destroy in creation order NOW - Driver Impl. Ma trigger driver Bug i.e. not postponing GL ctx destruction after releasing all refs.");
    }
    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            try {
              if (destroyCleanOrder) {
                f3.dispose();
              } else {
                f1.dispose();
              }
            } catch (final Throwable t) {
              throw new RuntimeException(t);
            }
          }
        });

    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            try {
              f2.dispose();
            } catch (final Throwable t) {
              throw new RuntimeException(t);
            }
          }
        });

    javax.swing.SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            try {
              if (destroyCleanOrder) {
                f1.dispose();
              } else {
                f3.dispose();
              }
            } catch (final Throwable t) {
              throw new RuntimeException(t);
            }
          }
        });

    Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, false));
    Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, false));
    Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, false));
  }
Пример #13
-1
  public static void setClippingEnabled(final RendererRecord rendRecord, final boolean enabled) {
    final GL gl = GLContext.getCurrentGL();

    if (enabled && (!rendRecord.isClippingTestValid() || !rendRecord.isClippingTestEnabled())) {
      gl.glEnable(GL.GL_SCISSOR_TEST);
      rendRecord.setClippingTestEnabled(true);
    } else if (!enabled
        && (!rendRecord.isClippingTestValid() || rendRecord.isClippingTestEnabled())) {
      gl.glDisable(GL.GL_SCISSOR_TEST);
      rendRecord.setClippingTestEnabled(false);
    }
    rendRecord.setClippingTestValid(true);
  }