1 package com.badlogic.gdx.tests.g3d;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input.Keys;
5 import com.badlogic.gdx.graphics.GL10;
6 import com.badlogic.gdx.graphics.g3d.Environment;
7 import com.badlogic.gdx.graphics.g3d.Model;
8 import com.badlogic.gdx.graphics.g3d.ModelBatch;
9 import com.badlogic.gdx.graphics.g3d.ModelInstance;
10 import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
11 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
12 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
13 import com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight;
14 import com.badlogic.gdx.graphics.g3d.environment.PointLight;
15 import com.badlogic.gdx.graphics.g3d.model.Animation;
16 import com.badlogic.gdx.graphics.g3d.model.NodeAnimation;
17 import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
18 import com.badlogic.gdx.graphics.g3d.utils.AnimationController;
19 import com.badlogic.gdx.math.Quaternion;
20 import com.badlogic.gdx.math.Rectangle;
21 import com.badlogic.gdx.math.Vector3;
22 import com.badlogic.gdx.math.collision.BoundingBox;
23 import com.badlogic.gdx.math.collision.Ray;
24 import com.badlogic.gdx.utils.Array;
25 import com.badlogic.gdx.utils.ObjectMap;
26 import com.badlogic.gdx.utils.StringBuilder;
28 public class ModelTest extends BaseG3dHudTest {
31 ObjectMap<ModelInstance, AnimationController> animationControllers = new ObjectMap<ModelInstance, AnimationController>();
34 public void create () {
36 lights = new Environment();
37 lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
38 lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f));
40 cam.position.set(1,1,1);
45 onModelClicked("g3d/teapot.g3db");
48 private final Vector3 tmpV = new Vector3();
49 private final Quaternion tmpQ = new Quaternion();
50 private final BoundingBox bounds = new BoundingBox();
52 protected void render (ModelBatch batch, Array<ModelInstance> instances) {
53 for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries())
54 e.value.update(Gdx.graphics.getDeltaTime());
55 batch.render(instances, lights);
59 protected void getStatus (StringBuilder stringBuilder) {
60 super.getStatus(stringBuilder);
62 for (final ModelInstance instance : instances) {
63 if (instance.animations.size > 0) {
64 stringBuilder.append(" press space or menu to switch animation");
70 protected String currentlyLoading;
72 protected void onModelClicked(final String name) {
76 currentlyLoading = "data/"+name;
77 assets.load(currentlyLoading, Model.class);
82 protected void onLoaded() {
83 if (currentlyLoading == null || currentlyLoading.isEmpty())
87 animationControllers.clear();
88 final ModelInstance instance = new ModelInstance(assets.get(currentlyLoading, Model.class));
89 instances.add(instance);
90 if (instance.animations.size > 0)
91 animationControllers.put(instance, new AnimationController(instance));
92 currentlyLoading = null;
94 instance.calculateBoundingBox(bounds);
95 cam.position.set(1,1,1).nor().scl(bounds.getDimensions().len() * 0.75f + bounds.getCenter().len());
98 cam.far = bounds.getDimensions().len() * 2.0f;
102 protected void switchAnimation() {
103 for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries()) {
105 if (e.value.current != null) {
106 for (int i = 0; i < e.key.animations.size; i++) {
107 final Animation animation = e.key.animations.get(i);
108 if (e.value.current.animation == animation) {
114 animIndex = (animIndex + 1) % (e.key.animations.size + 1);
115 e.value.animate((animIndex == e.key.animations.size) ? null : e.key.animations.get(animIndex).id, -1, 1f, null, 0.2f);
120 public boolean needsGL20 () {
125 public boolean keyUp (int keycode) {
126 if (keycode == Keys.SPACE || keycode == Keys.MENU)
128 return super.keyUp(keycode);