Пример #1
0
  @Override
  public void init(GLAutoDrawable drawable) {
    try {
      gl = drawable.getGL().getGL2();

      gl.glClearColor(0.7f, 0.7f, 0.9f, 1.0f);
      // gl.setSwapInterval(1);

      gl.glEnable(GL.GL_DEPTH_TEST);
      gl.glEnable(GL2.GL_LIGHTING);

      gl.glEnable(GL.GL_CULL_FACE);
      gl.glCullFace(GL.GL_BACK);

      /////////////////////////////////
      // carregando e compilando objetos
      /////////////////////////////////
      createSceneObjects();

      Iterator<SceneObject> it = scene.getSceneObjectsIterator();
      while (it.hasNext()) {
        SceneObject sceneObject = it.next();

        JWavefrontModel[] models = sceneObject.getAnimationModels();
        if (models == null) continue;
        for (JWavefrontModel model : models) {
          if (sceneObject.useTexture())
            model.compile(
                drawable,
                JWavefrontModel.WF_MATERIAL
                    | JWavefrontModel.WF_TEXTURE
                    | JWavefrontModel.WF_SMOOTH);
          else model.compile(drawable, JWavefrontModel.WF_COLOR | JWavefrontModel.WF_SMOOTH);
        }
      }

      // light
      setSunLight();
      // fog
      setFog();

      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
      float h = (float) mainFrame.getWidth() / (float) mainFrame.getHeight();
      glu.gluPerspective(FPCamera.FOV, h, FPCamera.NEAR, FPCamera.FAR);

    } catch (IOException ex) {
      Logger.getLogger(Renderer.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Пример #2
0
  @Override
  public void display(GLAutoDrawable drawable) {

    gl = drawable.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();

    // desenha ou nao a bounding sphere de cada objeto
    if (InputHandler.getInstance().isKeyPressed(KeyEvent.VK_B)) { // tecla B
      BoundingVolume.isVisible = !BoundingVolume.isVisible;
    }
    // habilita ou nao fog
    if (InputHandler.getInstance().isKeyPressed(KeyEvent.VK_F)) { // tecla F
      fogEnabled = !fogEnabled;
      if (fogEnabled) gl.glEnable(GL2.GL_FOG);
      else gl.glDisable(GL2.GL_FOG);
    }

    // atualiza camera
    camera.update();
    // atualiza objetos
    {
      Iterator<SceneObject> it = Scene.getInstance().getSceneObjectsIterator();
      while (it.hasNext()) {
        SceneObject so = it.next();
        // animacao da porta
        if (so.getName().contains("door")) {
          if (Collision.collide(
              camera.getSceneObject().getBoundingSphere(), so.getBoundingSphere()))
            so.setAnimating(true);
          else so.setAnimating(false);
        }

        so.update();
      }
    }

    glu.gluLookAt(
        FPCamera.position.x,
        FPCamera.position.y,
        FPCamera.position.z,
        FPCamera.position.x + FPCamera.look.x,
        FPCamera.position.y + FPCamera.look.y,
        FPCamera.position.z + FPCamera.look.z,
        FPCamera.up.x,
        FPCamera.up.y,
        FPCamera.up.z);

    if (FPCamera.changed) { // se direcao da camera mudar
      FPCamera.changed = false;

      frustum.generate(drawable); // gera o frustum

      Scene.getInstance().clearVisibleObjectsList();

      Iterator<SceneObject> it = Scene.getInstance().getSceneObjectsIterator();
      while (it.hasNext()) {
        SceneObject so = it.next();
        if (frustum.sphereInFrustum(so.getBoundingSphere()))
          Scene.getInstance().addVisibleObject(so);
      }

      //            System.out.println("Renderable objects: "+ Scene.getInstance().getSize());
      //            System.out.println(FPCamera.position);
    }

    // desenha objetos
    {
      Iterator<SceneObject> it = Scene.getInstance().getVisibleObjectsIterator();
      SceneObject so;
      while (it.hasNext()) {
        so = it.next();

        if (so.getName().contains("lake")) {
          gl.glPushAttrib(GL2.GL_ENABLE_BIT);
          gl.glEnable(GL.GL_BLEND);
          gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
          so.draw(drawable);
          gl.glPopAttrib();
          continue;
        }
        so.draw(drawable);
      }
    }

    //        int e = gl.glGetError();
    //        if(e != GL.GL_NO_ERROR) {
    //            System.err.println("Erro: "+ gl.glGetString(e));
    //        }
    // calculo do fps
    ++frame;
    long time = System.currentTimeMillis();
    elapsed = time - timebase;
    if (elapsed > 1000) {
      long fps = frame * 1000 / (time - timebase);
      timebase = time;
      frame = 0;

      String str = "FPS: " + fps;
      mainFrame.setTitle(str);
    }

    // gl.glFlush();
  }