@Override
  protected void init() {
    initializeProgram();

    try {
      cylinderMesh = new Mesh("UnitCylinder.xml");
      planeMesh = new Mesh("LargePlane.xml");
    } catch (Exception exception) {
      exception.printStackTrace();
      System.exit(-1);
    }

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CW);

    glEnable(GL_DEPTH_TEST);
    glDepthMask(true);
    glDepthFunc(GL_LEQUAL);
    glDepthRange(0.0f, 1.0f);
    glEnable(GL_DEPTH_CLAMP);

    projectionUniformBuffer = glGenBuffers();
    glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer);
    glBufferData(GL_UNIFORM_BUFFER, ProjectionBlock.SIZE, GL_DYNAMIC_DRAW);

    // Bind the static buffers.
    glBindBufferRange(
        GL_UNIFORM_BUFFER, projectionBlockIndex, projectionUniformBuffer, 0, ProjectionBlock.SIZE);

    glBindBuffer(GL_UNIFORM_BUFFER, 0);
  }
 private static void setUpDisplay() {
   try {
     Display.setDisplayMode(new DisplayMode(640, 480));
     Display.setVSyncEnabled(true);
     Display.setTitle("Core Lighting Demo");
     Display.create();
   } catch (LWJGLException e) {
     System.err.println("The display wasn't initialized correctly. :(");
     Display.destroy();
     System.exit(1);
   }
 }
 private static void setUpVBOs() {
   int[] vbos;
   try {
     model = OBJLoader.loadModel(new File(MODEL_LOCATION));
     vbos = OBJLoader.createVBO(model);
     vboVertexHandle = vbos[0];
     vboNormalHandle = vbos[1];
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   } catch (IOException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   }
   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_NORMAL_ARRAY);
   glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
   glVertexPointer(3, GL_FLOAT, 0, 0L);
   glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
   glNormalPointer(GL_FLOAT, 0, 0L);
 }
 private static void setUpVBOs() {
   int[] vbos;
   try {
     model = OBJLoader.loadModel(new File(MODEL_LOCATION));
     int vboVertexHandle = glGenBuffers();
     int vboNormalHandle = glGenBuffers();
     FloatBuffer vertices = BufferTools.reserveData(model.faces.size() * 9);
     FloatBuffer normals = BufferTools.reserveData(model.faces.size() * 9);
     for (Face face : model.faces) {
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.x - 1)));
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.y - 1)));
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.z - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.x - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.y - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.z - 1)));
     }
     vertices.flip();
     normals.flip();
     glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
     glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
     glEnableVertexAttribArray(attributeVertex);
     glVertexAttribPointer(attributeVertex, 3, false, 0, vertices);
     glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
     glEnableVertexAttribArray(attributeNormal);
     glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
     glNormalPointer(GL_FLOAT, 0, 0L);
     // TODO: This really isn't finished yet. :-(
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   } catch (IOException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   }
 }
Exemple #5
0
  void renderLoop() {
    long startTime = System.currentTimeMillis() + 5000;
    long fps = 0;

    while (glfwWindowShouldClose(window.handle) == GLFW_FALSE) {
      Runnable event;
      while ((event = events.poll()) != null) event.run();

      try {
        display();
      } catch (Exception e) {
        e.printStackTrace();
        break;
      }

      glfwSwapBuffers(window.handle);

      if (startTime > System.currentTimeMillis()) {
        fps++;
      } else {
        long timeUsed = 5000 + (startTime - System.currentTimeMillis());
        startTime = System.currentTimeMillis() + 5000;
        log(
            String.format(
                "%s: %d frames in 5 seconds = %.2f",
                getPlatformInfoStringUTF8(platform, CL_PLATFORM_VENDOR),
                fps,
                fps / (timeUsed / 1000f)));
        fps = 0;
      }
    }

    cleanup();

    window.signal.countDown();
  }
 public static void main(String[] args) {
   setUpDisplay();
   setUpVBOs();
   setUpCamera();
   setUpShaders();
   setUpLighting();
   while (!Display.isCloseRequested()) {
     render();
     checkInput();
     Display.update();
     Display.sync(60);
   }
   cleanUp();
   System.exit(0);
 }