Ejemplo n.º 1
0
 @Override
 public void destroy() {
   resourceManager.unloadAll();
   Display.destroy();
 }
Ejemplo n.º 2
0
  @Override
  public void update(Engine engine, double time, double dt) {
    Window window = engine.getGlobal(Window.class);
    window.closing = Display.isCloseRequested();
    if (Display.wasResized()) {
      window.width = Display.getWidth();
      window.height = Display.getHeight();
    }

    KeyboardComponent keyboard = engine.getGlobal(KeyboardComponent.class);
    Keyboard.poll();
    for (int i = 0; i < keyboard.down.length; i++) {
      keyboard.pressed[i] = false;
      keyboard.released[i] = false;
      keyboard.down[i] = Keyboard.isKeyDown(i);
    }
    while (Keyboard.next()) {
      int key = Keyboard.getEventKey();
      keyboard.pressed[key] = Keyboard.getEventKeyState();
      keyboard.released[key] = !keyboard.pressed[key];
    }

    MouseComponent mouse = engine.getGlobal(MouseComponent.class);
    Mouse.poll();
    for (int i = 0; i < Mouse.getButtonCount(); i++) {
      mouse.pressed[i] = false;
      mouse.pressed[i] = false;
      mouse.down[i] = Mouse.isButtonDown(i);
      mouse.dx = 0;
      mouse.dy = 0;
    }
    while (Mouse.next()) {
      int btn = Mouse.getEventButton();
      if (btn != -1) {
        mouse.pressed[btn] = Mouse.getEventButtonState();
        mouse.released[btn] = !mouse.pressed[btn];
      } else {
        mouse.dx += Mouse.getEventDX();
        mouse.dy += Mouse.getEventDY();
      }
      mouse.x = Mouse.getEventX();
      mouse.y = Mouse.getEventY();
    }
    mouse.nx = ((float) mouse.x / (float) window.width) * 2.0f - 1.0f;
    mouse.ny = ((float) mouse.y / (float) window.height) * 2.0f - 1.0f;

    Vec3[] mp = MouseUtils.mouseToWorld(window, engine.getGlobal(Camera.class), mouse);

    mouse.near = mp[0];
    mouse.far = mp[1];

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for (Map m : objects.values()) {
      m.clear();
    }

    // calculate the world matrix of each renderable,
    // and add them to their respective "shader buckets"
    for (RenderNode node : engine.getNodeList(RenderNode.class)) {
      ID<Entity> id = node.getEntity().getId();
      worlds.put(id, StateUtils.getMatrix(node.state));

      Program program = resourceManager.getProgram(node.renderable.shader);
      Map<ID<Entity>, RenderNode> renderables = objects.get(program);
      if (renderables == null) {
        renderables = new HashMap<>();
        objects.put(program, renderables);
      }
      renderables.put(id, node);
    }

    for (ViewportNode vpnode : engine.getNodeList(ViewportNode.class)) {
      Camera camera = vpnode.camera;
      glViewport(
          (int) (window.width * vpnode.viewport.x),
          (int) (window.height * vpnode.viewport.y),
          (int) (window.width * vpnode.viewport.width),
          (int) (window.height * vpnode.viewport.height));

      // for each shader, draw each object that uses that shader
      for (Map.Entry<Program, Map<ID<Entity>, RenderNode>> e : objects.entrySet()) {
        Program program = e.getKey();
        program.use();
        program.setView(camera.viewMatrix);
        program.setProjection(camera.projection);
        program.setSun(engine.getGlobal(Sun.class).location);
        program.setEye(camera.eye);
        for (Map.Entry<ID<Entity>, RenderNode> e2 : e.getValue().entrySet()) {
          RenderNode node = e2.getValue();
          program.setWorld(worlds.get(e2.getKey()));
          program.setColour(node.renderable.colour);
          program.setOpacity(node.renderable.opacity);
          Mesh mesh = resourceManager.getMesh(node.mesh);
          mesh.draw();
        }
      }

      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      glDisable(GL_CULL_FACE);

      Program program = resourceManager.getProgram("passthrough");
      program.use();
      program.setView(camera.viewMatrix);
      program.setProjection(camera.projection);
      for (BBoxNode node : engine.getNodeList(BBoxNode.class)) {
        program.setWorld(node.bbox.getMatrix());

        Vec3 e = node.bbox.getExtents();

        glBegin(GL_QUADS);
        {
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), e.getY(), -e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), e.getY(), -e.getZ());

          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), -e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), -e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), -e.getY(), -e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), -e.getY(), -e.getZ());

          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), -e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), -e.getY(), -e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(e.getX(), e.getY(), -e.getZ());

          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), -e.getY(), e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), -e.getY(), -e.getZ());
          glColor3f(1, 0, 1);
          glVertex3f(-e.getX(), e.getY(), -e.getZ());
        }
        glEnd();
      }
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      glEnable(GL_CULL_FACE);
    }

    Display.update();
  }