• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.badlogic.gdx.tests.g3d;
3 
4 import com.badlogic.gdx.Gdx;
5 import com.badlogic.gdx.graphics.Color;
6 import com.badlogic.gdx.graphics.GL20;
7 import com.badlogic.gdx.graphics.Pixmap;
8 import com.badlogic.gdx.graphics.Texture;
9 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
10 import com.badlogic.gdx.graphics.g3d.Environment;
11 import com.badlogic.gdx.graphics.g3d.Material;
12 import com.badlogic.gdx.graphics.g3d.ModelBatch;
13 import com.badlogic.gdx.graphics.g3d.ModelInstance;
14 import com.badlogic.gdx.graphics.g3d.Renderable;
15 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
16 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
17 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
18 import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
19 import com.badlogic.gdx.math.MathUtils;
20 import com.badlogic.gdx.math.collision.BoundingBox;
21 import com.badlogic.gdx.utils.Array;
22 
23 /** Simple test showing how to use a height map. Uses {@link HeightField}.
24  * @author Xoppa */
25 public class HeightMapTest extends BaseG3dTest {
26 	HeightField field;
27 	Renderable ground;
28 	Environment environment;
29 	boolean morph = true;
30 	Texture texture;
31 
32 	@Override
create()33 	public void create () {
34 		super.create();
35 		environment = new Environment();
36 		environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
37 		environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f));
38 
39 		texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
40 
41 		int w = 20, h = 20;
42 		Pixmap data = new Pixmap(Gdx.files.internal("data/g3d/heightmap.png"));
43 		field = new HeightField(true, data, true, Usage.Position | Usage.Normal | Usage.ColorUnpacked | Usage.TextureCoordinates);
44 		data.dispose();
45 		field.corner00.set(-10f, 0, -10f);
46 		field.corner10.set(10f, 0, -10f);
47 		field.corner01.set(-10f, 0, 10f);
48 		field.corner11.set(10f, 0, 10f);
49 		field.color00.set(0, 0, 1, 1);
50 		field.color01.set(0, 1, 1, 1);
51 		field.color10.set(1, 0, 1, 1);
52 		field.color11.set(1, 1, 1, 1);
53 		field.magnitude.set(0f, 5f, 0f);
54 		field.update();
55 
56 		ground = new Renderable();
57 		ground.environment = environment;
58 		ground.meshPart.mesh = field.mesh;
59 		ground.meshPart.primitiveType = GL20.GL_TRIANGLES;
60 		ground.meshPart.offset = 0;
61 		ground.meshPart.size = field.mesh.getNumIndices();
62 		ground.meshPart.update();
63 		ground.material = new Material(TextureAttribute.createDiffuse(texture));
64 	}
65 
66 	@Override
render(ModelBatch batch, Array<ModelInstance> instances)67 	protected void render (ModelBatch batch, Array<ModelInstance> instances) {
68 		batch.render(instances);
69 		batch.render(ground);
70 	}
71 
72 	@Override
dispose()73 	public void dispose () {
74 		super.dispose();
75 		texture.dispose();
76 		field.dispose();
77 	}
78 }
79