public class Basic3DTest implements ApplicationListener
{ @Override public void create () { } @Override public void render () { } @Override public void dispose () { } @Override public void resume () { } @Override public void resize ( int width, int height)
{ } @Override public void pause () { } } |
public class Basic3DTest implements ApplicationListener
{ public PerspectiveCamera cam; @Override public void create () { cam = new PerspectiveCamera( 67 ,
Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam.position.set(10f, 10f, 10f); cam.lookAt( 0 , 0 , 0 ); cam.near = 1f; cam.far = 300f; cam.update(); } ... } |
public class Basic3DTest implements ApplicationListener
{ public PerspectiveCamera cam; public Model model; public ModelInstance instance; @Override public void create () { cam = new PerspectiveCamera( 67 ,
Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); ... cam.update(); ModelBuilder modelBuilder = new ModelBuilder(); model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position | Usage.Normal); instance = new ModelInstance(model); } @Override public void dispose () { model.dispose(); } ... } |
ModelBuilder
,它可用于在代码中创建模型。然后我们建立了一个5*5*5的立方体,设置Color绿色,设置位置Usage.Position,设置Usage.Normal(光线等)。Usage继承
VertexAttributes。
public class Basic3DTest implements ApplicationListener
{ public PerspectiveCamera cam; public ModelBatch modelBatch; public Model model; public ModelInstance instance; @Override public void create () { modelBatch = new ModelBatch(); cam = new PerspectiveCamera( 67 ,
Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); ... cam.update(); ModelBuilder modelBuilder = new ModelBuilder(); model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); instance = new ModelInstance(model); } @Override public void render () { Gdx.gl.glViewport( 0 , 0 ,
Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); modelBatch.begin(cam); modelBatch.render(instance); modelBatch.end(); } @Override public void dispose () { modelBatch.dispose(); model.dispose(); } ... } |
ModelBatch用来渲染图像,在create()中初始化,在render()中clean,begin,render,end。记得在dispose中销毁它。效果如图:
public class Basic3DTest implements ApplicationListener
{ public Environment environment; ... @Override public void create () { environment = new Environment(); environment.set( new ColorAttribute(ColorAttribute.AmbientLight, 0 .4f, 0 .4f, 0 .4f,
1f)); environment.add( new DirectionalLight().set( 0 .8f, 0 .8f, 0 .8f,
-1f, - 0 .8f, - 0 .2f)); ... } @Override public void render () { Gdx.gl.glViewport( 0 , 0 ,
Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); modelBatch.begin(cam); modelBatch.render(instance, environment); modelBatch.end(); } ... } |
Environment
。我们构建并设置了环境光( 0.4 , 0.4 , 0.4 ) ,注意alpha值将被忽略。然后我们添加一个DirectionalLight为( 0.8 ,0.8, 0.8)的颜色和(-1.0 ,- 0.8F , 0.2)的方向。效果如图:
public class Basic3DTest implements ApplicationListener
{ ... public CameraInputController camController; ... @Override public void create () { ... camController = new CameraInputController(cam); Gdx.input.setInputProcessor(camController); } @Override public void render () { camController.update(); ... } ... } |
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.*;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
|
libgdx游戏引擎3D开发教程-第一章-基础教程,布布扣,bubuko.com
原文地址:http://blog.csdn.net/enight0000/article/details/25234417