Home | History | Annotate | Download | only in tests
      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.Gdx;
     22 import com.badlogic.gdx.Input.Keys;
     23 import com.badlogic.gdx.graphics.Camera;
     24 import com.badlogic.gdx.graphics.Color;
     25 import com.badlogic.gdx.graphics.GL20;
     26 import com.badlogic.gdx.graphics.PerspectiveCamera;
     27 import com.badlogic.gdx.graphics.Texture;
     28 import com.badlogic.gdx.graphics.g2d.BitmapFont;
     29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
     30 import com.badlogic.gdx.graphics.g2d.TextureRegion;
     31 import com.badlogic.gdx.graphics.g3d.Model;
     32 import com.badlogic.gdx.graphics.g3d.ModelBatch;
     33 import com.badlogic.gdx.graphics.g3d.ModelInstance;
     34 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
     35 import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
     36 import com.badlogic.gdx.math.Vector3;
     37 import com.badlogic.gdx.tests.utils.GdxTest;
     38 
     39 public class ProjectTest extends GdxTest {
     40 
     41 	Model sphere;
     42 	Camera cam;
     43 	SpriteBatch batch;
     44 	BitmapFont font;
     45 	ModelInstance[] instances = new ModelInstance[100];
     46 	ModelBatch modelBatch;
     47 	Vector3 tmp = new Vector3();
     48 	TextureRegion logo;
     49 
     50 	@Override
     51 	public void create () {
     52 		ObjLoader objLoader = new ObjLoader();
     53 		sphere = objLoader.loadModel(Gdx.files.internal("data/sphere.obj"));
     54 		sphere.materials.get(0).set(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE));
     55 		cam = new PerspectiveCamera(45, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
     56 		cam.far = 200;
     57 		Random rand = new Random();
     58 		for (int i = 0; i < instances.length; i++) {
     59 			instances[i] = new ModelInstance(sphere, rand.nextFloat() * 100 - rand.nextFloat() * 100, rand.nextFloat() * 100
     60 				- rand.nextFloat() * 100, rand.nextFloat() * -100 - 3);
     61 		}
     62 		batch = new SpriteBatch();
     63 		font = new BitmapFont();
     64 		logo = new TextureRegion(new Texture(Gdx.files.internal("data/badlogicsmall.jpg")));
     65 		modelBatch = new ModelBatch();
     66 	}
     67 
     68 	@Override
     69 	public void render () {
     70 
     71 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
     72 		Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
     73 
     74 		cam.update();
     75 
     76 		modelBatch.begin(cam);
     77 
     78 		int visible = 0;
     79 		for (int i = 0; i < instances.length; i++) {
     80 			instances[i].transform.getTranslation(tmp);
     81 			if (cam.frustum.sphereInFrustum(tmp, 1)) {
     82 				((ColorAttribute)instances[i].materials.get(0).get(ColorAttribute.Diffuse)).color.set(Color.WHITE);
     83 				visible++;
     84 			} else {
     85 				((ColorAttribute)instances[i].materials.get(0).get(ColorAttribute.Diffuse)).color.set(Color.RED);
     86 			}
     87 			modelBatch.render(instances[i]);
     88 		}
     89 		modelBatch.end();
     90 
     91 		if (Gdx.input.isKeyPressed(Keys.A)) cam.rotate(20 * Gdx.graphics.getDeltaTime(), 0, 1, 0);
     92 		if (Gdx.input.isKeyPressed(Keys.D)) cam.rotate(-20 * Gdx.graphics.getDeltaTime(), 0, 1, 0);
     93 
     94 		Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
     95 		batch.begin();
     96 		for (int i = 0; i < instances.length; i++) {
     97 			instances[i].transform.getTranslation(tmp);
     98 			cam.project(tmp);
     99 			if (tmp.z < 0) continue;
    100 			batch.draw(logo, tmp.x, tmp.y);
    101 		}
    102 		batch.end();
    103 	}
    104 }
    105