Commit 8bcfc6b2 authored by Matthias Betz's avatar Matthias Betz
Browse files

Add MeshAccumulator building interleaved vertex arrays

parent 24c5c355
package de.hft.stuttgart.citydoctor2.gui.gl;
import de.hft.stuttgart.citydoctor2.math.Triangle3d;
import de.hft.stuttgart.citydoctor2.math.Vector3d;
/**
* Builds interleaved-by-attribute vertex arrays from triangles. Vertices are stored per triangle corner
* (3 per triangle) so a triangle's vertices occupy a contiguous, predictable range — which lets the
* draw index set be built by triangle without dedup bookkeeping.
*/
public class MeshAccumulator {
private final Vector3d center;
private final FloatList positions = new FloatList();
private final FloatList colors = new FloatList();
private final IntList ids = new IntList();
private int vertexCount;
public MeshAccumulator(Vector3d center) {
this.center = center;
}
public void addTriangle(Triangle3d t, int id, float r, float g, float b) {
addVertex(t.getP1(), id, r, g, b);
addVertex(t.getP2(), id, r, g, b);
addVertex(t.getP3(), id, r, g, b);
}
private void addVertex(Vector3d p, int id, float r, float g, float b) {
positions.add((float) (p.getX() - center.getX()),
(float) (p.getY() - center.getY()),
(float) (p.getZ() - center.getZ()));
colors.add(r, g, b);
ids.add(id);
vertexCount++;
}
public int vertexCount() { return vertexCount; }
public float[] positions() { return positions.toArray(); }
public float[] colors() { return colors.toArray(); }
public int[] ids() { return ids.toArray(); }
}
package de.hft.stuttgart.citydoctor2.gui.gl;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import de.hft.stuttgart.citydoctor2.math.Triangle3d;
import de.hft.stuttgart.citydoctor2.math.Vector3d;
import org.junit.Test;
public class MeshAccumulatorTest {
@Test
public void emitsNineFloatsAndThreeIdsPerTriangleRecentered() {
MeshAccumulator acc = new MeshAccumulator(new Vector3d(1, 0, 0));
Triangle3d t = new Triangle3d(
new Vector3d(1, 0, 0), new Vector3d(2, 0, 0), new Vector3d(1, 1, 0));
acc.addTriangle(t, 7, 0.5f, 0.25f, 0.125f);
assertEquals(3, acc.vertexCount());
assertArrayEquals(new float[]{0,0,0, 1,0,0, 0,1,0}, acc.positions(), 1e-6f);
assertArrayEquals(new float[]{0.5f,0.25f,0.125f, 0.5f,0.25f,0.125f, 0.5f,0.25f,0.125f},
acc.colors(), 1e-6f);
assertArrayEquals(new int[]{7, 7, 7}, acc.ids());
}
@Test
public void accumulatesMultipleTriangles() {
MeshAccumulator acc = new MeshAccumulator(new Vector3d(0, 0, 0));
Triangle3d t = new Triangle3d(
new Vector3d(0, 0, 0), new Vector3d(1, 0, 0), new Vector3d(0, 1, 0));
acc.addTriangle(t, 1, 1, 1, 1);
acc.addTriangle(t, 2, 0, 0, 0);
assertEquals(6, acc.vertexCount());
assertEquals(18, acc.positions().length);
assertArrayEquals(new int[]{1, 1, 1, 2, 2, 2}, acc.ids());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment