• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import java.util.Random;
20 
21 import com.badlogic.gdx.ApplicationListener;
22 import com.badlogic.gdx.Gdx;
23 import com.badlogic.gdx.Input.Keys;
24 import com.badlogic.gdx.graphics.Camera;
25 import com.badlogic.gdx.graphics.Color;
26 import com.badlogic.gdx.graphics.GL20;
27 import com.badlogic.gdx.graphics.OrthographicCamera;
28 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
29 import com.badlogic.gdx.graphics.g2d.BitmapFont;
30 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
31 import com.badlogic.gdx.graphics.g3d.Material;
32 import com.badlogic.gdx.graphics.g3d.Model;
33 import com.badlogic.gdx.graphics.g3d.ModelBatch;
34 import com.badlogic.gdx.graphics.g3d.ModelInstance;
35 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
36 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
37 import com.badlogic.gdx.math.Vector3;
38 import com.badlogic.gdx.tests.utils.GdxTest;
39 
40 public class CullTest extends GdxTest implements ApplicationListener {
41 
42 	Model sphere;
43 	Camera cam;
44 	SpriteBatch batch;
45 	ModelBatch modelBatch;
46 	BitmapFont font;
47 	ModelInstance[] instances = new ModelInstance[100];
48 	final Vector3 pos = new Vector3();
49 
50 	@Override
create()51 	public void create () {
52 		ModelBuilder builder = new ModelBuilder();
53 		sphere = builder.createSphere(2f, 2f, 2f, 16, 16, new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)),
54 			Usage.Position | Usage.Normal);
55 		// cam = new PerspectiveCamera(45, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
56 		cam = new OrthographicCamera(45, 45 * (Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight()));
57 
58 		cam.near = 1;
59 		cam.far = 200;
60 
61 		Random rand = new Random();
62 		for (int i = 0; i < instances.length; i++) {
63 			pos.set(rand.nextFloat() * 100 - rand.nextFloat() * 100, rand.nextFloat() * 100 - rand.nextFloat() * 100,
64 				rand.nextFloat() * -100 - 3);
65 			instances[i] = new ModelInstance(sphere, pos);
66 		}
67 		modelBatch = new ModelBatch();
68 
69 		batch = new SpriteBatch();
70 		font = new BitmapFont();
71 		// Gdx.graphics.setVSync(true);
72 		// Gdx.app.log("CullTest", "" + Gdx.graphics.getBufferFormat().toString());
73 	}
74 
75 	@Override
render()76 	public void render () {
77 		GL20 gl = Gdx.gl20;
78 
79 		gl.glClearColor(0, 0, 0, 0);
80 		gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
81 		gl.glEnable(GL20.GL_DEPTH_TEST);
82 
83 		cam.update();
84 		modelBatch.begin(cam);
85 
86 		int visible = 0;
87 		for (int i = 0; i < instances.length; i++) {
88 			instances[i].transform.getTranslation(pos);
89 			if (cam.frustum.sphereInFrustum(pos, 1)) {
90 				((ColorAttribute)instances[i].materials.get(0).get(ColorAttribute.Diffuse)).color.set(Color.WHITE);
91 				visible++;
92 			} else {
93 				((ColorAttribute)instances[i].materials.get(0).get(ColorAttribute.Diffuse)).color.set(Color.RED);
94 			}
95 			modelBatch.render(instances[i]);
96 		}
97 		modelBatch.end();
98 
99 		if (Gdx.input.isKeyPressed(Keys.A)) cam.rotate(20 * Gdx.graphics.getDeltaTime(), 0, 1, 0);
100 		if (Gdx.input.isKeyPressed(Keys.D)) cam.rotate(-20 * Gdx.graphics.getDeltaTime(), 0, 1, 0);
101 
102 		gl.glDisable(GL20.GL_DEPTH_TEST);
103 		batch.begin();
104 		font.draw(batch, "visible: " + visible + "/100" + ", fps: " + Gdx.graphics.getFramesPerSecond(), 0, 20);
105 		batch.end();
106 	}
107 
108 	@Override
dispose()109 	public void dispose () {
110 		batch.dispose();
111 		font.dispose();
112 		sphere.dispose();
113 	}
114 }
115