• 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;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.graphics.FPSLogger;
21 import com.badlogic.gdx.graphics.GL20;
22 import com.badlogic.gdx.graphics.PerspectiveCamera;
23 import com.badlogic.gdx.graphics.Texture;
24 import com.badlogic.gdx.graphics.g2d.TextureRegion;
25 import com.badlogic.gdx.graphics.g3d.decals.CameraGroupStrategy;
26 import com.badlogic.gdx.graphics.g3d.decals.Decal;
27 import com.badlogic.gdx.graphics.g3d.decals.DecalBatch;
28 import com.badlogic.gdx.math.Vector3;
29 import com.badlogic.gdx.tests.utils.GdxTest;
30 import com.badlogic.gdx.tests.utils.PerspectiveCamController;
31 import com.badlogic.gdx.utils.Array;
32 
33 public class SimpleDecalTest extends GdxTest {
34 	private static final int NUM_DECALS = 3;
35 	DecalBatch batch;
36 	Array<Decal> decals = new Array<Decal>();
37 	PerspectiveCamera camera;
38 	PerspectiveCamController controller;
39 	FPSLogger logger = new FPSLogger();
40 
create()41 	public void create () {
42 		float width = Gdx.graphics.getWidth();
43 		float height = Gdx.graphics.getHeight();
44 
45 		camera = new PerspectiveCamera(45, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
46 		camera.near = 1;
47 		camera.far = 300;
48 		camera.position.set(0, 0, 5);
49 		controller = new PerspectiveCamController(camera);
50 
51 		Gdx.input.setInputProcessor(controller);
52 		batch = new DecalBatch(new CameraGroupStrategy(camera));
53 
54 		TextureRegion[] textures = {new TextureRegion(new Texture(Gdx.files.internal("data/egg.png"))),
55 			new TextureRegion(new Texture(Gdx.files.internal("data/wheel.png"))),
56 			new TextureRegion(new Texture(Gdx.files.internal("data/badlogic.jpg")))};
57 
58 		Decal decal = Decal.newDecal(1, 1, textures[1]);
59 		decal.setPosition(0, 0, 0);
60 		decals.add(decal);
61 
62 		decal = Decal.newDecal(1, 1, textures[0], true);
63 		decal.setPosition(0.5f, 0.5f, 1);
64 		decals.add(decal);
65 
66 		decal = Decal.newDecal(1, 1, textures[0], true);
67 		decal.setPosition(1, 1, -1);
68 		decals.add(decal);
69 
70 		decal = Decal.newDecal(1, 1, textures[2]);
71 		decal.setPosition(1.5f, 1.5f, -2);
72 		decals.add(decal);
73 
74 		decal = Decal.newDecal(1, 1, textures[1]);
75 		decal.setPosition(2, 2, -1.5f);
76 		decals.add(decal);
77 	}
78 
79 	Vector3 dir = new Vector3();
80 	private boolean billboard = true;
81 
render()82 	public void render () {
83 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
84 		Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
85 
86 		camera.update();
87 		for (int i = 0; i < decals.size; i++) {
88 			Decal decal = decals.get(i);
89 			if (billboard) {
90 				// billboarding for ortho cam :)
91 // dir.set(-camera.direction.x, -camera.direction.y, -camera.direction.z);
92 // decal.setRotation(dir, Vector3.Y);
93 
94 				// billboarding for perspective cam
95 				decal.lookAt(camera.position, camera.up);
96 			}
97 			batch.add(decal);
98 		}
99 		batch.flush();
100 		logger.log();
101 	}
102 
103 	@Override
dispose()104 	public void dispose () {
105 		batch.dispose();
106 	}
107 }
108