importjava.util.ArrayList;
importjava.util.Arrays;
importorg.obsgolem.crystalia.gfx.Renderer;
importcom.badlogic.gdx.graphics.Mesh;
importcom.badlogic.gdx.graphics.VertexAttribute;
importcom.badlogic.gdx.graphics.VertexAttributes.Usage;
importcom.badlogic.gdx.math.Vector3;
importcom.badlogic.gdx.utils.Array;
importjava.util.*;
publicclassMeshBatch
{
privatefinalstaticVertexAttribute[] attributeConfig = newVertexAttribute[]{
newVertexAttribute(Usage.Position, 3, "a_position"),
newVertexAttribute(Usage.ColorPacked, 4, "a_color"),
newVertexAttribute(Usage.Normal, 3, "a_normal")};
privatefinalstaticintVERTEX_SIZE = 3 + 4 + 3;
// static
// {
// int s = 0;
// for(VertexAttribute va: attributeConfig)
// s += va.getElementCount(); //don't know what this method is callind in libgdx or if it exsist.
// VERTEX_SIZE = s;
// }
privateMeshm;
privateList<Float> vertices = newArrayList<>();
privateList<Short> indices = newArrayList<>();
/**
* why to you get a Short array instead of an primitiv?
*/publicvoidaddMesh(float[] vert, Short[] ind)
{
intoffset = vertices.size() / VERTEX_SIZE;
//You have to throw an exception when you get over the limit of short indices
if (offset + vert.length / VERTEX_SIZE > Short.MAX_VALUE) {
thrownewRuntimeException("blablabla");
}
for (shorti : addOffset(ind, offset)) {
indices.add(i);
}
for (floatv : vert) {
vertices.add(v);
}
}
publicshort[] addOffset(Short[] ind, intoffset)
{
short[] indarr = newshort[ind.length];
for (inti = 0; i < ind.length; ++i) {
//Do you really need this check? You are the only one using this code
//so make sure that you never provide a null value. If you really want to have a chekc throw an exception instead
shortvalue = ind[i] == null ? 0 : ind[i];
indarr[i] = (short) (value + offset);
}
returnindarr;
}
/**
* the finalize method is a reserved method in Java, it gets called by the
* Garbage Collector when it destroys an Object.
* <p/>
* So don't use it ever for something other!!!
*/publicvoidfinalizeOtherNamePlease()
{
m = newMesh(false, vertices.size(), indices.size(), attributeConfig);
m.setVertices(Renderer.makeFloatArray(vertices));
m.setIndices(Renderer.makeShortArray(indices));
}
/**
* I don't want to talk about Globaltons right here, but this method is just
* totaly useless. Rethink your design.
*/publicvoidrender()
{
Renderer.getInstance().render(m);
}
}
Special syntax:
To highlight a line (yellow background), prefix it with '@@'
To indicate that a line should be removed (red background), prefix it with '-'
To indicate that a line should be added (green background), prefix it with '+'
To post multiple snippets, seperate them by '~~~~'