1 package com.badlogic.gdx.tests.g3d; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.InputMultiplexer; 5 import com.badlogic.gdx.graphics.Color; 6 import com.badlogic.gdx.graphics.GL20; 7 import com.badlogic.gdx.graphics.PerspectiveCamera; 8 import com.badlogic.gdx.graphics.Texture; 9 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 10 import com.badlogic.gdx.graphics.g2d.Animation; 11 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 12 import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; 13 import com.badlogic.gdx.graphics.g2d.TextureRegion; 14 import com.badlogic.gdx.graphics.g3d.Environment; 15 import com.badlogic.gdx.graphics.g3d.Material; 16 import com.badlogic.gdx.graphics.g3d.Model; 17 import com.badlogic.gdx.graphics.g3d.ModelBatch; 18 import com.badlogic.gdx.graphics.g3d.ModelInstance; 19 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 20 import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute; 21 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; 22 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 23 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; 24 import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider; 25 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 26 import com.badlogic.gdx.tests.utils.GdxTest; 27 import com.badlogic.gdx.utils.Array; 28 29 public class TextureRegion3DTest extends GdxTest { 30 PerspectiveCamera cam; 31 CameraInputController inputController; 32 ModelBatch modelBatch; 33 Model model; 34 ModelInstance instance; 35 Environment environment; 36 TextureAtlas atlas; 37 Array<AtlasRegion> regions; 38 TextureAttribute attribute; 39 float time = 1; 40 int index = -1; 41 42 @Override create()43 public void create () { 44 Gdx.gl.glClearColor(0.2f, 0.3f, 1.0f, 0.f); 45 46 atlas = new TextureAtlas(Gdx.files.internal("data/testpack")); 47 regions = atlas.getRegions(); 48 49 modelBatch = new ModelBatch(new DefaultShaderProvider()); 50 51 environment = new Environment(); 52 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f)); 53 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); 54 55 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 56 cam.position.set(10f, 10f, 10f); 57 cam.lookAt(0, 0, 0); 58 cam.near = 0.1f; 59 cam.far = 300f; 60 cam.update(); 61 62 ModelBuilder modelBuilder = new ModelBuilder(); 63 final Material material = new Material(ColorAttribute.createDiffuse(1f, 1f, 1f, 1f), new TextureAttribute(TextureAttribute.Diffuse)); 64 model = modelBuilder.createBox(5f, 5f, 5f, material, Usage.Position | Usage.Normal | Usage.TextureCoordinates); 65 instance = new ModelInstance(model); 66 attribute = instance.materials.get(0).get(TextureAttribute.class, TextureAttribute.Diffuse); 67 68 Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(cam))); 69 } 70 71 @Override render()72 public void render () { 73 inputController.update(); 74 if ((time += Gdx.graphics.getDeltaTime()) >= 1f) { 75 time -= 1f; 76 index = (index + 1) % regions.size; 77 attribute.set(regions.get(index)); 78 Gdx.app.log("TextureRegion3DTest", "Current region = "+regions.get(index).name); 79 } 80 81 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 82 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 83 84 modelBatch.begin(cam); 85 modelBatch.render(instance, environment); 86 modelBatch.end(); 87 } 88 89 @Override dispose()90 public void dispose () { 91 modelBatch.dispose(); 92 model.dispose(); 93 atlas.dispose(); 94 } 95 needsGL20()96 public boolean needsGL20 () { 97 return true; 98 } 99 resume()100 public void resume () { 101 } 102 resize(int width, int height)103 public void resize (int width, int height) { 104 } 105 pause()106 public void pause () { 107 } 108 }