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
17package com.badlogic.gdx.tests.g3d;
18
19import com.badlogic.gdx.ApplicationListener;
20import com.badlogic.gdx.Gdx;
21import com.badlogic.gdx.assets.AssetManager;
22import com.badlogic.gdx.graphics.GL20;
23import com.badlogic.gdx.graphics.PerspectiveCamera;
24import com.badlogic.gdx.graphics.g3d.Environment;
25import com.badlogic.gdx.graphics.g3d.Model;
26import com.badlogic.gdx.graphics.g3d.ModelBatch;
27import com.badlogic.gdx.graphics.g3d.ModelInstance;
28import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
29import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
30import com.badlogic.gdx.graphics.g3d.model.Node;
31import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
32import com.badlogic.gdx.tests.utils.GdxTest;
33import com.badlogic.gdx.utils.Array;
34
35public class Basic3DSceneTest extends GdxTest implements ApplicationListener {
36	public PerspectiveCamera cam;
37	public CameraInputController camController;
38	public ModelBatch modelBatch;
39	public AssetManager assets;
40	public Array<ModelInstance> instances = new Array<ModelInstance>();
41	public Environment lights;
42	public boolean loading;
43
44	public Array<ModelInstance> blocks = new Array<ModelInstance>();
45	public Array<ModelInstance> invaders = new Array<ModelInstance>();
46	public ModelInstance ship;
47	public ModelInstance space;
48
49	@Override
50	public void create () {
51		modelBatch = new ModelBatch();
52		lights = new Environment();
53		lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
54		lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
55
56		cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
57		cam.position.set(0f, 7f, 10f);
58		cam.lookAt(0, 0, 0);
59		cam.near = 0.1f;
60		cam.far = 300f;
61		cam.update();
62
63		camController = new CameraInputController(cam);
64		Gdx.input.setInputProcessor(camController);
65
66		assets = new AssetManager();
67		assets.load("data/g3d/invaders.g3dj", Model.class);
68		loading = true;
69	}
70
71	private void doneLoading () {
72		Model model = assets.get("data/g3d/invaders.g3dj", Model.class);
73		for (int i = 0; i < model.nodes.size; i++) {
74			String id = model.nodes.get(i).id;
75			ModelInstance instance = new ModelInstance(model, id);
76			Node node = instance.getNode(id);
77
78			instance.transform.set(node.globalTransform);
79			node.translation.set(0, 0, 0);
80			node.scale.set(1, 1, 1);
81			node.rotation.idt();
82			instance.calculateTransforms();
83
84			if (id.equals("space")) {
85				space = instance;
86				continue;
87			}
88
89			instances.add(instance);
90
91			if (id.equals("ship"))
92				ship = instance;
93			else if (id.startsWith("block"))
94				blocks.add(instance);
95			else if (id.startsWith("invader")) invaders.add(instance);
96		}
97
98		loading = false;
99	}
100
101	@Override
102	public void render () {
103		if (loading && assets.update()) doneLoading();
104		camController.update();
105
106		Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
107		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
108
109		modelBatch.begin(cam);
110		for (ModelInstance instance : instances)
111			modelBatch.render(instance, lights);
112		if (space != null) modelBatch.render(space);
113		modelBatch.end();
114	}
115
116	@Override
117	public void dispose () {
118		modelBatch.dispose();
119		instances.clear();
120		assets.dispose();
121	}
122}
123