LibGDX 中图元 Mesh 的使用以及参考示例
这篇文章可能不完整
好吧最近撸码撸得想吐,什么都不想干了,抽点时间写写博客。以下是之前在 libGDX 学习中遇到的一些关于 Mesh 问题。关于 Mesh (图元)在 libgdx 中的使用,本来对于国内 libgdx 的开发资料比较缺乏,之前也看了网上的一些教程,感觉很对都没有写清楚(还是我理解能力太差?)或者是把它讲得比较复杂,难以理解。浏览了部分资料后自己写了一个 demo。 本文采用 libGDX-0.99 版本,1.x 版本 API 可能会有变化。
API
qute: A Mesh consists of vertices and optionally indices which specify which vertices define a triangle. Each vertex is composed of attributes such as position, normal, color or texture coordinate. Note that not all of this attributes must be given, except for position which is non-optional. Each attribute has an alias which is used when rendering a Mesh in OpenGL ES 2.0. The alias is used to bind a specific vertex attribute to a shader attribute. The shader source and the alias of the attribute must match exactly for this to work.
大致说明了一个 mesh 包含了多个 vertice 即顶点,每个顶点都有各自的属性,包括颜色、位置、纹理等等。平时常用的构造函数就是:
1 | Mesh(boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes) |
构造函数是比较简单的,以上官方的参数讲解,其中最重要的就是 attribute 需要用到 VertexAttribute 这个类其常用的构造函数 VertexAttribute(usage, numComponents, alias); 第一个参数 usage 需要一个 Usage 类型他的父类是 VertexAttributes ,第二个参数是这个顶点属性组成数量范围是 1-4,比如说颜色 color = new VertexAttribute(Usage.ColorPacked, 4, “color”) 这样这个顶点属性就是指明为颜色,由四个参数组成,因为颜色是 rgba 还有一个是透明度所以是 4 个,后面参数 alias 顾名思义是别名的意思,就是这个顶点的一个名字 String 类型,据说和 Shader 有关联,还没有用到过,这里就不考虑,写上一个 string 就行了。
mesh 中有一个重要的方法就是 setVertices() 就是设置顶点属性了了,之前的 VertexAttribute 只是指明了属性是位置属性还是颜色属性还是其他的属性。好了说了一堆 API 的东西还是看看 demo 比较好。
Demo
这里展示了一个 mesh 包含了其中位置、颜色、和纹理的展示
1 | private Mesh mesh; |
下面附上完整代码
1 | package com.jiavan.libgdx.mesh; |
参考文档
LibGDX 中图元 Mesh 的使用以及参考示例