Advertisement
Guest User

Untitled

a guest
May 31st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1.     public GameScreen(ArcaneArts game){
  2.         this.game = game;
  3.        
  4.        
  5.         stage = new Stage();
  6.         font = game.getFont(10, "FantaisieArtistique");
  7.         label = new Label(" ", new Label.LabelStyle(font, Color.BLACK));
  8.         stage.addActor(label);
  9.         strBuilder = new StringBuilder();
  10.        
  11.         modelBatch = new ModelBatch();
  12.        
  13.         camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  14.         camera.position.set(10f, 10f, 10f);
  15.         camera.lookAt(0,0,0);
  16.         camera.near = 1f;
  17.         camera.far = 300f;
  18.         camera.update();
  19.        
  20.         environment = new Environment();
  21.         environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
  22.         environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
  23.        
  24.         camController = new CameraInputController(camera);
  25.         Gdx.input.setInputProcessor(camController);
  26.        
  27.         //Load 3d scene
  28.         Model sceneModel = game.assetManager.get("3d/boxes.g3db", Model.class);
  29.        
  30.         for (int i = 0; i<sceneModel.nodes.size; i++){
  31.             String id = sceneModel.nodes.get(i).id;
  32.             GameObject instance = new GameObject(sceneModel, id, true);
  33.            
  34.             ColorAttribute attr = ColorAttribute.createDiffuse(MathUtils.random(), MathUtils.random(), MathUtils.random(), MathUtils.random());
  35.            
  36.             instance.materials.get(0).set(attr);
  37.            
  38.             instances.add(instance);
  39.         }
  40.        
  41.         shader = new TestShader();
  42.         shader.init();
  43.    
  44.     }
  45.  
  46.     private int visibleCount;
  47.     @Override
  48.     public void render(float delta) {
  49.         camController.update();
  50.        
  51.         Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  52.         Gdx.gl.glClearColor(.5f, .5f, .5f, 1f);
  53.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  54.        
  55.         visibleCount = 0;
  56.         modelBatch.begin(camera);
  57.         for (GameObject instance : instances)
  58.             if (isVisible(camera, instance)){
  59.                 modelBatch.render(instance, shader);
  60.                 visibleCount++;
  61.             }
  62.         modelBatch.end();
  63.        
  64.         if (running)
  65.             gameLogic(delta);
  66.        
  67.         strBuilder.setLength(0);
  68.         strBuilder.append(" FPS ").append(Gdx.graphics.getFramesPerSecond())
  69.         .append(" Visible: ").append(visibleCount);
  70.        
  71.         label.setText(strBuilder);
  72.         stage.draw();
  73.     }
  74.    
  75.     private Vector3 position = new Vector3();
  76.     private boolean isVisible(final Camera camera, final GameObject instance){
  77.         instance.transform.getTranslation(position);
  78.         position.add(instance.center);
  79.         System.out.println(instance.radius);
  80.         return camera.frustum.sphereInFrustum(position, instance.radius);
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement