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.InputMultiplexer; 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.Environment; 26 import com.badlogic.gdx.graphics.g3d.Material; 27 import com.badlogic.gdx.graphics.g3d.Model; 28 import com.badlogic.gdx.graphics.g3d.ModelBatch; 29 import com.badlogic.gdx.graphics.g3d.ModelInstance; 30 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 31 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 32 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; 33 import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider; 34 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 35 import com.badlogic.gdx.tests.utils.GdxTest; 36 37 public class Basic3DTest extends GdxTest { 38 public PerspectiveCamera cam; 39 public CameraInputController inputController; 40 public ModelBatch modelBatch; 41 public Model model; 42 public ModelInstance instance; 43 public Environment environment; 44 45 @Override create()46 public void create () { 47 modelBatch = new ModelBatch(new DefaultShaderProvider()); 48 49 environment = new Environment(); 50 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f)); 51 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); 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 = 1f; 57 cam.far = 30f; 58 cam.update(); 59 60 ModelBuilder modelBuilder = new ModelBuilder(); 61 model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position 62 | Usage.Normal); 63 instance = new ModelInstance(model); 64 65 Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(cam))); 66 } 67 68 @Override render()69 public void render () { 70 inputController.update(); 71 72 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 73 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 74 75 modelBatch.begin(cam); 76 modelBatch.render(instance, environment); 77 modelBatch.end(); 78 } 79 80 @Override dispose()81 public void dispose () { 82 modelBatch.dispose(); 83 model.dispose(); 84 } 85 needsGL20()86 public boolean needsGL20 () { 87 return true; 88 } 89 resume()90 public void resume () { 91 } 92 resize(int width, int height)93 public void resize (int width, int height) { 94 } 95 pause()96 public void pause () { 97 } 98 } 99