• 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.assets.AssetManager;
21 import com.badlogic.gdx.graphics.Color;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.PerspectiveCamera;
24 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
25 import com.badlogic.gdx.graphics.g3d.Material;
26 import com.badlogic.gdx.graphics.g3d.Model;
27 import com.badlogic.gdx.graphics.g3d.ModelBatch;
28 import com.badlogic.gdx.graphics.g3d.ModelInstance;
29 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
30 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
31 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
32 import com.badlogic.gdx.tests.utils.GdxTest;
33 import com.badlogic.gdx.utils.Array;
34 
35 public abstract class BaseG3dTest extends GdxTest {
36 	public AssetManager assets;
37 
38 	public PerspectiveCamera cam;
39 	public CameraInputController inputController;
40 	public ModelBatch modelBatch;
41 	public Model axesModel;
42 	public ModelInstance axesInstance;
43 	public boolean showAxes = true;
44 	public Array<ModelInstance> instances = new Array<ModelInstance>();
45 	public final Color bgColor = new Color(0, 0, 0, 1);
46 
47 	@Override
create()48 	public void create () {
49 		if (assets == null) assets = new AssetManager();
50 
51 		modelBatch = new ModelBatch();
52 
53 		cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
54 		cam.position.set(10f, 10f, 10f);
55 		cam.lookAt(0, 0, 0);
56 		cam.near = 0.1f;
57 		cam.far = 1000f;
58 		cam.update();
59 
60 		createAxes();
61 
62 		Gdx.input.setInputProcessor(inputController = new CameraInputController(cam));
63 	}
64 
65 	final float GRID_MIN = -10f;
66 	final float GRID_MAX = 10f;
67 	final float GRID_STEP = 1f;
68 
createAxes()69 	private void createAxes () {
70 		ModelBuilder modelBuilder = new ModelBuilder();
71 		modelBuilder.begin();
72 		MeshPartBuilder builder = modelBuilder.part("grid", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
73 		builder.setColor(Color.LIGHT_GRAY);
74 		for (float t = GRID_MIN; t <= GRID_MAX; t += GRID_STEP) {
75 			builder.line(t, 0, GRID_MIN, t, 0, GRID_MAX);
76 			builder.line(GRID_MIN, 0, t, GRID_MAX, 0, t);
77 		}
78 		builder = modelBuilder.part("axes", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
79 		builder.setColor(Color.RED);
80 		builder.line(0, 0, 0, 100, 0, 0);
81 		builder.setColor(Color.GREEN);
82 		builder.line(0, 0, 0, 0, 100, 0);
83 		builder.setColor(Color.BLUE);
84 		builder.line(0, 0, 0, 0, 0, 100);
85 		axesModel = modelBuilder.end();
86 		axesInstance = new ModelInstance(axesModel);
87 	}
88 
render(final ModelBatch batch, final Array<ModelInstance> instances)89 	protected abstract void render (final ModelBatch batch, final Array<ModelInstance> instances);
90 
91 	protected boolean loading = false;
92 
onLoaded()93 	protected void onLoaded () {
94 	}
95 
render(final Array<ModelInstance> instances)96 	public void render (final Array<ModelInstance> instances) {
97 		modelBatch.begin(cam);
98 		if (showAxes) modelBatch.render(axesInstance);
99 		if (instances != null) render(modelBatch, instances);
100 		modelBatch.end();
101 	}
102 
103 	@Override
render()104 	public void render () {
105 		if (loading && assets.update()) {
106 			loading = false;
107 			onLoaded();
108 		}
109 
110 		inputController.update();
111 
112 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
113 		Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
114 
115 		render(instances);
116 	}
117 
118 	@Override
dispose()119 	public void dispose () {
120 		modelBatch.dispose();
121 		assets.dispose();
122 		assets = null;
123 		axesModel.dispose();
124 		axesModel = null;
125 	}
126 }
127