• 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.g3d;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.Input.Keys;
21 import com.badlogic.gdx.graphics.g3d.Environment;
22 import com.badlogic.gdx.graphics.g3d.Model;
23 import com.badlogic.gdx.graphics.g3d.ModelBatch;
24 import com.badlogic.gdx.graphics.g3d.ModelInstance;
25 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
26 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
27 import com.badlogic.gdx.graphics.g3d.model.Animation;
28 import com.badlogic.gdx.graphics.g3d.utils.AnimationController;
29 import com.badlogic.gdx.math.Quaternion;
30 import com.badlogic.gdx.math.Vector3;
31 import com.badlogic.gdx.math.collision.BoundingBox;
32 import com.badlogic.gdx.utils.Array;
33 import com.badlogic.gdx.utils.ObjectMap;
34 import com.badlogic.gdx.utils.StringBuilder;
35 
36 public class ModelTest extends BaseG3dHudTest {
37 	protected Environment environment;
38 
39 	ObjectMap<ModelInstance, AnimationController> animationControllers = new ObjectMap<ModelInstance, AnimationController>();
40 
41 	@Override
create()42 	public void create () {
43 		super.create();
44 		environment = new Environment();
45 		environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
46 		environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f));
47 
48 		cam.position.set(1, 1, 1);
49 		cam.lookAt(0, 0, 0);
50 		cam.update();
51 		showAxes = true;
52 
53 		onModelClicked("g3d/teapot.g3db");
54 	}
55 
56 	private final Vector3 tmpV1 = new Vector3(), tmpV2 = new Vector3();
57 	private final Quaternion tmpQ = new Quaternion();
58 	private final BoundingBox bounds = new BoundingBox();
59 
60 	@Override
render(ModelBatch batch, Array<ModelInstance> instances)61 	protected void render (ModelBatch batch, Array<ModelInstance> instances) {
62 		for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries())
63 			e.value.update(Gdx.graphics.getDeltaTime());
64 		batch.render(instances, environment);
65 	}
66 
67 	@Override
getStatus(StringBuilder stringBuilder)68 	protected void getStatus (StringBuilder stringBuilder) {
69 		super.getStatus(stringBuilder);
70 
71 		for (final ModelInstance instance : instances) {
72 			if (instance.animations.size > 0) {
73 				stringBuilder.append(" press space or menu to switch animation");
74 				break;
75 			}
76 		}
77 	}
78 
79 	protected String currentlyLoading;
80 
81 	@Override
onModelClicked(final String name)82 	protected void onModelClicked (final String name) {
83 		if (name == null) return;
84 
85 		currentlyLoading = "data/" + name;
86 		assets.load(currentlyLoading, Model.class);
87 		loading = true;
88 	}
89 
90 	@Override
onLoaded()91 	protected void onLoaded () {
92 		if (currentlyLoading == null || currentlyLoading.length() == 0) return;
93 
94 		instances.clear();
95 		animationControllers.clear();
96 		final ModelInstance instance = new ModelInstance(assets.get(currentlyLoading, Model.class));
97 		instance.transform = transform;
98 		instances.add(instance);
99 		if (instance.animations.size > 0) animationControllers.put(instance, new AnimationController(instance));
100 		currentlyLoading = null;
101 
102 		instance.calculateBoundingBox(bounds);
103 		cam.position.set(1, 1, 1).nor().scl(bounds.getDimensions(tmpV1).len() * 0.75f + bounds.getCenter(tmpV2).len());
104 		cam.up.set(0, 1, 0);
105 		cam.lookAt(0, 0, 0);
106 		cam.far = 50f + bounds.getDimensions(tmpV1).len() * 2.0f;
107 		cam.update();
108 	}
109 
switchAnimation()110 	protected void switchAnimation () {
111 		for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries()) {
112 			int animIndex = 0;
113 			if (e.value.current != null) {
114 				for (int i = 0; i < e.key.animations.size; i++) {
115 					final Animation animation = e.key.animations.get(i);
116 					if (e.value.current.animation == animation) {
117 						animIndex = i;
118 						break;
119 					}
120 				}
121 			}
122 			animIndex = (animIndex + 1) % (e.key.animations.size + 1);
123 			e.value.animate((animIndex == e.key.animations.size) ? null : e.key.animations.get(animIndex).id, -1, 1f, null, 0.2f);
124 		}
125 	}
126 
127 	@Override
keyUp(int keycode)128 	public boolean keyUp (int keycode) {
129 		if (keycode == Keys.SPACE || keycode == Keys.MENU) switchAnimation();
130 		return super.keyUp(keycode);
131 	}
132 }
133