Mesh class addition?

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Mesh class addition?

Postby bobcook47 » Tue Jun 14, 2011 1:25 am

I've gotten some good use out of the following code, which should probably be in the Mesh class.
the method cycles the colors if there are fewer colors than vertices, which can generate some nice effects.

public static Mesh mesh(float Verts[], float Normals[], float Tex[], short Indexs[], float... Colors) {
Mesh cube;
int i=1;
if (Normals!=null) i++;
if (Tex!=null) i++;
if (Colors!=null) i++;
VertexAttribute vas[]=new VertexAttribute[i]; System.out.println(Colors.length);
i=1;
vas[0]=new VertexAttribute(Usage.Position, 3, "a_position");
if (Colors!=null) vas[i++]=new VertexAttribute(Usage.ColorPacked, 4, "a_color");
if (Normals!=null) vas[i++]=new VertexAttribute(Usage.Normal, 3, "a_normal");
if (Tex!=null) vas[i++]=new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords");
cube = new Mesh(true, Verts.length, Indexs!=null?Indexs.length:0, vas);
if (Normals!=null || Tex!=null || Colors!=null) {
float[] vertices = new float[Verts.length+(Colors!=null?Verts.length/3:0)+(Normals!=null?Verts.length:0)+(Tex!=null?Verts.length/3*2:0)];
int pIdx = 0;
int cIdx = 0;
int nIdx = 0;
int tIdx = 0;
for(i = 0; i < vertices.length; ) {
vertices[i++] = Verts[pIdx++];
vertices[i++] = Verts[pIdx++];
vertices[i++] = Verts[pIdx++];
if (Colors!=null) {
vertices[i++] = Colors[cIdx];
cIdx = (cIdx+1)%Colors.length;
}
if (Normals!=null) {
vertices[i++] = Normals[nIdx++];
vertices[i++] = Normals[nIdx++];
vertices[i++] = Normals[nIdx++];
}
if (Tex!=null) {
vertices[i++] = Tex[tIdx++];
vertices[i++] = Tex[tIdx++];
}
}
cube.setVertices(vertices);
} else cube.setVertices(Verts);
if (Indexs!=null) cube.setIndices(Indexs);
return cube;
}
bobcook47
 
Posts: 4
Joined: Fri May 13, 2011 6:43 pm

Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest