OSDN Git Service

6eb567e0ea7c117f3a3f0acd11b6c3ead210da03
[mikumikustudio/libgdx-mikumikustudio.git] / tests / gdx-tests / src / com / badlogic / gdx / tests / g3d / ModelTest.java
1 package com.badlogic.gdx.tests.g3d;
2
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;
27
28 public class ModelTest extends BaseG3dHudTest {
29         Environment lights;
30         
31         ObjectMap<ModelInstance, AnimationController> animationControllers = new ObjectMap<ModelInstance, AnimationController>(); 
32
33         @Override
34         public void create () {
35                 super.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));
39                 
40                 cam.position.set(1,1,1);
41                 cam.lookAt(0,0,0);
42                 cam.update();
43                 showAxes = true;
44                 
45                 onModelClicked("g3d/teapot.g3db");
46         }
47
48         private final Vector3 tmpV = new Vector3();
49         private final Quaternion tmpQ = new Quaternion();
50         private final BoundingBox bounds = new BoundingBox();
51         @Override
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);
56         }
57         
58         @Override
59         protected void getStatus (StringBuilder stringBuilder) {
60                 super.getStatus(stringBuilder);
61
62                 for (final ModelInstance instance : instances) {
63                         if (instance.animations.size > 0) {
64                                 stringBuilder.append(" press space or menu to switch animation");
65                                 break;
66                         }
67                 }
68         }
69         
70         protected String currentlyLoading;
71         @Override
72         protected void onModelClicked(final String name) {
73                 if (name == null)
74                         return;
75                 
76                 currentlyLoading = "data/"+name; 
77                 assets.load(currentlyLoading, Model.class);
78                 loading = true;
79         }
80
81         @Override
82         protected void onLoaded() {
83                 if (currentlyLoading == null || currentlyLoading.isEmpty())
84                         return;
85                 
86                 instances.clear();
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;
93                 
94                 instance.calculateBoundingBox(bounds);
95                 cam.position.set(1,1,1).nor().scl(bounds.getDimensions().len() * 0.75f + bounds.getCenter().len());
96                 cam.up.set(0,1,0);
97                 cam.lookAt(0,0,0);
98                 cam.far = bounds.getDimensions().len() * 2.0f;
99                 cam.update();
100         }
101         
102         protected void switchAnimation() {
103                 for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries()) {
104                         int animIndex = 0;
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) {
109                                                 animIndex = i;
110                                                 break;
111                                         }
112                                 }
113                         }
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);
116                 }
117         }
118
119         @Override
120         public boolean needsGL20 () {
121                 return true;
122         }
123         
124         @Override
125         public boolean keyUp (int keycode) {
126                 if (keycode == Keys.SPACE || keycode == Keys.MENU)
127                         switchAnimation();
128                 return super.keyUp(keycode);
129         }
130 }