做好我自己

mesh就是网格,在d3d中有严格的定义,, 也有专门的工具来写.x提供给d3d生成mesh,

什么是mesh?

简单的说就是那些点坐标,颜色,纹理,材质,光照等等的一个集合

libgdx定义的mesh

public class Mesh implements Disposable {public enum VertexDataType {VertexArray, VertexBufferObject, VertexBufferObjectSubData,}

/** list of all meshes **/static final Map<Application, List<Mesh>> meshes = new HashMap<Application, List<Mesh>>();

/** used for benchmarking **/public static boolean forceVBO = false;

final VertexData vertices;final IndexData indices;boolean autoBind = true;final boolean isVertexArray;int refCount = 0;

/** Creates a new Mesh with the given attributes. * * @param isStatic whether this mesh is static or not. Allows for internal optimizations. * @param maxVertices the maximum number of vertices this mesh can hold * @param maxIndices the maximum number of indices this mesh can hold * @param attributes the {@link VertexAttribute}s. Each vertex attribute defines one property of a vertex such as position, * normal or texture coordinate */

public Mesh (boolean isStatic, int maxVertices, int maxIndices, VertexAttribute… attributes) {if (Gdx.gl20 != null || Gdx.gl11 != null || Mesh.forceVBO) {vertices = new VertexBufferObject(isStatic, maxVertices, attributes);indices = new IndexBufferObject(isStatic, maxIndices);isVertexArray = false;} else {vertices = new VertexArray(maxVertices, attributes);indices = new IndexBufferObject(maxIndices);isVertexArray = true;}

addManagedMesh(Gdx.app, this);}

… …

保存了顶点和索引

当然顶点是有不同定义方式的,d3d就是这么做的,这里libgdx有了一个VertexAttribute的概念

VertexAttribute

/** Constructs a new VertexAttribute. * * @param usage the usage, used for the fixed function pipeline. Generic attributes are not supported in the fixed function * pipeline. * @param numComponents the number of components of this attribute, must be between 1 and 4. * @param alias the alias used in a shader for this attribute. Can be changed after construction. */public VertexAttribute (int usage, int numComponents, String alias) {this.usage = usage;//属性的作用this.numComponents = numComponents; //这个属性占3个数字this.alias = alias;}

构造这个mesh通常是从文件里面来,例子里面就是这么做的

public Renderer (Application app) {try {spriteBatch = new SpriteBatch();

InputStream in = Gdx.files.internal("data/ship.obj").read();shipMesh = ModelLoaderOld.loadObj(in);in.close();

in = Gdx.files.internal("data/invader.obj").read();invaderMesh = ModelLoaderOld.loadObj(in);in.close();

in = Gdx.files.internal("data/block.obj").read();blockMesh = ModelLoaderOld.loadObj(in);in.close();

in = Gdx.files.internal("data/shot.obj").read();shotMesh = ModelLoaderOld.loadObj(in);in.close();

这段是invader的Renderer,它就是这么做的

invaderMesh = ModelLoaderOld.loadObj(in);

public class ModelLoaderOld {/** Loads a Wavefront OBJ file from the given InputStream. The OBJ file must only contain triangulated meshes. Materials are * ignored. * * @param in the InputStream * @return a Mesh holding the OBJ data or null in case something went wrong. */public static Mesh loadObj (InputStream in) {return ObjLoader.loadObj(in);}}

ObjLoader就是这个加载器

public static Mesh loadObjFromString (String obj, boolean flipV) {String[] lines = obj.split("\n");float[] vertices = new float[lines.length * 3];float[] normals = new float[lines.length * 3];float[] uv = new float[lines.length * 3];

int numVertices = 0;int numNormals = 0;int numUV = 0;int numFaces = 0;

int[] facesVerts = new int[lines.length * 3];int[] facesNormals = new int[lines.length * 3];int[] facesUV = new int[lines.length * 3];int vertexIndex = 0;int normalIndex = 0;int uvIndex = 0;int faceIndex = 0;

无神的瞳孔,我迫切想逃离这周遭被钢筋混凝土堆架的城市,

做好我自己

相关文章:

你感兴趣的文章:

标签云: