OSDN Git Service

c0c499b4bb71f85275ccf3bca0ff9b308999a61e
[mikumikustudio/libgdx-mikumikustudio.git] / tests / gdx-tests / src / com / badlogic / gdx / tests / g3d / BaseG3dTest.java
1 package com.badlogic.gdx.tests.g3d;
2
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.assets.AssetManager;
5 import com.badlogic.gdx.graphics.Color;
6 import com.badlogic.gdx.graphics.GL10;
7 import com.badlogic.gdx.graphics.PerspectiveCamera;
8 import com.badlogic.gdx.graphics.VertexAttribute;
9 import com.badlogic.gdx.graphics.VertexAttributes;
10 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
11 import com.badlogic.gdx.graphics.g3d.Material;
12 import com.badlogic.gdx.graphics.g3d.Model;
13 import com.badlogic.gdx.graphics.g3d.ModelBatch;
14 import com.badlogic.gdx.graphics.g3d.ModelInstance;
15 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
16 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
17 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
18 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
19 import com.badlogic.gdx.tests.utils.GdxTest;
20 import com.badlogic.gdx.utils.Array;
21
22 public abstract class BaseG3dTest extends GdxTest {
23         public AssetManager assets;
24         
25         public PerspectiveCamera cam;
26         public CameraInputController inputController;
27         public ModelBatch modelBatch;
28         public Model axesModel;
29         public ModelInstance axesInstance;
30         public boolean showAxes = true;
31         public Array<ModelInstance> instances = new Array<ModelInstance>();
32         
33         @Override
34         public void create () {
35                 if (assets == null)
36                         assets = new AssetManager();
37                 
38                 modelBatch = new ModelBatch();
39                 
40                 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
41                 cam.position.set(10f, 10f, 10f);
42                 cam.lookAt(0,0,0);
43                 cam.near = 0.1f;
44                 cam.far = 1000f;
45                 cam.update();
46                 
47                 createAxes();
48
49                 Gdx.input.setInputProcessor(inputController = new CameraInputController(cam));
50         }
51         
52         final float GRID_MIN = -10f;
53         final float GRID_MAX = 10f;
54         final float GRID_STEP = 1f;
55         private void createAxes() {
56                 ModelBuilder modelBuilder = new ModelBuilder();
57                 modelBuilder.begin();
58                 MeshPartBuilder builder = modelBuilder.part("grid", GL10.GL_LINES, Usage.Position | Usage.Color, new Material());
59                 builder.setColor(Color.LIGHT_GRAY);
60                 for (float t = GRID_MIN; t <= GRID_MAX; t+=GRID_STEP) {
61                         builder.line(t, 0, GRID_MIN, t, 0, GRID_MAX);
62                         builder.line(GRID_MIN, 0, t, GRID_MAX, 0, t);
63                 }
64                 builder = modelBuilder.part("axes", GL10.GL_LINES, Usage.Position | Usage.Color, new Material());
65                 builder.setColor(Color.RED);
66                 builder.line(0, 0, 0, 100, 0, 0);
67                 builder.setColor(Color.GREEN);
68                 builder.line(0, 0, 0, 0, 100, 0);
69                 builder.setColor(Color.BLUE);
70                 builder.line(0, 0, 0, 0, 0, 100);
71                 axesModel = modelBuilder.end();
72                 axesInstance = new ModelInstance(axesModel);
73         }
74         
75         protected abstract void render(final ModelBatch batch, final Array<ModelInstance> instances);
76         
77         protected boolean loading = false;
78         protected void onLoaded() {}
79         
80         public void render(final Array<ModelInstance> instances) {
81                 modelBatch.begin(cam);
82                 if (showAxes)
83                         modelBatch.render(axesInstance);
84                 if (instances != null)
85                         render(modelBatch, instances);
86                 modelBatch.end();
87         }
88         
89         @Override
90         public void render () {
91                 if (loading && assets.update()) {
92                         loading = false;
93                         onLoaded();
94                 }
95                 
96                 inputController.update();
97                 
98                 Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
99                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
100
101                 render(instances);
102         }
103         
104         @Override
105         public void resize (int width, int height) {
106                 super.resize(width, height);
107                 cam.viewportWidth = width;
108                 cam.viewportHeight = height;
109                 cam.update();
110         }
111         
112         @Override
113         public void dispose () {
114                 modelBatch.dispose();
115                 assets.dispose();
116                 assets = null;
117                 axesModel.dispose();
118                 axesModel = null;
119         }
120 }