I am having problem building mesh for the first phase.
For the first phase mesh should look something like this. I wanted to build it using mesh mode Lines.
I made in Heightfield class that represents the result of first phase, method that will return its data as mesh.
[java]public Mesh drawHeightfieldSolid() {
Mesh mesh = new Mesh();
mesh.setMode(Mesh.Mode.Lines);
Vector3f origin = getMinBounds();
float cellSize = getCellSize();
float cellHeight = getCellHeight();
int width = getWidth();
int height = getHeight();
List<int[]> indices = new LinkedList<>();
List<Vector3f[]> vertices = new LinkedList<>();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float fx = origin.getX() + x * cellSize;
float fz = origin.getZ() + y * cellSize;
Span s = getSpans()[x + y * width];
while (s != null) {
Vector3f min = new Vector3f(fx, origin.getY() + s.getMinSpanLimit() * cellHeight, fz);
Vector3f max = new Vector3f(fx + cellSize, origin.getY() + s.getMaxSpanLimit() * cellHeight, fz + cellSize);
Mesh mesh1 = GraphicHelper.lineBox(min, max);
indices.add(GraphicHelper.getIndices(mesh1));
vertices.add(GraphicHelper.getVertices(mesh1));
s = s.getNext();
}
}
}
for (Vector3f[] vector3fs : vertices) {
mesh.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vector3fs));
}
for (int[] ints : indices) {
mesh.setBuffer(VertexBuffer.Type.Index, 2, BufferUtils.createIntBuffer(ints));
}
mesh.updateCounts();
mesh.updateBound();
return mesh;
}[/java]
I have built class GraphicHelper to make easier conversions as I will be using them a lot.
[java]public class GraphicHelper {
public static Vector3f[] getVertices(Mesh mesh) {
VertexBuffer buffer = mesh.getBuffer(VertexBuffer.Type.Position);
Vector3f[] array = new Vector3f[buffer.getNumElements()];
for (int i = 0; i < buffer.getNumElements(); i++) {
array[i] = new Vector3f();
array[i].setX((Float) buffer.getElementComponent(i, 0));
array[i].setY((Float) buffer.getElementComponent(i, 1));
array[i].setZ((Float) buffer.getElementComponent(i, 2));
}
return array;
}
public static int[] getIndices(Mesh mesh) {
int[] indices = new int[3];
int[] triangles = new int[mesh.getTriangleCount() * 3];
for (int i = 0; i < triangles.length; i += 3) {
mesh.getTriangle(i / 3, indices);
triangles[i] = indices[0];
triangles[i + 1] = indices[1];
triangles[i + 2] = indices[2];
}
return triangles;
}
public static Mesh lineBox(Vector3f min, Vector3f max) {
Mesh mesh = new Mesh();
mesh.setMode(Mesh.Mode.Lines);
Vector3f[] vertices = new Vector3f[]{
min,
new Vector3f(min.x, min.y, max.z),
new Vector3f(min.x, max.y, min.z),
new Vector3f(max.x, min.y, min.z),
new Vector3f(max.x, max.y, min.z),
new Vector3f(max.x, min.y, max.z),
new Vector3f(min.x, max.y, max.z),
max
};
int[] indices = new int[]{
0, 1, 0, 2, 0, 3,
1, 5, 1, 6,
2, 4, 2, 6,
3, 4, 3, 5,
4, 7, 5, 7, 6, 7
};
mesh.setBuffer(VertexBuffer.Type.Position,
3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(VertexBuffer.Type.Index, 2, BufferUtils.createIntBuffer(indices));
return mesh;
}
}[/java]
I have this problem with getIndices():
[java]
Jul 21, 2014 11:23:21 AM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:532)
at java.nio.DirectIntBufferU.get(DirectIntBufferU.java:253)
at com.jme3.scene.mesh.IndexIntBuffer.get(IndexIntBuffer.java:53)
at com.jme3.scene.Mesh.getTriangle(Mesh.java:855)
at com.jme3.ai.navigation.utils.GraphicHelper.getIndices(GraphicHelper.java:45)
[/java]
Also, if you have any idea how to improve these methods, let me know.