示例#1
0
  private void init() throws LWJGLException {
    // create Window of size 800x600
    Display.setDisplayMode(new DisplayMode(1024, 768));
    Display.setLocation(
        (Display.getDisplayMode().getWidth() - 300) / 2,
        (Display.getDisplayMode().getHeight() - 300) / 2);
    Display.setTitle("Gears");

    try {
      Display.create();
    } catch (LWJGLException e) {
      // This COULD be because of a bug! A delay followed by a new attempt is supposed to fix it.
      e.printStackTrace();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ignored) {
      }

      Display.create();
    }

    // setup ogl
    FloatBuffer pos = BufferUtils.createFloatBuffer(4).put(new float[] {5.0f, 5.0f, 10.0f, 0.0f});
    FloatBuffer red = BufferUtils.createFloatBuffer(4).put(new float[] {0.8f, 0.1f, 0.0f, 1.0f});
    FloatBuffer green = BufferUtils.createFloatBuffer(4).put(new float[] {0.0f, 0.8f, 0.2f, 1.0f});
    FloatBuffer blue = BufferUtils.createFloatBuffer(4).put(new float[] {0.2f, 0.2f, 1.0f, 1.0f});

    pos.flip();
    red.flip();
    green.flip();
    blue.flip();

    glLight(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_CULL_FACE);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    /* make the gears */
    gear1 = glGenLists(1);
    glNewList(gear1, GL_COMPILE);
    glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
    gear(1.0f, 4.0f, 1.0f, 20, 0.7f);
    glEndList();

    gear2 = glGenLists(1);
    glNewList(gear2, GL_COMPILE);
    glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
    gear(0.5f, 2.0f, 2.0f, 10, 0.7f);
    glEndList();

    gear3 = glGenLists(1);
    glNewList(gear3, GL_COMPILE);
    glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
    gear(1.3f, 2.0f, 0.5f, 10, 0.7f);
    glEndList();

    glEnable(GL_NORMALIZE);

    glMatrixMode(GL_PROJECTION);

    System.err.println("LWJGL: " + Sys.getVersion() + " / " + LWJGLUtil.getPlatformName());
    System.err.println("GL_VENDOR: " + glGetString(GL_VENDOR));
    System.err.println("GL_RENDERER: " + glGetString(GL_RENDERER));
    System.err.println("GL_VERSION: " + glGetString(GL_VERSION));
    System.err.println();
    System.err.println(
        "glLoadTransposeMatrixfARB() supported: "
            + GLContext.getCapabilities().GL_ARB_transpose_matrix);
    if (!GLContext.getCapabilities().GL_ARB_transpose_matrix) {
      // --- not using extensions
      glLoadIdentity();
    } else {
      // --- using extensions
      final FloatBuffer identityTranspose =
          BufferUtils.createFloatBuffer(16)
              .put(new float[] {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1});
      identityTranspose.flip();
      glLoadTransposeMatrixARB(identityTranspose);
    }

    float h = (float) 300 / (float) 300;
    glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -40.0f);
  }