参考教程地址:http://blog.xoppa.com/basic-3d-using-libgdx-2/
1. 创建项目
2. 代码
1 package com.mygdx.game; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.*; 6 import com.badlogic.gdx.graphics.g3d.*; 7 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 8 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 9 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;10 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;11 12 public class MyGdxGame extends ApplicationAdapter {13 public Environment environment;//可以包含点光源集合和线光源集合14 public PerspectiveCamera cam;//3D视角15 public Model model;//模型16 public ModelInstance instance;//模型实例17 public ModelBatch modelBatch;18 19 public CameraInputController camController;//视角控制器20 21 @Override22 public void create () {23 environment = new Environment();24 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));//环境光25 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));//直线光源26 27 modelBatch = new ModelBatch();28 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());//67可以理解成一个定值,视角宽度(67度)29 cam.position.set(10f, 10f, 10f);30 cam.lookAt(0,0,0);31 cam.near = 1f;32 cam.far = 300f;33 cam.update();34 35 camController = new CameraInputController(cam);36 Gdx.input.setInputProcessor(camController);37 38 ModelBuilder modelBuilder = new ModelBuilder();//Helper class39 model = modelBuilder.createBox(5f, 5f, 5f,40 new Material(ColorAttribute.createDiffuse(Color.GREEN)),41 VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);42 instance = new ModelInstance(model);43 44 45 46 }47 48 @Override49 public void render () {50 camController.update();51 Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());52 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);53 54 modelBatch.begin(cam);55 modelBatch.render(instance,environment);56 modelBatch.end();57 }58 59 @Override60 public void dispose() {61 model.dispose();62 super.dispose();63 }64 }
3. 运行效果图