首页 > 代码库 > libgdx游戏引擎3D开发教程-第一章-基础教程
libgdx游戏引擎3D开发教程-第一章-基础教程
开卷语:我最近才开始学习游戏编程,因为想做个网游玩,所以前几天找了不少引擎来看,于是不出意料的选中了libgdx,值得感谢的是libgdx的文档很多很全,所以没有走多少弯路就成功的配置好了环境。基础教程很完善,好多大神都已经写的很详细了,但是3D方面的很少见,所以我正好要学,索性直接翻译过来,大家共同进步。注意:教程基本是从Wiki上翻译过来,外加自己的小部分理解,所以一般来说应该没什么问题,如有错误请多多指教。
============================================================================================
============================================================================================
第一章 基础教程
Basic 3D using LibGDX
- 我们需要实现一个ApplicationListener来完成操作,如下:
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 () { } } |
- 增加一个camera来允许我们以3D视角来透视我们的场景
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。- 模型包含了如何渲染等很多工作,但是它不知道把自己放在哪里,所以我们需要建立ModelInstance。ModelInstance包含需要被渲染的位置,旋转和缩放,默认情况下是( 0,0,0 ) ,所以我们只需要创建设置为(0,0,0)的ModelInstance 。
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)的方向。效果如图:- 现在我们控制camera来从其他角度看模型
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(); ... } ... } |
建立CameraInputController来增加相机控制,这样就可以拖动旋转了。
- 补充import文件,可能有同学会遇到这类问题
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; |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。